-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
236 lines (199 loc) · 5.74 KB
/
main.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
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_TRUETYPE_IMPLEMENTATION
#include <rmkit.h>
#include <cstdio>
#include <tuple>
#include "debug.hpp"
#include "puzzles.hpp"
#include "ui/canvas.hpp"
#include "ui/puzzle_drawer.hpp"
#include <sys/time.h>
const int TIMER_INTERVAL_MS = 100;
const double TIMER_INTERVAL_S = (double)TIMER_INTERVAL_MS / 1000;
double timer_now() {
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_sec + (now.tv_usec * 0.000001);
}
class Frontend : public frontend
{
public:
ui::Text * status;
double timer_prev = 0.0;
bool timer_active = false;
Frontend(ui::Text * status)
: frontend(), status(status)
{
}
~Frontend() {}
void trigger_timer()
{
double now = timer_now();
if ((now - timer_prev) > TIMER_INTERVAL_S) {
midend_timer(me, (float)(now - timer_prev));
timer_prev = now;
}
}
void frontend_default_colour(float *output)
{
output[0] = output[1] = output[2] = 1.f;
}
void activate_timer()
{
timer_prev = timer_now();
timer_active = true;
}
void deactivate_timer()
{
timer_active = false;
}
void status_bar(const char *text)
{
// TODO: this doesn't actually clear all the existing text
status->undraw();
status->text = text;
status->dirty = 1;
}
};
// TODO: use the full game list
extern const game lightup;
class OutlineButton : public ui::Button {
public:
using ui::Button::Button;
void before_render()
{
// center vertically
y_padding = (h - textWidget->font_size) / 2;
ui::Button::before_render();
}
void render()
{
ui::Button::render();
fb->draw_rect(x, y, w, h, BLACK, false);
}
};
class App {
public:
Canvas * canvas;
PuzzleDrawer * drawer;
ui::Text * status;
Frontend * fe;
int timer_start = 0;
App()
{
auto fb = framebuffer::get();
fb->clear_screen();
fb->waveform_mode = WAVEFORM_MODE_INIT;
fb->redraw_screen(true);
int w, h;
std::tie(w, h) = fb->get_display_size();
auto scene = ui::make_scene();
ui::MainLoop::set_scene(scene);
// ----- Layout -----
int tb_h = 100;
ui::Text::DEFAULT_FS = 30;
auto v0 = ui::VerticalLayout(0, 0, w, h, scene);
auto toolbar = ui::HorizontalLayout(0, 0, w, tb_h, scene);
v0.pack_start(toolbar);
// Status bar
status = new ui::Text(10, 0, fb->width - 20, 50, "");
status->justify = ui::Text::LEFT;
v0.pack_end(status, 10);
// Canvas
canvas = new Canvas(100, 0, w - 200, h - 400);
drawer = new PuzzleDrawer(canvas);
v0.pack_center(canvas);
// Toolbar
auto new_game = new OutlineButton(0, 0, 300, tb_h, "New Game");
toolbar.pack_start(new_game);
auto restart = new OutlineButton(0, 0, 300, tb_h, "Restart");
toolbar.pack_start(restart);
auto redo = new OutlineButton(0, 0, 100, tb_h, "=>");
toolbar.pack_end(redo);
auto undo = new OutlineButton(0, 0, 100, tb_h, "<=");
toolbar.pack_end(undo);
// ----- Events -----
new_game->mouse.click += [=](auto &ev) {
start_game();
};
restart->mouse.click += [=](auto &ev) {
restart_game();
};
undo->mouse.click += [=](auto &ev) {
midend_process_key(fe->me, 0, 0, UI_UNDO);
};
redo->mouse.click += [=](auto &ev) {
midend_process_key(fe->me, 0, 0, UI_REDO);
};
// Canvas handlers
canvas->mouse.down += [=](auto &ev) {
canvas_mouse_event(ev, LEFT_BUTTON);
};
canvas->mouse.up += [=](auto &ev) {
canvas_mouse_event(ev, LEFT_RELEASE);
};
canvas->mouse.leave += [=](auto &ev) {
canvas_mouse_event(ev, LEFT_RELEASE);
};
// Frontend
fe = new Frontend(status);
}
void canvas_mouse_event(input::SynMotionEvent &ev, int key_id)
{
int x = canvas->logical_x(ev.x);
int y = canvas->logical_y(ev.y);
debugf("KEY %d (%d,%d); screen (%d,%d)\n", key_id, x, y, ev.x, ev.y);
midend_process_key(fe->me, x, y, key_id);
}
void start_game()
{
midend_new_game(fe->me);
fe->status_bar("");
int x = canvas->w;
int y = canvas->h;
midend_size(fe->me, &x, &y, /* user_size = */ true);
debugf("midend_size(%p, %d, %d, false) => (%d, %d)\n", (void*)fe->me, canvas->w, canvas->h, x, y);
// center the canvas
canvas->translate((canvas->w - x) / 2, (canvas->h - y) / 2);
midend_redraw(fe->me);
ui::MainLoop::refresh();
ui::MainLoop::redraw();
}
void restart_game()
{
midend_restart_game(fe->me);
fe->status_bar("");
ui::MainLoop::refresh();
ui::MainLoop::redraw();
}
void run()
{
fe->init_midend(drawer, &lightup);
start_game();
while (1) {
// Check timer
if (fe->timer_active) {
fe->trigger_timer();
}
// Check win/lose
int status = midend_status(fe->me);
if (status > 0) {
fe->status_bar("You win!");
} else if (status < 0) {
fe->status_bar("You lose");
}
// Process events and redraw
ui::MainLoop::main();
ui::MainLoop::redraw();
ui::MainLoop::read_input(fe->timer_active ? TIMER_INTERVAL_MS : 0);
}
}
};
int main()
{
App app;
app.run();
return 0;
}