/* * utils.c * * Utility functions */ #include "grv.h" /* Foreground color of a specific character */ int fg(int r, int c) { return screen1(r,c) & 15; } /* Background color of a specific character */ int bg(int r, int c) { return (screen1(r,c) >> 4) & 7; } /* Is there something at this screen location? */ int busy(int r, int c) { return (screen(r,c) != ' ') || (bg(r,c) == 0); } /* Format time elapsed in MM:SS.s format */ char *format_time(double t) { static char ts[10]; int ds = (int)(t*10.0); /* Deciseconds */ sprintf(ts, "%02d:%02d.%01d", ds/600, (ds%600)/10, ds%10); return ts; } /* Standard callback routine to post a periodic user event */ /* (int)param is the event code */ Uint32 post_periodic(Uint32 interval, void *param) { SDL_Event event; event.type = SDL_USEREVENT; event.user.code = (size_t)param; event.user.data1 = event.user.data2 = 0; SDL_PushEvent(&event); return interval; } /* Standard callback routine to post a periodic user event */ /* (int)param is the event code */ Uint32 post_oneshot(Uint32 interval, void *param) { SDL_Event event; event.type = SDL_USEREVENT; event.user.code = (size_t)param; event.user.data1 = event.user.data2 = 0; (void)interval; SDL_PushEvent(&event); return 0; } /* Very simple event handler which just posts a timer and handles blink events until the timer expires */ void mymssleep(int ms) { SDL_TimerID mytimer = SDL_AddTimer(ms, post_oneshot, (void *)event_sleep); SDL_Event event; while ( SDL_WaitEvent(&event) ) { if ( event.type == SDL_USEREVENT ) { if ( event.user.code == event_sleep ) { SDL_RemoveTimer(mytimer); return; } else if ( event.user.code == event_blink ) update_blink(); } else if ( event.type == SDL_KEYDOWN ) { push_key(&event.key); } } }