aboutsummaryrefslogtreecommitdiff
path: root/src/library_sdl.js
diff options
context:
space:
mode:
authorEhsan Akhgari <ehsan.akhgari@gmail.com>2012-01-20 16:04:51 -0500
committerEhsan Akhgari <ehsan.akhgari@gmail.com>2012-01-20 16:04:51 -0500
commit0208f854ad57be31fba3d999c2d77c8fe70d0747 (patch)
treea5214ca6dc14002743548a2129f5b827cc75b3b5 /src/library_sdl.js
parent426ab9f67ac01bdf008168d22d3f8a64ed1d098e (diff)
Unify the 2D and WebGL contexts
Diffstat (limited to 'src/library_sdl.js')
-rw-r--r--src/library_sdl.js29
1 files changed, 15 insertions, 14 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js
index 24886713..ce26a106 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -1,16 +1,14 @@
//"use strict";
// To use emscripten's SDL library here, you need to define
-// Module.canvas and at least one of Module.ctx2D, Module.ctxGL.
+// Module.canvas.
//
-// More specifically, for 2D our SDL implementation will look for
-// Module.canvas and Module.ctx2D. You should fill them using
-// something like
+// More specifically, our SDL implementation will look for
+// Module.canvas. You should fill it using something like
//
// function onLoad() {
// // Pass canvas and context to the generated code
// Module.canvas = document.getElementById('canvas');
-// Module.ctx2D = Module.canvas.getContext('2d');
// }
//
// Note that this must be called during onload, since you will
@@ -49,11 +47,6 @@
function onLoad() {
// Pass canvas and context to the generated code, and do the actual run() here
Module.canvas = document.getElementById('canvas');
- Module.ctx2D = Module.canvas.getContext('2d');
- if (!Module.ctx2D) {
- alert('Canvas not available :(');
- return;
- }
Module.run();
}
</script>
@@ -70,9 +63,6 @@
//
// * Make sure alpha values are proper in your input. If they are all 0, everything will be transparent!
//
-// * It's best to set the ctx stuff in your html file, as above. Otherwise you will need
-// to edit the generated .js file each time you generate it.
-//
// * Your code should not write a 32-bit value and expect that to set an RGBA pixel.
// The reason is that that data will be read as 8-bit values, and according to the
// load-store consistency assumption, it should be written that way (see docs/paper.pdf).
@@ -173,7 +163,7 @@ mergeInto(LibraryManager.library, {
width: width,
height: height,
canvas: Module['canvas'],
- ctx: useWebGL ? Module['ctxGL'] : Module['ctx2D'],
+ ctx: SDL.createContext(useWebGL),
surf: surf,
buffer: buffer,
pixelFormat: pixelFormat,
@@ -182,6 +172,17 @@ mergeInto(LibraryManager.library, {
return surf;
},
+ createContext: function(useWebGL) {
+ try {
+ var ctx = Module.canvas.getContext(useWebGL ? 'experimental-webgl' : '2d');
+ if (!ctx) throw 'Could not create canvas :(';
+ return Module.ctx = ctx;
+ } catch (e) {
+ Module.print('(canvas not available)');
+ return null;
+ }
+ },
+
freeSurface: function(surf) {
_free(SDL.surfaces[surf].buffer);
_free(SDL.surfaces[surf].pixelFormat);