diff --git a/src/core/GameApplication.cpp b/src/core/GameApplication.cpp index 24843b9..f95797a 100644 --- a/src/core/GameApplication.cpp +++ b/src/core/GameApplication.cpp @@ -6,9 +6,9 @@ GameApplication::GameApplication() { } -void GameApplication::initialize() { +bool GameApplication::initialize() { m_gameSession->initialize(); - + return true; } SDL_AppResult GameApplication::handleInputEvent(SDL_Event* event) { diff --git a/src/core/GameApplication.h b/src/core/GameApplication.h index 46c3e8c..58953bf 100644 --- a/src/core/GameApplication.h +++ b/src/core/GameApplication.h @@ -1,6 +1,6 @@ #pragma once -#include "core/GameSession.h" +#include "game/GameSession.h" #include "input/InputManager.h" #include "utils/Config.h" @@ -17,7 +17,7 @@ private: public: GameApplication(); ~GameApplication(); - void initialize(); + bool initialize(); SDL_AppResult handleInputEvent(SDL_Event* event); void run(); diff --git a/src/main.cpp b/src/main.cpp index 98eac95..20a5745 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,16 +1,10 @@ #define SDL_MAIN_USE_CALLBACKS 1 // 启用回调函数模式 #include #include -#include "core/Game.h" -#include "ui/Render.h" -#include "input/InputManager.h" +#include "core/GameApplication.h" //使用SDL3的appstate进行隔离,避免全局变量 -struct AppState { - std::unique_ptr g_game; - std::unique_ptr g_renderer; - std::unique_ptr g_input; -}; + @@ -30,45 +24,43 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { } // 创建游戏实例 - auto state = new AppState(); - state->g_game = std::make_unique(); - state->g_renderer = std::make_unique(WIDTH, HEIGHT); - state->g_input = std::make_unique(); + auto gameapp = new GameApplication(); - if (!state->g_game->initialize() || !state->g_renderer->initialize()) { + + if (!gameapp->initialize()) { SDL_Log("游戏初始化失败!"); - delete state; + delete gameapp; return SDL_APP_FAILURE; } - *appstate = state; + *appstate = gameapp; return SDL_APP_CONTINUE; } // 2. 事件处理回调 SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { - auto s = static_cast(appstate); + auto gameapp = static_cast(appstate); - auto result = s->g_input->handleInputEvent(event, s->g_renderer.get(), s->g_game.get()); + auto result = gameapp->handleInputEvent(event); return result; } // 3. 主循环迭代回调(每帧调用) SDL_AppResult SDL_AppIterate(void *appstate) { - auto s = static_cast(appstate); - if (s) { //防止空指针 - // 更新游戏逻辑 + auto gameapp = static_cast(appstate); + if (gameapp) { //防止空指针 + + gameapp->run(); + - // 渲染帧 - s->g_renderer->render(); } return SDL_APP_CONTINUE; } // 4. 应用退出回调(清理资源) void SDL_AppQuit(void *appstate, SDL_AppResult result) { - delete static_cast(appstate); + delete static_cast(appstate); SDL_Quit(); } \ No newline at end of file