-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemscripten.c
490 lines (421 loc) · 8.98 KB
/
emscripten.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
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
#include <string.h>
#include <emscripten.h>
#include "puzzles.h"
extern const game *gamelist[];
extern const int gamecount;
extern frontend *fe;
/* Defined in JS */
int get_width(void);
int get_height(void);
void init_colour(int,int,int,int);
void canvas_text(int,int,int,int,char*,char*,int,char*);
void canvas_rect(int,int,int,int,int);
void set_canvas_size(int,int);
void canvas_circle(int,int,int,int,int);
void canvas_blitter_save(int,int,int,int,int);
void canvas_blitter_load(int,int,int);
void canvas_blitter_free(int);
void canvas_clip(int,int,int,int);
void canvas_unclip(void);
void canvas_beginPath(void);
void canvas_moveTo(int,int);
void canvas_lineTo(int,int,float);
void canvas_strokeandfill(int,int);
int is_selected_game(const char*);
void save_state_reset(void);
void save_state_add(char *str);
void save_state_save(void);
void load_state(void);
int load_state_read(void *ctx, void *buf, int len);
void set_undo_redo(int, int);
void set_status_bar(char *);
struct frontend
{
midend *me;
const game *game;
};
void save_state_callback(void *ctx, void *buf, int len)
{
save_state_add(buf);
}
void save_state(void)
{
save_state_reset();
midend_serialise(fe->me, save_state_callback, NULL);
save_state_save();
set_undo_redo(midend_can_undo(fe->me), midend_can_redo(fe->me));
}
static void em_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
int align, int colour, char *text)
{
char *alignstr = "start";
char *valignstr = "alphabetic";
switch(align) {
case ALIGN_VCENTRE | ALIGN_HCENTRE:
alignstr = "center";
valignstr = "middle";
break;
case ALIGN_VCENTRE:
alignstr = "left";
valignstr = "middle";
break;
default:
printf("Unknown text alignment (%d)\n", align);
}
canvas_text(x, y, fonttype == FONT_FIXED, fontsize,
alignstr, valignstr, colour, text);
}
static void em_draw_rect(void *handle, int x, int y, int w, int h, int colour)
{
canvas_rect(x, y, w, h, colour);
}
static void em_draw_line(void *hande, int x1, int y1, int x2, int y2,
int colour)
{
canvas_beginPath();
canvas_moveTo(x1, y1);
canvas_lineTo(x2, y2, 1);
canvas_strokeandfill(-1, colour);
}
static void em_draw_polygon(void *handle, int *coords, int npoints,
int fillcolour, int outlinecolour)
{
int i;
canvas_beginPath();
canvas_moveTo(coords[0], coords[1]);
for(i=1;i<npoints;i++) {
canvas_lineTo(coords[i*2], coords[i*2+1], 1);
}
canvas_strokeandfill(fillcolour, outlinecolour);
}
static void em_draw_circle(void *handle, int cx, int cy, int radius,
int fillcolour, int outlinecolour)
{
canvas_circle(cx, cy, radius, fillcolour, outlinecolour);
}
static void em_draw_update(void *handle, int x, int y, int w, int h)
{
}
static void em_clip(void *handle, int x, int y, int w, int h)
{
canvas_clip(x, y, w, h);
}
static void em_unclip(void *handle)
{
canvas_unclip();
}
static void em_start_draw(void *handle)
{
}
static void em_end_draw(void *handle)
{
}
static void em_status_bar(void *handle, char *text)
{
set_status_bar(text);
}
struct blitter
{
int id;
int x, y, w, h;
};
static blitter *em_blitter_new(void *handle, int w, int h)
{
struct blitter *b = snew(blitter);
b->w = w;
b->h = h;
b->id = (int)b;
return b;
}
static void em_blitter_free(void *handle, blitter *bl)
{
canvas_blitter_free(bl->id);
sfree(bl);
}
static void em_blitter_save(void *handle, blitter *bl, int x, int y)
{
canvas_blitter_save(bl->id, x, y, bl->w, bl->h);
bl->x = x;
bl->y = y;
}
static void em_blitter_load(void *handle, blitter *bl, int x, int y)
{
if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) {
x = bl->x; y = bl->y;
}
canvas_blitter_load(bl->id, x, y);
}
static void em_begin_doc(void *handle, int pages)
{
printf("TODO: begin doc\n");
}
static void em_begin_page(void *handle, int number)
{
printf("TODO: begin page\n");
}
static void em_begin_puzzle(void *handle, float xm, float xc,
float ym, float yc, int pw, int ph, float wmm)
{
printf("TODO: begin puzzle\n");
}
static void em_end_puzzle(void *handle)
{
printf("TODO: end puzzle\n");
}
static void em_end_page(void *handle, int number)
{
printf("TODO: end page\n");
}
static void em_end_doc(void *handle)
{
printf("TODO: end doc\n");
}
static void em_line_width(void *handle, float width)
{
printf("TODO: line width\n");
}
static void em_line_dotted(void *handle, int dotted)
{
printf("TODO: line dotted\n");
}
static char *em_text_fallback(void *handle, const char *const *strings,
int nstrings)
{
printf("TODO: text_fallback\n");
return dupstr(strings[0]);
}
static void em_draw_thick_line(void *handle, float thickness,
float x1, float y1, float x2, float y2,
int colour)
{
canvas_beginPath();
canvas_moveTo(x1, y1);
canvas_lineTo(x2, y2, thickness);
canvas_strokeandfill(-1, colour);
}
static struct drawing_api drapi = {
em_draw_text,
em_draw_rect,
em_draw_line,
em_draw_polygon,
em_draw_circle,
em_draw_update,
em_clip,
em_unclip,
em_start_draw,
em_end_draw,
em_status_bar,
em_blitter_new,
em_blitter_free,
em_blitter_save,
em_blitter_load,
em_begin_doc,
em_begin_page,
em_begin_puzzle,
em_end_puzzle,
em_end_page,
em_end_doc,
em_line_width,
em_line_dotted,
em_text_fallback,
em_draw_thick_line,
};
static const game *pick_game(void)
{
int i;
for(i=0;i<gamecount;i++) {
if (is_selected_game(gamelist[i]->name)) {
return gamelist[i];
}
}
return gamelist[0];
}
static const game *find_game(char *name)
{
int i;
for(i=0;i<gamecount;i++) {
if (!strcmp(name, gamelist[i]->name)) {
return gamelist[i];
}
}
return NULL;
}
/*
static void pick_preset(void)
{
int num = midend_num_presets(fe->me);
int i;
for(i=0;i<num;i++) {
char *name;
game_params *params;
midend_fetch_preset(fe->me, i, &name, ¶ms);
if (!strcmp(name, "15x15 medium")) {
midend_set_params(fe->me, params);
}
}
}
*/
void c_callback(const char *name,int id,int sel);
void populate_game_menu()
{
int i;
for(i=0;i<gamecount;i++) {
c_callback(gamelist[i]->name, i, 0);
}
}
void populate_type_menu()
{
int num = midend_num_presets(fe->me);
int i;
int sel = midend_which_preset(fe->me);
for(i=0;i<num;i++) {
char *name;
game_params *params;
midend_fetch_preset(fe->me, i, &name, ¶ms);
c_callback(name, i, i==sel);
}
}
void em_new_game(int start_new)
{
int x, y;
if (start_new) {
midend_new_game(fe->me);
if (midend_wants_statusbar(fe->me)) {
set_status_bar(".");
} else {
set_status_bar(NULL);
}
}
x = get_width();
y = get_height();
midend_size(fe->me, &x, &y, 1);
set_canvas_size(x,y);
midend_redraw(fe->me);
save_state();
}
void set_preset(int i)
{
char *name;
game_params *params;
midend_fetch_preset(fe->me, i, &name, ¶ms);
midend_set_params(fe->me, params);
em_new_game(1);
}
void frontend_default_colour(frontend *fe, float *output)
{
output[0] = 0.8f;
output[1] = 0.8f;
output[2] = 0.8f;
}
void snaffle_colours(frontend *fe)
{
int ncolours, i;
float *colours;
colours = midend_colours(fe->me, &ncolours);
for(i=0; i<ncolours; i++) {
init_colour(i, colours[i*3]*0xFF,
colours[i*3+1]*0xFF,
colours[i*3+2]*0xFF);
}
}
void em_puzzle_init(void)
{
char *game;
char *err;
fe = snew(frontend);
memset(fe, 0, sizeof(*fe));
load_state();
err = identify_game(&game, load_state_read, NULL);
if (game) {
fe->game = find_game(game);
} else {
printf("%s\n", err);
}
if (!fe->game) {
fe->game = pick_game();
game = NULL;
}
fe->me = midend_new(fe, fe->game, &drapi, fe);
snaffle_colours(fe);
if (game) {
load_state();
err = midend_deserialise(fe->me, load_state_read, NULL);
if (err) {
printf("%s\n", err);
}
}
em_new_game(game==NULL);
}
void change_game(int id)
{
midend_free(fe->me);
fe->game = gamelist[id];
fe->me = midend_new(fe, fe->game, &drapi, fe);
snaffle_colours(fe);
em_new_game(1);
}
void get_random_seed(void **randseed, int *randseedsize)
{
float *r = snew(float);
*r = emscripten_random();
*randseed = r;
*randseedsize = sizeof(float);
}
static int button_drag_down = 0;
void em_mousedown(int x, int y, int which)
{
int button = 0;
static const int button_down[] = {
LEFT_BUTTON, MIDDLE_BUTTON, RIGHT_BUTTON
};
static const int button_drag[] = {
LEFT_DRAG, MIDDLE_DRAG, RIGHT_DRAG
};
if (which < 3 && which >= 0) {
button = button_down[which];
button_drag_down = button_drag[which];
} else
return;
midend_process_key(fe->me, x, y, button);
}
void em_mousemove(int x, int y)
{
if (button_drag_down) {
midend_process_key(fe->me, x, y, button_drag_down);
}
}
void em_mouseup(int x, int y, int which)
{
int button = 0;
static const int button_up[] = {
LEFT_RELEASE, MIDDLE_RELEASE, RIGHT_RELEASE
};
if (which < 3 && which >= 0) {
button = button_up[which];
} else
return;
midend_process_key(fe->me, x, y, button);
button_drag_down = 0;
save_state();
}
void do_timer(int t)
{
float tplus = t/1000.f;
midend_timer(fe->me, tplus);
}
int em_undo(void)
{
midend_process_key(fe->me, 0, 0, 'u');
save_state();
return 0;
}
int em_redo(void)
{
midend_process_key(fe->me, 0, 0, 'r');
save_state();
return 0;
}
int em_status(void)
{
return midend_status(fe->me);
}