diff options
Diffstat (limited to 'src/library_sdl.js')
-rw-r--r-- | src/library_sdl.js | 174 |
1 files changed, 106 insertions, 68 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index 71ab2277..a5080a99 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -41,15 +41,11 @@ var LibrarySDL = { // Note that images loaded before SDL_SetVideoMode will not get this optimization keyboardState: null, - shiftKey: false, - ctrlKey: false, - altKey: false, + keyboardMap: {}, textInput: false, startTime: null, - mouseX: 0, - mouseY: 0, buttonState: 0, DOMButtons: [0, 0, 0], @@ -410,10 +406,6 @@ var LibrarySDL = { } SDL.events.push(event); - if (SDL.events.length >= 10000) { - Module.printErr('SDL event queue full, dropping earliest event'); - SDL.events.shift(); - } break; case 'mouseout': // Un-press all pressed mouse buttons, because we might miss the release outside of the canvas @@ -429,6 +421,17 @@ var LibrarySDL = { } } break; + case 'blur': + case 'visibilitychange': { + // Un-press all pressed keys: TODO + for (var code in SDL.keyboardMap) { + SDL.events.push({ + type: 'keyup', + keyCode: SDL.keyboardMap[code] + }); + } + break; + } case 'unload': if (Browser.mainLoop.runner) { SDL.events.push(event); @@ -440,6 +443,10 @@ var LibrarySDL = { SDL.events.push(event); break; } + if (SDL.events.length >= 10000) { + Module.printErr('SDL event queue full, dropping events'); + SDL.events = SDL.events.slice(0, 10000); + } return false; }, @@ -476,14 +483,12 @@ var LibrarySDL = { {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.mod', '0', 'i32') }}} {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.unicode', 'key', 'i32') }}} - {{{ makeSetValue('SDL.keyboardState', 'SDL.keyCodes[event.keyCode] || event.keyCode', 'event.type == "keydown"', 'i8') }}}; - - if (event.keyCode == 16) { //shift - SDL.shiftKey = event.type == "keydown"; - } else if (event.keyCode == 17) { //control - SDL.ctrlKey = event.type == "keydown"; - } else if (event.keyCode == 18) { //alt - SDL.altKey = event.type == "keydown"; + 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]; } break; @@ -508,54 +513,22 @@ var LibrarySDL = { } // fall through case 'mousemove': { - if (Browser.pointerLock) { - // When the pointer is locked, calculate the coordinates - // based on the movement of the mouse. - // Workaround for Firefox bug 764498 - if (event.type != 'mousemove' && - ('mozMovementX' in event)) { - var movementX = 0, movementY = 0; - } else { - var movementX = Browser.getMovementX(event); - var movementY = Browser.getMovementY(event); - } - var x = SDL.mouseX + movementX; - var y = SDL.mouseY + movementY; - } else { - // Otherwise, calculate the movement based on the changes - // in the coordinates. - var rect = Module["canvas"].getBoundingClientRect(); - var x = event.pageX - (window.scrollX + rect.left); - var y = event.pageY - (window.scrollY + rect.top); - - // the canvas might be CSS-scaled compared to its backbuffer; - // SDL-using content will want mouse coordinates in terms - // of backbuffer units. - var cw = Module["canvas"].width; - var ch = Module["canvas"].height; - x = x * (cw / rect.width); - y = y * (ch / rect.height); - - var movementX = x - SDL.mouseX; - var movementY = y - SDL.mouseY; - } + Browser.calculateMouseEvent(event); if (event.type != 'mousemove') { var down = event.type === 'mousedown'; {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}}; {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.button', 'event.button+1', 'i8') }}}; // DOM buttons are 0-2, SDL 1-3 {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.state', 'down ? 1 : 0', 'i8') }}}; - {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.x', 'x', 'i32') }}}; - {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.y', 'y', 'i32') }}}; + {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.x', 'Browser.mouseX', 'i32') }}}; + {{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.y', 'Browser.mouseY', 'i32') }}}; } else { {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}}; {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.state', 'SDL.buttonState', 'i8') }}}; - {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.x', 'x', 'i32') }}}; - {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.y', 'y', 'i32') }}}; - {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.xrel', 'movementX', 'i32') }}}; - {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.yrel', 'movementY', 'i32') }}}; + {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.x', 'Browser.mouseX', 'i32') }}}; + {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.y', 'Browser.mouseY', 'i32') }}}; + {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.xrel', 'Browser.mouseMovementX', 'i32') }}}; + {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.yrel', 'Browser.mouseMovementY', 'i32') }}}; } - SDL.mouseX = x; - SDL.mouseY = y; break; } case 'unload': { @@ -574,9 +547,11 @@ var LibrarySDL = { estimateTextWidth: function(fontData, text) { var h = fontData.size; - var fontString = h + 'px sans-serif'; - // TODO: use temp context, not screen's, to avoid affecting its performance? - var tempCtx = SDL.surfaces[SDL.screen].ctx; + var fontString = h + 'px ' + fontData.name; + var tempCtx = SDL.ttfContext; +#if ASSERTIONS + assert(tempCtx, 'TTF_Init must have been called'); +#endif tempCtx.save(); tempCtx.font = fontString; var ret = tempCtx.measureText(text).width | 0; @@ -642,9 +617,11 @@ var LibrarySDL = { document.onkeydown = SDL.receiveEvent; document.onkeyup = SDL.receiveEvent; document.onkeypress = SDL.receiveEvent; + document.onblur = SDL.receiveEvent; + document.addEventListener("visibilitychange", SDL.receiveEvent); } window.onunload = SDL.receiveEvent; - SDL.keyboardState = _malloc(0x10000); + SDL.keyboardState = _malloc(0x10000); // Our SDL needs 512, but 64K is safe for older SDLs _memset(SDL.keyboardState, 0, 0x10000); // Initialize this structure carefully for closure SDL.DOMEventToSDLEvent['keydown'] = 0x300 /* SDL_KEYDOWN */; @@ -909,14 +886,14 @@ var LibrarySDL = { SDL_GetModState: function() { // TODO: numlock, capslock, etc. - return (SDL.shiftKey ? 0x0001 | 0x0002 : 0) | // KMOD_LSHIFT & KMOD_RSHIFT - (SDL.ctrlKey ? 0x0040 | 0x0080 : 0) | // KMOD_LCTRL & KMOD_RCTRL - (SDL.altKey ? 0x0100 | 0x0200 : 0); // KMOD_LALT & KMOD_RALT + return (SDL.keyboardState[16] ? 0x0001 | 0x0002 : 0) | // KMOD_LSHIFT & KMOD_RSHIFT + (SDL.keyboardState[17] ? 0x0040 | 0x0080 : 0) | // KMOD_LCTRL & KMOD_RCTRL + (SDL.keyboardState[18] ? 0x0100 | 0x0200 : 0); // KMOD_LALT & KMOD_RALT }, SDL_GetMouseState: function(x, y) { - if (x) {{{ makeSetValue('x', '0', 'SDL.mouseX', 'i32') }}}; - if (y) {{{ makeSetValue('y', '0', 'SDL.mouseY', 'i32') }}}; + if (x) {{{ makeSetValue('x', '0', 'Browser.mouseX', 'i32') }}}; + if (y) {{{ makeSetValue('y', '0', 'Browser.mouseY', 'i32') }}}; return SDL.buttonState; }, @@ -1372,6 +1349,9 @@ var LibrarySDL = { channelInfo.audio = audio = audio.cloneNode(true); audio.numChannels = info.audio.numChannels; audio.frequency = info.audio.frequency; + + // TODO: handle N loops. Behavior matches Mix_PlayMusic + audio.loop = loops != 0; if (SDL.channelFinished) { audio['onended'] = function() { // TODO: cache these Runtime.getFuncWrapper(SDL.channelFinished, 'vi')(channel); @@ -1473,7 +1453,7 @@ var LibrarySDL = { loops = Math.max(loops, 1); var audio = SDL.audios[id].audio; if (!audio) return 0; - audio.loop = loops != 1; // TODO: handle N loops for finite N + audio.loop = loops != 0; // TODO: handle N loops for finite N if (SDL.audios[id].buffer) { audio["mozWriteAudio"](SDL.audios[id].buffer); } else { @@ -1586,7 +1566,11 @@ var LibrarySDL = { // SDL TTF - TTF_Init: function() { return 0 }, + TTF_Init: function() { + var canvas = document.createElement('canvas'); + SDL.ttfContext = canvas.getContext('2d'); + return 0; + }, TTF_OpenFont: function(filename, size) { filename = FS.standardizePath(Pointer_stringify(filename)); @@ -1609,7 +1593,7 @@ var LibrarySDL = { var w = SDL.estimateTextWidth(fontData, text); var h = fontData.size; var color = SDL.loadColorToCSSRGB(color); // XXX alpha breaks fonts? - var fontString = h + 'px sans-serif'; + var fontString = h + 'px ' + fontData.name; var surf = SDL.makeSurface(w, h, 0, false, 'text:' + text); // bogus numbers.. var surfData = SDL.surfaces[surf]; surfData.ctx.save(); @@ -1696,6 +1680,60 @@ var LibrarySDL = { SDL_GL_SwapBuffers: function() {}, + // SDL 2 + + SDL_GL_ExtensionSupported: function(extension) { + return Module.ctx.getExtension(extension) | 0; + }, + + SDL_DestroyWindow: function(window) {}, + + SDL_DestroyRenderer: function(renderer) {}, + + SDL_GetWindowFlags: function(x, y) { + if (Browser.isFullScreen) { + return 1; + } + + return 0; + }, + + SDL_GL_SwapWindow: function(window) {}, + + SDL_GL_MakeCurrent: function(window, context) {}, + + SDL_GL_DeleteContext: function(context) {}, + + SDL_GL_SetSwapInterval: function(state) {}, + + SDL_SetWindowTitle: function(window, title) { + if (title) document.title = Pointer_stringify(title); + }, + + SDL_GetWindowSize: function(window, width, height){ + var w = Module['canvas'].width; + var h = Module['canvas'].height; + if (width) {{{ makeSetValue('width', '0', 'w', 'i32') }}}; + if (height) {{{ makeSetValue('height', '0', 'h', 'i32') }}}; + }, + + SDL_LogSetOutputFunction: function(callback, userdata) {}, + + SDL_SetWindowFullscreen: function(window, fullscreen) { + if (Browser.isFullScreen) { + Module['canvas'].cancelFullScreen(); + return 1; + } else { + return 0; + } + }, + + SDL_GetWindowFlags: function() {}, + + SDL_ClearError: function() {}, + + SDL_getenv: 'getenv', + // TODO SDL_SetGamma: function(r, g, b) { |