r/o
1
#include "renderer.h"
2
#include "qb.h"
3
#include "text.h"
4
5
static void loop(void);
6
7
int main(int argc, char **argv) {
8
int r = renderer_init();
9
if (r) {
10
return r;
11
}
12
13
qb_init();
14
15
loop();
16
17
renderer_quit();
18
19
return 0;
20
}
21
22
#define FPS 60 /* Technically: vsync rate */
23
#define TICK_LENGTH (1000 / FPS)
24
#define FLIP_CURSOR 16 /* frame count */
25
26
#define TYPEMATIC_DELAY 250
27
#define TYPEMATIC_REPEAT 100
28
29
void loop(void) {
30
SDL_Event event;
31
Uint32 last_tick = SDL_GetTicks();
32
33
text_refresh();
34
int until_flip = FLIP_CURSOR;
35
36
Uint32 keydown_tick = 0;
37
SDL_Keycode keydown_sym;
38
Uint16 keydown_mod;
39
int typematic_on = 0;
40
41
while (qb_running) {
42
if (keydown_tick && !typematic_on && last_tick >= keydown_tick + TYPEMATIC_DELAY) {
43
typematic_on = 1;
44
keydown_tick = last_tick;
45
qb_keypress(keydown_sym, keydown_mod);
46
} else if (keydown_tick && typematic_on && last_tick >= keydown_tick + TYPEMATIC_REPEAT) {
47
keydown_tick = last_tick;
48
qb_keypress(keydown_sym, keydown_mod);
49
}
50
51
while (SDL_PollEvent(&event)) {
52
switch (event.type) {
53
case SDL_QUIT:
54
return;
55
56
case SDL_KEYDOWN:
57
if (event.key.repeat) {
58
break;
59
}
60
61
qb_keypress(event.key.keysym.sym, event.key.keysym.mod);
62
keydown_tick = SDL_GetTicks();
63
keydown_sym = event.key.keysym.sym;
64
keydown_mod = event.key.keysym.mod;
65
typematic_on = 0;
66
break;
67
68
case SDL_KEYUP:
69
keydown_tick = 0;
70
break;
71
}
72
}
73
74
Uint32 next_tick = last_tick + TICK_LENGTH;
75
last_tick = SDL_GetTicks();
76
77
if (last_tick < next_tick) {
78
SDL_Delay(next_tick - last_tick);
79
last_tick = SDL_GetTicks();
80
}
81
82
--until_flip;
83
if (until_flip == 0) {
84
until_flip = FLIP_CURSOR;
85
text_cursor_toggle();
86
text_refresh();
87
}
88
}
89
}
90
91
/* vim: set sw=4 et: */
92