-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserenity.cpp
541 lines (463 loc) · 16.2 KB
/
serenity.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include <AK/Random.h>
#include <LibCore/System.h>
#include <LibGfx/Path.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Desktop.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Statusbar.h>
#include <LibGUI/Widget.h>
#include <LibGUI/Window.h>
#include <LibMain/Main.h>
#include <stdio.h>
#include <unistd.h>
#include "puzzles.h"
void draw_text(void *handle, int x, int y, int fonttype, int fontsize,
int align, int colour, const char *text);
void draw_rect(void *handle, int x, int y, int w, int h, int colour);
void draw_line(void *handle, int x1, int y1, int x2, int y2,
int colour);
void draw_polygon(void *handle, int *coords, int npoints,
int fillcolour, int outlinecolour);
void draw_circle(void *handle, int cx, int cy, int radius,
int fillcolour, int outlinecolour);
void draw_update(void *handle, int x, int y, int w, int h);
void clip(void *handle, int x, int y, int w, int h);
void unclip(void *handle);
void start_draw(void *handle);
void end_draw(void *handle);
void status_bar(void *handle, const char *text);
blitter *blitter_new(void *handle, int w, int h);
void blitter_free(void *handle, blitter *bl);
void blitter_save(void *handle, blitter *bl, int x, int y);
void blitter_load(void *handle, blitter *bl, int x, int y);
char *text_fallback(void *handle, const char *const *strings,
int nstrings);
void draw_thick_line(void *handle, float thickness,
float x1, float y1, float x2, float y2,
int colour);
const drawing_api dr_api = {
draw_text,
draw_rect,
draw_line,
draw_polygon,
draw_circle,
draw_update,
clip,
unclip,
start_draw,
end_draw,
status_bar,
blitter_new,
blitter_free,
blitter_save,
blitter_load,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
draw_thick_line
};
class Frontend;
struct frontend {
Frontend* frontend;
};
struct blitter {
Gfx::Bitmap* bitmap;
int x, y, w, h;
};
class Frontend : public GUI::Widget {
C_OBJECT(Frontend);
public:
void new_game() {
m_game_started = true;
midend_new_game(m_midend);
resize_game();
midend_redraw(m_midend);
}
void resize_game() {
int game_width = this->rect().width(), game_height = this->rect().height();
midend_reset_tilesize(m_midend);
midend_size(m_midend, &game_width, &game_height, true);
m_x_offset = (this->rect().width() - game_width) / 2;
m_y_offset = (this->rect().height() - game_height) / 2;
m_framebuffer = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, {game_width, game_height}).value();
VERIFY(m_framebuffer);
m_painter = make<GUI::Painter>(*m_framebuffer);
}
void set_game_params(game_params* params) {
midend_set_params(m_midend, params);
new_game();
}
void restart_game() {
midend_restart_game(m_midend);
}
void solve_game() {
midend_solve(m_midend);
}
preset_menu* get_presets() {
return m_preset_menu;
}
~Frontend() {
midend_free(m_midend);
}
void draw_rect(int x, int y, int w, int h, int colour) {
m_painter->fill_rect({x, y, w, h}, get_color(colour));
}
void activate_timer() {
if (!m_timer_enabled) {
start_timer(20);
m_timer_enabled = true;
}
}
void deactivate_timer() {
if (m_timer_enabled) {
stop_timer();
m_timer_enabled = false;
}
}
void draw_update(int x, int y, int w, int h) {
update();
}
void draw_text(int x, int y, int fonttype, int fontsize,
int align, int colour, const char *text) {
StringView text_view(text, strlen(text));
auto length = fontsize * text_view.length();
if (align & ALIGN_VCENTRE)
y -= fontsize / 2;
else if (align & ALIGN_VNORMAL)
y -= fontsize;
if (align & ALIGN_HCENTRE)
x -= length / 2;
else if (align & ALIGN_HRIGHT)
x -= length;
Gfx::IntRect rect {x, y, static_cast<int>(length), fontsize};
m_painter->draw_text(rect, text_view, Gfx::TextAlignment::Center, get_color(colour));
}
void draw_polygon(int *coords, int npoints,
int fillcolour, int outlinecolour) {
VERIFY(outlinecolour != -1);
Gfx::Path polygon;
polygon.move_to({coords[0], coords[1]});
for (int i = 2; i < npoints * 2; i += 2) {
polygon.line_to({coords[i], coords[i + 1]});
}
polygon.line_to({coords[0], coords[1]});
m_painter->fill_path(polygon, get_color(fillcolour));
m_painter->stroke_path(polygon, get_color(outlinecolour), 1);
}
void draw_circle(int cx, int cy, int radius,
int fillcolour, int outlinecolour) {
auto x = cx - radius, y = cy - radius, size = 2 * radius;
auto rect_size = floor(size / M_SQRT2), x_offset = ceil(x + (radius - rect_size / 2)), y_offset = ceil(y + (radius - rect_size / 2));
m_painter->fill_ellipse({x, y, size, size}, get_color(fillcolour));
m_painter->draw_ellipse_intersecting({x_offset, y_offset, rect_size, rect_size}, get_color(outlinecolour));
}
void draw_line(float x1, float y1, float x2, float y2,
int colour) {
m_painter->draw_line({x1, y1}, {x2, y2}, get_color(colour));
}
void draw_thick_line(float thickness,
float x1, float y1, float x2, float y2,
int colour) {
m_painter->draw_line({x1, y1}, {x2, y2}, get_color(colour), thickness);
}
void clip(int x, int y, int w, int h) {
m_painter->add_clip_rect({x, y, w, h});
}
void unclip() {
m_painter->clear_clip_rect();
}
blitter *blitter_new(int w, int h) {
auto blitter = snew(struct blitter);
blitter->w = w;
blitter->h = h;
blitter->bitmap = &Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, {w, h}).value().leak_ref();
return blitter;
}
void blitter_free(blitter *bl) {
sfree(bl);
}
void blitter_save(blitter *bl, int x, int y) {
bl->x = x;
bl->y = y;
auto w = bl->w, h = bl->h;
if (bl->x < 0) {
w += bl->x;
bl->x = 0;
}
if (bl->y < 0) {
h += bl->y;
bl->y = 0;
}
GUI::Painter painter(*bl->bitmap);
painter.clear_rect({0, 0, bl->w, bl->h}, Color::Transparent);
painter.blit({0, 0}, *m_framebuffer, {bl->x, bl->y, w, h});
}
void blitter_load(blitter *bl, int x, int y) {
if (x == BLITTER_FROMSAVED) x = bl->x;
if (y == BLITTER_FROMSAVED) y = bl->y;
auto w = bl->w, h = bl->h;
if (x < 0) {
w += x;
x = 0;
}
if (y < 0) {
h += y;
y = 0;
}
m_painter->blit({x, y}, *bl->bitmap, {0, 0, w, h});
}
void status_bar(const char *text) {
auto text_or_error = String::from_utf8({ text, strlen(text) });
if (text_or_error.is_error())
return;
m_statusbar->set_text(text_or_error.release_value());
}
bool wants_statusbar() {
return midend_wants_statusbar(m_midend);
}
void set_statusbar(NonnullRefPtr<GUI::Statusbar> statusbar) {
m_statusbar = statusbar;
}
private:
Frontend() {
m_midend = midend_new(&m_frontend, m_game, &dr_api, this);
int ncolors = 0;
auto colors = midend_colours(m_midend, &ncolors);
for (int i = 0; i < ncolors * 3; i += 3) {
m_colors.append({
static_cast<u8>(colors[i] * 255),
static_cast<u8>(colors[i + 1] * 255),
static_cast<u8>(colors[i + 2] * 255),
});
}
sfree(colors);
int id_limit;
m_preset_menu = midend_get_presets(m_midend, &id_limit);
}
virtual void paint_event(GUI::PaintEvent& event) override {
GUI::Painter painter(*this);
painter.clear_rect(this->rect(), { 204, 204, 204 });
painter.draw_scaled_bitmap({m_x_offset, m_y_offset, m_framebuffer->rect().width(), m_framebuffer->rect().height()}, *m_framebuffer, m_framebuffer->rect());
}
virtual void timer_event(Core::TimerEvent& event) override {
midend_timer(m_midend, 0.02);
update();
}
virtual void mousedown_event(GUI::MouseEvent& event) override {
int button = LEFT_BUTTON;
if (event.button() == GUI::MouseButton::Middle) {
button = MIDDLE_BUTTON;
}
else if (event.button() == GUI::MouseButton::Secondary) {
button = RIGHT_BUTTON;
}
if (!midend_process_key(m_midend, event.position().x() - m_x_offset, event.position().y() - m_y_offset, button)) GUI::Application::the()->quit();
}
virtual void mouseup_event(GUI::MouseEvent& event) override {
int button = LEFT_RELEASE;
if (event.button() == GUI::MouseButton::Middle) {
button = MIDDLE_RELEASE;
}
else if (event.button() == GUI::MouseButton::Secondary) {
button = RIGHT_RELEASE;
}
if (!midend_process_key(m_midend, event.position().x() - m_x_offset, event.position().y() - m_y_offset, button)) GUI::Application::the()->quit();
}
virtual void mousemove_event(GUI::MouseEvent& event) override {
int button = LEFT_DRAG;
if (event.button() == GUI::MouseButton::Middle) {
button = MIDDLE_DRAG;
}
else if (event.button() == GUI::MouseButton::Secondary) {
button = RIGHT_DRAG;
}
if (!midend_process_key(m_midend, event.position().x() - m_x_offset, event.position().y() - m_y_offset, button)) GUI::Application::the()->quit();
}
virtual void keydown_event(GUI::KeyEvent& event) override {
int button;
switch (event.key()) {
case Key_Up:
button = CURSOR_UP;
break;
case Key_Down:
button = CURSOR_DOWN;
break;
case Key_Left:
button = CURSOR_LEFT;
break;
case Key_Right:
button = CURSOR_RIGHT;
break;
default:
button = event.code_point();
break;
}
if (event.ctrl() && event.key() == Key_Z) button = UI_UNDO;
if (event.ctrl() && event.key() == Key_Y) button = UI_REDO;
if (event.ctrl() && event.key() == Key_N) button = UI_NEWGAME;
if (!midend_process_key(m_midend, 0, 0, button)) GUI::Application::the()->quit();
}
virtual void resize_event(GUI::ResizeEvent& event) override {
if (!m_game_started) return;
resize_game();
midend_force_redraw(m_midend);
}
Gfx::Color get_color(int n) {
if (n == -1) return Gfx::Color::Transparent;
return m_colors[n];
}
frontend m_frontend { this };
midend* m_midend { nullptr };
const game* m_game { &thegame };
preset_menu* m_preset_menu { nullptr };
int m_x_offset { 0 }, m_y_offset { 0 };
Vector<Gfx::Color> m_colors {};
RefPtr<Gfx::Bitmap> m_framebuffer { nullptr };
OwnPtr<GUI::Painter> m_painter { nullptr };
bool m_timer_enabled { false };
RefPtr<GUI::Statusbar> m_statusbar { nullptr };
bool m_game_started { false };
};
void draw_text(void *handle, int x, int y, int fonttype, int fontsize,
int align, int colour, const char *text) {
((Frontend*)handle)->draw_text(x, y, fonttype, fontsize, align, colour, text);
}
void draw_rect(void *handle, int x, int y, int w, int h, int colour) {
((Frontend*)handle)->draw_rect(x, y, w, h, colour);
}
void draw_line(void *handle, int x1, int y1, int x2, int y2,
int colour) {
((Frontend*)handle)->draw_line(x1, y1, x2, y2, colour);
}
void draw_polygon(void *handle, int *coords, int npoints,
int fillcolour, int outlinecolour) {
((Frontend*)handle)->draw_polygon(coords, npoints, fillcolour, outlinecolour);
}
void draw_circle(void *handle, int cx, int cy, int radius,
int fillcolour, int outlinecolour) {
((Frontend*)handle)->draw_circle(cx, cy, radius, fillcolour, outlinecolour);
}
void draw_update(void *handle, int x, int y, int w, int h) {
((Frontend*)handle)->draw_update(x, y, w, h);
}
void clip(void *handle, int x, int y, int w, int h) {
((Frontend*)handle)->clip(x, y, w, h);
}
void unclip(void *handle) {
((Frontend*)handle)->unclip();
}
void start_draw(void *handle) {}
void end_draw(void *handle) {}
void status_bar(void *handle, const char *text) {
((Frontend*)handle)->status_bar(text);
}
blitter *blitter_new(void *handle, int w, int h) {
return ((Frontend*)handle)->blitter_new(w, h);
}
void blitter_free(void *handle, blitter *bl) {
((Frontend*)handle)->blitter_free(bl);
}
void blitter_save(void *handle, blitter *bl, int x, int y) {
((Frontend*)handle)->blitter_save(bl, x, y);
}
void blitter_load(void *handle, blitter *bl, int x, int y) {
((Frontend*)handle)->blitter_load(bl, x, y);
}
void draw_thick_line(void *handle, float thickness,
float x1, float y1, float x2, float y2,
int colour) {
((Frontend*)handle)->draw_thick_line(thickness, x1, y1, x2, y2, colour);
}
void fatal(const char *fmt, ...) {
va_list var_list;
va_start(var_list, fmt);
vprintf(fmt, var_list);
va_end(var_list);
exit(1);
}
void frontend_default_colour(frontend *fe, float *output) {
output[0] = output[1] = output[2] = 0.80;
}
void deactivate_timer(frontend *fe) {
fe->frontend->deactivate_timer();
}
void activate_timer(frontend *fe) {
fe->frontend->activate_timer();
}
void get_random_seed(void **randseed, int *randseedsize) {
auto seed = snew(int);
*seed = get_random<int>();
*randseed = seed;
*randseedsize = sizeof(int);
}
struct param_ref {
NonnullRefPtr<GUI::Action> action;
game_params* params;
};
Vector<struct param_ref> g_params;
void create_preset_menu(NonnullRefPtr<GUI::Menu> menu, Frontend& frontend, preset_menu* presets, const int id = 0) {
for (int i = 0; i < presets->n_entries; i++) {
auto* preset = &presets->entries[i];
if (preset->params) {
auto action = GUI::Action::create(preset->title, [&](auto& action) {
for (auto ref : g_params)
if (ref.action == &action) {
frontend.set_game_params(ref.params);
break;
}
});
g_params.append({action, preset->params});
menu->add_action(action);
}
else {
auto title = MUST(String::from_utf8({ preset->title, strlen(preset->title) }));
auto submenu = menu->add_submenu(move(title));
create_preset_menu(submenu, frontend, preset->submenu);
}
}
}
ErrorOr<int> serenity_main(Main::Arguments arguments) {
TRY(Core::System::pledge("stdio rpath accept wpath cpath recvfd sendfd unix fattr"));
auto app = TRY(GUI::Application::create(arguments));
auto window = GUI::Window::construct();
window->set_title(thegame.name);
window->set_resizable(true);
window->resize(400, 400);
auto frontend = window->set_main_widget<Frontend>();
frontend->set_layout<GUI::VerticalBoxLayout>();
if (frontend->wants_statusbar()) {
auto& statusbar = frontend->add<GUI::Statusbar>();
frontend->set_statusbar(statusbar);
}
frontend->new_game();
auto game_menu = window->add_menu("&Game"_string);
game_menu->add_action(GUI::Action::create("&New Game", [&](auto&) {
frontend->new_game();
}));
game_menu->add_action(GUI::Action::create("&Restart Game", [&](auto&) {
frontend->restart_game();
}));
game_menu->add_action(GUI::Action::create("&Solve Game", [&](auto&) {
frontend->solve_game();
}));
game_menu->add_action(GUI::Action::create("&Quit Game", [&](auto&) {
GUI::Application::the()->quit();
}));
auto presets = frontend->get_presets();
if (presets) {
auto presets_menu = window->add_menu("&Type"_string);
create_preset_menu(presets_menu, frontend, presets);
}
window->show();
return app->exec();
}