From 578a8bf0958a2cd76eee167c8f4022aab7d95061 Mon Sep 17 00:00:00 2001 From: kichikuou Date: Sun, 26 Jun 2022 14:56:23 +0900 Subject: [PATCH] Emscripten: Expose sys_restart() function --- src/sdlmain.cpp | 28 ++++++++++++++++++++-------- src/sys/nact.cpp | 8 +++++++- src/sys/nact.h | 1 + src/sys/nact_input.cpp | 2 +- src/win/nact_win.cpp | 4 ++-- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/sdlmain.cpp b/src/sdlmain.cpp index 231b8a0..8fe64f1 100644 --- a/src/sdlmain.cpp +++ b/src/sdlmain.cpp @@ -15,6 +15,7 @@ #endif SDL_Window* g_window; +NACT* g_nact; int main(int argc, char *argv[]) { @@ -36,13 +37,13 @@ int main(int argc, char *argv[]) g_window = SDL_CreateWindow("Scenario Decoder SYSTEM3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 400, flags); // system3 初期化 - NACT* nact = NACT::create(config); + g_nact = NACT::create(config); if (!config.save_dir.empty()) { std::string path = config.save_dir; if (path[path.size() - 1] == '@') { path.pop_back(); - const char* gid = nact->get_game_id(); + const char* gid = g_nact->get_game_id(); if (gid) { path += gid; #ifdef WIN32 @@ -55,17 +56,17 @@ int main(int argc, char *argv[]) FILEIO::SetSaveDir(path); } - const char* title = nact->get_title(); + const char* title = g_nact->get_title(); if (title) { char buf[128]; - sprintf_s(buf, 128, "Scenario Decoder SYSTEM%d: %s", nact->sys_ver, title); + sprintf_s(buf, 128, "Scenario Decoder SYSTEM%d: %s", g_nact->sys_ver, title); #ifdef __EMSCRIPTEN__ EM_ASM_ARGS({ xsystem35.shell.setWindowTitle(UTF8ToString($0)); }, buf); #else SDL_SetWindowTitle(g_window, buf); #endif } else { - WARNING("Cannot determine game id. crc32_a: %08x, crc32_b: %08x", nact->crc32_a, nact->crc32_b); + WARNING("Cannot determine game id. crc32_a: %08x, crc32_b: %08x", g_nact->crc32_a, g_nact->crc32_b); } #ifdef __EMSCRIPTEN__ @@ -77,10 +78,10 @@ int main(int argc, char *argv[]) bool restart = true; while (restart) { - restart = nact->mainloop(); - delete nact; + restart = g_nact->mainloop(); + delete g_nact; if (restart) - nact = NACT::create(config); + g_nact = NACT::create(config); } SDL_DestroyWindow(g_window); @@ -88,3 +89,14 @@ int main(int argc, char *argv[]) return 0; } + +#ifdef __EMSCRIPTEN__ + +extern "C" +EMSCRIPTEN_KEEPALIVE +void sys_restart() +{ + g_nact->quit(true); +} + +#endif diff --git a/src/sys/nact.cpp b/src/sys/nact.cpp index 4e7d532..d79cf27 100644 --- a/src/sys/nact.cpp +++ b/src/sys/nact.cpp @@ -137,6 +137,12 @@ bool NACT::mainloop() return restart_after_terminate; } +void NACT::quit(bool restart) +{ + terminate = true; + restart_after_terminate = restart; +} + // コマンドパーサ EMSCRIPTEN_KEEPALIVE // Prevent inlining, because this function is listed in ASYNCIFY_ADD @@ -150,7 +156,7 @@ void NACT::execute() // 致命的なエラー発生 or 正常終了 if(fatal_error) { if(!post_quit) { - terminate = true; + quit(false); } post_quit = true; return; diff --git a/src/sys/nact.h b/src/sys/nact.h index 684fe54..a3b0a6e 100644 --- a/src/sys/nact.h +++ b/src/sys/nact.h @@ -253,6 +253,7 @@ protected: public: bool mainloop(); void sys_sleep(int ms); + void quit(bool restart); int get_screen_height(); diff --git a/src/sys/nact_input.cpp b/src/sys/nact_input.cpp index 70e30a9..8f714a8 100644 --- a/src/sys/nact_input.cpp +++ b/src/sys/nact_input.cpp @@ -25,7 +25,7 @@ void NACT::pump_events() switch (e.type) { case SDL_QUIT: - terminate = true; + quit(false); break; case SDL_WINDOWEVENT: diff --git a/src/win/nact_win.cpp b/src/win/nact_win.cpp index 2c3668a..b36c477 100644 --- a/src/win/nact_win.cpp +++ b/src/win/nact_win.cpp @@ -175,10 +175,10 @@ bool NACT::handle_platform_event(const SDL_Event& e) save_screenshot(ags); break; case ID_RESTART: - terminate = restart_after_terminate = true; + quit(true); break; case ID_EXIT: - terminate = true; + quit(false); break; case ID_SCREEN_WINDOW: SDL_SetWindowFullscreen(g_window, 0);