diff --git a/src/cdrom.emscripten.c b/src/cdrom.emscripten.c index 653416c..365fc4f 100644 --- a/src/cdrom.emscripten.c +++ b/src/cdrom.emscripten.c @@ -22,8 +22,9 @@ #include #include "portab.h" #include "cdrom.h" +#include "nact.h" -int cdrom_getpos_count; +static int frame_of_getpos = -1; static int cdrom_init(char *); static int cdrom_exit(); @@ -62,7 +63,10 @@ int cdrom_stop() { } int cdrom_getPlayingInfo(cd_time *info) { - cdrom_getpos_count++; + if (nact->frame_count == frame_of_getpos) + nact->wait_vsync = TRUE; + frame_of_getpos = nact->frame_count; + int t = EM_ASM_INT_V( return xsystem35.cdPlayer.getPosition(); ); info->t = t & 0xff; FRAMES_TO_MSF(t >> 8, &info->m, &info->s, &info->f); diff --git a/src/nact.c b/src/nact.c index 63de06f..5530616 100644 --- a/src/nact.c +++ b/src/nact.c @@ -183,29 +183,24 @@ static int checkMessage() { #define MAINLOOP_EVENTCHECK_INTERVAL 16 /* 16 msec */ -#ifdef __EMSCRIPTEN__ - -extern int cdrom_getpos_count; - -#endif // __EMSCRIPTEN__ - void nact_main() { reset_counter_high(SYSTEMCOUNTER_MSEC, 1, 0); reset_counter_high(SYSTEMCOUNTER_MAINLOOP, MAINLOOP_EVENTCHECK_INTERVAL, 0); - int c0; - static int cnt = 0; - -#ifdef __EMSCRIPTEN__ - cdrom_getpos_count = 0; -#endif - while (!nact->is_quit) { + int cnt = 0; #ifdef ENABLE_SDL - nact->input_state = InputNotChecked; + nact->frame_count = 0; + nact->cmd_count = 0; + nact->wait_vsync = FALSE; #endif + + while (!nact->is_quit) { DEBUG_MESSAGE("%d:%x\n", sl_getPage(), sl_getIndex()); if (!nact->popupmenu_opened) { - c0 = checkMessage(); + int c0 = checkMessage(); check_command(c0); +#ifdef ENABLE_SDL + nact->cmd_count++; +#endif } #ifndef __EMSCRIPTEN__ if (!nact->is_message_locked) { @@ -215,22 +210,15 @@ void nact_main() { } } #endif - if (cnt == 10000) { + if (++cnt == 10000) { Sleep(1); /* XXXX */ cnt = 0; } - cnt++; nact->callback(); -#ifdef __EMSCRIPTEN__ - if (cdrom_getpos_count >= 2) { - if (!nact->is_message_locked && nact->input_state == InputNotChecked) - sys_getInputInfo(); - WaitVsync(); - cdrom_getpos_count = 0; - } -#endif #ifdef ENABLE_SDL - if (nact->input_state == InputCheckMissed) { + if (nact->wait_vsync) { + nact->frame_count++; + nact->wait_vsync = FALSE; WaitVsync(); } #endif diff --git a/src/nact.h b/src/nact.h index 4a67cba..e9f6b1d 100644 --- a/src/nact.h +++ b/src/nact.h @@ -143,7 +143,9 @@ typedef struct { } files; #ifdef ENABLE_SDL - enum { InputNotChecked, InputCheckMissed, HadInput } input_state; + int frame_count; + int cmd_count; + boolean wait_vsync; #endif } NACTINFO; diff --git a/src/sdl_event.c b/src/sdl_event.c index 74eb9f0..69edf3d 100644 --- a/src/sdl_event.c +++ b/src/sdl_event.c @@ -64,15 +64,14 @@ static int mouse_to_rawkey(int button) { /* Event処理 */ static void sdl_getEvent(void) { + static int cmd_count_of_prev_input = -1; SDL_Event e; boolean m2b = FALSE, msg_skip = FALSE; int i; - - if (nact->input_state == InputNotChecked) - nact->input_state = InputCheckMissed; + boolean had_input = false; while (SDL_PollEvent(&e)) { - nact->input_state = HadInput; + had_input = true; switch (e.type) { case SDL_WINDOWEVENT: switch (e.window.event) { @@ -186,6 +185,11 @@ static void sdl_getEvent(void) { break; } } + if (had_input) { + cmd_count_of_prev_input = nact->cmd_count; + } else if (nact->cmd_count != cmd_count_of_prev_input) { + nact->wait_vsync = TRUE; + } if (m2b) { menu_open();