aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEhsan Akhgari <ehsan.akhgari@gmail.com>2012-04-13 10:32:45 -0400
committerEhsan Akhgari <ehsan.akhgari@gmail.com>2012-04-13 10:32:45 -0400
commit74cfb3b59c714d2e0ef91d0ad3fe02bc096faf56 (patch)
tree4e743664d4d139c35b4e656f267cb2a0d4799114
parent55d9a3bc975cb4e9fefb79aeca6d5b9ef5fcea1f (diff)
parentffae4e81b6eec71922b662eea9a75f5693994cb2 (diff)
Merge remote-tracking branch 'upstream/incoming' into glmatrix
Conflicts: src/library_gl.js
-rw-r--r--src/library_browser.js14
-rw-r--r--src/library_gl.js166
-rw-r--r--src/library_sdl.js1
-rwxr-xr-xtests/runner.py13
-rw-r--r--tests/screenshot.pngbin0 -> 329895 bytes
-rw-r--r--tests/sdl_gl_read.c155
-rw-r--r--tests/sdl_ogl.c148
-rw-r--r--tools/shared.py6
8 files changed, 483 insertions, 20 deletions
diff --git a/src/library_browser.js b/src/library_browser.js
index 17dff2eb..300f0b3b 100644
--- a/src/library_browser.js
+++ b/src/library_browser.js
@@ -47,13 +47,16 @@ mergeInto(LibraryManager.library, {
case 'function': {
wrapper[prop] = function() {
var printArgs = Array.prototype.slice.call(arguments).map(function(arg) {
+ if (!arg) return arg;
if (wrapper.objectMap[arg]) return '<' + arg + '|' + wrapper.objectMap[arg] + '>';
if (arg.toString() == '[object HTMLImageElement]') {
return arg + '\n\n';
}
if (arg.byteLength) {
+ return '{' + Array.prototype.slice.call(arg, 0, Math.min(arg.length, 40)) + '}'; // Useful for correct arrays, less so for compiled arrays, see the code below for that
var buf = new ArrayBuffer(32);
var i8buf = new Int8Array(buf);
+ var i16buf = new Int16Array(buf);
var f32buf = new Float32Array(buf);
switch(arg.toString()) {
case '[object Uint8Array]':
@@ -62,6 +65,9 @@ mergeInto(LibraryManager.library, {
case '[object Float32Array]':
f32buf.set(arg.subarray(0, 5));
break;
+ case '[object Uint16Array]':
+ i16buf.set(arg.subarray(0, 16));
+ break;
default:
alert('unknown array for debugging: ' + arg);
throw 'see alert';
@@ -75,25 +81,25 @@ mergeInto(LibraryManager.library, {
}
return arg;
});
- Module.printErr('[gl_f:' + prop + ':' + printArgs + ']');
+ console.log('[gl_f:' + prop + ':' + printArgs + ']');
var ret = tempCtx[prop].apply(tempCtx, arguments);
var printRet = ret;
if (typeof ret == 'object') {
wrapper.objectMap[ret] = wrapper.objectCounter++;
printRet = '<' + ret + '|' + wrapper.objectMap[ret] + '>';
}
- Module.printErr('[ gl:' + prop + ':return:' + printRet + ']');
+ if (typeof printRet != 'undefined') console.log('[ gl:' + prop + ':return:' + printRet + ']');
return ret;
}
break;
}
case 'number': case 'string': {
wrapper.__defineGetter__(prop, function() {
- //Module.printErr('[gl_g:' + prop + ':' + tempCtx[prop] + ']');
+ //console.log('[gl_g:' + prop + ':' + tempCtx[prop] + ']');
return tempCtx[prop];
});
wrapper.__defineSetter__(prop, function(value) {
- Module.printErr('[gl_s:' + prop + ':' + value + ']');
+ console.log('[gl_s:' + prop + ':' + value + ']');
tempCtx[prop] = value;
});
break;
diff --git a/src/library_gl.js b/src/library_gl.js
index d7bf31ea..f5235a3f 100644
--- a/src/library_gl.js
+++ b/src/library_gl.js
@@ -316,6 +316,10 @@ var LibraryGL = {
Module.ctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
},
+ glReadPixels: function(x, y, width, height, format, type, pixels) {
+ Module.ctx.readPixels(x, y, width, height, format, type, HEAPU8.subarray(pixels));
+ },
+
glBindTexture: function(target, texture) {
Module.ctx.bindTexture(target, GL.textures[texture]);
},
@@ -963,6 +967,11 @@ var LibraryGL = {
return 1 /* GL_TRUE */;
},
+ glEnable: function(cap) {
+ if (cap == 0x0DE1) return; // no GL_TEXTURE_2D in GLES or WebGL
+ Module.ctx.enable(cap);
+ },
+
// GL emulation: provides misc. functionality not present in OpenGL ES 2.0 or WebGL
$GLEmulation__deps: ['glCreateShader', 'glShaderSource', 'glCompileShader', 'glCreateProgram', 'glDeleteShader', 'glDeleteProgram', 'glAttachShader', 'glActiveTexture', 'glGetShaderiv', 'glGetProgramiv', 'glLinkProgram'],
@@ -1017,20 +1026,140 @@ var LibraryGL = {
}
},
- glBegin__deps: ['$GL', function() { return 'GL.matrix.lib = ' + read('gl-matrix.js') + ';\n' +
- 'GL.initMatrixLibrary();'
- }],
- glBegin: function() {
- Module.print('TODO');
- }
+ // Immediate mode
+
+ $GLImmediate: {
+ maxElements: 1024,
+ vertexData: null,
+ indexData: null,
+ vertexCounter: 0,
+ indexCounter: 0,
+ mode: 0,
+
+ initted: false,
+ init: function() {
+ this.vertexShader = Module.ctx.createShader(Module.ctx.VERTEX_SHADER);
+ Module.ctx.shaderSource(this.vertexShader, 'attribute vec4 a_position; \n\
+ attribute vec2 a_texCoord; \n\
+ varying vec2 v_texCoord; \n\
+ void main() \n\
+ { \n\
+ gl_Position = a_position; \n\
+ v_texCoord = a_texCoord; \n\
+ } \n');
+ Module.ctx.compileShader(this.vertexShader);
+
+ this.fragmentShader = Module.ctx.createShader(Module.ctx.FRAGMENT_SHADER);
+ Module.ctx.shaderSource(this.fragmentShader, 'precision mediump float; \n\
+ varying vec2 v_texCoord; \n\
+ uniform sampler2D s_texture; \n\
+ void main() \n\
+ { \n\
+ gl_FragColor = texture2D( s_texture, v_texCoord );\n\
+ } \n');
+ Module.ctx.compileShader(this.fragmentShader);
+
+ this.program = Module.ctx.createProgram();
+ Module.ctx.attachShader(this.program, this.vertexShader);
+ Module.ctx.attachShader(this.program, this.fragmentShader);
+ Module.ctx.linkProgram(this.program);
+
+ this.positionLocation = Module.ctx.getAttribLocation(this.program, 'a_position');
+ this.texCoordLocation = Module.ctx.getAttribLocation(this.program, 'a_texCoord');
+ this.textureLocation = Module.ctx.getUniformLocation(this.program, 's_texture');
+
+ // Buffer for data
+ this.vertexData = new Float32Array(5 * this.maxElements);
+ this.indexData = new Uint16Array(this.maxElements);
+
+ this.vertexObject = Module.ctx.createBuffer();
+ this.indexObject = Module.ctx.createBuffer();
+ },
+ flush: function() {
+ // Upload the data
+ Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, this.vertexObject);
+ Module.ctx.bufferData(Module.ctx.ARRAY_BUFFER, this.vertexData.subarray(0, 5*this.vertexCounter), Module.ctx.STATIC_DRAW);
+ Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, this.indexObject);
+ Module.ctx.bufferData(Module.ctx.ELEMENT_ARRAY_BUFFER, this.indexData.subarray(0, this.indexCounter), Module.ctx.STATIC_DRAW);
+
+ // Render
+ //Module.ctx.viewport(0, 0, Module.canvas.width, Module.canvas.height);
+ //Module.ctx.clear(Module.ctx.COLOR_BUFFER_BIT);
+
+ Module.ctx.useProgram(this.program);
+
+ Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, this.vertexObject);
+ Module.ctx.vertexAttribPointer(this.positionLocation, 3, Module.ctx.FLOAT,
+ false, 5 * 4, 0);
+ Module.ctx.vertexAttribPointer(this.texCoordLocation, 2, Module.ctx.FLOAT,
+ false, 5 * 4,
+ 3 * 4);
+
+ Module.ctx.enableVertexAttribArray(this.positionLoc);
+ Module.ctx.enableVertexAttribArray(this.texCoordLoc);
+
+ Module.ctx.activeTexture(Module.ctx.TEXTURE0);
+ Module.ctx.bindTexture(Module.ctx.TEXTURE_2D, this.textureId);
+
+ Module.ctx.uniform1i(this.textureLocation, 0);
+
+ Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, this.indexObject);
+ Module.ctx.drawElements(Module.ctx.TRIANGLES, this.indexCounter, Module.ctx.UNSIGNED_SHORT, 0);
+
+ this.vertexCounter = this.indexCounter = 0;
+ }
+ },
+
+ glBegin__deps: ['$GL', '$GLImmediate', function() { return 'GL.matrix.lib = ' + read('gl-matrix.js') +
+ ';\nGL.immediate = GLImmediate;\nGL.initMatrixLibrary();\n' }],
+ glBegin: function(mode) {
+ if (!GL.immediate.initted) GL.immediate.init();
+ GL.immediate.mode = mode;
+ },
+
+ glEnd: function() {
+ GL.immediate.flush();
+ },
+
+ glVertex3f: function(x, y, z) {
+ GL.immediate.vertexData[5*GL.immediate.vertexCounter ] = x;
+ GL.immediate.vertexData[5*GL.immediate.vertexCounter+1] = y;
+ GL.immediate.vertexData[5*GL.immediate.vertexCounter+2] = z;
+ GL.immediate.vertexCounter++;
+#if ASSERTIONS
+ assert(GL.immediate.vertexCounter < GL.immediate.maxElements, 'too many immediate mode vertexes');
+#endif
+ if (GL.immediate.mode == 7) { // GL_QUADS
+ if (GL.immediate.vertexCounter % 4 == 0) {
+ var start = GL.immediate.vertexCounter % 4;
+ GL.immediate.indexData[GL.immediate.indexCounter ] = start;
+ GL.immediate.indexData[GL.immediate.indexCounter+1] = start+1;
+ GL.immediate.indexData[GL.immediate.indexCounter+2] = start+2;
+ GL.immediate.indexData[GL.immediate.indexCounter+3] = start;
+ GL.immediate.indexData[GL.immediate.indexCounter+4] = start+2;
+ GL.immediate.indexData[GL.immediate.indexCounter+5] = start+3;
+ GL.immediate.indexCounter += 6;
+ }
+ } else {
+ throw 'only GL_QUADS supported so far';
+ }
+#if ASSERTIONS
+ assert(GL.immediate.indexCounter < GL.immediate.maxElements, 'too many immediate mode indexes');
+#endif
+ },
+
+ glTexCoord2i: function(u, v) {
+ GL.immediate.vertexData[5*GL.immediate.vertexCounter+3] = u;
+ GL.immediate.vertexData[5*GL.immediate.vertexCounter+4] = v;
+ },
};
-// Simple pass-through functions. Starred ones have return values.
+// Simple pass-through functions. Starred ones have return values. [X] ones have X in the C name but not in the JS name
[[0, 'shadeModel fogi fogfv getError finish flush'],
- [1, 'clearDepth depthFunc enable disable frontFace cullFace clear enableVertexAttribArray disableVertexAttribArray lineWidth clearStencil depthMask stencilMask stencilMaskSeparate checkFramebufferStatus* generateMipmap activeTexture blendEquation'],
- [2, 'pixelStorei blendFunc blendEquationSeparate'],
- [3, 'texParameteri texParameterf drawArrays vertexAttrib2f'],
- [4, 'viewport clearColor scissor vertexAttrib3f colorMask drawElements renderbufferStorage blendFuncSeparate'],
+ [1, 'clearDepth clearDepth[f] depthFunc disable frontFace cullFace clear enableVertexAttribArray disableVertexAttribArray lineWidth clearStencil depthMask stencilMask stencilMaskSeparate checkFramebufferStatus* generateMipmap activeTexture blendEquation polygonOffset hint sampleCoverage'],
+ [2, 'pixelStorei blendFunc blendEquationSeparate depthRange depthRange[f]'],
+ [3, 'texParameteri texParameterf drawArrays vertexAttrib2f stencilFunc stencilOp'],
+ [4, 'viewport clearColor scissor vertexAttrib3f colorMask drawElements renderbufferStorage blendFuncSeparate blendColor'],
[5, 'vertexAttrib4f'],
[6, 'vertexAttribPointer'],
[8, 'copyTexImage2D copyTexSubImage2D']].forEach(function(data) {
@@ -1039,15 +1168,20 @@ var LibraryGL = {
var args = range(num).map(function(i) { return 'x' + i }).join(', ');
var plainStub = '(function(' + args + ') { ' + (num > 0 ? 'Module.ctx.NAME(' + args + ')' : '') + ' })';
var returnStub = '(function(' + args + ') { ' + (num > 0 ? 'return Module.ctx.NAME(' + args + ')' : '') + ' })';
- names.split(' ').forEach(function(name_) {
+ names.split(' ').forEach(function(name) {
var stub = plainStub;
- if (name_[name_.length-1] == '*') {
- name_ = name_.substr(0, name_.length-1);
+ if (name[name.length-1] == '*') {
+ name = name.substr(0, name.length-1);
stub = returnStub;
}
- var cName = 'gl' + name_[0].toUpperCase() + name_.substr(1);
+ var cName = name;
+ if (name.indexOf('[') >= 0) {
+ cName = name.replace('[', '').replace(']', '');
+ name = cName.substr(0, cName.length-1);
+ }
+ var cName = 'gl' + cName[0].toUpperCase() + cName.substr(1);
assert(!(cName in LibraryGL), "Cannot reimplement the existing function " + cName);
- LibraryGL[cName] = eval(stub.replace('NAME', name_));
+ LibraryGL[cName] = eval(stub.replace('NAME', name));
});
});
diff --git a/src/library_sdl.js b/src/library_sdl.js
index b607bdb3..11431e4e 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -702,6 +702,7 @@ mergeInto(LibraryManager.library, {
surfData.ctx.drawImage(raw, 0, 0, raw.width, raw.height, 0, 0, raw.width, raw.height);
return surf;
},
+ SDL_LoadBMP: 'IMG_Load',
// SDL_Audio
diff --git a/tests/runner.py b/tests/runner.py
index 6db096d9..a479cf9f 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -6997,6 +6997,19 @@ elif 'browser' in str(sys.argv):
Popen(['python', EMCC, '-O2', '--minify', '0', os.path.join(self.get_dir(), 'sdl_audio.c'), '--preload-file', 'sound.ogg', '--preload-file', 'sound2.wav', '-o', 'page.html', '-s', 'EXPORTED_FUNCTIONS=["_main", "_play", "_play2"]']).communicate()
self.run_browser('page.html', '', '/report_result?1')
+ def test_sdl_gl_read(self):
+ # SDL, OpenGL, readPixels
+ open(os.path.join(self.get_dir(), 'sdl_gl_read.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_gl_read.c')).read()))
+ Popen(['python', EMCC, os.path.join(self.get_dir(), 'sdl_gl_read.c'), '-o', 'something.html']).communicate()
+ self.run_browser('something.html', '.', '/report_result?1')
+
+ def zzztest_sdl_ogl(self):
+ # SDL, OpenGL, textures, immediate mode
+ shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png'))
+ self.reftest(path_from_root('tests', 'gears.png'))
+ Popen(['python', EMCC, path_from_root('tests', 'sdl_ogl.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate()
+ self.run_browser('something.html', 'You should see animating gears.', '/report_result?1779')
+
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/screenshot.png b/tests/screenshot.png
new file mode 100644
index 00000000..44cf844a
--- /dev/null
+++ b/tests/screenshot.png
Binary files differ
diff --git a/tests/sdl_gl_read.c b/tests/sdl_gl_read.c
new file mode 100644
index 00000000..552eb8c0
--- /dev/null
+++ b/tests/sdl_gl_read.c
@@ -0,0 +1,155 @@
+// Built from glbook/hello triange and sdl_ogl, see details there
+
+#include "SDL/SDL.h"
+#include "SDL/SDL_image.h"
+#include "SDL/SDL_opengl.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+GLuint programObject;
+int width = 512;
+int height = 256;
+
+GLuint LoadShader ( GLenum type, const char *shaderSrc )
+{
+ GLuint shader;
+ GLint compiled;
+
+ shader = glCreateShader ( type );
+ if ( shader == 0 )
+ return 0;
+
+ glShaderSource ( shader, 1, &shaderSrc, NULL );
+ glCompileShader ( shader );
+ glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled );
+ if ( !compiled )
+ {
+ GLint infoLen = 0;
+ glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen );
+ if ( infoLen > 1 )
+ {
+ char* infoLog = malloc (sizeof(char) * infoLen );
+ glGetShaderInfoLog ( shader, infoLen, NULL, infoLog );
+ printf ( "Error compiling shader:\n%s\n", infoLog );
+ free ( infoLog );
+ }
+ glDeleteShader ( shader );
+ return 0;
+ }
+ return shader;
+}
+
+int Init ()
+{
+ GLbyte vShaderStr[] =
+ "attribute vec4 vPosition; \n"
+ "void main() \n"
+ "{ \n"
+ " gl_Position = vPosition; \n"
+ "} \n";
+
+ GLbyte fShaderStr[] =
+ "precision mediump float;\n"\
+ "void main() \n"
+ "{ \n"
+ " gl_FragColor = vec4 ( 0.0, 0.0, 1.0, 1.0 );\n"
+ "} \n";
+
+ GLuint vertexShader;
+ GLuint fragmentShader;
+ GLint linked;
+
+ vertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr );
+ fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr );
+
+ programObject = glCreateProgram ( );
+ if ( programObject == 0 )
+ return 0;
+
+ glAttachShader ( programObject, vertexShader );
+ glAttachShader ( programObject, fragmentShader );
+ glBindAttribLocation ( programObject, 0, "vPosition" );
+ glLinkProgram ( programObject );
+ glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );
+ if ( !linked )
+ {
+ GLint infoLen = 0;
+ glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );
+ if ( infoLen > 1 )
+ {
+ char* infoLog = malloc (sizeof(char) * infoLen );
+ glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );
+ printf ( "Error linking program:\n%s\n", infoLog );
+ free ( infoLog );
+ }
+ glDeleteProgram ( programObject );
+ return GL_FALSE;
+ }
+
+ glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
+ return GL_TRUE;
+}
+
+///
+// Draw a triangle using the shader pair created in Init()
+//
+void Draw ()
+{
+ GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
+ -0.5f, -0.5f, 0.0f,
+ 0.5f, -0.5f, 0.0f };
+
+ // No clientside arrays, so do this in a webgl-friendly manner
+ GLuint vertexPosObject;
+ glGenBuffers(1, &vertexPosObject);
+ glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject);
+ glBufferData(GL_ARRAY_BUFFER, 9*4, vVertices, GL_STATIC_DRAW);
+
+ glViewport ( 0, 0, width, height );
+ glClear ( GL_COLOR_BUFFER_BIT );
+ glUseProgram ( programObject );
+
+ glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject);
+ glVertexAttribPointer(0 /* ? */, 3, GL_FLOAT, 0, 0, 0);
+ glEnableVertexAttribArray(0);
+
+ glDrawArrays ( GL_TRIANGLES, 0, 3 );
+}
+
+void Verify() {
+ unsigned char *data = malloc(width*height*4);
+ glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
+ // Should see some blue, and nothing else
+ int seen = 0;
+ int ok = 1;
+ for (int x = 0; x < width*height; x++) {
+ seen = seen || data[x*4+2] != 0;
+ ok = ok && (data[x*4+0] == 0);
+ ok = ok && (data[x*4+1] == 0);
+ }
+ int result = seen && ok;
+ REPORT_RESULT();
+}
+
+int main(int argc, char *argv[])
+{
+ SDL_Surface *screen;
+ if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
+ printf("Unable to initialize SDL: %s\n", SDL_GetError());
+ return 1;
+ }
+
+ screen = SDL_SetVideoMode(width, height, 32, SDL_OPENGL);
+ if (!screen) {
+ printf("Unable to set video mode: %s\n", SDL_GetError());
+ return 1;
+ }
+
+ Init();
+ Draw();
+ Verify();
+
+ return 0;
+}
+
diff --git a/tests/sdl_ogl.c b/tests/sdl_ogl.c
new file mode 100644
index 00000000..6884f478
--- /dev/null
+++ b/tests/sdl_ogl.c
@@ -0,0 +1,148 @@
+/*******************************************************************
+ * *
+ * Using SDL With OpenGL *
+ * *
+ * Tutorial by Kyle Foley (sdw) *
+ * *
+ * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *
+ * *
+ *******************************************************************/
+
+/*
+THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
+AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.
+
+THE ORIGINAL AUTHOR IS KYLE FOLEY.
+
+THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
+OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
+MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
+ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
+RESULTING FROM THE USE, MODIFICATION, OR
+REDISTRIBUTION OF THIS SOFTWARE.
+*/
+
+#include "SDL/SDL.h"
+#include "SDL/SDL_image.h"
+#include "SDL/SDL_opengl.h"
+
+#include <stdio.h>
+#include <string.h>
+
+int main(int argc, char *argv[])
+{
+ SDL_Surface *screen;
+
+ // Slightly different SDL initialization
+ if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
+ printf("Unable to initialize SDL: %s\n", SDL_GetError());
+ return 1;
+ }
+
+ SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
+
+ screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
+ if ( !screen ) {
+ printf("Unable to set video mode: %s\n", SDL_GetError());
+ return 1;
+ }
+
+ // Set the OpenGL state after creating the context with SDL_SetVideoMode
+
+ glClearColor( 0, 0, 0, 0 );
+
+ glEnable( GL_TEXTURE_2D ); // Need this to display a texture
+
+ glViewport( 0, 0, 640, 480 );
+
+ glMatrixMode( GL_PROJECTION );
+ glLoadIdentity();
+
+ glOrtho( 0, 640, 480, 0, -1, 1 );
+
+ glMatrixMode( GL_MODELVIEW );
+ glLoadIdentity();
+
+ // Load the OpenGL texture
+
+ GLuint texture; // Texture object handle
+ SDL_Surface *surface; // Gives us the information to make the texture
+
+ if ( (surface = IMG_Load("screenshot.png")) ) {
+
+ // Check that the image's width is a power of 2
+ if ( (surface->w & (surface->w - 1)) != 0 ) {
+ printf("warning: image.bmp's width is not a power of 2\n");
+ }
+
+ // Also check if the height is a power of 2
+ if ( (surface->h & (surface->h - 1)) != 0 ) {
+ printf("warning: image.bmp's height is not a power of 2\n");
+ }
+
+ // Have OpenGL generate a texture object handle for us
+ glGenTextures( 1, &texture );
+
+ // Bind the texture object
+ glBindTexture( GL_TEXTURE_2D, texture );
+
+ // Set the texture's stretching properties
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+
+ // Add some greyness
+ memset(surface->pixels, 0x66, surface->w*surface->h);
+
+ // Edit the texture object's image data using the information SDL_Surface gives us
+ glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
+ }
+ else {
+ printf("SDL could not load image.bmp: %s\n", SDL_GetError());
+ SDL_Quit();
+ return 1;
+ }
+
+ // Free the SDL_Surface only if it was successfully created
+ if ( surface ) {
+ SDL_FreeSurface( surface );
+ }
+
+ // Clear the screen before drawing
+ glClear( GL_COLOR_BUFFER_BIT );
+
+ // Bind the texture to which subsequent calls refer to
+ glBindTexture( GL_TEXTURE_2D, texture );
+
+ glBegin( GL_QUADS );
+ // Top-left vertex (corner)
+ glTexCoord2i( 0, 0 );
+ glVertex3f( 100, 100, 0 );
+
+ // Bottom-left vertex (corner)
+ glTexCoord2i( 1, 0 );
+ glVertex3f( 600, 100, 0 );
+
+ // Bottom-right vertex (corner)
+ glTexCoord2i( 1, 1 );
+ glVertex3f( 600, 328, 0 );
+
+ // Top-right vertex (corner)
+ glTexCoord2i( 0, 1 );
+ glVertex3f( 100, 328, 0 );
+ glEnd();
+
+ SDL_GL_SwapBuffers();
+
+#if !EMSCRIPTEN
+ // Wait for 3 seconds to give us a chance to see the image
+ SDL_Delay(3000);
+#endif
+
+ // Now we can delete the OpenGL texture and close down SDL
+ glDeleteTextures( 1, &texture );
+
+ SDL_Quit();
+
+ return 0;
+}
diff --git a/tools/shared.py b/tools/shared.py
index 58ca4ca5..ec3e8d09 100644
--- a/tools/shared.py
+++ b/tools/shared.py
@@ -126,6 +126,12 @@ JS_OPTIMIZER = path_from_root('tools', 'js-optimizer.js')
# Temp dir. Create a random one, unless EMCC_DEBUG is set, in which case use TEMP_DIR/emscripten_temp
+try:
+ TEMP_DIR
+except:
+ print >> sys.stderr, 'TEMP_DIR not defined in ~/.emscripten, using /tmp'
+ TEMP_DIR = '/tmp'
+
CANONICAL_TEMP_DIR = os.path.join(TEMP_DIR, 'emscripten_temp')
EMSCRIPTEN_TEMP_DIR = None