From 6cbc04425131120096ce06081a2b17f871c7ac2b Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 31 Aug 2023 22:11:04 +0900 Subject: [PATCH] Add test fn to clear screens from js --- src/game.cc | 2 ++ src/game.h | 2 ++ src/main.cc | 14 ++++++++++++++ wasm_build/custom_shell.html | 3 +++ 4 files changed, 21 insertions(+) diff --git a/src/game.cc b/src/game.cc index 72359dc..6522250 100644 --- a/src/game.cc +++ b/src/game.cc @@ -21,3 +21,5 @@ void Game::update() { } void Game::draw() { screen_stack->draw(); } + +void Game::clear_screens() { screen_stack->clear_screens(); } diff --git a/src/game.h b/src/game.h index b3fd492..1d6c764 100644 --- a/src/game.h +++ b/src/game.h @@ -22,6 +22,8 @@ class Game { void update(); void draw(); + void clear_screens(); + private: ScreenStack::Ptr screen_stack; std::chrono::steady_clock::time_point prev_time; diff --git a/src/main.cc b/src/main.cc index 85a52fc..a0287e2 100644 --- a/src/main.cc +++ b/src/main.cc @@ -15,6 +15,9 @@ #include "game.h" #ifdef __EMSCRIPTEN__ + +extern Game *global_game_ptr = nullptr; + extern "C" { EM_BOOL resize_event_callback(int event_type, const EmscriptenUiEvent *event, @@ -25,6 +28,15 @@ EM_BOOL resize_event_callback(int event_type, const EmscriptenUiEvent *event, } return false; } // resize_event_callback(...) + +int EMSCRIPTEN_KEEPALIVE clear_all_screens() { + if (global_game_ptr) { + global_game_ptr->clear_screens(); + return 0; + } + return 1; +} + } // extern "C" #endif @@ -46,6 +58,8 @@ int main() { #ifdef __EMSCRIPTEN__ Game game{}; + global_game_ptr = &game; + SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height()); emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, &game, false, diff --git a/wasm_build/custom_shell.html b/wasm_build/custom_shell.html index b295623..2bf70b1 100644 --- a/wasm_build/custom_shell.html +++ b/wasm_build/custom_shell.html @@ -59,6 +59,9 @@ window.onerror = function() { console.log("onerror: " + event); }; + function demo0_clear_all_screens() { + Module.ccall('clear_all_screens', 'number', [], []); + }; {{{ SCRIPT }}}