1
#include "main.h"
2
#include "parser.h"
3
#include "qb.h"
4
#include "renderer.h"
5
#include "text.h"
6
7
int mouse_x = 0;
8
int mouse_y = 0;
9
10
static void loop(void);
11
12
int main(int argc, char **argv) {
13
if (argc == 2 && strcmp(argv[1], "parser-test") == 0) {
14
return parser_test();
15
}
16
17
int r = renderer_init();
18
if (r) {
19
return r;
20
}
21
22
qb_init();
23
24
loop();
25
26
renderer_quit();
27
28
return 0;
29
}
30
31
#define FPS 60 /* Technically: vsync rate */
32
#define TICK_LENGTH (1000 / FPS)
33
#define FLIP_CURSOR 16 /* frame count */
34
35
#define TYPEMATIC_DELAY 250
36
#define TYPEMATIC_REPEAT 100
37
38
void loop(void) {
39
SDL_Event event;
40
Uint32 last_tick = SDL_GetTicks();
41
42
text_refresh();
43
int until_flip = FLIP_CURSOR;
44
45
Uint32 keydown_tick = 0;
46
SDL_Keycode keydown_sym;
47
Uint16 keydown_mod;
48
int typematic_on = 0;
49
50
while (qb_running) {
51
if (keydown_tick && !typematic_on && last_tick >= keydown_tick + TYPEMATIC_DELAY) {
52
typematic_on = 1;
53
keydown_tick = last_tick;
54
qb_keypress(keydown_sym, keydown_mod);
55
} else if (keydown_tick && typematic_on && last_tick >= keydown_tick + TYPEMATIC_REPEAT) {
56
keydown_tick = last_tick;
57
qb_keypress(keydown_sym, keydown_mod);
58
}
59
60
while (SDL_PollEvent(&event)) {
61
switch (event.type) {
62
case SDL_QUIT:
63
return;
64
65
case SDL_KEYDOWN:
66
if (event.key.repeat) {
67
break;
68
}
69
70
qb_keydown(event.key.keysym.sym, event.key.keysym.mod);
71
qb_keypress(event.key.keysym.sym, event.key.keysym.mod);
72
keydown_tick = SDL_GetTicks();
73
keydown_sym = event.key.keysym.sym;
74
keydown_mod = event.key.keysym.mod;
75
typematic_on = 0;
76
break;
77
78
case SDL_KEYUP:
79
qb_keyup(event.key.keysym.sym);
80
keydown_tick = 0;
81
break;
82
83
case SDL_MOUSEMOTION: {
84
int old_x = mouse_x,
85
old_y = mouse_y;
86
mouse_x = event.motion.x;
87
mouse_y = event.motion.y;
88
89
if (mouse_x != old_x || mouse_y != old_y) {
90
text_refresh();
91
}
92
break;
93
}
94
95
case SDL_MOUSEBUTTONDOWN:
96
mouse_x = event.button.x;
97
mouse_y = event.button.y;
98
text_refresh();
99
if (event.button.button == SDL_BUTTON_LEFT ||
100
event.button.button == SDL_BUTTON_RIGHT) {
101
qb_mouseclick(event.button.button == SDL_BUTTON_LEFT ? 1 : 2);
102
}
103
break;
104
}
105
}
106
107
Uint32 next_tick = last_tick + TICK_LENGTH;
108
last_tick = SDL_GetTicks();
109
110
if (last_tick < next_tick) {
111
SDL_Delay(next_tick - last_tick);
112
last_tick = SDL_GetTicks();
113
}
114
115
--until_flip;
116
if (until_flip == 0) {
117
until_flip = FLIP_CURSOR;
118
text_cursor_toggle();
119
text_refresh();
120
}
121
}
122
}
123
124
/* vim: set sw=4 et: */
125