From eed7f2064fa1e963a010e7e53c6360a59f410c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sun, 11 Oct 2020 14:39:46 +0200 Subject: [PATCH] Add buttons to web version --- HTMLshell.html | 92 +++++++++++++++++++++++++++++++++++++------------- main_sdl.c | 14 ++++++++ make.sh | 2 +- 3 files changed, 83 insertions(+), 25 deletions(-) diff --git a/HTMLshell.html b/HTMLshell.html index ca4e3a9..9b589a5 100644 --- a/HTMLshell.html +++ b/HTMLshell.html @@ -18,34 +18,78 @@ game - + + - - - + return document.getElementById('canvas'); + } + )(), + onRuntimeInitialized: function() + { + pressFunc = Module.cwrap('webButton', '', ['number','number']); + } + }; - {{{ SCRIPT }}} + function down(button) + { + pressFunc(button,1); + } + + function up(button) + { + pressFunc(button,0); + } + + + {{{ SCRIPT }}} + + + + + +
diff --git a/main_sdl.c b/main_sdl.c index dec4ae1..bfe0322 100644 --- a/main_sdl.c +++ b/main_sdl.c @@ -88,6 +88,7 @@ #include "sounds.h" const uint8_t *sdlKeyboardState; +uint8_t webKeyboardState[SFG_KEY_COUNT]; uint8_t sdlMouseButtonState = 0; int8_t sdlMouseWheelState = 0; @@ -155,6 +156,13 @@ void SFG_sleepMs(uint16_t timeMs) #endif } +#ifdef __EMSCRIPTEN__ +void webButton(uint8_t key, uint8_t down) +{ + webKeyboardState[key] = down; +} +#endif + void SFG_getMouseOffset(int16_t *x, int16_t *y) { #ifndef __EMSCRIPTEN__ @@ -176,6 +184,9 @@ void SFG_processEvent(uint8_t event, uint8_t data) int8_t SFG_keyPressed(uint8_t key) { + if (webKeyboardState[key]) + return 1; + switch (key) { case SFG_KEY_UP: @@ -376,6 +387,9 @@ int main(int argc, char *argv[]) uint8_t argForceWindow = 0; uint8_t argForceFullscreen = 0; + for (uint8_t i = 0; i < SFG_KEY_COUNT; ++i) + webKeyboardState[i] = 0; + for (uint8_t i = 1; i < argc; ++i) { if (argv[i][0] == '-' && argv[i][1] == 'h' && argv[i][2] == 0) diff --git a/make.sh b/make.sh index b80a189..1d52d2e 100755 --- a/make.sh +++ b/make.sh @@ -64,7 +64,7 @@ elif [ $1 == "emscripten" ]; then # emscripten (browser Javascript) build, requires: # - emscripten - emcc ./main_sdl.c -s USE_SDL=2 -O3 -lopenal --shell-file HTMLshell.html -o game.html + ../emsdk/upstream/emscripten/emcc ./main_sdl.c -s USE_SDL=2 -O3 -lopenal --shell-file HTMLshell.html -o game.html -s EXPORTED_FUNCTIONS='["_main","_webButton"]' -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]' else echo "unknown parameter: $1" fi