-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgplife.c
378 lines (286 loc) · 7.03 KB
/
gplife.c
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
//SPDX-License-Identifier: GPL-2.0-or-later
/*
Copyright (C) 2007-2023 Cyril Hrubis <[email protected]>
*/
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <widgets/gp_widgets.h>
#include "board.h"
static struct board *board;
static struct view {
unsigned int x_off;
unsigned int y_off;
unsigned int zoom;
gp_widget *pixmap;
} view = {
.zoom = 10,
};
static gp_widget *w, *h;
static void view_draw(gp_pixmap *pixmap, const gp_widget_render_ctx *ctx)
{
gp_pixel bg = gp_rgb_to_pixmap_pixel(0xff, 0xff, 0xff, pixmap);
gp_pixel fg = gp_rgb_to_pixmap_pixel(0x00, 0x00, 0x00, pixmap);
gp_pixel fill = ctx->bg_color;
if (gp_widgets_color_scheme_get() == GP_WIDGET_COLOR_SCHEME_DARK)
GP_SWAP(bg, fg);
gp_fill(pixmap, fill);
if (view.x_off >= board->w)
return;
if (view.y_off >= board->h)
return;
unsigned int zoom = view.zoom;
unsigned int w = GP_MIN(board->w, gp_pixmap_w(pixmap)/zoom);
unsigned int h = GP_MIN(board->h, gp_pixmap_h(pixmap)/zoom);
unsigned int x_off = (gp_pixmap_w(pixmap) - w * zoom)/2;
unsigned int y_off = (gp_pixmap_h(pixmap) - h * zoom)/2;
unsigned int x, y;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
if (board_cell_get(board, x+view.x_off, y+view.y_off))
gp_fill_rect_xywh(pixmap, x_off + x*zoom, y_off + y*zoom, zoom, zoom, fg);
else
gp_fill_rect_xywh(pixmap, x_off + x*zoom, y_off + y*zoom, zoom, zoom, bg);
}
}
}
static int view_map(unsigned int vx, unsigned int vy, unsigned int *bx, unsigned int *by)
{
unsigned int zoom = view.zoom;
size_t pw = gp_widget_pixmap_w(view.pixmap);
size_t ph = gp_widget_pixmap_h(view.pixmap);
unsigned int w = GP_MIN(board->w, pw/zoom);
unsigned int h = GP_MIN(board->h, ph/zoom);
unsigned int x_off = (pw - w * zoom)/2;
unsigned int y_off = (ph - h * zoom)/2;
*bx = (vx-x_off)/zoom + view.x_off;
*by = (vy-y_off)/zoom + view.y_off;
if (*bx >= board->w)
return 0;
if (*by >= board->h)
return 0;
return 1;
}
#define ZOOM_MAX 100
static int view_zoom_in(unsigned int zoom)
{
if (view.zoom >= ZOOM_MAX)
return 0;
if (view.zoom + zoom > ZOOM_MAX)
view.zoom = ZOOM_MAX;
else
view.zoom += zoom;
return 1;
}
static int view_zoom_out(unsigned int zoom)
{
if (view.zoom <= 1)
return 0;
if (view.zoom - 1 < zoom)
view.zoom = 1;
else
view.zoom -= zoom;
return 1;
}
static void view_zoom(gp_event *ev)
{
if (ev->val < 0) {
if (!view_zoom_out(-ev->val))
return;
} else {
if (!view_zoom_in(ev->val))
return;
}
gp_widget_redraw(view.pixmap);
}
void view_click(gp_event *ev)
{
unsigned int bx, by;
if (!view_map(ev->st->cursor_x, ev->st->cursor_y, &bx, &by))
return;
board_cell_set(board, bx, by, !board_cell_get(board, bx, by));
gp_widget_redraw(view.pixmap);
}
static int view_input(gp_widget_event *ev)
{
switch (ev->input_ev->type) {
case GP_EV_REL:
switch (ev->input_ev->code) {
case GP_EV_REL_WHEEL:
view_zoom(ev->input_ev);
return 1;
break;
}
break;
case GP_EV_KEY:
if (ev->input_ev->val == GP_BTN_LEFT &&
ev->input_ev->code == 1) {
view_click(ev->input_ev);
}
break;
}
return 0;
}
static int pixmap_on_event(gp_widget_event *ev)
{
switch (ev->type) {
case GP_WIDGET_EVENT_REDRAW:
view_draw(gp_widget_pixmap_get(ev->self), ev->ctx);
return 1;
case GP_WIDGET_EVENT_INPUT:
return view_input(ev);
}
return 0;
}
int step(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
board_tick(board);
gp_widget_redraw(view.pixmap);
return 0;
}
uint32_t sim_tmr_callback(gp_timer *self)
{
board_tick(board);
gp_widget_redraw(view.pixmap);
return self->period;
}
static gp_timer sim_tmr = {
.period = 100,
.callback = sim_tmr_callback,
.id = "Simulation timer",
};
int run(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
gp_widgets_timer_ins(&sim_tmr);
return 0;
}
int stop(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
gp_widgets_timer_rem(&sim_tmr);
return 0;
}
int randomize(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
board_randomize(board);
gp_widget_redraw(view.pixmap);
return 0;
}
int clear(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
board_clear(board);
gp_widget_redraw(view.pixmap);
return 0;
}
int zoom_in(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
if (view_zoom_in(1))
gp_widget_redraw(view.pixmap);
return 0;
}
int zoom_out(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
if (view_zoom_out(1))
gp_widget_redraw(view.pixmap);
return 0;
}
int resize(gp_widget_event *ev)
{
struct board *new;
if (ev->type == GP_WIDGET_EVENT_NEW) {
gp_widget_int_min_set(ev->self, 1);
gp_widget_int_max_set(ev->self, BOARD_SIZE_MAX);
}
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
new = board_resize(board, gp_widget_int_val_get(w), gp_widget_int_val_get(h));
if (new)
board = new;
gp_widget_redraw(view.pixmap);
return 0;
}
int file_save(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
gp_dialog *file_save = gp_dialog_file_save_new(NULL, NULL);
if (gp_dialog_run(file_save) != GP_WIDGET_DIALOG_PATH)
return 0;
if (board_save(board, gp_dialog_file_path(file_save))) {
gp_dialog_msg_printf_run(GP_DIALOG_MSG_ERR,
"Failed to save file",
"%s", strerror(errno));
}
gp_dialog_free(file_save);
return 0;
}
int file_open(gp_widget_event *ev)
{
struct board *new;
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
gp_dialog *file_open = gp_dialog_file_open_new(NULL, NULL);
if (gp_dialog_run(file_open) != GP_WIDGET_DIALOG_PATH)
return 0;
new = board_load(gp_dialog_file_path(file_open));
if (!new) {
gp_dialog_msg_printf_run(GP_DIALOG_MSG_ERR,
"Failed to load file",
"%s", strerror(errno));
} else {
board_free(board);
board = new;
gp_widget_int_val_set(w, board->w);
gp_widget_int_val_set(h, board->h);
gp_widget_redraw(view.pixmap);
}
gp_dialog_free(file_open);
return 0;
}
gp_app_info app_info = {
.name = "gplife",
.desc = "Conway's Game of Life",
.version = "1.0",
.license = "GPL-2.0-or-later",
.url = "http://github.com/gfxprim/gplife",
.authors = (gp_app_info_author []) {
{.name = "Cyril Hrubis", .email = "[email protected]", .years = "2007-2022"},
{}
}
};
int main(int argc, char *argv[])
{
gp_htable *uids;
gp_widget *layout = gp_app_layout_load("gplife", &uids);
view.pixmap = gp_widget_by_uid(uids, "board", GP_WIDGET_PIXMAP);
if (view.pixmap) {
gp_widget_on_event_set(view.pixmap, pixmap_on_event, NULL);
gp_widget_events_unmask(view.pixmap, GP_WIDGET_EVENT_INPUT | GP_WIDGET_EVENT_REDRAW);
}
w = gp_widget_by_cuid(uids, "w", GP_WIDGET_CLASS_INT);
h = gp_widget_by_cuid(uids, "h", GP_WIDGET_CLASS_INT);
if (!w || !h) {
fprintf(stderr, "No size!\n");
return 1;
}
board = board_new(gp_widget_int_val_get(w), gp_widget_int_val_get(h));
if (!board) {
fprintf(stderr, "Malloc failed!\n");
}
gp_htable_free(uids);
gp_widgets_main_loop(layout, NULL, argc, argv);
return 0;
}