aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-01-20 18:48:51 -0800
committerAlon Zakai <alonzakai@gmail.com>2012-01-20 18:48:51 -0800
commit02cf5021ed08214e85b4b9b7a887f65ea2ae473a (patch)
treeca36689452b7b569863f9791e67749b51f2d7b43 /src
parent63f02726973bd02c7b19a1e814aeb041cf832ed4 (diff)
parent717051225652b89ede1984ba1d63e828298cb6aa (diff)
Merge branch 'master' into libcxx
Diffstat (limited to 'src')
-rw-r--r--src/library_gl.js42
-rw-r--r--src/library_sdl.js32
-rw-r--r--src/modules.js2
-rw-r--r--src/shell.html6
4 files changed, 48 insertions, 34 deletions
diff --git a/src/library_gl.js b/src/library_gl.js
index f22eabcb..2be881df 100644
--- a/src/library_gl.js
+++ b/src/library_gl.js
@@ -10,12 +10,12 @@ var LibraryGL = {
glGetString: function(name_) {
switch(name_) {
- case Module.ctxGL.VENDOR:
- case Module.ctxGL.RENDERER:
- case Module.ctxGL.VERSION:
- return allocate(intArrayFromString(Module.ctxGL.getParameter(name_)), 'i8', ALLOC_NORMAL);
+ case Module.ctx.VENDOR:
+ case Module.ctx.RENDERER:
+ case Module.ctx.VERSION:
+ return allocate(intArrayFromString(Module.ctx.getParameter(name_)), 'i8', ALLOC_NORMAL);
case 0x1F03: // Extensions
- return allocate(intArrayFromString(Module.ctxGL.getSupportedExtensions().join(' ')), 'i8', ALLOC_NORMAL);
+ return allocate(intArrayFromString(Module.ctx.getSupportedExtensions().join(' ')), 'i8', ALLOC_NORMAL);
default:
throw 'Failure: Invalid glGetString value: ' + name_;
}
@@ -23,8 +23,8 @@ var LibraryGL = {
glGetIntegerv: function(name_, p) {
switch(name_) {
- case Module.ctxGL.MAX_TEXTURE_SIZE:
- IHEAP[p] = Module.ctxGL.getParameter(name_);
+ case Module.ctx.MAX_TEXTURE_SIZE:
+ IHEAP[p] = Module.ctx.getParameter(name_);
break;
default:
throw 'Failure: Invalid glGetIntegerv value: ' + name_;
@@ -35,7 +35,7 @@ var LibraryGL = {
glGenTextures: function(n, textures) {
for (var i = 0; i < n; i++) {
var id = GL.textureCounter++;
- GL.textures[id] = Module.ctxGL.createTexture();
+ GL.textures[id] = Module.ctx.createTexture();
IHEAP[textures+QUANTUM_SIZE*i] = id;
}
},
@@ -43,7 +43,7 @@ var LibraryGL = {
glDeleteTextures: function(n, textures) {
for (var i = 0; i < n; i++) {
var id = IHEAP[textures+QUANTUM_SIZE*i];
- Module.ctxGL.deleteTexture(GL.textures[id]);
+ Module.ctx.deleteTexture(GL.textures[id]);
delete GL.textures[id];
}
},
@@ -52,18 +52,34 @@ var LibraryGL = {
if (pixels) {
pixels = new Uint8Array(IHEAP.slice(pixels, pixels + width*height*4)); // TODO: optimize
}
- Module.ctxGL.texImage2D(target, level, internalformat, width, height, border, format, type, pixels);
+ Module.ctx.texImage2D(target, level, internalformat, width, height, border, format, type, pixels);
},
glTexSubImage2D: function(target, level, xoffset, yoffset, width, height, format, type, pixels) {
if (pixels) {
pixels = new Uint8Array(IHEAP.slice(pixels, pixels + width*height*4)); // TODO: optimize
}
- Module.ctxGL.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
+ Module.ctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
},
glBindTexture: function(target, texture) {
- Module.ctxGL.bindTexture(target, GL.textures[texture]);
+ Module.ctx.bindTexture(target, GL.textures[texture]);
+ },
+
+ glClearColor: function(red, green, blue, alpha) {
+ Module.ctx.clearColor(red, green, blue, alpha);
+ },
+
+ glClear: function(mask) {
+ Module.ctx.clear(mask);
+ },
+
+ glEnable: function(cap) {
+ Module.ctx.enable(cap);
+ },
+
+ glScissor: function(x, y, width, height) {
+ Module.ctx.scissor(x, y, width, height);
},
};
@@ -83,7 +99,7 @@ var LibraryGL = {
var num = data[0];
var names = data[1];
var args = range(num).map(function(i) { return 'x' + i }).join(', ');
- var stub = '(function(' + args + ') { ' + (num > 0 ? 'Module.ctxGL.NAME(' + args + ')' : '') + ' })';
+ var stub = '(function(' + args + ') { ' + (num > 0 ? 'Module.ctx.NAME(' + args + ')' : '') + ' })';
names.split(' ').forEach(function(name_) {
var cName = 'gl' + name_[0].toUpperCase() + name_.substr(1);
LibraryGL[cName] = eval(stub.replace('NAME', name_));
diff --git a/src/library_sdl.js b/src/library_sdl.js
index 32c93f67..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).
@@ -166,11 +156,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: SDL.createContext(useWebGL),
surf: surf,
buffer: buffer,
pixelFormat: pixelFormat,
@@ -179,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);
diff --git a/src/modules.js b/src/modules.js
index ce54af98..7b8ac390 100644
--- a/src/modules.js
+++ b/src/modules.js
@@ -322,7 +322,7 @@ var LibraryManager = {
load: function() {
assert(!this.library);
- for (var suffix in set('', '_sdl', '_browser')) {
+ for (var suffix in set('', '_sdl', '_browser', '_gl')) {
eval(processMacros(preprocess(read('library' + suffix + '.js'))));
}
},
diff --git a/src/shell.html b/src/shell.html
index c096a314..6b6bbdad 100644
--- a/src/shell.html
+++ b/src/shell.html
@@ -19,12 +19,6 @@
})(),
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)');
- }
// The compiled code
{{{ SCRIPT_CODE }}}