diff --git a/Makefile.emscripten b/Makefile.emscripten index b1019f4..864943e 100644 --- a/Makefile.emscripten +++ b/Makefile.emscripten @@ -13,8 +13,9 @@ EMFLAGS += -s NO_EXIT_RUNTIME=1 LFLAGS := $(EMFLAGS) LFLAGS += -s EXPORTED_FUNCTIONS="['_main','_ags_setAntialiasedStringMode', '_ald_getdata', '_ald_freedata', '_sjis2unicode']" LFLAGS += -s EXTRA_EXPORTED_RUNTIME_METHODS="['getValue','getMemory','addRunDependency','removeRunDependency']" +LFLAGS += --js-library emscripten/library.js -SYNCLIST := -s EMTERPRETIFY_SYNCLIST="['_muspcm_load_no','_muspcm_load_mixlr']" +SYNCLIST := -s EMTERPRETIFY_SYNCLIST="['_muspcm_load_no','_muspcm_load_mixlr','_wait_vsync']" WASMFLAGS := -s WASM=1 -s "BINARYEN_TRAP_MODE='allow'" ASMJSFLAGS := diff --git a/emscripten/library.js b/emscripten/library.js new file mode 100644 index 0000000..fbf5dda --- /dev/null +++ b/emscripten/library.js @@ -0,0 +1,10 @@ +mergeInto(LibraryManager.library, { + wait_vsync: function() { + EmterpreterAsync.handle(function(resume) { + window.requestAnimationFrame(function() { + if (ABORT) return; // do this manually; we can't call into Browser.safeSetTimeout, because that is paused/resumed! + resume(); + }); + }); + } +}); diff --git a/emterpretify_whitelist b/emterpretify_whitelist index 85ba630..841cfe3 100644 --- a/emterpretify_whitelist +++ b/emterpretify_whitelist @@ -1 +1 @@ - -s EMTERPRETIFY_WHITELIST='["_ags_eCopyArea", "_ags_fadeIn", "_ags_fadeOut", "_ags_fader", "_ags_whiteIn", "_ags_whiteOut", "_checkMessage", "_check_command", "_commandCD", "_commandCE", "_commandH", "_commandHH", "_commandIK", "_commandMP", "_commandPF", "_commandPW", "_commandSM", "_commandSP", "_commandSQ", "_commandVA", "_commandY", "_commandZT20", "_commandZT21", "_commands2F1E", "_commands2F21", "_commands2F7C", "_commands2F7D", "_commands2F7E", "_commands2F7F", "_eCopyArea1", "_eCopyArea11", "_eCopyArea12", "_eCopyArea13", "_eCopyArea14", "_eCopyArea15", "_eCopyArea16", "_eCopyArea17", "_eCopyArea18", "_eCopyArea19", "_eCopyArea2", "_eCopyArea20", "_eCopyArea21", "_eCopyArea22", "_eCopyArea23", "_eCopyArea24", "_eCopyArea25", "_eCopyArea26", "_eCopyArea3", "_eCopyArea33", "_eCopyArea34", "_eCopyArea39", "_eCopyArea4", "_eCopyArea40", "_eCopyArea48", "_eCopyArea49", "_eCopyArea5", "_eCopyArea5sp", "_eCopyArea6", "_eCopyArea7", "_eCopyArea8", "_eCopyArea9", "_main", "_msg_putMessage", "_mus_pcm_load", "_mus_pcm_mix", "_mus_pcm_start", "_nact_main", "_sdl_keywait", "_sdl_sleep", "_sel_select", "_sys_addMsg", "_sys_hit_any_key", "_sys_key_releasewait"]' + -s EMTERPRETIFY_WHITELIST='["_ags_eCopyArea", "_ags_fadeIn", "_ags_fadeOut", "_ags_fader", "_ags_whiteIn", "_ags_whiteOut", "_checkMessage", "_check_command", "_commandCD", "_commandCE", "_commandH", "_commandHH", "_commandIK", "_commandMP", "_commandPF", "_commandPW", "_commandSM", "_commandSP", "_commandSQ", "_commandVA", "_commandY", "_commandZT20", "_commandZT21", "_commands2F1E", "_commands2F21", "_commands2F7C", "_commands2F7D", "_commands2F7E", "_commands2F7F", "_eCopyArea1", "_eCopyArea11", "_eCopyArea12", "_eCopyArea13", "_eCopyArea14", "_eCopyArea15", "_eCopyArea16", "_eCopyArea17", "_eCopyArea18", "_eCopyArea19", "_eCopyArea2", "_eCopyArea20", "_eCopyArea21", "_eCopyArea22", "_eCopyArea23", "_eCopyArea24", "_eCopyArea25", "_eCopyArea26", "_eCopyArea3", "_eCopyArea33", "_eCopyArea34", "_eCopyArea39", "_eCopyArea4", "_eCopyArea40", "_eCopyArea48", "_eCopyArea49", "_eCopyArea5", "_eCopyArea5sp", "_eCopyArea6", "_eCopyArea7", "_eCopyArea8", "_eCopyArea9", "_main", "_msg_putMessage", "_mus_pcm_load", "_mus_pcm_mix", "_mus_pcm_start", "_nact_main", "_sdl_keywait", "_sdl_sleep", "_sdl_wait_vsync", "_sel_select", "_sys_addMsg", "_sys_hit_any_key", "_sys_key_releasewait"]' diff --git a/src/nact.c b/src/nact.c index cfa4b4f..63de06f 100644 --- a/src/nact.c +++ b/src/nact.c @@ -225,15 +225,13 @@ void nact_main() { if (cdrom_getpos_count >= 2) { if (!nact->is_message_locked && nact->input_state == InputNotChecked) sys_getInputInfo(); - sdl_updateScreen(); - Sleep(10); + WaitVsync(); cdrom_getpos_count = 0; } #endif #ifdef ENABLE_SDL if (nact->input_state == InputCheckMissed) { - sdl_updateScreen(); - Sleep(10); + WaitVsync(); } #endif } diff --git a/src/sdl_core.h b/src/sdl_core.h index d4b8a01..b91f1a1 100644 --- a/src/sdl_core.h +++ b/src/sdl_core.h @@ -101,6 +101,7 @@ extern void sdl_setAutoRepeat(boolean bool); extern void sdl_mainIteration(); extern boolean RawKeyInfo[]; extern void sdl_sleep(int msec); +extern void sdl_wait_vsync(); extern void sdl_sync(); /* 初期化関係 */ @@ -172,5 +173,11 @@ extern void sdl_sync(); #define ResourceInit(c,v) sdl_ResourceInit((c),(v)) #define SetNoShmMode() /* NO */ #define Sleep(ms) sdl_sleep(ms) +#define WaitVsync() sdl_wait_vsync() + +#ifdef __EMSCRIPTEN__ +// library.js +void wait_vsync(void); +#endif #endif /* !__SDL_CORE__ */ diff --git a/src/sdl_draw.c b/src/sdl_draw.c index 1d00d10..2fc41e2 100644 --- a/src/sdl_draw.c +++ b/src/sdl_draw.c @@ -84,6 +84,15 @@ void sdl_sleep(int msec) { #endif } +void sdl_wait_vsync() { + sdl_updateScreen(); +#ifdef __EMSCRIPTEN__ + wait_vsync(); +#else + SDL_Delay(16); +#endif +} + /* off-screen の指定領域を Main Window へ転送 */ void sdl_updateArea(MyRectangle *src, MyPoint *dst) { SDL_Rect rect_s, rect_d; diff --git a/src/sdl_event.c b/src/sdl_event.c index c72b610..74eb9f0 100644 --- a/src/sdl_event.c +++ b/src/sdl_event.c @@ -22,6 +22,7 @@ #include "config.h" +#include #include #include #include @@ -195,10 +196,13 @@ static void sdl_getEvent(void) { int sdl_keywait(int msec, boolean cancel) { int key=0, n; - int end = get_high_counter(SYSTEMCOUNTER_MSEC) + msec; + int end = msec == INT_MAX ? INT_MAX : get_high_counter(SYSTEMCOUNTER_MSEC) + msec; while ((n = end - get_high_counter(SYSTEMCOUNTER_MSEC)) > 0) { - sdl_sleep(n < 16 ? n : 16); + if (n <= 16) + sdl_sleep(n); + else + sdl_wait_vsync(); nact->callback(); sdl_getEvent(); key = check_button() | sdl_getKeyInfo() | joy_getinfo(); diff --git a/src/xcore.h b/src/xcore.h index 951664c..ea29d65 100644 --- a/src/xcore.h +++ b/src/xcore.h @@ -168,5 +168,6 @@ extern boolean RawKeyInfo[256]; // #define MainIteration() Xcore_mainIteration() #define EventCallback Xcore_eventCallback #define Sleep(ms) usleep((ms) * 1000) +#define WaitVsync() Sleep(16) #endif /* !__XCORE_H__ */