* chore(ci): add clang-format gate, CODEOWNERS and agent collaboration rules * refactor(modules): clear remaining back-edges and split the largest TUs Three known dependency back-edges are gone (module_depgraph.py KNOWN_BACK_EDGES is now empty; --check passes with 0 violations, 0 cycles): - window -> graphics: window queries the new IWindowSurfaceHost capability (common/WindowSurfaceHost.h) instead of holding a Graphics pointer; Window::setGraphics was removed and all call sites updated. - window -> image: Window::setIcon was replaced by setIconRGBA(const uint8_t*, w, h); the window no longer decodes ImageData. - scene -> graphics: pickScreenAt / collectFrustumIdsAt implementations moved to graphics/ScenePicking.cpp (graphics -> scene is a legal downward edge); scene now depends only on spatial. TU splits (pure moves, verified identical method sets and brace balance): - vulkan/Graphics.cpp (5118 lines) -> Graphics.cpp (lifecycle), GraphicsPipeline, Graphics2D, Graphics3D, GraphicsMesh + shared GraphicsInternal.h. - scene/Scene.cpp -> Scene.cpp (script layer / host lifecycle / serialization) + SceneNodes.cpp + shared SceneInternal.h. Verified with a real build (MSVC 19.51, prebuilt third-party): full build green; window.* and Scene.* suites pass, including scene picking/culling and GPU cases. * refactor(graphics): narrow Graphics interfaces and clear public-header leaks Step 4 — interface segregation: - New lean interfaces: IGraphics2D, IGraphics3D, IResourceFactory, IPostFX (pure virtuals, forward-declared types only; graphics/Color.h extracted from Canvas.h so modules that only need the color type stop pulling Canvas.h). - Graphics now implements all four facets; consumers can depend on the narrow interface they use instead of the 129-method god class. - Migrated voxel (VoxelWorld::drawVisible -> IGraphics3D*) and model3d (ModelRenderer::buildRenderable(s) -> IResourceFactory&). - Graphics.h slims from 24 sub-header includes to a handful of small type headers + forward declarations; the destructor moved out-of-line so unique_ptr effect members are destroyed where types are complete. Step 5 — header leaks (module_depgraph.py '*' marks: 14 -> 7): - map/TileLayer.h, particles/ParticleEmitter.h, avatar/AvatarInstance.h, animation/SpineAnim.h, map/TileSystem.h, model3d/ModelData.h, model3d/ModelRenderer.h, audio/Audio.h + Source.h, image/ImageData.h + Image.h, dialogue/Dialogue.h, procgen Palette/OutputSpec/TextureRecipe/ PbrMaterial -> forward declarations, includes moved to .cpp. - voxel/VoxelWorld.h: terrain sampler Pimpl'd (unique_ptr + out-of-line destructor), removing the procgen header dependency. - gpgpu/vulkan backend headers stop pulling graphics/vulkan/Graphics.h. - Remaining '*' marks are deliberate: tiny POD/enum headers (GridConfig, MapObject, BlendMode, Color), an internal backend header (GraphicsInternal.h -> filesystem), and lean interface references. Verified with a full clean build (MSVC) and tests: window/Scene/voxel/ RenderSystem/Quad/shader/TextureSampler/resourceLifetime pass; broad suite only shows the pre-existing PHYSFS-init filesystem-environment failures. * feat(resources): revive ResourceManager as unified IAssetReloader-backed cache * fix(resources): leak ResourceManager singleton to avoid exit-time teardown crashes Cached CPU resources may own third-party handles (FreeType faces, Assimp scenes, image decode handlers) whose libraries are torn down at process exit in an unspecified TU order; destroying cached entries from the singleton destructor can segfault at exit (seen on CI Windows in model3d.materialApi.objMtl). Keep the singleton alive until OS reclaim; explicit unload()/clear() still release entries during the run. * refactor(devtools): consume business modules through capability interfaces DevTools / cmdline no longer include business-module headers, so trimming scene / physics / procgen / particles / audio / ui / graphics no longer requires disabling devtools, and devtools can be switched off with -DEVENGINE_MODULE_DEVTOOLS=OFF. New capability interfaces in common/: - ISceneQuery (scene) - host/node inspection + mutation + transform sync - IRenderCapture (graphics) - render status, PNG capture, camera control, visible-entity snapshot, entity-ID mask, G-buffer PNGs - IPhysicsQuery (physics) - 2D world lifecycle + ray casts - IProcgenQuery (procgen) - recipe listing, map/mesh generation - IParticlesQuery (particles) - emitter status + one-shot emit - IAudioQuery (audio) - volume / stop-all - IEditorHost (ui) - declarative editor host (used by cmdline too) Providers register in their module constructors. DevTools rewires McpServer (render/scene/procgen/physics/particles/audio/host tools), SceneInspect (now delegates to ISceneQuery + IRenderCapture) and RenderVision (uses IRenderCapture::capturePngDataUrl); cmdline/Mcp.cpp uses IEditorHost. manifest: devtools DEPS graphics scene physics procgen particles audio ui image event filesystem -> event; cmdline DEPS filesystem ui -> filesystem. Verified: full build green; devtools.mcp.* tests pass; broad suite shows only the pre-existing PHYSFS-init filesystem-environment failures. * style: apply clang-format to changed lines after dev merge * fix(webgpu): include RenderControl/TextureSampler explicitly after Graphics.h slimming * fix(webgpu): provide newFont/print stubs to satisfy IGraphics2D/IResourceFactory vtables * fix(common): make ProcgenQuery.h self-sufficient (cstdint/utility for GCC)
317 lines
8.6 KiB
C++
317 lines
8.6 KiB
C++
#include "zeroerr/assert.h"
|
|
#include "zeroerr/unittest.h"
|
|
|
|
#include "audio/Audio.h"
|
|
#include "audio/Source.h"
|
|
#include "common/Exception.h"
|
|
#include "data/ByteData.h"
|
|
#include "filesystem/FileData.h"
|
|
#include "sound/Decoder.h"
|
|
#include "sound/Sound.h"
|
|
#include "sound/SoundData.h"
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <cstring>
|
|
#include <cstdint>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
#include "PathBesideSource.h"
|
|
EVE_DEFINE_PATH_BESIDE_SOURCE()
|
|
|
|
std::vector<char> readBinaryFile(const std::string &path) {
|
|
std::ifstream in(path, std::ios::binary);
|
|
if (!in)
|
|
return {};
|
|
return std::vector<char>((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
|
}
|
|
|
|
} // namespace
|
|
|
|
static eve::audio::Audio *tryCreateAudio() {
|
|
try {
|
|
return eve::audio::Audio::create();
|
|
} catch (const eve::Exception &) {
|
|
std::cerr << "N/A: OpenAL device not available\n";
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
static std::vector<char> makeSilentWav(int samples = 441) {
|
|
const int dataBytes = samples * 2;
|
|
std::vector<char> buf(44 + dataBytes, 0);
|
|
auto w32 = [&](int off, uint32_t v) { std::memcpy(buf.data() + off, &v, 4); };
|
|
auto w16 = [&](int off, uint16_t v) { std::memcpy(buf.data() + off, &v, 2); };
|
|
std::memcpy(buf.data(), "RIFF", 4);
|
|
w32(4, 36 + dataBytes);
|
|
std::memcpy(buf.data() + 8, "WAVEfmt ", 8);
|
|
w32(16, 16);
|
|
w16(20, 1);
|
|
w16(22, 1);
|
|
w32(24, 44100);
|
|
w32(28, 44100 * 2);
|
|
w16(32, 2);
|
|
w16(34, 16);
|
|
std::memcpy(buf.data() + 36, "data", 4);
|
|
w32(40, dataBytes);
|
|
return buf;
|
|
}
|
|
|
|
TEST_CASE("audio.device") {
|
|
auto *audio = tryCreateAudio();
|
|
if (!audio)
|
|
return;
|
|
audio->setVolume(0.5f);
|
|
CHECK(audio->getVolume() == 0.5f);
|
|
audio->setPosition(1.f, 2.f, 3.f);
|
|
audio->pump();
|
|
}
|
|
|
|
TEST_CASE("audio.staticSource.playStop") {
|
|
auto *audio = tryCreateAudio();
|
|
if (!audio)
|
|
return;
|
|
auto *sound = eve::sound::Sound::create();
|
|
auto wav = makeSilentWav(4410);
|
|
eve::data::ByteData data(wav.data(), wav.size());
|
|
auto *sd = sound->newSoundData(&data);
|
|
auto *src = audio->newSource(sd);
|
|
REQUIRE(src != nullptr);
|
|
src->setVolume(0.2f);
|
|
src->setPitch(1.0f);
|
|
src->setLooping(false);
|
|
src->play();
|
|
CHECK(src->isPlaying());
|
|
src->stop();
|
|
CHECK(!src->isPlaying());
|
|
delete src;
|
|
delete sd;
|
|
}
|
|
|
|
TEST_CASE("audio.streamSource.pump") {
|
|
auto *audio = tryCreateAudio();
|
|
if (!audio)
|
|
return;
|
|
auto *sound = eve::sound::Sound::create();
|
|
auto wav = makeSilentWav(44100);
|
|
eve::data::ByteData data(wav.data(), wav.size());
|
|
auto *src = audio->newSourceFromData(&data, "stream");
|
|
REQUIRE(src != nullptr);
|
|
src->setLooping(true);
|
|
src->play();
|
|
for (int i = 0; i < 30; ++i) {
|
|
audio->pump();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
CHECK(src->isPlaying());
|
|
src->stop();
|
|
audio->pump();
|
|
delete src;
|
|
}
|
|
|
|
TEST_CASE("audio.spatial.api") {
|
|
auto *audio = tryCreateAudio();
|
|
if (!audio)
|
|
return;
|
|
auto *sound = eve::sound::Sound::create();
|
|
auto wav = makeSilentWav();
|
|
eve::data::ByteData data(wav.data(), wav.size());
|
|
auto *sd = sound->newSoundData(&data);
|
|
auto *src = audio->newSource(sd);
|
|
audio->setPosition(0, 0, 0);
|
|
src->setRelative(false);
|
|
src->setPosition(5.f, 0.f, 0.f);
|
|
src->setAttenuationDistances(1.f, 50.f);
|
|
src->play();
|
|
audio->pump();
|
|
src->stop();
|
|
delete src;
|
|
delete sd;
|
|
}
|
|
|
|
TEST_CASE("audio.stopAll") {
|
|
auto *audio = tryCreateAudio();
|
|
if (!audio)
|
|
return;
|
|
|
|
auto *sound = eve::sound::Sound::create();
|
|
auto wav = makeSilentWav(44100);
|
|
eve::data::ByteData data(wav.data(), wav.size());
|
|
auto *sd = sound->newSoundData(&data);
|
|
auto *src1 = audio->newSource(sd);
|
|
auto *src2 = audio->newSource(sd);
|
|
src1->play();
|
|
src2->play();
|
|
audio->pump();
|
|
CHECK(src1->isPlaying());
|
|
CHECK(src2->isPlaying());
|
|
audio->stopAll();
|
|
audio->pump();
|
|
CHECK(!src1->isPlaying());
|
|
CHECK(!src2->isPlaying());
|
|
delete src1;
|
|
delete src2;
|
|
delete sd;
|
|
}
|
|
|
|
TEST_CASE("audio.pause") {
|
|
auto *audio = tryCreateAudio();
|
|
if (!audio)
|
|
return;
|
|
|
|
auto *sound = eve::sound::Sound::create();
|
|
auto wav = makeSilentWav(44100);
|
|
eve::data::ByteData data(wav.data(), wav.size());
|
|
auto *sd = sound->newSoundData(&data);
|
|
auto *src = audio->newSource(sd);
|
|
src->play();
|
|
audio->pump();
|
|
CHECK(src->isPlaying());
|
|
audio->pause(src);
|
|
CHECK(!src->isPlaying());
|
|
delete src;
|
|
delete sd;
|
|
}
|
|
|
|
TEST_CASE("audio.source.seekTell") {
|
|
auto *audio = tryCreateAudio();
|
|
if (!audio)
|
|
return;
|
|
|
|
auto *sound = eve::sound::Sound::create();
|
|
auto wav = makeSilentWav(44100);
|
|
eve::data::ByteData data(wav.data(), wav.size());
|
|
auto *sd = sound->newSoundData(&data);
|
|
auto *src = audio->newSource(sd);
|
|
|
|
CHECK(src->getDuration() > 0.9);
|
|
src->play();
|
|
audio->pump();
|
|
CHECK(src->seek(0.5));
|
|
audio->pump();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
double pos = src->tell();
|
|
// OpenAL tell() can drift after pump/sleep; keep a loose window around seek(0.5).
|
|
CHECK(pos >= 0.40);
|
|
CHECK(pos <= 0.65);
|
|
src->stop();
|
|
|
|
delete src;
|
|
delete sd;
|
|
}
|
|
|
|
TEST_CASE("audio.source.pitch") {
|
|
auto *audio = tryCreateAudio();
|
|
if (!audio)
|
|
return;
|
|
|
|
auto *sound = eve::sound::Sound::create();
|
|
auto wav = makeSilentWav();
|
|
eve::data::ByteData data(wav.data(), wav.size());
|
|
auto *sd = sound->newSoundData(&data);
|
|
auto *src = audio->newSource(sd);
|
|
|
|
src->setPitch(1.5f);
|
|
CHECK(src->getPitch() == 1.5f);
|
|
src->setPitch(0.5f);
|
|
CHECK(src->getPitch() == 0.5f);
|
|
|
|
delete src;
|
|
delete sd;
|
|
}
|
|
|
|
TEST_CASE("audio.volumeRoundTrip") {
|
|
auto *audio = tryCreateAudio();
|
|
if (!audio)
|
|
return;
|
|
|
|
audio->setVolume(0.75f);
|
|
CHECK(audio->getVolume() == 0.75f);
|
|
|
|
auto *sound = eve::sound::Sound::create();
|
|
auto wav = makeSilentWav();
|
|
eve::data::ByteData data(wav.data(), wav.size());
|
|
auto *sd = sound->newSoundData(&data);
|
|
auto *src = audio->newSource(sd);
|
|
|
|
src->setVolume(0.3f);
|
|
CHECK(src->getVolume() == 0.3f);
|
|
|
|
delete src;
|
|
delete sd;
|
|
}
|
|
|
|
TEST_CASE("audio.modplug.midi.play") {
|
|
auto *audio = tryCreateAudio();
|
|
if (!audio)
|
|
return;
|
|
|
|
const std::string path = pathBesideThisSource("angel.mid");
|
|
auto bytes = readBinaryFile(path);
|
|
REQUIRE(!bytes.empty());
|
|
|
|
eve::filesystem::FileData fd("angel.mid", bytes.size());
|
|
std::memcpy(fd.getData(), bytes.data(), bytes.size());
|
|
|
|
// Prefill stream buffers before play — empty AL sources leave AL_INITIAL.
|
|
eve::audio::Source *src = nullptr;
|
|
try {
|
|
src = audio->newSourceFromData(&fd, "stream");
|
|
} catch (const eve::Exception &e) {
|
|
std::cerr << "MIDI stream source failed: " << e.what() << "\n";
|
|
CHECK(false);
|
|
return;
|
|
}
|
|
REQUIRE(src != nullptr);
|
|
std::cerr << "Playing angel.mid stream for ~4s (duration=" << src->getDuration() << "s)\n";
|
|
std::cerr << "(ModPlug may warn about missing timidity.cfg / instrument patches)\n";
|
|
|
|
src->setVolume(1.0f);
|
|
for (int i = 0; i < 20; ++i) {
|
|
audio->pump();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
|
}
|
|
src->play();
|
|
bool heardPlaying = false;
|
|
double maxTell = 0.0;
|
|
for (int i = 0; i < 400; ++i) {
|
|
audio->pump();
|
|
if (src->isPlaying())
|
|
heardPlaying = true;
|
|
maxTell = std::max(maxTell, src->tell());
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
std::cerr << "stream: isPlayingSeen=" << (heardPlaying ? "yes" : "no")
|
|
<< " maxTell=" << maxTell << "s\n";
|
|
CHECK(heardPlaying);
|
|
CHECK(maxTell > 0.05); // stream path currently underfills; static path is the reliable play test
|
|
src->stop();
|
|
audio->pump();
|
|
delete src;
|
|
|
|
// Also verify static path (full decode → OpenAL buffer) briefly.
|
|
eve::filesystem::FileData fd2("angel.mid", bytes.size());
|
|
std::memcpy(fd2.getData(), bytes.data(), bytes.size());
|
|
auto *sound = eve::sound::Sound::create();
|
|
auto *sd = sound->newSoundData(&fd2);
|
|
REQUIRE(sd != nullptr);
|
|
CHECK(sd->getSampleCount() > 0);
|
|
auto *staticSrc = audio->newSource(sd);
|
|
std::cerr << "Playing angel.mid static for ~2s (samples=" << sd->getSampleCount() << ")\n";
|
|
staticSrc->setVolume(1.0f);
|
|
staticSrc->play();
|
|
CHECK(staticSrc->isPlaying());
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
|
|
audio->pump();
|
|
CHECK(staticSrc->tell() > 0.5);
|
|
staticSrc->stop();
|
|
delete staticSrc;
|
|
delete sd;
|
|
}
|