diff options
-rw-r--r-- | src/analyzer.js | 10 | ||||
-rw-r--r-- | src/library.js | 13 | ||||
-rw-r--r-- | src/library_openal.js | 2 | ||||
-rw-r--r-- | src/library_sdl.js | 76 | ||||
-rw-r--r-- | tests/cases/phi24_ta2.ll | 3 | ||||
-rwxr-xr-x | tests/runner.py | 13 | ||||
-rw-r--r-- | tests/sdl_pumpevents.c | 54 |
7 files changed, 138 insertions, 33 deletions
diff --git a/src/analyzer.js b/src/analyzer.js index 1a752305..931ce421 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -765,7 +765,15 @@ function analyzer(data, sidePass) { } break; } - case 'add': case 'sub': case 'sdiv': case 'udiv': case 'mul': case 'urem': case 'srem': + case 'add': case 'sub': case 'sdiv': case 'udiv': case 'mul': case 'urem': case 'srem': { + if (sourceBits < 32) { + // when we add illegal types like i24, we must work on the singleton chunks + item.assignTo += '$0'; + item.params[0].ident += '$0'; + item.params[1].ident += '$0'; + } + // fall through + } case 'uitofp': case 'sitofp': case 'fptosi': case 'fptoui': { // We cannot do these in parallel chunks of 32-bit operations. We will handle these in processMathop i++; diff --git a/src/library.js b/src/library.js index fedb8760..815badc1 100644 --- a/src/library.js +++ b/src/library.js @@ -1413,9 +1413,16 @@ LibraryManager.library = { // http://pubs.opengroup.org/onlinepubs/000095399/functions/usleep.html // We're single-threaded, so use a busy loop. Super-ugly. var msec = useconds / 1000; - var start = Date.now(); - while (Date.now() - start < msec) { - // Do nothing. + if (ENVIRONMENT_IS_WEB && window['performance'] && window['performance']['now']) { + var start = window['performance']['now'](); + while (window['performance']['now']() - start < msec) { + // Do nothing. + } + } else { + var start = Date.now(); + while (Date.now() - start < msec) { + // Do nothing. + } } return 0; }, diff --git a/src/library_openal.js b/src/library_openal.js index 1be5e372..cbb5c0bb 100644 --- a/src/library_openal.js +++ b/src/library_openal.js @@ -162,7 +162,7 @@ var LibraryOpenAL = { alcDestroyContext: function(context) { // Stop playback, etc - clearInterval(context.interval); + clearInterval(AL.contexts[context - 1].interval); }, alcCloseDevice: function(device) { 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/cases/phi24_ta2.ll b/tests/cases/phi24_ta2.ll index b5b0664b..4894d5e6 100644 --- a/tests/cases/phi24_ta2.ll +++ b/tests/cases/phi24_ta2.ll @@ -549,7 +549,8 @@ safe_mod_func_uint32_t_u_u.exit.i.i: ; preds = %189, %.preheader..p %p_5.sroa.0.0.extract.trunc2674116.i.i = phi i8 [ %p_5.sroa.0.0.extract.trunc2670.i.i, %189 ], [ -1, %.preheader..preheader.split_crit_edge.i.i ] %p_5.sroa.1.sroa.0.0.load6982115.i.i = phi i24 [ %p_5.sroa.1.sroa.0.0.load6978.i.i, %189 ], [ -1, %.preheader..preheader.split_crit_edge.i.i ] store i16 0, i16* @g_84, align 2 - %p_5.sroa.1.1.insert.ext36.i.i = trunc i24 %p_5.sroa.1.sroa.0.0.load6982115.i.i to i16 + %adddd = add i24 %p_5.sroa.1.sroa.0.0.load6982115.i.i, 1 ; test i24 add + %p_5.sroa.1.1.insert.ext36.i.i = trunc i24 %adddd to i16 %p_5.sroa.1.1.insert.shift37.i.i = shl i16 %p_5.sroa.1.1.insert.ext36.i.i, 8 %p_5.sroa.0.0.insert.ext10.i.i = zext i8 %p_5.sroa.0.0.extract.trunc2674116.i.i to i16 %p_5.sroa.0.0.insert.insert12.i.i = or i16 %p_5.sroa.1.1.insert.shift37.i.i, %p_5.sroa.0.0.insert.ext10.i.i 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; +} |