refactor(core): 重构资源销毁流程,改为集中式管理
将各模块的析构函数中自动调用shutdown()的逻辑移除,改为在Application::shutdown()中统一手动调用 调整SDL初始化和退出流程,避免重复调用 添加测试用的1秒定时退出逻辑 清理主程序中的示例代码
This commit is contained in:
@@ -39,124 +39,124 @@ int main(int argc, char **argv) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_Log("Starting main loop...");
|
||||
// SDL_Log("Starting main loop...");
|
||||
|
||||
auto menuScene = MakePtr<Scene>();
|
||||
SceneManager::get().PushScene(menuScene);
|
||||
// auto menuScene = MakePtr<Scene>();
|
||||
// SceneManager::get().PushScene(menuScene);
|
||||
|
||||
// 尝试加载精灵
|
||||
auto sprite = Sprite::createFromFile("assets/player.png");
|
||||
if (sprite) {
|
||||
sprite->SetPosition(320, 300);
|
||||
sprite->SetOpacity(0.8f);
|
||||
// sprite->SetAnchor(Vec2(0.5f, 0.5f));
|
||||
// sprite->SetRotation(30.f);
|
||||
sprite->SetZOrder(2000);
|
||||
// sprite->SetScale(Vec2(-1.0f, 1.0f));
|
||||
menuScene->AddChild(sprite);
|
||||
SDL_Log("Sprite created and added to scene");
|
||||
} else {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create sprite from file!");
|
||||
}
|
||||
|
||||
// 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!");
|
||||
}
|
||||
|
||||
NpkArchive &npk = NpkArchive::get();
|
||||
npk.setImagePackDirectory("assets/ImagePacks2");
|
||||
npk.setDefaultImg("sprite/interface/base.img", 0);
|
||||
npk.init();
|
||||
|
||||
auto sprite1 = Sprite::createFromNpk("sprite/newtitle/nangua.img", 0);
|
||||
if (sprite1) {
|
||||
sprite1->SetPosition(220, 10);
|
||||
// sprite1->SetScale(2.0f);
|
||||
sprite->AddChild(sprite1);
|
||||
} else {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create sprite from NPK!");
|
||||
}
|
||||
|
||||
SoundPackArchive &archive = SoundPackArchive::get();
|
||||
archive.setSoundPackDirectory("assets/SoundPacks");
|
||||
archive.init();
|
||||
|
||||
// auto sound = Sound::loadFromNpk("sounds/ui/adventurer_maker_name.ogg");
|
||||
// if (sound) {
|
||||
// sound->play();
|
||||
// // 尝试加载精灵
|
||||
// auto sprite = Sprite::createFromFile("assets/player.png");
|
||||
// if (sprite) {
|
||||
// sprite->SetPosition(320, 300);
|
||||
// sprite->SetOpacity(0.8f);
|
||||
// // sprite->SetAnchor(Vec2(0.5f, 0.5f));
|
||||
// // sprite->SetRotation(30.f);
|
||||
// sprite->SetZOrder(2000);
|
||||
// // sprite->SetScale(Vec2(-1.0f, 1.0f));
|
||||
// menuScene->AddChild(sprite);
|
||||
// SDL_Log("Sprite created and added to scene");
|
||||
// } else {
|
||||
// SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load sound!");
|
||||
// SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create sprite from file!");
|
||||
// }
|
||||
|
||||
auto &audioDB = AudioDatabase::get();
|
||||
audioDB.loadFromFile("assets/audio.xml");
|
||||
// // 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;
|
||||
// // // }
|
||||
// // // }
|
||||
// // // }
|
||||
|
||||
// ========== 方式 1: 最简单的 API(推荐) ==========
|
||||
std::string path = audioDB.filePath("P_ICECANNON_SHOT");
|
||||
if (!path.empty()) {
|
||||
SDL_Log("=== 方式 1: 直接获取文件路径 ===");
|
||||
SDL_Log("File: %s", path.c_str());
|
||||
}
|
||||
// // // // 方式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!");
|
||||
// }
|
||||
|
||||
// NpkArchive &npk = NpkArchive::get();
|
||||
// npk.setImagePackDirectory("assets/ImagePacks2");
|
||||
// npk.setDefaultImg("sprite/interface/base.img", 0);
|
||||
// npk.init();
|
||||
|
||||
// auto sprite1 = Sprite::createFromNpk("sprite/newtitle/nangua.img", 0);
|
||||
// if (sprite1) {
|
||||
// sprite1->SetPosition(220, 10);
|
||||
// // sprite1->SetScale(2.0f);
|
||||
// sprite->AddChild(sprite1);
|
||||
// } else {
|
||||
// SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create sprite from NPK!");
|
||||
// }
|
||||
|
||||
// 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();
|
||||
|
||||
SDL_Log("Application exited normally");
|
||||
app.shutdown();
|
||||
|
||||
SDL_Log("Application exited normally");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user