This commit is contained in:
Eric House 2021-02-02 13:49:27 -08:00
parent a047a9c865
commit 9b4328d6dc
3 changed files with 75 additions and 1 deletions

View file

@ -3,6 +3,7 @@
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.
#include <sys/time.h>
#include <stdio.h>
#include <stdarg.h>
#include <SDL2/SDL.h>
@ -36,6 +37,7 @@ static void
initGlobals( Globals* globals )
{
globals->cp.showBoardArrow = XP_TRUE;
globals->cp.allowPeek = XP_TRUE;
globals->gi.serverRole = SERVER_STANDALONE;
globals->gi.nPlayers = 2;
@ -82,6 +84,45 @@ makeAndDraw( Globals* globals )
board_draw( globals->game.board, NULL );
}
static time_t
getCurMS()
{
struct timeval tv;
gettimeofday( &tv, NULL );
time_t result = tv.tv_sec * 1000; /* convert to millis */
result += tv.tv_usec / 1000; /* convert to millis too */
// LOG_RETURNF( "%x", result );
return result;
}
static void
checkForTimers( Globals* globals )
{
time_t now = getCurMS();
for ( XWTimerReason why = 0; why < NUM_TIMERS_PLUS_ONE; ++why ) {
TimerState* timer = &globals->timers[why];
XWTimerProc proc = timer->proc;
if ( !!proc && now >= timer->when ) {
timer->proc = NULL;
XP_LOGFF( "timer fired (why=%d): calling proc", why );
(*proc)( timer->closure, NULL, why );
XP_LOGFF( "back from proc" );
}
}
}
void
main_set_timer( Globals* globals, XWTimerReason why, XP_U16 when,
XWTimerProc proc, void* closure )
{
TimerState* timer = &globals->timers[why];
timer->proc = proc;
timer->closure = closure;
time_t now = getCurMS();
timer->when = now + (1000 * when);
}
static void
checkForEvent( Globals* globals )
{
@ -120,6 +161,7 @@ static void
looper( void* closure )
{
Globals* globals = (Globals*)closure;
checkForTimers( globals );
checkForEvent( globals );
}

View file

@ -7,6 +7,13 @@
#include "game.h"
typedef struct _TimerState {
void* closure;
XWTimerReason why;
XWTimerProc proc;
time_t when;
} TimerState;
typedef struct _Globals {
SDL_Window* window;
SDL_Renderer* renderer;
@ -20,7 +27,13 @@ typedef struct _Globals {
TransportProcs procs;
CommonPrefs cp;
TimerState timers[NUM_TIMERS_PLUS_ONE];
MemPoolCtx* mpool;
} Globals;
void main_set_timer( Globals* globals, XWTimerReason why, XP_U16 when,
XWTimerProc proc, void* closure );
#endif

View file

@ -158,7 +158,11 @@ static void
wasm_util_setTimer( XW_UtilCtxt* uc, XWEnv xwe, XWTimerReason why, XP_U16 when,
XWTimerProc proc, void* closure )
{
LOG_FUNC();
XP_LOGFF( "(why: %d)", why );
WasmUtilCtx* wuctxt = (WasmUtilCtx*)uc;
Globals* globals = (Globals*)wuctxt->closure;
main_set_timer( globals, why, when, proc, closure );
LOG_RETURN_VOID();
}
static void
@ -216,6 +220,18 @@ wasm_util_formatPauseHistory( XW_UtilCtxt* uc, XWEnv xwe, XWStreamCtxt* stream,
LOG_FUNC();
}
static void
wasm_util_bonusSquareHeld( XW_UtilCtxt* uc, XWEnv xwe, XWBonusType bonus )
{
LOG_FUNC();
}
static void
wasm_util_playerScoreHeld( XW_UtilCtxt* uc, XWEnv xwe, XP_U16 player )
{
LOG_FUNC();
}
#ifdef XWFEATURE_BOARDWORDS
static void
wasm_util_cellSquareHeld( XW_UtilCtxt* uc, XWEnv xwe, XWStreamCtxt* words )
@ -316,6 +332,9 @@ wasm_util_make( MPFORMAL CurGameInfo* gi, XW_DUtilCtxt* dctxt, void* closure )
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_remSelected, wasm );
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_timerSelected, wasm );
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_formatPauseHistory, wasm );
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_bonusSquareHeld, wasm );
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_playerScoreHeld, wasm );
#ifdef XWFEATURE_BOARDWORDS
SET_VTABLE_ENTRY( wuctxt->super.vtable, util_cellSquareHeld, wasm );
#endif