emscripten: Remove use of EM_ASM from SDL_timer code.

Instead use the native emscripten timer API.

See https://github.com/emscripten-core/emscripten/issues/17941
This commit is contained in:
Sam Clegg 2022-09-29 07:36:14 -07:00 committed by Ryan C. Gordon
parent a97d2e6958
commit cfab203f91
2 changed files with 19 additions and 20 deletions

View File

@ -9,7 +9,7 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: mymindstorm/setup-emsdk@v10 - uses: mymindstorm/setup-emsdk@v10
with: with:
version: 2.0.31 version: 2.0.32
- name: Configure CMake - name: Configure CMake
run: | run: |
emcmake cmake -S . -B build \ emcmake cmake -S . -B build \

View File

@ -375,11 +375,15 @@ SDL_RemoveTimer(SDL_TimerID id)
#else #else
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#include <emscripten/eventloop.h>
typedef struct _SDL_TimerMap typedef struct _SDL_TimerMap
{ {
int timerID; int timerID;
int timeoutID; int timeoutID;
Uint32 interval;
SDL_TimerCallback callback;
void *param;
struct _SDL_TimerMap *next; struct _SDL_TimerMap *next;
} SDL_TimerMap; } SDL_TimerMap;
@ -391,18 +395,14 @@ typedef struct {
static SDL_TimerData SDL_timer_data; static SDL_TimerData SDL_timer_data;
static void static void
SDL_Emscripten_TimerHelper(SDL_TimerMap *entry, Uint32 interval, SDL_TimerCallback callback, void *param) SDL_Emscripten_TimerHelper(void *userdata)
{ {
Uint32 new_timeout; SDL_TimerMap *entry = (SDL_TimerMap*)userdata;
entry->interval = entry->callback(entry->interval, entry->param);
new_timeout = callback(interval, param); if (entry->interval > 0) {
entry->timeoutID = emscripten_set_timeout(&SDL_Emscripten_TimerHelper,
if (new_timeout != 0) { entry->interval,
entry->timeoutID = EM_ASM_INT({ entry);
return Browser.safeSetTimeout(function() {
dynCall('viiii', $0, [$1, $2, $3, $4]);
}, $2);
}, &SDL_Emscripten_TimerHelper, entry, interval, callback, param);
} }
} }
@ -437,12 +437,13 @@ SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *param)
return 0; return 0;
} }
entry->timerID = ++data->nextID; entry->timerID = ++data->nextID;
entry->callback = callback;
entry->param = param;
entry->interval = interval;
entry->timeoutID = EM_ASM_INT({ entry->timeoutID = emscripten_set_timeout(&SDL_Emscripten_TimerHelper,
return Browser.safeSetTimeout(function() { entry->interval,
dynCall('viiii', $0, [$1, $2, $3, $4]); entry);
}, $2);
}, &SDL_Emscripten_TimerHelper, entry, interval, callback, param);
entry->next = data->timermap; entry->next = data->timermap;
data->timermap = entry; data->timermap = entry;
@ -470,9 +471,7 @@ SDL_RemoveTimer(SDL_TimerID id)
} }
if (entry) { if (entry) {
EM_ASM_({ emscripten_clear_timeout(entry->timeoutID);
window.clearTimeout($0);
}, entry->timeoutID);
SDL_free(entry); SDL_free(entry);
return SDL_TRUE; return SDL_TRUE;