aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-03-24 15:04:41 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-03-24 15:04:41 -0700
commit6ad919104f7c00ae3774be7d275d9a259869dbc9 (patch)
treef6025334ba38604c3bd82fe359c46c930dd1fd6d
parent63af5eac0a10434cc62244d80675f13473d38b02 (diff)
SDL mouse support and emscripten_set_main_loop
-rw-r--r--src/library_browser.js18
-rw-r--r--src/library_sdl.js58
-rw-r--r--system/include/emscripten.h9
-rwxr-xr-xtests/runner.py32
-rw-r--r--tests/sdl_mouse.c55
5 files changed, 165 insertions, 7 deletions
diff --git a/src/library_browser.js b/src/library_browser.js
index 55de7a5c..f5f0ed82 100644
--- a/src/library_browser.js
+++ b/src/library_browser.js
@@ -3,9 +3,27 @@
// Utilities for browser environments
mergeInto(LibraryManager.library, {
+ emscripten_set_main_loop: function(func, fps) {
+ fps = fps || 60; // TODO: use requestAnimationFrame
+ _emscripten_set_main_loop.cancel = false;
+ var jsFunc = FUNCTION_TABLE[func];
+ function doOne() {
+ if (_emscripten_set_main_loop.cancel) return;
+ jsFunc();
+ setTimeout(doOne, 1000/fps); // doing this each time means that on exception, we stop
+ }
+ setTimeout(doOne, 1000/fps);
+ },
+
+ emscripten_cancel_main_loop: function(func) {
+ _emscripten_set_main_loop.cancel = true;
+ },
+
$Browser: {
// Given binary data for an image, in a format like PNG or JPG, we convert it
// to flat pixel data. We do so using the browser's native code.
+ // This is deprecated, it is preferred to load binary files, createObjectURL, etc.,
+ // see the sdl_* tests.
decodeImage: function(pixels, format) {
function encodeBase64(data) {
var BASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
diff --git a/src/library_sdl.js b/src/library_sdl.js
index 4c21394f..aafa1df8 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -90,6 +90,8 @@ mergeInto(LibraryManager.library, {
fonts: [null],
keyboardState: null,
+ mouseX: 0,
+ mouseY: 0,
keyCodes: { // DOM code ==> SDL code
38: 1106, // up arrow
@@ -127,6 +129,28 @@ mergeInto(LibraryManager.library, {
['i16', 'mod'],
['i32', 'unicode']
]),
+ MouseMotionEvent: Runtime.generateStructInfo([
+ ['i32', 'type'],
+ ['i32', 'windowID'],
+ ['i8', 'state'],
+ ['i8', 'padding1'],
+ ['i8', 'padding2'],
+ ['i8', 'padding3'],
+ ['i32', 'x'],
+ ['i32', 'y'],
+ ['i32', 'xrel'],
+ ['i32', 'yrel']
+ ]),
+ MouseButtonEvent: Runtime.generateStructInfo([
+ ['i32', 'type'],
+ ['i32', 'windowID'],
+ ['i8', 'button'],
+ ['i8', 'state'],
+ ['i8', 'padding1'],
+ ['i8', 'padding2'],
+ ['i32', 'x'],
+ ['i32', 'y']
+ ]),
AudioSpec: Runtime.generateStructInfo([
['i32', 'freq'],
['i16', 'format'],
@@ -245,7 +269,7 @@ mergeInto(LibraryManager.library, {
receiveEvent: function(event) {
switch(event.type) {
- case 'keydown': case 'keyup':
+ case 'keydown': case 'keyup': case 'mousedown': case 'mouseup': case 'mouseover':
SDL.events.push(event);
break;
}
@@ -261,7 +285,7 @@ mergeInto(LibraryManager.library, {
}
switch(event.type) {
- case 'keydown': case 'keyup':
+ case 'keydown': case 'keyup': {
var down = event.type === 'keydown';
var key = SDL.keyCodes[event.keyCode] || event.keyCode;
if (key >= 65 && key <= 90) {
@@ -280,7 +304,28 @@ mergeInto(LibraryManager.library, {
{{{ makeSetValue('SDL.keyboardState', 'SDL.keyCodes[event.keyCode] || event.keyCode', 'event.type == "keydown"', 'i8') }}};
break;
- case 'keypress': break // TODO
+ }
+ case 'mousedown': case 'mouseup': {
+ var down = event.type === 'mousedown';
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.type', 'down ? 0x401 : 0x402', 'i32') }}};
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.button', 'event.button', 'i8') }}};
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.state', 'down ? 1 : 0', 'i8') }}};
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.x', 'event.clientX', 'i32') }}};
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.y', 'event.clientY', 'i32') }}};
+ break;
+ }
+ case 'mouseover': {
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.type', '0x400', 'i32') }}};
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.button', 'event.button', 'i8') }}};
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.state', 'down ? 1 : 0', 'i8') }}};
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.x', 'event.clientX', 'i32') }}};
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.y', 'event.clientY', 'i32') }}};
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.xrel', 'event.clientX - SDL.mouseX', 'i32') }}};
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.yrel', 'event.clientY - SDL.mouseY', 'i32') }}};
+ SDL.mouseX = event.clientX;
+ SDL.mouseY = event.clientY;
+ break;
+ }
default:
throw 'Unhandled SDL event: ' + event.type;
}
@@ -314,7 +359,7 @@ mergeInto(LibraryManager.library, {
SDL_Init__deps: ['$SDL'],
SDL_Init: function(what) {
SDL.startTime = Date.now();
- ['keydown', 'keyup', 'keypress'].forEach(function(event) {
+ ['keydown', 'keyup', 'mousedown', 'mouseup', 'mouseover'].forEach(function(event) {
addEventListener(event, SDL.receiveEvent, true);
});
SDL.keyboardState = _malloc(0x10000);
@@ -471,9 +516,8 @@ mergeInto(LibraryManager.library, {
},
SDL_GetMouseState: function(x, y) {
- // TODO:
- if (x) {{{ makeSetValue('x', '0', '0', 'i32') }}};
- if (y) {{{ makeSetValue('y', '0', '0', 'i32') }}};
+ if (x) {{{ makeSetValue('x', '0', 'SDL.mouseX', 'i32') }}};
+ if (y) {{{ makeSetValue('y', '0', 'SDL.mouseY', 'i32') }}};
return 0;
},
diff --git a/system/include/emscripten.h b/system/include/emscripten.h
index 7b135c5a..ea078e8c 100644
--- a/system/include/emscripten.h
+++ b/system/include/emscripten.h
@@ -19,6 +19,15 @@ extern void emscripten_run_script(const char *script);
extern int emscripten_run_script_int(const char *script);
/*
+ * Set a C function as the main event loop. The JS environment
+ * will call that function at a specified number of frames per
+ * second. Setting 0 as the fps will use the default browser
+ * frame rate.
+ */
+extern void emscripten_set_main_loop(void (*func)(), int fps);
+extern void emscripten_cancel_main_loop();
+
+/*
* This macro-looking function will cause Emscripten to
* generate a comment in the generated code.
* XXX This is deprecated for now, because it requires us to
diff --git a/tests/runner.py b/tests/runner.py
index 3b4680a2..fa566c33 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -6409,6 +6409,38 @@ f.close()
Popen(['python', EMCC, os.path.join(self.get_dir(), 'sdl_key.c'), '-o', 'page.html', '--pre-js', 'pre.js']).communicate()
self.run_browser('page.html', '', '/report_result?30030')
+ def test_sdl_mouse(self):
+ open(os.path.join(self.get_dir(), 'pre.js'), 'w').write('''
+ function simulateMouseEvent(x, y, button) {
+ var event = document.createEvent("MouseEvents");
+ if (button) {
+ var event1 = document.createEvent("MouseEvents");
+ event1.initMouseEvent('mousedown', true, true, window,
+ 1, x, y, x, y,
+ 0, 0, 0, 0,
+ button, null);
+ dispatchEvent(event1);
+ var event2 = document.createEvent("MouseEvents");
+ event2.initMouseEvent('mouseup', true, true, window,
+ 1, x, y, x, y,
+ 0, 0, 0, 0,
+ button, null);
+ dispatchEvent(event2);
+ } else {
+ var event1 = document.createEvent("MouseEvents");
+ event1.initMouseEvent('mouseover', true, true, window,
+ 0, x, y, x, y,
+ 0, 0, 0, 0,
+ 0, null);
+ dispatchEvent(event1);
+ }
+ }
+ ''')
+ open(os.path.join(self.get_dir(), 'sdl_mouse.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_mouse.c')).read()))
+
+ Popen(['python', EMCC, os.path.join(self.get_dir(), 'sdl_mouse.c'), '-o', 'page.html', '--pre-js', 'pre.js']).communicate()
+ self.run_browser('page.html', '', '/report_result?740')
+
def test_worker(self):
# Test running in a web worker
output = Popen(['python', EMCC, path_from_root('tests', 'hello_world_worker.cpp'), '-o', 'worker.js'], stdout=PIPE, stderr=PIPE).communicate()
diff --git a/tests/sdl_mouse.c b/tests/sdl_mouse.c
new file mode 100644
index 00000000..87619ef2
--- /dev/null
+++ b/tests/sdl_mouse.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <SDL/SDL.h>
+#include <SDL/SDL_ttf.h>
+#include <assert.h>
+#include <emscripten.h>
+
+int result = 1;
+
+void one() {
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch(event.type) {
+ case SDL_MOUSEMOTION: {
+ SDL_MouseMotionEvent *m = (SDL_MouseMotionEvent*)&event;
+ int x, y;
+ SDL_GetMouseState(&x, &y);
+ assert(x == m->x && y == m->y);
+ printf("motion: %d,%d %d,%d\n", m->x, m->y, m->xrel, m->yrel);
+ result += 2 * (m->x + m->y + m->xrel + m->yrel);
+ break;
+ }
+ case SDL_MOUSEBUTTONDOWN: {
+ SDL_MouseButtonEvent *m = (SDL_MouseButtonEvent*)&event;
+ if (m->button == 2) {
+ REPORT_RESULT();
+ emscripten_run_script("throw 'done'");
+ }
+ printf("button down: %d,%d %d,%d\n", m->button, m->state, m->x, m->y);
+ result += 3 * (m->button + m->state + m->x + m->y);
+ break;
+ }
+ case SDL_MOUSEBUTTONUP: {
+ SDL_MouseButtonEvent *m = (SDL_MouseButtonEvent*)&event;
+ printf("button up: %d,%d %d,%d\n", m->button, m->state, m->x, m->y);
+ result += 5 * (m->button + m->state + m->x + m->y);
+ break;
+ }
+ }
+ }
+}
+
+int main() {
+ SDL_Init(SDL_INIT_VIDEO);
+ SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
+
+ emscripten_run_script("simulateMouseEvent(10, 20, 0)"); // move from 0,0 to 10,20
+ emscripten_run_script("simulateMouseEvent(10, 20, 1)"); // click
+ emscripten_run_script("simulateMouseEvent(30, 77, 0)"); // move some more
+ emscripten_run_script("simulateMouseEvent(30, 77, 2)"); // trigger the end
+
+ emscripten_set_main_loop(one, 0);
+
+ return 0;
+}
+