diff options
Diffstat (limited to 'src/library_sdl.js')
-rw-r--r-- | src/library_sdl.js | 73 |
1 files changed, 64 insertions, 9 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index aa42f01c..40fc5e7f 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -1,5 +1,64 @@ // To use emscripten's SDL library here, you need to define // Module.canvas and at least one of Module.ctx2D, Module.ctxGL. +// +// More specifically, our SDL implementation will look for +// Module.canvas and Module.ctx2D. You should fill them 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 +// only be able to access the canvas element in the page after +// it loads. You will likely also want to disable running by +// default, with something like +// +// var Module = { +// noInitialRun: true +// }; +// +// which is defined BEFORE you load the compiled code. Here +// is a full example: + +/* +<html> + <head> + <title>Demo</title> + <script type='text/javascript'> + var Module = { + noInitialRun: true + }; + + // implement print + var print = function(text) { + var element = document.getElementById('output') + element.innerHTML = text.replace('\n', '<br>', 'g') + element.innerHTML; + } + </script> + <script src='doom.ccsimple.js' type='text/javascript'></script> + <script type='text/javascript'> + 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> + <body onload='onLoad()' style='background-color: black; color: white'> + <center> + <canvas id='canvas' width='320' height='200'></canvas> + </center> + <div id='output'></div> + </body> +</html> +*/ mergeInto(Library, { $SDL__deps: ['$Browser'], @@ -204,14 +263,14 @@ mergeInto(Library, { // Copy pixel data to somewhere accessible to 'C/C++' var num2 = surfData.image.data.length; for (var i = 0; i < num2; i++) { - IHEAP[surfData.buffer+i] = surfData.image.data[i]; + {{{ makeSetValue('surfData.buffer', 'i', 'surfData.image.data[i]', 'i8') }}}; } } // Mark in C/C++-accessible SDL structure // SDL_Surface has the following fields: Uint32 flags, SDL_PixelFormat *format; int w, h; Uint16 pitch; void *pixels; ... // So we have fields all of the same size, and 5 of them before us. // TODO: Use macros like in library.js - IHEAP[surf + 5*QUANTUM_SIZE] = surfData.buffer; + {{{ makeSetValue('surf', '5*QUANTUM_SIZE', 'surfData.buffer', 'void*') }}}; }, SDL_UnlockSurface: function(surf) { @@ -220,7 +279,7 @@ mergeInto(Library, { var num = surfData.image.data.length; if (!surfData.colors) { for (var i = 0; i < num; i++) { - surfData.image.data[i] = IHEAP[surfData.buffer+i]; // XXX - make sure alpha values are proper in your input + surfData.image.data[i] = {{{ makeGetValue('surfData.buffer', 'i', 'i8') }}}; // XXX - make sure alpha values are proper in your input } } else { var width = Module['canvas'].width; @@ -231,7 +290,7 @@ mergeInto(Library, { for (var y = 0; y < height; y++) { var base = y*width*4; for (var x = 0; x < width; x++) { - var val = IHEAP[s++]; + var val = {{{ makeGetValue('s++', '0', 'i8') }}}; var color = colors[val] || [Math.floor(Math.random()*255),Math.floor(Math.random()*255),Math.floor(Math.random()*255)]; // XXX var start = base + x*4; data[start] = color[0]; @@ -325,11 +384,7 @@ mergeInto(Library, { var surfData = SDL.surfaces[surf]; surfData.colors = []; for (var i = firstColor; i < nColors; i++) { -#if USE_TYPED_ARRAYS - surfData.colors[i] = Array.prototype.slice.call(IHEAP, colors + i*4, colors + i*4 + 4); -#else - surfData.colors[i] = IHEAP.slice(colors + i*4, colors + i*4 + 4); -#endif + surfData.colors[i] = Array_copy(colors + i*4, colors + i*4 + 4); } return 1; }, |