summaryrefslogtreecommitdiffstats
path: root/utils.c
blob: 4b3b94b3ba32eef77de7e0aeca8c2d2a5283f781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * utils.c
 *
 * Utility functions
 */

#include "graphics.h"
#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;

  update_screen();

  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);
    }
  }
}