diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-12-12 11:24:16 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-12-12 11:24:16 -0800 |
commit | a5bd3ed73db5f3d499eab532034bb9f6960df28f (patch) | |
tree | aab395631955f7359389f344b75432a784886799 /src | |
parent | ae02feec361db8a69d51a69d51b02ea742212a94 (diff) |
support for generating html in emcc, and a test for that that also tests SDL, and some fixes for SDL
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler.js | 3 | ||||
-rw-r--r-- | src/library_sdl.js | 40 | ||||
-rw-r--r-- | src/preamble.js | 6 | ||||
-rw-r--r-- | src/shell.html | 32 | ||||
-rw-r--r-- | src/shell.js | 3 |
5 files changed, 60 insertions, 24 deletions
diff --git a/src/compiler.js b/src/compiler.js index 94085c3f..2015a09d 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -54,6 +54,9 @@ if (ENVIRONMENT_IS_NODE) { printErr = function(x) { console.log(x); }; + if (typeof print === 'undefined') { + print = printErr; + } read = function(url) { var xhr = new XMLHttpRequest(); diff --git a/src/library_sdl.js b/src/library_sdl.js index 45b0bb53..806ed0ec 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -22,8 +22,12 @@ // noInitialRun: true // }; // -// which is defined BEFORE you load the compiled code. Here -// is a full example: +// which is defined BEFORE you load the compiled code. + +// The test_emcc test in the tests/runner.py will test this +// in its last phase, where it generates HTML. You can see +// a concrete example there. The HTML source is in src/shell.html. +// Here is a more comprehensive example: /* <html> @@ -141,19 +145,19 @@ mergeInto(LibraryManager.library, { }, makeSurface: function(width, height, flags) { - var surf = _malloc(14*QUANTUM_SIZE); // SDL_Surface has 14 fields of quantum size + var surf = _malloc(14*Runtime.QUANTUM_SIZE); // SDL_Surface has 14 fields of quantum size var buffer = _malloc(width*height*4); - var pixelFormat = _malloc(18*QUANTUM_SIZE); + var pixelFormat = _malloc(18*Runtime.QUANTUM_SIZE); flags |= 1; // SDL_HWSURFACE - this tells SDL_MUSTLOCK that this needs to be locked - {{{ makeSetValue('surf+QUANTUM_SIZE*0', '0', 'flags', 'i32') }}} // SDL_Surface.flags - {{{ makeSetValue('surf+QUANTUM_SIZE*1', '0', 'pixelFormat', 'void*') }}} // SDL_Surface.format TODO - {{{ makeSetValue('surf+QUANTUM_SIZE*2', '0', 'width', 'i32') }}} // SDL_Surface.w - {{{ makeSetValue('surf+QUANTUM_SIZE*3', '0', 'height', 'i32') }}} // SDL_Surface.h - {{{ makeSetValue('surf+QUANTUM_SIZE*4', '0', 'width*4', 'i16') }}} // SDL_Surface.pitch, assuming RGBA for now, + {{{ makeSetValue('surf+Runtime.QUANTUM_SIZE*0', '0', 'flags', 'i32') }}} // SDL_Surface.flags + {{{ makeSetValue('surf+Runtime.QUANTUM_SIZE*1', '0', 'pixelFormat', 'void*') }}} // SDL_Surface.format TODO + {{{ makeSetValue('surf+Runtime.QUANTUM_SIZE*2', '0', 'width', 'i32') }}} // SDL_Surface.w + {{{ makeSetValue('surf+Runtime.QUANTUM_SIZE*3', '0', 'height', 'i32') }}} // SDL_Surface.h + {{{ makeSetValue('surf+Runtime.QUANTUM_SIZE*4', '0', 'width*4', 'i16') }}} // SDL_Surface.pitch, assuming RGBA for now, // since that is what ImageData gives us in browsers - {{{ makeSetValue('surf+QUANTUM_SIZE*5', '0', 'buffer', 'void*') }}} // SDL_Surface.pixels - {{{ makeSetValue('surf+QUANTUM_SIZE*6', '0', '0', 'i32*') }}} // SDL_Surface.offset + {{{ makeSetValue('surf+Runtime.QUANTUM_SIZE*5', '0', 'buffer', 'void*') }}} // SDL_Surface.pixels + {{{ makeSetValue('surf+Runtime.QUANTUM_SIZE*6', '0', '0', 'i32*') }}} // SDL_Surface.offset {{{ makeSetValue('pixelFormat + SDL.structs.PixelFormat.palette', '0', '0', 'i32') }}} // TODO {{{ makeSetValue('pixelFormat + SDL.structs.PixelFormat.BitsPerPixel', '0', '32', 'i8') }}} // TODO @@ -239,12 +243,12 @@ mergeInto(LibraryManager.library, { SDL_GetVideoInfo: function() { // %struct.SDL_VideoInfo = type { i32, i32, %struct.SDL_PixelFormat*, i32, i32 } - 5 fields of quantum size - var ret = _malloc(5*QUANTUM_SIZE); - {{{ makeSetValue('ret+QUANTUM_SIZE*0', '0', '0', 'i32') }}} // TODO - {{{ makeSetValue('ret+QUANTUM_SIZE*1', '0', '0', 'i32') }}} // TODO - {{{ makeSetValue('ret+QUANTUM_SIZE*2', '0', '0', 'void*') }}} - {{{ makeSetValue('ret+QUANTUM_SIZE*3', '0', 'SDL.defaults.width', 'i32') }}} - {{{ makeSetValue('ret+QUANTUM_SIZE*4', '0', 'SDL.defaults.height', 'i32') }}} + var ret = _malloc(5*Runtime.QUANTUM_SIZE); + {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*0', '0', '0', 'i32') }}} // TODO + {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*1', '0', '0', 'i32') }}} // TODO + {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*2', '0', '0', 'void*') }}} + {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*3', '0', 'SDL.defaults.width', 'i32') }}} + {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*4', '0', 'SDL.defaults.height', 'i32') }}} return ret; }, @@ -298,7 +302,7 @@ mergeInto(LibraryManager.library, { // 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 - {{{ makeSetValue('surf', '5*QUANTUM_SIZE', 'surfData.buffer', 'void*') }}}; + {{{ makeSetValue('surf', '5*Runtime.QUANTUM_SIZE', 'surfData.buffer', 'void*') }}}; }, SDL_UnlockSurface: function(surf) { diff --git a/src/preamble.js b/src/preamble.js index 1e9e1f11..cd9138d4 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -690,12 +690,6 @@ Module['String_copy'] = String_copy; // Tools -if (typeof console === 'object' && typeof console.log === 'function') { - this['print'] = function(x) { console.log(x) }; // web console -} else if (typeof print === 'undefined') { - this['print'] = function(){}; // harmless no-op -} - // This processes a JS string into a C-line array of numbers, 0-terminated. // For LLVM-originating strings, see parser.js:parseLLVMString function function intArrayFromString(stringy, dontAddNull) { diff --git a/src/shell.html b/src/shell.html new file mode 100644 index 00000000..284bc8d6 --- /dev/null +++ b/src/shell.html @@ -0,0 +1,32 @@ +<html> + <head> + <title>Emscripten-Generated Code</title> + <body> + <center> + <canvas id='canvas' width='256' height='256'></canvas> + </center> + <hr> + <div id='output'></div> + <hr> + <script type='text/javascript'> + // implement print + var print = (function() { + var element = document.getElementById('output'); + return function(text) { + element.innerHTML += text.replace('\n', '<br>', 'g') + '<br>'; + }; + })(); + + // connect to canvas + var Module = { + canvas: document.getElementById('canvas'), + ctx2D: document.getElementById('canvas').getContext('2d') + }; + if (!Module.ctx2D) alert('Canvas not available :('); + + // The compiled code + {{{ SCRIPT_CODE }}} + </script> + </body> +</html> + diff --git a/src/shell.js b/src/shell.js index d52cd77d..038fde02 100644 --- a/src/shell.js +++ b/src/shell.js @@ -46,6 +46,9 @@ if (ENVIRONMENT_IS_NODE) { printErr = function(x) { console.log(x); }; + if (typeof print === 'undefined') { + print = printErr; + } read = function(url) { var xhr = new XMLHttpRequest(); |