From 2c5bcc7a99d82b054ae592fb2fb756b85b93de29 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 2 Jan 2023 12:11:46 +0900 Subject: [PATCH] WIP more work (glue code) --- src/ems.cc | 58 +++++++++++++++++++++++++++++++++++++++++++ src/ems.h | 9 +++++++ src/main.cc | 35 ++++++++++++++++++-------- wasm_build/.gitignore | 1 + wasm_build/Makefile | 11 +++++--- 5 files changed, 100 insertions(+), 14 deletions(-) create mode 100644 src/ems.cc create mode 100644 src/ems.h diff --git a/src/ems.cc b/src/ems.cc new file mode 100644 index 0000000..7dd7774 --- /dev/null +++ b/src/ems.cc @@ -0,0 +1,58 @@ +#include "ems.h" + +#ifdef __EMSCRIPTEN__ +#include +#include + +EM_JS(void, js_set_ready, (), { Rune.actions.set_ready(); }); + +EM_JS(void, js_set_choices, + (const char *first, const char *second, const char *third), { + Rune.actions.set_choices(UTF8ToString(first), UTF8ToString(second), + UTF8ToString(third)); + }); + +EM_JS(int, canvas_get_width, (), + { return document.getElementById("canvas").clientWidth; }); + +EM_JS(int, canvas_get_height, (), + { return document.getElementById("canvas").clientHeight; }); +#else +#include +#endif + +void call_js_set_ready() { +#ifdef __EMSCRIPTEN__ + js_set_ready(); +#else + std::clog << "WARNING: emscripten not enabled, cannot call js_set_ready()!" + << std::endl; +#endif +} + +void call_js_set_choices(const char *first, const char *second, + const char *third) { +#ifdef __EMSCRIPTEN__ + js_set_choices(first, second, third); +#else + std::clog << "WARNING: emscripten not enabled, cannot call js_set_choices()!" + << std::endl; +#endif +} + +int call_js_get_canvas_width() { + +#ifdef __EMSCRIPTEN__ + return canvas_get_width(); +#else + return 800; +#endif +} + +int call_js_get_canvas_height() { +#ifdef __EMSCRIPTEN__ + return canvas_get_height(); +#else + return 500; +#endif +} diff --git a/src/ems.h b/src/ems.h new file mode 100644 index 0000000..9218531 --- /dev/null +++ b/src/ems.h @@ -0,0 +1,9 @@ +#ifndef ROCK_PAPER_SCISSORS_DUEL_EMSCRIPTEN_H_ +#define ROCK_PAPER_SCISSORS_DUEL_EMSCRIPTEN_H_ + +extern void call_js_set_ready(); +extern void call_js_set_choices(const char *first, const char *second, const char *third); +extern int call_js_get_canvas_width(); +extern int call_js_get_canvas_height(); + +#endif diff --git a/src/main.cc b/src/main.cc index f18db16..4936126 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,6 +2,8 @@ #ifdef __EMSCRIPTEN__ #include #include + +#include "ems.h" #else #include #endif @@ -26,15 +28,22 @@ void EMSCRIPTEN_KEEPALIVE game_visual_update( } // end em exposed functions -EM_JS(void, js_set_ready, (), { Rune.actions.set_ready(); }); - -EM_JS(void, js_set_choices, - (const char *first, const char *second, const char *third), { - Rune.actions.set_choices(UTF8ToString(first), UTF8ToString(second), - UTF8ToString(third)); - }); +EM_BOOL resize_event_callback(int event_type, const EmscriptenUiEvent *event, + void *ud) { + if (event_type == EMSCRIPTEN_EVENT_RESIZE) { + SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height()); + } + return false; +} #endif +void game_update(void *game_ptr) { + BeginDrawing(); + ClearBackground(BLACK); + DrawText("Testing...", 100, 100, 30, RAYWHITE); + EndDrawing(); +} + int main() { #ifdef __EMSCRIPTEN__ InitWindow(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, @@ -44,14 +53,18 @@ int main() { #endif #ifdef __EMSCRIPTEN__ + SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height()); + + emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, false, + resize_event_callback); + + // TODO set game ptr + emscripten_set_main_loop_arg(game_update, nullptr, 0, 1); #else SetTargetFPS(60); while (!WindowShouldClose()) { - BeginDrawing(); - ClearBackground(BLACK); - DrawText("Testing...", 100, 100, 30, RAYWHITE); - EndDrawing(); + game_update(nullptr); } CloseAudioDevice(); diff --git a/wasm_build/.gitignore b/wasm_build/.gitignore index f60af13..91769f1 100644 --- a/wasm_build/.gitignore +++ b/wasm_build/.gitignore @@ -2,3 +2,4 @@ /rock_paper_scissors_duel.js /rock_paper_scissors_duel.wasm /rock_paper_scissors_duel.data +/index.html diff --git a/wasm_build/Makefile b/wasm_build/Makefile index 5a932af..3370736 100644 --- a/wasm_build/Makefile +++ b/wasm_build/Makefile @@ -5,14 +5,15 @@ else endif SOURCES = \ - ../src/main.cc + ../src/main.cc \ + ../src/ems.cc HEADERS = \ ../src/constants.h CXX = source ${HOME}/git/emsdk/emsdk_env.sh && em++ -all: | format rock_paper_scissors_duel.html +all: | format rock_paper_scissors_duel.html index.html rock_paper_scissors_duel.html: ${SOURCES} ${HEADERS} ${CXX} -o rock_paper_scissors_duel.html \ @@ -23,13 +24,17 @@ rock_paper_scissors_duel.html: ${SOURCES} ${HEADERS} ${OTHER_FLAGS} \ ${SOURCES} -.PHONY: clean format +.PHONY: clean format index.html + +index.html: + ln -sf rock_paper_scissors_duel.html index.html clean: rm -f rock_paper_scissors_duel.html rm -f rock_paper_scissors_duel.js rm -f rock_paper_scissors_duel.wasm rm -f rock_paper_scissors_duel.data + rm -f index.html format: clang-format -i --style=file ${SOURCES} ${HEADERS} -- 2.49.0