aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library_browser.js2
-rw-r--r--src/library_sdl.js182
-rw-r--r--system/include/SDL/SDL_keycode.h3
-rwxr-xr-xtests/runner.py7
-rw-r--r--tests/sdl_gfx_primitives.pngbin0 -> 2357 bytes
-rw-r--r--tests/sdl_rotozoom.c20
-rw-r--r--tests/sdl_rotozoom.pngbin360054 -> 437956 bytes
7 files changed, 168 insertions, 46 deletions
diff --git a/src/library_browser.js b/src/library_browser.js
index d9fd3ee5..822e99d6 100644
--- a/src/library_browser.js
+++ b/src/library_browser.js
@@ -84,7 +84,7 @@ mergeInto(LibraryManager.library, {
var imagePlugin = {};
imagePlugin['canHandle'] = function(name) {
- return !Module.noImageDecoding && /\.(jpg|jpeg|png|bmp)$/.exec(name);
+ return !Module.noImageDecoding && /\.(jpg|jpeg|png|bmp)$/i.test(name);
};
imagePlugin['handle'] = function(byteArray, name, onload, onerror) {
var b = null;
diff --git a/src/library_sdl.js b/src/library_sdl.js
index 176a2fae..646aa54c 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -246,7 +246,7 @@ var LibrarySDL = {
},
translateRGBAToCSSRGBA: function(r, g, b, a) {
- return 'rgba(' + r + ',' + g + ',' + b + ',' + (a/255) + ')';
+ return 'rgba(' + (r&0xff) + ',' + (g&0xff) + ',' + (b&0xff) + ',' + (a&0xff)/255 + ')';
},
translateRGBAToColor: function(r, g, b, a) {
@@ -1022,6 +1022,7 @@ var LibrarySDL = {
surfData.ctx.fillStyle = SDL.translateColorToCSSRGBA(color);
surfData.ctx.fillRect(r.x, r.y, r.w, r.h);
surfData.ctx.restore();
+ return 0;
},
SDL_BlitSurface__deps: ['SDL_UpperBlit'],
@@ -1031,11 +1032,19 @@ var LibrarySDL = {
zoomSurface: function(src, x, y, smooth) {
var srcData = SDL.surfaces[src];
- var w = srcData.width*x;
- var h = srcData.height*y;
- var ret = SDL.makeSurface(w, h, srcData.flags, false, 'zoomSurface');
+ var w = srcData.width * x;
+ var h = srcData.height * y;
+ var ret = SDL.makeSurface(Math.abs(w), Math.abs(h), srcData.flags, false, 'zoomSurface');
var dstData = SDL.surfaces[ret];
- dstData.ctx.drawImage(srcData.canvas, 0, 0, w, h);
+ if (x >= 0 && y >= 0) dstData.ctx.drawImage(srcData.canvas, 0, 0, w, h);
+ else {
+ dstData.ctx.save();
+ dstData.ctx.scale(x < 0 ? -1 : 1, y < 0 ? -1 : 1);
+ dstData.ctx.drawImage(srcData.canvas, w < 0 ? w : 0, h < 0 ? h : 0, Math.abs(w), Math.abs(h));
+ // XXX I think this should work according to the spec, but currently
+ // fails on FF: dstData.ctx.drawImage(srcData.canvas, 0, 0, w, h);
+ dstData.ctx.restore();
+ }
return ret;
},
@@ -1056,6 +1065,14 @@ var LibrarySDL = {
SDL.surfaces[surf].alpha = alpha;
},
+ SDL_SetColorKey: function(surf, flag, key) {
+ // SetColorKey assigns one color to be rendered as transparent. I don't
+ // think the canvas API allows for anything like this, and iterating through
+ // each pixel to replace that color seems prohibitively expensive.
+ Runtime.warnOnce('SDL_SetColorKey is a no-op for performance reasons');
+ return 0;
+ },
+
SDL_GetTicks: function() {
return Math.floor(Date.now() - SDL.startTime);
},
@@ -1472,15 +1489,20 @@ var LibrarySDL = {
},
Mix_HaltChannel: function(channel) {
- var info = SDL.channels[channel];
- if (info.audio) {
- info.audio.pause();
- info.audio = null;
- } else {
- Module.printErr('No Audio for channel: ' + channel);
+ function halt(channel) {
+ var info = SDL.channels[channel];
+ if (info.audio) {
+ info.audio.pause();
+ info.audio = null;
+ }
+ if (SDL.channelFinished) {
+ Runtime.getFuncWrapper(SDL.channelFinished, 'vi')(channel);
+ }
}
- if (SDL.channelFinished) {
- Runtime.getFuncWrapper(SDL.channelFinished, 'vi')(channel);
+ if (channel != -1) {
+ halt(channel);
+ } else {
+ for (var i = 0; i < SDL.channels.length; ++i) halt(i);
}
return 0;
},
@@ -1667,6 +1689,7 @@ var LibrarySDL = {
},
TTF_RenderText_Blended: 'TTF_RenderText_Solid', // XXX ignore blending vs. solid
TTF_RenderText_Shaded: 'TTF_RenderText_Solid', // XXX ignore blending vs. solid
+ TTF_RenderUTF8_Solid: 'TTF_RenderText_Solid',
TTF_SizeText: function(font, text, w, h) {
var fontData = SDL.fonts[font];
@@ -1691,35 +1714,114 @@ var LibrarySDL = {
// SDL gfx
+ $SDL_gfx: {
+ drawRectangle: function(surf, x1, y1, x2, y2, action, cssColor) {
+ x1 = x1 << 16 >> 16;
+ y1 = y1 << 16 >> 16;
+ x2 = x2 << 16 >> 16;
+ y2 = y2 << 16 >> 16;
+ var surfData = SDL.surfaces[surf];
+ assert(!surfData.locked); // but we could unlock and re-lock if we must..
+ // TODO: if ctx does not change, leave as is, and also do not re-set xStyle etc.
+ var x = x1 < x2 ? x1 : x2;
+ var y = y1 < y2 ? y1 : y2;
+ var w = Math.abs(x2 - x1);
+ var h = Math.abs(y2 - y1);
+ surfData.ctx.save();
+ surfData.ctx[action + 'Style'] = cssColor;
+ surfData.ctx[action + 'Rect'](x, y, w, h);
+ surfData.ctx.restore();
+ },
+ drawLine: function(surf, x1, y1, x2, y2, cssColor) {
+ x1 = x1 << 16 >> 16;
+ y1 = y1 << 16 >> 16;
+ x2 = x2 << 16 >> 16;
+ y2 = y2 << 16 >> 16;
+ var surfData = SDL.surfaces[surf];
+ assert(!surfData.locked); // but we could unlock and re-lock if we must..
+ surfData.ctx.save();
+ surfData.ctx.strokeStyle = cssColor;
+ surfData.ctx.beginPath();
+ surfData.ctx.moveTo(x1, y1);
+ surfData.ctx.lineTo(x2, y2);
+ surfData.ctx.stroke();
+ surfData.ctx.restore();
+ },
+ // See http://stackoverflow.com/questions/2172798/how-to-draw-an-oval-in-html5-canvas
+ drawEllipse: function(surf, x, y, rx, ry, action, cssColor) {
+ x = x << 16 >> 16;
+ y = y << 16 >> 16;
+ rx = rx << 16 >> 16;
+ ry = ry << 16 >> 16;
+ var surfData = SDL.surfaces[surf];
+ assert(!surfData.locked); // but we could unlock and re-lock if we must..
+
+ surfData.ctx.save();
+ surfData.ctx.beginPath();
+ surfData.ctx.translate(x, y);
+ surfData.ctx.scale(rx, ry);
+ surfData.ctx.arc(0, 0, 1, 0, 2 * Math.PI);
+ surfData.ctx.restore();
+
+ surfData.ctx.save();
+ surfData.ctx[action + 'Style'] = cssColor;
+ surfData.ctx[action]();
+ surfData.ctx.restore();
+ },
+ // the gfx library uses something different from the rest of SDL...
+ translateColorToCSSRGBA: function(rgba) {
+ return 'rgba(' + (rgba>>>24) + ',' + (rgba>>16 & 0xff) + ',' + (rgba>>8 & 0xff) + ',' + (rgba&0xff) + ')';
+ }
+ },
+
+ boxColor__deps: ['$SDL_gfx'],
+ boxColor: function(surf, x1, y1, x2, y2, color) {
+ return SDL_gfx.drawRectangle(surf, x1, y1, x2, y2, 'fill', SDL_gfx.translateColorToCSSRGBA(color));
+ },
+
+ boxRGBA__deps: ['$SDL_gfx'],
boxRGBA: function(surf, x1, y1, x2, y2, r, g, b, a) {
- var surfData = SDL.surfaces[surf];
- assert(!surfData.locked); // but we could unlock and re-lock if we must..
- // TODO: if ctx does not change, leave as is, and also do not re-set xStyle etc.
- surfData.ctx.save();
- surfData.ctx.fillStyle = SDL.translateRGBAToCSSRGBA(r, g, b, a);
- surfData.ctx.fillRect(x1, y1, x2-x1, y2-y1);
- surfData.ctx.restore();
+ return SDL_gfx.drawRectangle(surf, x1, y1, x2, y2, 'fill', SDL.translateRGBAToCSSRGBA(r, g, b, a));
},
+ rectangleColor__deps: ['$SDL_gfx'],
+ rectangleColor: function(surf, x1, y1, x2, y2, color) {
+ return SDL_gfx.drawRectangle(surf, x1, y1, x2, y2, 'stroke', SDL_gfx.translateColorToCSSRGBA(color));
+ },
+
+ rectangleRGBA__deps: ['$SDL_gfx'],
rectangleRGBA: function(surf, x1, y1, x2, y2, r, g, b, a) {
- var surfData = SDL.surfaces[surf];
- assert(!surfData.locked); // but we could unlock and re-lock if we must..
- surfData.ctx.save();
- surfData.ctx.strokeStyle = SDL.translateRGBAToCSSRGBA(r, g, b, a);
- surfData.ctx.strokeRect(x1, y1, x2-x1, y2-y1);
- surfData.ctx.restore();
+ return SDL_gfx.drawRectangle(surf, x1, y1, x2, y2, 'stroke', SDL.translateRGBAToCSSRGBA(r, g, b, a));
+ },
+
+ ellipseColor__deps: ['$SDL_gfx'],
+ ellipseColor: function(surf, x, y, rx, ry, color) {
+ return SDL_gfx.drawEllipse(surf, x, y, rx, ry, 'stroke', SDL_gfx.translateColorToCSSRGBA(color));
+ },
+
+ ellipseRGBA__deps: ['$SDL_gfx'],
+ ellipseRGBA: function(surf, x, y, rx, ry, r, g, b, a) {
+ return SDL_gfx.drawEllipse(surf, x, y, rx, ry, 'stroke', SDL.translateRGBAToCSSRGBA(r, g, b, a));
+ },
+
+ filledEllipseColor__deps: ['$SDL_gfx'],
+ filledEllipseColor: function(surf, x, y, rx, ry, color) {
+ return SDL_gfx.drawEllipse(surf, x, y, rx, ry, 'fill', SDL_gfx.translateColorToCSSRGBA(color));
+ },
+
+ filledEllipseRGBA__deps: ['$SDL_gfx'],
+ filledEllipseRGBA: function(surf, x, y, rx, ry, r, g, b, a) {
+ return SDL_gfx.drawEllipse(surf, x, y, rx, ry, 'fill', SDL.translateRGBAToCSSRGBA(r, g, b, a));
},
+ lineColor__deps: ['$SDL_gfx'],
+ lineColor: function(surf, x1, y1, x2, y2, color) {
+ return SDL_gfx.drawLine(surf, x1, y1, x2, y2, SDL_gfx.translateColorToCSSRGBA(color));
+ },
+
+ lineRGBA__deps: ['$SDL_gfx'],
lineRGBA: function(surf, x1, y1, x2, y2, r, g, b, a) {
- var surfData = SDL.surfaces[surf];
- assert(!surfData.locked); // but we could unlock and re-lock if we must..
- surfData.ctx.save();
- surfData.ctx.strokeStyle = SDL.translateRGBAToCSSRGBA(r, g, b, a);
- surfData.ctx.beginPath();
- surfData.ctx.moveTo(x1, y1);
- surfData.ctx.lineTo(x2, y2);
- surfData.ctx.stroke();
- surfData.ctx.restore();
+ return SDL_gfx.drawLine(surf, x1, y1, x2, y2, SDL.translateRGBAToCSSRGBA(r, g, b, a));
},
pixelRGBA__deps: ['boxRGBA'],
@@ -1805,12 +1907,18 @@ var LibrarySDL = {
return -1;
},
+ // Joysticks
+
+ SDL_NumJoysticks: function() { return 0 },
+
+ SDL_JoystickOpen: function(deviceIndex) { return 0 },
+
+ SDL_JoystickGetButton: function(joystick, button) { return 0 },
+
// Misc
SDL_InitSubSystem: function(flags) { return 0 },
- SDL_NumJoysticks: function() { return 0 },
-
SDL_RWFromFile: function(filename, mode) {
return filename; // XXX We just forward the filename
},
diff --git a/system/include/SDL/SDL_keycode.h b/system/include/SDL/SDL_keycode.h
index 472ca28e..84063b8b 100644
--- a/system/include/SDL/SDL_keycode.h
+++ b/system/include/SDL/SDL_keycode.h
@@ -310,7 +310,8 @@ enum
SDLK_KBDILLUMDOWN = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KBDILLUMDOWN),
SDLK_KBDILLUMUP = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_KBDILLUMUP),
SDLK_EJECT = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_EJECT),
- SDLK_SLEEP = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_SLEEP)
+ SDLK_SLEEP = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_SLEEP),
+ SDLK_LAST = SDL_SCANCODE_TO_KEYCODE(SDL_NUM_SCANCODES)
};
/**
diff --git a/tests/runner.py b/tests/runner.py
index b819af25..b21eee08 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -13183,8 +13183,11 @@ Press any key to continue.'''
self.btest('sdl_maprgba.c', reference='sdl_maprgba.png', reference_slack=3)
def test_sdl_rotozoom(self):
- shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'example.png'))
- self.btest('sdl_rotozoom.c', reference='sdl_rotozoom.png', args=['--preload-file', 'example.png'])
+ shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png'))
+ self.btest('sdl_rotozoom.c', reference='sdl_rotozoom.png', args=['--preload-file', 'screenshot.png'], reference_slack=3)
+
+ def test_sdl_gfx_primitives(self):
+ self.btest('sdl_gfx_primitives.c', reference='sdl_gfx_primitives.png', reference_slack=1)
def test_sdl_canvas_palette_2(self):
open(os.path.join(self.get_dir(), 'pre.js'), 'w').write('''
diff --git a/tests/sdl_gfx_primitives.png b/tests/sdl_gfx_primitives.png
new file mode 100644
index 00000000..525b4f8f
--- /dev/null
+++ b/tests/sdl_gfx_primitives.png
Binary files differ
diff --git a/tests/sdl_rotozoom.c b/tests/sdl_rotozoom.c
index b3970f6c..e81258d9 100644
--- a/tests/sdl_rotozoom.c
+++ b/tests/sdl_rotozoom.c
@@ -6,15 +6,18 @@
#include "emscripten.h"
#endif
+const int numSprites = 8;
SDL_Surface *screen;
-SDL_Surface *sprite[6];
+SDL_Surface *sprite[numSprites];
void mainloop() {
int i;
+ int row = 0;
SDL_Rect rect = { 0, 0, 100, 100 };
- for (i = 0; i < 6; i++) {
+ for (i = 0; i < numSprites; i++) {
rect.x = i & 1 ? 200 : 0;
- rect.y = i & 2 ? 200 : 0;
+ rect.y = row * 200;
+ row += i & 1;
SDL_BlitSurface(sprite[i], 0, screen, &rect);
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
@@ -23,20 +26,27 @@ void mainloop() {
int main(int argc, char **argv) {
SDL_Init(SDL_INIT_VIDEO);
- screen = SDL_SetVideoMode(400, 400, 32, SDL_SWSURFACE);
+ const int width = 400;
+ const int height = 200 * (numSprites + 1) / 2;
+ screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);
+ SDL_Rect rect = { 0, 0, width, height };
+ SDL_FillRect(screen, &rect, SDL_MapRGBA(screen->format, 0, 0, 0, 0xff));
- sprite[0] = IMG_Load("example.png");
+ sprite[0] = IMG_Load("screenshot.png");
sprite[1] = SDL_CreateRGBSurface(SDL_SWSURFACE, 100, 100, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF);
SDL_FillRect(sprite[1], 0, 0xA0A0A0A0);
sprite[2] = zoomSurface(sprite[0], 0.5, 0.5, SMOOTHING_ON);
sprite[3] = zoomSurface(sprite[1], 0.5, 0.5, SMOOTHING_ON);
sprite[4] = rotozoomSurface(sprite[0], -20, 0.3, SMOOTHING_ON);
sprite[5] = rotozoomSurface(sprite[1], 45, 0.5, SMOOTHING_ON);
+ sprite[6] = zoomSurface(sprite[0], -0.5, 0.5, SMOOTHING_ON);
+ sprite[7] = zoomSurface(sprite[0], -0.5, -0.5, SMOOTHING_ON);
mainloop();
#ifndef EMSCRIPTEN
SDL_Event evt;
+ SDL_SaveBMP(screen, "native_output.bmp");
while (1) {
if (SDL_PollEvent(&evt) != 0 && evt.type == SDL_QUIT) break;
//mainloop();
diff --git a/tests/sdl_rotozoom.png b/tests/sdl_rotozoom.png
index 3ca8da70..5c5dec2f 100644
--- a/tests/sdl_rotozoom.png
+++ b/tests/sdl_rotozoom.png
Binary files differ