From 42d796bb83d39127cf2942583d8e93ba8a1be4d3 Mon Sep 17 00:00:00 2001 From: Ziyu Date: Wed, 25 Feb 2026 10:19:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96web=E7=89=88=E9=80=80?= =?UTF-8?q?=E5=87=BA=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BC=98=E5=8C=96readme?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README-ZN.md | 5 ++--- README.md | 5 ++--- src/engine/core/game_app.cpp | 41 ++++++++++++++++++++++++++++++------ src/engine/core/game_app.h | 3 +++ 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/README-ZN.md b/README-ZN.md index b3960aa..6f1b306 100644 --- a/README-ZN.md +++ b/README-ZN.md @@ -69,9 +69,8 @@ cmake --build build 若需支持或反馈,请通过本仓库的 GitHub issues 板块联系我们。您的反馈对改进本系列教程至关重要! ## 请我喝咖啡 -[!["Buy Me A Coffee"](https://storage.ko-fi.com/cdn/kofi2.png?v=3)](https://ko-fi.com/ziyugamedev) -[!["Support me on Afdian"](https://pic1.afdiancdn.com/static/img/welcome/button-sponsorme.png)](https://afdian.com/a/ziyugamedev) - +Buy Me A Coffee +Support me on Afdian ## QQ 交流群和我的微信二维码 diff --git a/README.md b/README.md index 0d23ddc..777b5ac 100644 --- a/README.md +++ b/README.md @@ -69,9 +69,8 @@ If you encounter trouble downloading from GitHub (especially on networks in main For support or feedback, please contact us through the GitHub issues section of this repository. Your feedback is crucial for making this series of tutorials better! ## Buy Me a Coffee -[!["Buy Me A Coffee"](https://storage.ko-fi.com/cdn/kofi2.png?v=3)](https://ko-fi.com/ziyugamedev) -[!["Support me on Afdian"](https://pic1.afdiancdn.com/static/img/welcome/button-sponsorme.png)](https://afdian.com/a/ziyugamedev) - +Buy Me A Coffee +Support me on Afdian ## QQ Discussion Group and My WeChat QR Code diff --git a/src/engine/core/game_app.cpp b/src/engine/core/game_app.cpp index f949d5b..e0cc04c 100644 --- a/src/engine/core/game_app.cpp +++ b/src/engine/core/game_app.cpp @@ -36,6 +36,7 @@ void GameApp::run() { } #ifdef __EMSCRIPTEN__ + web_main_loop_cancelled_ = false; // WebAssembly 环境下使用回调函数 // 0 表示无限帧率(由浏览器 requestAnimationFrame 控制),1 表示模拟无限循环 emscripten_set_main_loop_arg([](void* arg) { @@ -51,24 +52,50 @@ void GameApp::run() { } void GameApp::oneIter() { +#ifdef __EMSCRIPTEN__ + if (!is_running_) { + if (!web_main_loop_cancelled_) { + emscripten_cancel_main_loop(); + web_main_loop_cancelled_ = true; + close(); + } + return; + } +#else if (!is_running_) return; // 防止在退出标志设置后的额外调用 +#endif time_->update(); float delta_time = time_->getDeltaTime(); input_manager_->update(); // 每帧首先更新输入管理器 handleEvents(); + if (!is_running_) { +#ifdef __EMSCRIPTEN__ + if (!web_main_loop_cancelled_) { + emscripten_cancel_main_loop(); + web_main_loop_cancelled_ = true; + close(); + } +#endif + return; + } + update(delta_time); + if (!is_running_) { +#ifdef __EMSCRIPTEN__ + if (!web_main_loop_cancelled_) { + emscripten_cancel_main_loop(); + web_main_loop_cancelled_ = true; + close(); + } +#endif + return; + } + render(); // spdlog::info("delta_time: {}", delta_time); - -#ifdef __EMSCRIPTEN__ - if (!is_running_) { - emscripten_cancel_main_loop(); - close(); - } -#endif } void GameApp::registerSceneSetup(std::function func) diff --git a/src/engine/core/game_app.h b/src/engine/core/game_app.h index 0a2c9c6..53984e1 100644 --- a/src/engine/core/game_app.h +++ b/src/engine/core/game_app.h @@ -46,6 +46,9 @@ private: SDL_Window* window_ = nullptr; SDL_Renderer* sdl_renderer_ = nullptr; bool is_running_ = false; +#ifdef __EMSCRIPTEN__ + bool web_main_loop_cancelled_ = false; +#endif /// @brief 游戏场景设置函数,用于在运行游戏前设置初始场景 (GameApp不再决定初始场景是什么) std::function scene_setup_func_;