diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/library_sdl.js | 5 | ||||
-rw-r--r-- | src/shell.html | 38 |
2 files changed, 36 insertions, 7 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index 32c93f67..24886713 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -166,11 +166,14 @@ mergeInto(LibraryManager.library, { {{{ makeSetValue('pixelFormat + SDL.structs.PixelFormat.Bmask', '0', '0xff', 'i32') }}} {{{ makeSetValue('pixelFormat + SDL.structs.PixelFormat.Amask', '0', '0xff', 'i32') }}} + // Decide if we want to use WebGL or not + var useWebGL = (flags & 0x04000000) != 0; // SDL_OPENGL + SDL.surfaces[surf] = { width: width, height: height, canvas: Module['canvas'], - ctx: Module['ctx2D'], + ctx: useWebGL ? Module['ctxGL'] : Module['ctx2D'], surf: surf, buffer: buffer, pixelFormat: pixelFormat, diff --git a/src/shell.html b/src/shell.html index c096a314..95ab5a6a 100644 --- a/src/shell.html +++ b/src/shell.html @@ -19,12 +19,38 @@ })(), canvas: document.getElementById('canvas') }; - try { - Module.ctx2D = Module.canvas.getContext('2d'); - if (!Module.ctx2D) throw 'Could not create canvas :('; - } catch (e) { - Module.print('(canvas not available)'); - } + // Define the 2D and WebGL contexts lazily, because a canvas can only + // have one context type. + Module.__defineGetter__("ctx2D", function() { + try { + var ctx = Module.canvas.getContext('2d'); + if (!ctx) throw 'Could not create canvas :('; + delete Module["ctxGL"]; + Module.__defineGetter__("ctxGL", function() { + throw 'Could not create a WebGL context for a 2D canvas :('; + }); + delete Module["ctx2D"]; + return Module["ctx2D"] = ctx; + } catch (e) { + Module.print('(canvas not available)'); + return null; + } + }); + Module.__defineGetter__("ctxGL", function() { + try { + var ctx = Module.canvas.getContext('experimental-webgl'); + if (!ctx) throw 'Could not create canvas :('; + delete Module["ctx2D"]; + Module.__defineGetter__("ctx2D", function() { + throw 'Could not create a 2D context for a WebGL canvas :('; + }); + delete Module["ctxGL"]; + return Module["ctxGL"] = ctx; + } catch (e) { + Module.print('(canvas not available)'); + return null; + } + }); // The compiled code {{{ SCRIPT_CODE }}} |