-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.cpp
446 lines (379 loc) · 9.61 KB
/
app.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <thread>
#include <iostream>
#include <signal.h>
#include "app.h"
#include "button.h"
#include "video.h"
#define DARTPUNK_QUIT 0
#define DARTPUNK_RESTART -1
#define DARTPUNK_DEFAULT 1
#if DARTPUNK_BOARD_ED900
#include "board/ed900.h"
#elif DARTPUNK_BOARD_VIRTUAL
#include "board/virtual.h"
#else
#error DARTPUNK_BOARD
#endif
#if DARTPUNK_INPUT_JOYSTICK
#include "input/joystick.h"
#elif DARTPUNK_INPUT_KEYBOARD
#include "input/keyboard.h"
#elif DARTPUNK_INPUT_VIRTUAL
#define DARTPUNK_BUTTON_1 SDLK_1
#define DARTPUNK_BUTTON_2 SDLK_2
#define DARTPUNK_BUTTON_3 SDLK_3
#define DARTPUNK_BUTTON_4 SDLK_4
#define DARTPUNK_BUTTON_5 SDLK_SPACE
#else
#error DARTPUNK_INPUT
#endif
#if DARTPUNK_DISPLAY_MATRIX
#include "display/matrix.h"
#elif DARTPUNK_DISPLAY_VIRTUAL
#include "display/virtual.h"
#else
#error DARTPUNK_DISPLAY
#endif
using namespace std;
using namespace chrono;
namespace dartpunk
{
volatile sig_atomic_t status = DARTPUNK_DEFAULT;
App::App () :
matrix {},
fonts {},
state {State::BLUETOOTH},
bluetooth {},
settings {},
game {nullptr}
{
fonts.emplace_back (FontTomThumb {});
fonts.emplace_back (FontTopaz {});
fonts.emplace_back (FontScore {});
fonts.emplace_back (FontRoboto {});
fonts.emplace_back (FontCricket {});
auto f = [] (int s) {
switch (s)
{
case SIGINT:
case SIGTERM:
status = DARTPUNK_QUIT;
break;
default:
status = DARTPUNK_RESTART;
break;
}
};
signal (SIGINT, f);
signal (SIGTERM, f);
#if DARTPUNK_SDL2
SDL_Init (SDL_INIT_EVERYTHING);
#endif
}
App::~App ()
{
delete game;
fonts.clear ();
#if DARTPUNK_SDL2
SDL_Quit ();
#endif
}
void App::button1 (bool special)
{
if (special)
exit (DARTPUNK_QUIT);
else
settings.select (1);
}
void App::button2 (bool special)
{
if (special)
exit (DARTPUNK_RESTART);
else
settings.select (2);
}
void App::button3 ()
{
settings.select (3);
}
void App::button4 ()
{
settings.select (4);
}
void App::button5 (bool special)
{
if (special)
cancel ();
else
confirm ();
}
void App::settings_confirm ()
{
game::Game * g = settings.confirm ();
if (g)
{
game = g;
state = State::GAME;
}
}
void App::game_button (board::Button b)
{
if (game->button (b))
game_stop ();
}
void App::game_stop ()
{
delete game;
game = nullptr;
state = State::SETTINGS;
}
void App::exit (int s)
{
status = s;
}
void App::cancel ()
{
switch (state)
{
case State::SETTINGS:
settings.cancel ();
break;
case State::GAME:
game_button (board::Button::CANCEL);
break;
}
}
void App::confirm ()
{
switch (state)
{
case State::SETTINGS:
settings_confirm ();
break;
case State::GAME:
game_button (board::Button::NEXT);
break;
}
}
void App::board_connection (board::EventPtr e)
{
auto c = dynamic_pointer_cast<board::ConnectionEvent> (e);
state = c->connected ? (game ? State::GAME : State::SETTINGS) : State::BLUETOOTH;
}
void App::board_dart (board::EventPtr e)
{
auto d = dynamic_pointer_cast<board::DartEvent> (e);
cout << d->text (true) << endl;
if (game) game->dart (*d);
}
void App::board_button (board::EventPtr e)
{
auto b = dynamic_pointer_cast<board::ButtonEvent> (e);
if (game && game->button (*b)) game_stop ();
}
int App::run ()
{
#if DARTPUNK_BOARD_ED900
board::ED900 board {"/org/bluez/hci0"};
#elif DARTPUNK_BOARD_VIRTUAL
board::Virtual board;
#endif
#if DARTPUNK_INPUT_JOYSTICK
input::Device input {"/dev/input/event0"};
#elif DARTPUNK_INPUT_KEYBOARD
input::Device input {"/dev/input/event4"};
#endif
#if DARTPUNK_DISPLAY_MATRIX
display::Matrix display;
#elif DARTPUNK_DISPLAY_VIRTUAL
display::Virtual display {5};
#endif
Button b1 {5s};
Button b2 {3s};
Button b5 {1s};
for (TimePoint t1 {steady_clock::now () + 40ms}; status > 0; t1 += 40ms)
{
#if DARTPUNK_SDL2
SDL_Event e;
while (SDL_PollEvent (&e))
{
switch (e.type)
{
#if DARTPUNK_BOARD_VIRTUAL
case SDL_MOUSEBUTTONUP:
board.click (e.button);
break;
case SDL_MOUSEMOTION:
board.motion (e.motion);
break;
#endif
#if DARTPUNK_INPUT_VIRTUAL
case SDL_KEYDOWN:
{
TimePoint t {milliseconds {e.key.timestamp}};
if (! e.key.repeat)
{
switch (e.key.keysym.sym)
{
case SDLK_1: b1 = t; break;
case SDLK_2: b2 = t; break;
case SDLK_SPACE: b5 = t; break;
}
}
break;
}
case SDL_KEYUP:
{
TimePoint t {milliseconds {e.key.timestamp}};
switch (e.key.keysym.sym)
{
case SDLK_ESCAPE:
exit (DARTPUNK_QUIT);
break;
case SDLK_BACKSPACE:
cancel ();
break;
case DARTPUNK_BUTTON_1: button1 (b1 [t]); break;
case DARTPUNK_BUTTON_2: button2 (b2 [t]); break;
case DARTPUNK_BUTTON_3: button3 (/****/); break;
case DARTPUNK_BUTTON_4: button4 (/****/); break;
case DARTPUNK_BUTTON_5: button5 (b5 [t]); break;
}
break;
}
#endif
}
}
#endif
#if DARTPUNK_EVDEV
for (input::EventPtr e = input.poll (); e; e = input.poll ())
{
switch (e->type)
{
case EV_KEY:
{
auto t = e->time_point ();
#if 0
cout << libevdev_event_type_get_name (e->type) << " : "
<< libevdev_event_code_get_name (e->type, e->code) << " : "
<< e->value << endl;
#endif
switch (e->value)
{
// Down.
case 1:
switch (e->code)
{
case DARTPUNK_BUTTON_1: b1 = t; break;
case DARTPUNK_BUTTON_2: b2 = t; break;
case DARTPUNK_BUTTON_5: b5 = t; break;
}
break;
// Repeat.
case 2:
break;
// Up.
case 0:
{
//cout << "Button: " << (uint16_t) DARTPUNK_BUTTON (e) << endl;
switch (e->code)
{
#if DARTPUNK_INPUT_KEYBOARD
case KEY_ESC:
exit (DARTPUNK_QUIT);
break;
case KEY_BACKSPACE:
cancel ();
break;
#endif
case DARTPUNK_BUTTON_1: button1 (b1 [t]); break;
case DARTPUNK_BUTTON_2: button2 (b2 [t]); break;
case DARTPUNK_BUTTON_3: button3 (/****/); break;
case DARTPUNK_BUTTON_4: button4 (/****/); break;
case DARTPUNK_BUTTON_5: button5 (b5 [t]); break;
}
break;
}
}
}
}
}
#endif
for (board::EventPtr e = board.poll (); e; e = board.poll ())
{
switch (e->type)
{
case board::EventType::CONNECTION: board_connection (e); break;
case board::EventType::DART: board_dart (e); break;
case board::EventType::BUTTON: board_button (e); break;
}
}
memset (&(matrix[0][0]), 0, 4 * WIDTH * HEIGHT);
switch (state)
{
case State::BLUETOOTH :
bluetooth.render (this);
break;
case State::SETTINGS :
settings.render (this);
break;
case State::GAME:
game->render (this);
break;
}
static Video v1 {"dawson.png", 64};
static Video v2 {"reset.png", 16, 2};
#if DARTPUNK_INPUT_VIRTUAL
TimePoint t {milliseconds {SDL_GetTicks ()}};
#else
TimePoint t {input::now ()};
#endif
v1.render (this, b1 (t));
v2.render (this, b2 (t));
display (matrix);
this_thread::sleep_until (t1);
}
return status;
}
const Color App::COLORS [] =
{
{0xEE, 0x00, 0x00, 0xFF}, // R // 157 0 0
{0x00, 0x00, 0xFF, 0xFF}, // B // 0 0 255
{0xCC, 0xCC, 0x00, 0xFF}, // Y // 91 91 0
{0x00, 0xDD, 0x00, 0xFF} // G // 0 112 0
};
void App::draw (const Rect & r, const Color & c, const Blender & b)
{
for (int y = r.y; y < r.y + r.h; ++y)
for (int x = r.x; x < r.x + r.w; ++x)
matrix [y][x] = b (c, matrix [y][x]);
}
void App::draw (const string & text, const Point & p, uint8_t font, const Blender & b)
{
if (font < fonts.size ())
fonts [font].draw (this, text, p, b);
}
void App::draw (const string & text, const Rect & r, const Point & p, uint8_t font, const Blender & b)
{
draw (text, {r.x + p.x, r.y + p.y}, font, b);
}
void App::draw (const string & text, const Rect & r, const Point & p, uint8_t font, const Color & c, bool selected)
{
if (selected)
{
draw (r, c);
draw (text, r, p, font);
}
else
{
Blender b = blend::ModAlpha (c);
draw (text, r, p, font, b);
}
}
void App::draw (const Image & image, const Rect & src, const Point & dst, const Blender & b)
{
for (int y = 0, sy = src.y, dy = dst.y; y < src.h; ++y, ++sy, ++dy)
for (int x = 0, sx = src.x, dx = dst.x; x < src.w; ++x, ++sx, ++dx)
matrix [dy][dx] = b (image [sy][sx], matrix [dy][dx]);
}
}