refactor(main): 重构主循环初始化逻辑,将资源初始化移到运行循环中

将PVF、NPK、SNPK、字体和音频系统的初始化从主函数移到应用运行循环中
统一资源初始化顺序,提高代码可读性和维护性
This commit is contained in:
2026-04-01 05:38:37 +08:00
parent 648b94e741
commit 31e2c249bb

View File

@@ -43,197 +43,43 @@ int main(int argc, char **argv) {
Application& app = Application::get();
// 初始化应用
if (!app.init(config)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize application!");
return -1;
}
// SDL_Log("Starting main loop...");
SquirrelVM::get().setScriptDirectory("assets/scripts");
SquirrelVM::get().init();
app.run([]() {
// 初始化PVF
auto &pvf = PvfArchive::get();
if (pvf.open("assets/Script.pvf")) {
pvf.init();
SDL_Log("PVF initialized successfully");
}
NpkArchive &npk = NpkArchive::get();
// 初始化NPK
auto &npk = NpkArchive::get();
npk.setImagePackDirectory("assets/ImagePacks2");
npk.setDefaultImg("sprite/interface/base.img", 0);
npk.init();
auto menuScene = MakePtr<Scene>();
SceneManager::get().PushScene(menuScene);
// 初始化SNPK
auto &archive = SoundPackArchive::get();
archive.setSoundPackDirectory("assets/SoundPacks");
archive.init();
FontManager::get().init();
FontManager::get().registerFont("default",
"assets/Fonts/VonwaonBitmap-12px.ttf", 12);
auto text = TextSprite::create("你好世界", "default");
text->SetPosition(100, 100);
text->SetTextColor(Colors::Red);
menuScene->AddChild(text);
// 初始化字体
auto &fontManager = FontManager::get();
fontManager.init();
fontManager.registerFont("default", "assets/Fonts/VonwaonBitmap-12px.ttf",
12);
auto ani = MakePtr<Animation>(
"monster/event/bluemarble/goblin/animation_goblin2/move.ani");
if (ani) {
SDL_Log("Animation created successfully");
// ani->SetAnchor(Vec2(0.5f, 0.5f));
ani->SetPosition(640, 360);
auto uuid = ani->GetUUIDString();
SDL_Log("Animation UUID: %s", uuid.c_str());
menuScene->AddChild(ani);
}
auto TestActor = MakePtr<Actor>();
menuScene->AddChild(TestActor);
TestActor->EnableEventReceive();
// 监听手柄按键按下
TestActor->AddEventListener(
EventType::JoystickButtonDown, [](const Event &event) {
const auto &ke = static_cast<const JoystickButtonEvent &>(event);
SDL_Log("Joystick Button Down: device=%d, button=%d", ke.getDeviceId(),
ke.getButton());
if(ke.getButton() == 6) {
Application::get().quit();
}
if (ke.getButton() == 0) {
Application::get().getWindow()->setPos(0, 0);
SDL_Log("setPos ret=%d, %d", Application::get().getWindow()->pos().x, Application::get().getWindow()->pos().y);
Application::get().getWindow()->setSize(1920, 1080);
}
if (ke.getButton() == 1) {
Application::get().getWindow()->setPos(0, 0);
Application::get().getWindow()->setSize(1280, 720);
}
return true;
});
// 监听手柄按键抬起
TestActor->AddEventListener(
EventType::JoystickButtonUp, [](const Event &event) {
const auto &ke = static_cast<const JoystickButtonEvent &>(event);
SDL_Log("Joystick Button Up: device=%d, button=%d", ke.getDeviceId(),
ke.getButton());
return true;
});
// 监听手柄轴
TestActor->AddEventListener(EventType::JoystickAxis, [](const Event &event) {
const auto &ae = static_cast<const JoystickAxisEvent &>(event);
if (std::abs(ae.getNormalizedValue()) > 0.1f) {
SDL_Log("Joystick Axis: device=%d, axis=%d, value=%.2f", ae.getDeviceId(),
ae.getAxis(), ae.getNormalizedValue());
}
return true;
});
auto sprite1 = Sprite::createFromNpk("sprite/newtitle/nangua.img", 0);
if (sprite1) {
sprite1->SetPosition(220, 10);
// sprite1->SetScale(2.0f);
menuScene->AddChild(sprite1);
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create sprite from NPK!");
}
// // auto &archive = PvfArchive::get();
// // if (archive.open("assets/Script.pvf")) {
// // archive.init();
// // // 文件内容读取
// // if (auto rawData = archive.getFileRawData("region/balmayer_north.rgn")) {
// // ScriptParser parser(*rawData, "script/example.bin");
// // // // 方式1迭代解析
// // // while (!parser.isEnd()) {
// // // if (auto value = parser.next()) {
// // // switch (value->type) {
// // // case ScriptValueType::Integer:
// // // SDL_Log("Integer: %d", value->intValue);
// // // break;
// // // case ScriptValueType::Float:
// // // SDL_Log("Float: %f", value->floatValue);
// // // break;
// // // case ScriptValueType::String:
// // // SDL_Log("String: %s", value->stringValue.c_str());
// // // break;
// // // default:
// // // break;
// // // }
// // // }
// // // }
// // // // 方式2批量解析
// // // parser.reset();
// // auto allValues = parser.parseAll();
// // for (const auto &value : allValues) {
// // // 处理 value
// // switch (value.type) {
// // case ScriptValueType::Integer:
// // SDL_Log("Integer: %d", value.intValue);
// // break;
// // case ScriptValueType::Float:
// // SDL_Log("Float: %f", value.floatValue);
// // break;
// // case ScriptValueType::String:
// // SDL_Log("String: %s", value.stringValue.c_str());
// // break;
// // default:
// // break;
// // }
// // }
// // }
// // }
// AudioSystem::get().init();
// AudioSystem::get().setMasterVolume(1.0f);
// AudioSystem::get().setSoundVolume(0.8f);
// AudioSystem::get().setMusicVolume(0.6f);
// auto bgMusic = Music::loadFromFile("assets/BackgroundMusic.mp3");
// if (bgMusic) {
// bgMusic->play();
// } else {
// SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load background music!");
// }
// SoundPackArchive &archive = SoundPackArchive::get();
// archive.setSoundPackDirectory("assets/SoundPacks");
// archive.init();
// // auto sound = Sound::loadFromNpk("sounds/ui/adventurer_maker_name.ogg");
// // if (sound) {
// // sound->play();
// // } else {
// // SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load sound!");
// // }
// auto &audioDB = AudioDatabase::get();
// audioDB.loadFromFile("assets/audio.xml");
// // ========== 方式 1: 最简单的 API推荐 ==========
// std::string path = audioDB.filePath("P_ICECANNON_SHOT");
// if (!path.empty()) {
// SDL_Log("=== 方式 1: 直接获取文件路径 ===");
// SDL_Log("File: %s", path.c_str());
// }
app.run([]() {
if (SquirrelVM::get().isInitialized()) {
SquirrelVM::get().run();
}
// 初始化音频系统
auto &audioSystem = AudioSystem::get();
audioSystem.init();
audioSystem.setMasterVolume(1.0f);
audioSystem.setSoundVolume(0.8f);
audioSystem.setMusicVolume(0.6f);
});
SDL_Log("Application exited normally");