Navigation Menu

Skip to content

Commit

Permalink
Emscripten: implement custom cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
Daft-Freak committed Feb 17, 2017
1 parent 33bddcf commit 0d647d3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
75 changes: 66 additions & 9 deletions src/video/emscripten/SDL_emscriptenmouse.c
Expand Up @@ -33,7 +33,7 @@
#include "SDL_assert.h" #include "SDL_assert.h"


static SDL_Cursor* static SDL_Cursor*
Emscripten_CreateCursorFromString(const char* cursor_str) Emscripten_CreateCursorFromString(const char* cursor_str, SDL_bool is_custom)
{ {
SDL_Cursor* cursor; SDL_Cursor* cursor;
Emscripten_CursorData *curdata; Emscripten_CursorData *curdata;
Expand All @@ -48,6 +48,7 @@ Emscripten_CreateCursorFromString(const char* cursor_str)
} }


curdata->system_cursor = cursor_str; curdata->system_cursor = cursor_str;
curdata->is_custom = is_custom;
cursor->driverdata = curdata; cursor->driverdata = curdata;
} }
else { else {
Expand All @@ -60,16 +61,71 @@ Emscripten_CreateCursorFromString(const char* cursor_str)
static SDL_Cursor* static SDL_Cursor*
Emscripten_CreateDefaultCursor() Emscripten_CreateDefaultCursor()
{ {
return Emscripten_CreateCursorFromString("default"); return Emscripten_CreateCursorFromString("default", SDL_FALSE);
} }


/*
static SDL_Cursor* static SDL_Cursor*
Emscripten_CreateCursor(SDL_Surface* sruface, int hot_x, int hot_y) Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
{ {
return Emscripten_CreateDefaultCursor(); const char *cursor_url = NULL;
SDL_Surface *conv_surf;

conv_surf = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, 0);

if (!conv_surf) {
return NULL;
}

cursor_url = (const char *)EM_ASM_INT({
var w = $0;
var h = $1;
var pixels = $2;

var canvas = document.createElement("canvas");
canvas.width = w;
canvas.height = h;

var ctx = canvas.getContext("2d");

var image = ctx.createImageData(w, h);
var data = image.data;
var src = pixels >> 2;
var dst = 0;
var num;
if (typeof CanvasPixelArray !== 'undefined' && data instanceof CanvasPixelArray) {
// IE10/IE11: ImageData objects are backed by the deprecated CanvasPixelArray,
// not UInt8ClampedArray. These don't have buffers, so we need to revert
// to copying a byte at a time. We do the undefined check because modern
// browsers do not define CanvasPixelArray anymore.
num = data.length;
while (dst < num) {
var val = HEAP32[src]; // This is optimized. Instead, we could do {{{ makeGetValue('buffer', 'dst', 'i32') }}};
data[dst ] = val & 0xff;
data[dst+1] = (val >> 8) & 0xff;
data[dst+2] = (val >> 16) & 0xff;
data[dst+3] = (val >> 24) & 0xff;
src++;
dst += 4;
}
} else {
var data32 = new Int32Array(data.buffer);
num = data32.length;
data32.set(HEAP32.subarray(src, src + num));
}

ctx.putImageData(image, 0, 0);
var url = "url(" + canvas.toDataURL() + "), auto";

var urlBuf = _malloc(url.length + 1);
stringToUTF8(url, urlBuf, url.length + 1);

return urlBuf;
}, surface->w, surface->h, conv_surf->pixels);

SDL_FreeSurface(conv_surf);

return Emscripten_CreateCursorFromString(cursor_url, SDL_TRUE);
} }
*/


static SDL_Cursor* static SDL_Cursor*
Emscripten_CreateSystemCursor(SDL_SystemCursor id) Emscripten_CreateSystemCursor(SDL_SystemCursor id)
Expand Down Expand Up @@ -117,7 +173,7 @@ Emscripten_CreateSystemCursor(SDL_SystemCursor id)
return NULL; return NULL;
} }


return Emscripten_CreateCursorFromString(cursor_name); return Emscripten_CreateCursorFromString(cursor_name, SDL_FALSE);
} }


static void static void
Expand All @@ -128,6 +184,9 @@ Emscripten_FreeCursor(SDL_Cursor* cursor)
curdata = (Emscripten_CursorData *) cursor->driverdata; curdata = (Emscripten_CursorData *) cursor->driverdata;


if (curdata != NULL) { if (curdata != NULL) {
if (curdata->is_custom) {
SDL_free((char *)curdata->system_cursor);
}
SDL_free(cursor->driverdata); SDL_free(cursor->driverdata);
} }


Expand Down Expand Up @@ -190,9 +249,7 @@ Emscripten_InitMouse()
{ {
SDL_Mouse* mouse = SDL_GetMouse(); SDL_Mouse* mouse = SDL_GetMouse();


/*
mouse->CreateCursor = Emscripten_CreateCursor; mouse->CreateCursor = Emscripten_CreateCursor;
*/
mouse->ShowCursor = Emscripten_ShowCursor; mouse->ShowCursor = Emscripten_ShowCursor;
mouse->FreeCursor = Emscripten_FreeCursor; mouse->FreeCursor = Emscripten_FreeCursor;
mouse->WarpMouse = Emscripten_WarpMouse; mouse->WarpMouse = Emscripten_WarpMouse;
Expand Down
3 changes: 3 additions & 0 deletions src/video/emscripten/SDL_emscriptenmouse.h
Expand Up @@ -23,9 +23,12 @@
#ifndef _SDL_emscriptenmouse_h #ifndef _SDL_emscriptenmouse_h
#define _SDL_emscriptenmouse_h #define _SDL_emscriptenmouse_h


#include "SDL_stdinc.h"

typedef struct _Emscripten_CursorData typedef struct _Emscripten_CursorData
{ {
const char *system_cursor; const char *system_cursor;
SDL_bool is_custom;
} Emscripten_CursorData; } Emscripten_CursorData;


extern void extern void
Expand Down

0 comments on commit 0d647d3

Please sign in to comment.