diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-08-12 14:15:58 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-08-12 14:21:09 -0700 |
commit | b77b7482bf0570efa9b8f319ccb591fbab2d5f89 (patch) | |
tree | 3628a0ea7e99114dcd6daf74371424fc5e6496e6 /tests | |
parent | 7c9c4d8cdd8f4a00322916d8d701f155e60eac49 (diff) |
split out handling SDL events from making C events for them, to make SDL_PumpEvents work; fixes #1419
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/runner.py | 13 | ||||
-rw-r--r-- | tests/sdl_pumpevents.c | 54 |
2 files changed, 67 insertions, 0 deletions
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; +} |