diff options
-rw-r--r-- | src/library_sdl.js | 76 | ||||
-rwxr-xr-x | tests/runner.py | 13 | ||||
-rw-r--r-- | tests/sdl_pumpevents.c | 54 |
3 files changed, 116 insertions, 27 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index 24f3de1b..92cfc7e5 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -519,6 +519,46 @@ var LibrarySDL = { return; }, + handleEvent: function(event) { + if (event.handled) return; + event.handled = true; + + switch (event.type) { + case 'keydown': case 'keyup': { + var down = event.type === 'keydown'; + var code = SDL.keyCodes[event.keyCode] || event.keyCode; + + {{{ makeSetValue('SDL.keyboardState', 'code', 'down', 'i8') }}}; + // TODO: lmeta, rmeta, numlock, capslock, KMOD_MODE, KMOD_RESERVED + SDL.modState = ({{{ makeGetValue('SDL.keyboardState', '1248', 'i8') }}} ? 0x0040 | 0x0080 : 0) | // KMOD_LCTRL & KMOD_RCTRL + ({{{ makeGetValue('SDL.keyboardState', '1249', 'i8') }}} ? 0x0001 | 0x0002 : 0) | // KMOD_LSHIFT & KMOD_RSHIFT + ({{{ makeGetValue('SDL.keyboardState', '1250', 'i8') }}} ? 0x0100 | 0x0200 : 0); // KMOD_LALT & KMOD_RALT + + if (down) { + SDL.keyboardMap[code] = event.keyCode; // save the DOM input, which we can use to unpress it during blur + } else { + delete SDL.keyboardMap[code]; + } + + break; + } + case 'mousedown': case 'mouseup': + if (event.type == 'mousedown') { + // SDL_BUTTON(x) is defined as (1 << ((x)-1)). SDL buttons are 1-3, + // and DOM buttons are 0-2, so this means that the below formula is + // correct. + SDL.buttonState |= 1 << event.button; + } else if (event.type == 'mouseup') { + SDL.buttonState &= ~(1 << event.button); + } + // fall through + case 'mousemove': { + Browser.calculateMouseEvent(event); + break; + } + } + }, + makeCEvent: function(event, ptr) { if (typeof event === 'number') { // This is a pointer to a native C event that was SDL_PushEvent'ed @@ -526,7 +566,9 @@ var LibrarySDL = { return; } - switch(event.type) { + SDL.handleEvent(event); + + switch (event.type) { case 'keydown': case 'keyup': { var down = event.type === 'keydown'; //Module.print('Received key event: ' + event.keyCode); @@ -543,19 +585,6 @@ var LibrarySDL = { scan = SDL.scanCodes[key] || key; } - var code = SDL.keyCodes[event.keyCode] || event.keyCode; - {{{ makeSetValue('SDL.keyboardState', 'code', 'down', 'i8') }}}; - if (down) { - SDL.keyboardMap[code] = event.keyCode; // save the DOM input, which we can use to unpress it during blur - } else { - delete SDL.keyboardMap[code]; - } - - // TODO: lmeta, rmeta, numlock, capslock, KMOD_MODE, KMOD_RESERVED - SDL.modState = ({{{ makeGetValue('SDL.keyboardState', '1248', 'i8') }}} ? 0x0040 | 0x0080 : 0) | // KMOD_LCTRL & KMOD_RCTRL - ({{{ makeGetValue('SDL.keyboardState', '1249', 'i8') }}} ? 0x0001 | 0x0002 : 0) | // KMOD_LSHIFT & KMOD_RSHIFT - ({{{ makeGetValue('SDL.keyboardState', '1250', 'i8') }}} ? 0x0100 | 0x0200 : 0); // KMOD_LALT & KMOD_RALT - {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}} {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.state', 'down ? 1 : 0', 'i8') }}} {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.repeat', '0', 'i8') }}} // TODO @@ -575,18 +604,7 @@ var LibrarySDL = { } break; } - case 'mousedown': case 'mouseup': - if (event.type == 'mousedown') { - // SDL_BUTTON(x) is defined as (1 << ((x)-1)). SDL buttons are 1-3, - // and DOM buttons are 0-2, so this means that the below formula is - // correct. - SDL.buttonState |= 1 << event.button; - } else if (event.type == 'mouseup') { - SDL.buttonState &= ~(1 << event.button); - } - // fall through - case 'mousemove': { - Browser.calculateMouseEvent(event); + case 'mousedown': case 'mouseup': case 'mousemove': { if (event.type != 'mousemove') { var down = event.type === 'mousedown'; {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}}; @@ -1174,7 +1192,11 @@ var LibrarySDL = { } }, - SDL_PumpEvents: function(){}, + SDL_PumpEvents: function(){ + SDL.events.forEach(function(event) { + SDL.handleEvent(event); + }); + }, SDL_SetColors: function(surf, colors, firstColor, nColors) { var surfData = SDL.surfaces[surf]; diff --git a/tests/runner.py b/tests/runner.py index 9a8670b6..8d2cbcb4 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -13471,6 +13471,19 @@ Press any key to continue.''' Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_mouse.c'), '-O2', '--minify', '0', '-o', 'sdl_mouse.js', '--pre-js', 'pre.js']).communicate() self.run_browser('page.html', '', '/report_result?600') + def test_sdl_pumpevents(self): + # key events should be detected using SDL_PumpEvents + open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' + function keydown(c) { + var event = document.createEvent("KeyboardEvent"); + event.initKeyEvent("keydown", true, true, window, + 0, 0, 0, 0, + c, c); + document.dispatchEvent(event); + } + ''') + self.btest('sdl_pumpevents.c', expected='3', args=['--pre-js', 'pre.js']) + def test_sdl_audio(self): shutil.copyfile(path_from_root('tests', 'sounds', 'alarmvictory_1.ogg'), os.path.join(self.get_dir(), 'sound.ogg')) shutil.copyfile(path_from_root('tests', 'sounds', 'alarmcreatemiltaryfoot_1.wav'), os.path.join(self.get_dir(), 'sound2.wav')) diff --git a/tests/sdl_pumpevents.c b/tests/sdl_pumpevents.c new file mode 100644 index 00000000..64becaad --- /dev/null +++ b/tests/sdl_pumpevents.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <stdlib.h> +#include <SDL/SDL.h> + +#include <emscripten.h> +// bug - SDL_GetKeyboardState doesn't return scancodes, it returns keycodes, so acts exactly like +// SDL_GetKeyState instead +#define SDL_GetKeyState SDL_GetKeyboardState + +int result = 0; + +int loop1() +{ + unsigned i; + int r = 0; + + // method 1: SDL_PollEvent loop + SDL_Event e; + while (SDL_PollEvent(&e)); + + const Uint8 *keys = SDL_GetKeyState(NULL); + if (keys[SDLK_LEFT]) + r = 1; + + return r; +} + +int loop2() +{ + unsigned i; + int r = 0; + + // method 2: SDL_PumpEvents + SDL_PumpEvents(); + + const Uint8 *keys = SDL_GetKeyState(NULL); + if (keys[SDLK_RIGHT]) + r = 2; + + return r; +} + +int main(int argc, char *argv[]) +{ + SDL_Init(SDL_INIT_EVERYTHING); + SDL_SetVideoMode(600, 400, 32, SDL_SWSURFACE); + + emscripten_run_script("keydown(37);"); // left + result += loop1(); + emscripten_run_script("keydown(39);"); // right + result += loop2(); + REPORT_RESULT(); + return 0; +} |