mirror of
https://github.com/Relintai/sdl2_frt.git
synced 2024-12-20 22:16:49 +01:00
Emscripten: implement custom cursors
This commit is contained in:
parent
33bddcfda2
commit
0d647d35b0
@ -33,7 +33,7 @@
|
||||
#include "SDL_assert.h"
|
||||
|
||||
static SDL_Cursor*
|
||||
Emscripten_CreateCursorFromString(const char* cursor_str)
|
||||
Emscripten_CreateCursorFromString(const char* cursor_str, SDL_bool is_custom)
|
||||
{
|
||||
SDL_Cursor* cursor;
|
||||
Emscripten_CursorData *curdata;
|
||||
@ -48,6 +48,7 @@ Emscripten_CreateCursorFromString(const char* cursor_str)
|
||||
}
|
||||
|
||||
curdata->system_cursor = cursor_str;
|
||||
curdata->is_custom = is_custom;
|
||||
cursor->driverdata = curdata;
|
||||
}
|
||||
else {
|
||||
@ -60,16 +61,71 @@ Emscripten_CreateCursorFromString(const char* cursor_str)
|
||||
static SDL_Cursor*
|
||||
Emscripten_CreateDefaultCursor()
|
||||
{
|
||||
return Emscripten_CreateCursorFromString("default");
|
||||
return Emscripten_CreateCursorFromString("default", SDL_FALSE);
|
||||
}
|
||||
|
||||
/*
|
||||
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*
|
||||
Emscripten_CreateSystemCursor(SDL_SystemCursor id)
|
||||
@ -117,7 +173,7 @@ Emscripten_CreateSystemCursor(SDL_SystemCursor id)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return Emscripten_CreateCursorFromString(cursor_name);
|
||||
return Emscripten_CreateCursorFromString(cursor_name, SDL_FALSE);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -128,6 +184,9 @@ Emscripten_FreeCursor(SDL_Cursor* cursor)
|
||||
curdata = (Emscripten_CursorData *) cursor->driverdata;
|
||||
|
||||
if (curdata != NULL) {
|
||||
if (curdata->is_custom) {
|
||||
SDL_free((char *)curdata->system_cursor);
|
||||
}
|
||||
SDL_free(cursor->driverdata);
|
||||
}
|
||||
|
||||
@ -190,9 +249,7 @@ Emscripten_InitMouse()
|
||||
{
|
||||
SDL_Mouse* mouse = SDL_GetMouse();
|
||||
|
||||
/*
|
||||
mouse->CreateCursor = Emscripten_CreateCursor;
|
||||
*/
|
||||
mouse->ShowCursor = Emscripten_ShowCursor;
|
||||
mouse->FreeCursor = Emscripten_FreeCursor;
|
||||
mouse->WarpMouse = Emscripten_WarpMouse;
|
||||
|
@ -23,9 +23,12 @@
|
||||
#ifndef _SDL_emscriptenmouse_h
|
||||
#define _SDL_emscriptenmouse_h
|
||||
|
||||
#include "SDL_stdinc.h"
|
||||
|
||||
typedef struct _Emscripten_CursorData
|
||||
{
|
||||
const char *system_cursor;
|
||||
SDL_bool is_custom;
|
||||
} Emscripten_CursorData;
|
||||
|
||||
extern void
|
||||
|
Loading…
Reference in New Issue
Block a user