aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoralon@honor <none@none>2010-09-26 15:59:54 -0700
committeralon@honor <none@none>2010-09-26 15:59:54 -0700
commit7ffcdba83938c3bada5c76165f6c1216e3e9083f (patch)
tree2a758d473b4b471e4ec33ff66c1e034a1823a62d /src
parentd4221c3e1a6606ad517eca4ba8bc500ed1c0b315 (diff)
beginnings of SDL support; minor fixes for SDL; raytrace web demo
Diffstat (limited to 'src')
-rw-r--r--src/jsifier.js3
-rw-r--r--src/library.js2
-rw-r--r--src/library_sdl.js65
-rw-r--r--src/parseTools.js3
4 files changed, 70 insertions, 3 deletions
diff --git a/src/jsifier.js b/src/jsifier.js
index 8553676c..2501d1e6 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -704,7 +704,6 @@ function JSify(data) {
substrate.addItems(data.functionStubs, 'FunctionStub');
var params = { 'QUANTUM_SIZE': QUANTUM_SIZE };
- return preprocess(read('preamble.js'), params) + finalCombiner(substrate.solve()) + preprocess(read('postamble.js'), params);
-// return finalCombiner(substrate.solve());
+ return preprocess(read('preamble.js') + finalCombiner(substrate.solve()) + read('postamble.js'), params);
}
diff --git a/src/library.js b/src/library.js
index b0dfeb01..e7c53be9 100644
--- a/src/library.js
+++ b/src/library.js
@@ -164,3 +164,5 @@ var Library = {
llvm_sqrt_f64: 'sqrt',
};
+load('library_sdl.js');
+
diff --git a/src/library_sdl.js b/src/library_sdl.js
new file mode 100644
index 00000000..95842271
--- /dev/null
+++ b/src/library_sdl.js
@@ -0,0 +1,65 @@
+mergeInto(Library, {
+ SDL_Init: function(what) {
+ SDL_SURFACES = {};
+ return 1;
+ },
+
+ SDL_SetVideoMode: function(width, height, depth, flags, canvas) {
+ // ^^^^^^ a 'canvas' parameter is added here; supply a canvas from JS there
+ var surf = _malloc(14*QUANTUM_SIZE); // SDL_Surface has 14 fields of quantum size
+ SDL_SURFACES[surf] = {
+ width: width,
+ height: height,
+ canvas: canvas,
+ ctx: canvas.getContext('2d'),
+ surf: surf,
+ };
+ return surf;
+ },
+
+ SDL_Quit: function() {
+ return 1;
+ },
+
+ SDL_LockSurface: function(surf) {
+ var surfData = SDL_SURFACES[surf];
+ surfData.image = surfData.ctx.getImageData(0, 0, surfData.width, surfData.height);
+ // Copy pixel data to somewhere accessible to 'C/C++'
+ var num = surfData.image.data.length;
+ surfData.buffer = _malloc(num);
+ for (var i = 0; i < num; i++) {
+ HEAP[surfData.buffer+i] = surfData.image.data[i];
+ }
+ // 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.
+ HEAP[surf + 5*QUANTUM_SIZE] = surfData.buffer;
+ },
+
+ SDL_UnlockSurface: function(surf) {
+ var surfData = SDL_SURFACES[surf];
+ // Copy pixel data to image
+ var num = surfData.image.data.length;
+ for (var i = 0; i < num; i++) {
+ surfData.image.data[i] = HEAP[surfData.buffer+i];
+ }
+ for (var i = 0; i < num/4; i++) {
+ surfData.image.data[i*4+3] = 255; // opacity, as canvases blend alpha
+ }
+ // Copy to canvas
+ surfData.ctx.putImageData(surfData.image, 0, 0);
+ // Cleanup
+ surfData.image = null;
+ _free(surfData.buffer);
+ surfData.buffer = null;
+ },
+
+ SDL_Flip: function(surf) {
+ // We actually do this in Unlock...
+ },
+
+ SDL_Delay: function(delay) {
+ // No can do... unless you were a generator...
+ },
+});
+
diff --git a/src/parseTools.js b/src/parseTools.js
index 5d868920..8cbf0166 100644
--- a/src/parseTools.js
+++ b/src/parseTools.js
@@ -54,7 +54,7 @@ function toNiceIdent(ident) {
}
function isNumberType(type) {
- var types = ['i1', 'i8', 'i32', 'i64', 'float', 'double'];
+ var types = ['i1', 'i8', 'i16', 'i32', 'i64', 'float', 'double'];
return types.indexOf(type) != -1;
}
@@ -389,6 +389,7 @@ function getNativeFieldSize(field, alone) {
size = {
'i1': alone ? 1 : 4, // inside a struct, aligned to 4,
'i8': alone ? 1 : 4, // most likely...? XXX
+ 'i16': alone ? 2 : 4, // ditto
'i32': 4,
'i64': 8,
'float': 4,