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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
#ifndef INCLUDE_ALL_C
#define INCLUDE_ALL_C
#include <stddef.h>
#include <stdint.h>
#include "types.c"
#include "levels.c"
#include "tick.c"
static const Color colors[N_COLORS][4] = {
{
{255, 255, 255, 255},
{255, 255, 255, 255},
{255, 255, 255, 255},
{255, 255, 255, 255},
},
{
{0, 0, 0, 255},
{0, 0, 0, 255},
{0, 0, 0, 255},
{0, 0, 0, 255},
},
{
{255, 0, 0, 255},
{255, 0, 0, 255},
{255, 0, 0, 255},
{255, 0, 0, 255},
},
{
{255, 255, 0, 255},
{255, 255, 0, 255},
{255, 255, 0, 255},
{255, 255, 0, 255},
},
{
{255, 0, 0, 255},
{255, 0, 0, 255},
{255, 255, 0, 255},
{255, 255, 0, 255},
},
{
{255, 255, 0, 255},
{255, 255, 0, 255},
{255, 0, 0, 255},
{255, 0, 0, 255},
},
{
{255, 0, 0, 255},
{255, 255, 0, 255},
{255, 0, 0, 255},
{255, 255, 0, 255},
},
{
{255, 255, 0, 255},
{255, 0, 0, 255},
{255, 255, 0, 255},
{255, 0, 0, 255},
},
{
{0, 0, 255, 255},
{0, 0, 255, 255},
{0, 0, 255, 255},
{0, 0, 255, 255},
},
{
{0, 0, 255, 255},
{0, 0, 255, 255},
{255, 255, 255, 255},
{255, 255, 255, 255},
},
{
{255, 255, 255, 255},
{255, 255, 255, 255},
{0, 0, 255, 255},
{0, 0, 255, 255},
},
{
{0, 0, 255, 255},
{255, 255, 255, 255},
{0, 0, 255, 255},
{255, 255, 255, 255},
},
{
{255, 255, 255, 255},
{0, 0, 255, 255},
{255, 255, 255, 255},
{0, 0, 255, 255},
},
};
#define BUTTON_SIZE 32
#define BUTTON_SPACING 16
#define BUTTON_SPACER(i) (BUTTON_SPACING + BUTTON_SIZE + (BUTTON_SIZE * (i) + (i) * BUTTON_SPACING))
// reverse order as they're anchored to bottom
static const Button buttons[N_BUTTONS] = {
[BUTTON_BACK] = {.x = BUTTON_SPACING, .y = BUTTON_SPACER(BUTTON_BACK), .w = BUTTON_SIZE, .h = BUTTON_SIZE},
[BUTTON_RETRY] = {.x = BUTTON_SPACING, .y = BUTTON_SPACER(BUTTON_RETRY), .w = BUTTON_SIZE, .h = BUTTON_SIZE},
[BUTTON_PLAY] = {.x = BUTTON_SPACING, .y = BUTTON_SPACER(BUTTON_PLAY), .w = BUTTON_SIZE, .h = BUTTON_SIZE},
[BUTTON_CONTINUE] = {.x = BUTTON_SPACING, .y = BUTTON_SPACER(BUTTON_CONTINUE), .w = BUTTON_SIZE, .h = BUTTON_SIZE},
};
static int getPlaceAction(int currentColor, int cellx, int celly, float cellWidth, float cellHeight) {
int hoverColor = EMPTY;
switch (currentColor) {
case BLACK:
if (cellx < cellWidth / 4 && cellx < celly && cellx < cellHeight - celly) {
hoverColor = RED_LEFT;
} else if (celly < cellHeight / 4 && celly < cellx && celly < cellWidth - cellx) {
hoverColor = RED_UP;
} else if (cellx > cellWidth / 4 * 3 && cellWidth - cellx < celly && cellWidth - cellx < cellHeight - celly) {
hoverColor = RED_RIGHT;
} else if (celly > cellHeight / 4 * 3 && cellHeight - celly < cellx && cellHeight - celly < cellWidth - cellx) {
hoverColor = RED_DOWN;
} else {
hoverColor = RED;
}
break;
case EMPTY:
hoverColor = BLACK;
break;
}
return hoverColor;
}
static DrawList *render(State *state, UI *ui, Arena *a) {
DrawList *drawList = new(a, 1, DrawList);
float cellWidth = (float) (ui->width - GRID_OFFSET_X) / GRIDWIDTH;
float cellHeight = (float) ui->height / GRIDHEIGHT;
for (int x = 0; x < GRIDWIDTH; x++) {
for (int y = 0; y < GRIDHEIGHT; y++) {
int image;
switch (state->grid[x + GRIDWIDTH * y]) {
case RED:
image = IMAGE_NELSON;
break;
case RED_LEFT:
image = IMAGE_NELSON_LEFT;
break;
case RED_UP:
image = IMAGE_NELSON_UP;
break;
case RED_RIGHT:
image = IMAGE_NELSON_RIGHT;
break;
case RED_DOWN:
image = IMAGE_NELSON_DOWN;
break;
default:
image = 0;
break;
}
if (image) {
drawList->els[drawList->len++] = (DrawElement) {
.x = cellWidth * x + GRID_OFFSET_X,
.y = cellHeight * y,
.w = cellWidth,
.h = cellHeight,
.image = image,
};
} else {
for (int subx = 0; subx < 2; subx++) {
for (int suby = 0; suby < 2; suby++) {
drawList->els[drawList->len++] = (DrawElement) {
.x = cellWidth * x + GRID_OFFSET_X + subx * cellWidth / 2,
.y = cellHeight * y + suby * cellHeight / 2,
.w = cellWidth / 2 + (1 - subx),
.h = cellHeight / 2 + (1 - suby),
.fill = colors[state->grid[x + GRIDWIDTH * y]][subx + 2 * suby],
};
}
}
}
}
}
// Vertical grid lines
for (int x = 1; x < GRIDWIDTH; x++) {
drawList->els[drawList->len++] = (DrawElement) {
.x = cellWidth * x + GRID_OFFSET_X,
.y = 0,
.w = 1,
.h = ui->height,
.fill = {0, 0, 0, 255},
};
}
// Horizontal grid lines
for (int y = 1; y < GRIDHEIGHT; y++) {
drawList->els[drawList->len++] = (DrawElement) {
.x = GRID_OFFSET_X,
.y = y * cellHeight,
.w = ui->width - GRID_OFFSET_X,
.h = 1,
.fill = {0, 0, 0, 255},
};
}
// Goal
drawList->els[drawList->len++] = (DrawElement) {
.x = cellWidth * state->goalx + GRID_OFFSET_X,
.y = cellHeight * state->goaly,
.w = cellWidth,
.h = cellHeight,
.fill = {255, 0, 0, 63},
.border = {255, 0, 0, 255},
};
// Hover
if (ui->mousex >= GRID_OFFSET_X) {
int hoverx = (ui->mousex - GRID_OFFSET_X) * GRIDWIDTH / (ui->width - GRID_OFFSET_X);
int hovery = ui->mousey * GRIDHEIGHT / ui->height;
int cellx = ui->mousex - GRID_OFFSET_X - hoverx * cellWidth;
int celly = ui->mousey - hovery * cellHeight;
int hoverColor = getPlaceAction(
state->grid[hoverx + hovery * GRIDWIDTH],
cellx,
celly,
cellWidth,
cellHeight
);
if (hoverColor != EMPTY) {
int subCellWidth = cellWidth / 2;
int subCellHeight = cellHeight / 2;
for (int subx = 0; subx < 2; subx++) {
for (int suby = 0; suby < 2; suby++) {
Color fill = colors[hoverColor][subx + 2 * suby];
fill.a = 127;
drawList->els[drawList->len++] = (DrawElement) {
.x = cellWidth * hoverx + GRID_OFFSET_X + (subx * subCellWidth),
.y = cellHeight * hovery + (suby * subCellHeight),
.w = subCellWidth,
.h = subCellHeight,
.fill = fill,
};
}
}
}
}
// Side panel
drawList->els[drawList->len++] = (DrawElement) {
.x = 0,
.y = 0,
.w = GRID_OFFSET_X,
.h = ui->height,
.fill = {100, 100, 100, 255},
.border = {0, 0, 0, 255},
};
// render buttons
for (int i = 0; i < N_BUTTONS; i++) {
Color colour = {0, 0, 0, 255};
switch (state->buttonStates[i]) {
case BUTTON_STATE_IDLE:
colour = (Color) {0, 0, 0, 255};
break;
case BUTTON_STATE_HOVERED:
colour = (Color) {255, 255, 0, 255};
break;
case BUTTON_STATE_PRESSED:
colour = (Color) {255, 0, 0, 255};
break;
}
int image = IMAGE_NULL;
switch (i) {
case BUTTON_CONTINUE:
image = IMAGE_CONTINUE;
break;
case BUTTON_BACK:
image = IMAGE_EXIT;
break;
case BUTTON_RETRY:
image = IMAGE_RETRY;
break;
case BUTTON_PLAY:
image = state->playing ? IMAGE_PAUSE : IMAGE_PLAY;
break;
}
drawList->els[drawList->len++] = (DrawElement) {
.x = buttons[i].x,
.y = ui->height - buttons[i].y,
.w = buttons[i].w,
.h = buttons[i].h,
.image = image,
};
}
// render placeable cells
int cellCount = 0; // doesn't increment when ignoring empty cells
for (int i = 0; i < MAX_PLACEABLE_CELLS; i++) {
if (levels[state->currentLevel].placeableCells[i] == EMPTY)
continue;
// TODO - FIXME (how to tell how many cells we have left of a colour? Maybe placedCells is a bad idea!)
int canContinue = 1;
for (int j = 0; j < MAX_PLACEABLE_CELLS; j++) {
if (levels[state->currentLevel].placeableCells[i] == state->placedCells[j]) {
canContinue = 0;
break;
}
}
if (canContinue == 0)
continue;
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
drawList->els[drawList->len++] = (DrawElement) {
.x = BUTTON_SIZE / 2 + x * (BUTTON_SIZE / 2),
.y = (BUTTON_SIZE / 2 + y * (BUTTON_SIZE / 2) + BUTTON_SPACING * (cellCount % 2 + 1)) * (cellCount + 1), // TODO - padding
.w = BUTTON_SIZE / 2,
.h = BUTTON_SIZE / 2,
.fill = colors[cellCount + 1][x + 2 * y],
.border = {0, 0, 0, 0},
};
}
}
cellCount++;
}
return drawList;
}
static void restart_level(Game* game) {
const int level = game->state.currentLevel;
xmemcpy(&game->state.grid, &levels[level].grid, sizeof(game->state.grid));
game->state.goalx = levels[level].goalx;
game->state.goaly = levels[level].goaly;
game->state.playing = 0;
}
static void update(Game *game, uint64_t now, Arena a) {
// TODO - offset some more because of right black bar, dependent on ui size and window size?
const int offset_width = game->ui.width - GRID_OFFSET_X;
switch (game->input) {
int x, y;
case INPUT_CLICK:
x = (game->ui.mousex - GRID_OFFSET_X) * GRIDWIDTH / offset_width;
y = game->ui.mousey * GRIDHEIGHT / game->ui.height;
if (game->ui.mousex >= GRID_OFFSET_X) {
float cellWidth = (float) (game->ui.width - GRID_OFFSET_X) / GRIDWIDTH;
float cellHeight = (float) game->ui.height / GRIDHEIGHT;
int cellx = game->ui.mousex - GRID_OFFSET_X - x * cellWidth;
int celly = game->ui.mousey - y * cellHeight;
// Keeping this around for testing purposes
// game->state.grid[x + y * GRIDWIDTH] = (game->state.grid[x + y * GRIDWIDTH] + 1) % (sizeof(colors) / sizeof(colors[0]));
game->state.grid[x + y * GRIDWIDTH] = getPlaceAction(
game->state.grid[x + y * GRIDWIDTH],
cellx,
celly,
cellWidth,
cellHeight
);
}
// TODO - some ignore list for which cells we have left to place
game->state.placedCells[0] = BLACK;
for (int i = 0; i < N_BUTTONS; i++) {
if (
game->ui.mousex > buttons[i].x && game->ui.mousex < buttons[i].x + buttons[i].w &&
game->ui.mousey > game->ui.height - buttons[i].y &&
game->ui.mousey < game->ui.height - buttons[i].y + buttons[i].h
) {
// TODO - CLICK THINGS
//game->state.buttonStates[i] = BUTTON_STATE_PRESSED;
switch (i) {
case BUTTON_RETRY:
restart_level(game);
break;
case BUTTON_CONTINUE:
// TODO - don't allow if level has not been completed
if (game->state.currentLevel + 1 >= 2) // TODO - get size of levels array
break;
game->state.currentLevel++;
restart_level(game);
break;
case BUTTON_BACK:
break;
case BUTTON_PLAY:
game->state.playing = !game->state.playing;
break;
}
}
}
break;
case INPUT_RCLICK:
x = (game->ui.mousex - GRID_OFFSET_X) * GRIDWIDTH / (game->ui.width - GRID_OFFSET_X);
y = game->ui.mousey * GRIDHEIGHT / game->ui.height;
game->state.grid[x + y * GRIDWIDTH] = EMPTY;
break;
case INPUT_PAUSE_PLAY:
game->state.playing = !game->state.playing;
break;
case INPUT_RESTART:
restart_level(game);
break;
default:
break;
}
if (game->state.playing && game->state.lastTick + TICK_LENGTH <= now) {
game->state.lastTick = now;
tick(game, a);
}
}
#if SDL
#include <SDL3/SDL.h>
typedef struct {
int width, height;
Color colors[16];
const char *pixels;
} Image;
#include "../build/images.c"
SDL_Texture *textures[sizeof(images) / sizeof(images[0])];
#include "../build/music.c"
SDL_AudioStream *stream;
int main(int argc, char **argv) {
(void) argc;
(void) argv;
char *mem = SDL_malloc(MEM_SIZE);
Arena a = {
.start = mem,
.end = mem + MEM_SIZE,
};
Game *game = new(&a, 1, Game);
game->state.currentLevel = 0;
restart_level(game);
game->ui = (UI) {
.width = 640,
.height = 480,
.mousex = 0,
.mousey = 0,
};
for (int i = 0; i < N_BUTTONS; i++) {
game->state.buttonStates[i] = BUTTON_STATE_IDLE;
}
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
SDL_Window *w = SDL_CreateWindow(
"LDJam 57",
game->ui.width,
game->ui.height,
SDL_WINDOW_RESIZABLE
);
SDL_PropertiesID renderProps = SDL_CreateProperties();
SDL_SetPointerProperty(renderProps, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, w);
SDL_SetNumberProperty(renderProps, SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER, 1);
SDL_Renderer *r = SDL_CreateRendererWithProperties(renderProps);
SDL_DestroyProperties(renderProps);
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_BLEND);
for (int j = 1; j < (int) (sizeof(images) / sizeof(images[0])); j++) {
textures[j] = SDL_CreateTexture(
r,
SDL_PIXELFORMAT_ABGR8888,
SDL_TEXTUREACCESS_STATIC,
images[j].width,
images[j].height
);
SDL_SetTextureScaleMode(textures[j], SDL_SCALEMODE_NEAREST);
Arena scratch = a;
Color *pixels = new(&scratch, images[j].width * images[j].height, Color);
for (int i = 0; i < images[j].width * images[j].height; i++) {
pixels[i] = images[j].colors[(int) images[j].pixels[i]];
}
SDL_UpdateTexture(textures[j], NULL, pixels, images[j].width * 4);
}
SDL_AudioSpec audioSpec = {
.format = SDL_AUDIO_S16LE,
.channels = 2,
.freq = 48000,
};
stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audioSpec, NULL, NULL);
SDL_PutAudioStreamData(stream, musicBytes, sizeof(musicBytes));
SDL_ResumeAudioStreamDevice(stream);
for (;;) {
uint64_t now = SDL_GetTicks();
game->input = INPUT_NONE;
SDL_Event e = {0};
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_EVENT_QUIT:
return 0;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
game->ui.mousex = (int) e.button.x;
game->ui.mousey = (int) e.button.y;
if (e.button.button == 1) {
game->input = INPUT_CLICK;
} else if (e.button.button == 3) {
game->input = INPUT_RCLICK;
}
break;
case SDL_EVENT_MOUSE_MOTION:
game->ui.mousex = (int) e.motion.x;
game->ui.mousey = (int) e.motion.y;
game->input = INPUT_MOVE;
break;
case SDL_EVENT_KEY_DOWN:
switch (e.key.key) {
case SDLK_SPACE:
game->input = INPUT_PAUSE_PLAY;
break;
case SDLK_R:
game->input = INPUT_RESTART;
break;
}
break;
case SDL_EVENT_WINDOW_RESIZED:
SDL_GetWindowSize(w, &game->ui.width, &game->ui.height);
break;
}
}
Arena scratch = a;
DrawList *drawList = render(&game->state, &game->ui, &scratch);
SDL_SetRenderDrawColor(r, 0, 0, 0, 255);
SDL_RenderFillRect(r, NULL);
for (int i = 0; i < drawList->len; i++) {
SDL_FRect rect = {
.x = (float) drawList->els[i].x,
.y = (float) drawList->els[i].y,
.w = (float) drawList->els[i].w,
.h = (float) drawList->els[i].h
};
SDL_SetRenderDrawColor(
r,
drawList->els[i].fill.r,
drawList->els[i].fill.g,
drawList->els[i].fill.b,
drawList->els[i].fill.a
);
SDL_RenderFillRect(r, &rect);
SDL_SetRenderDrawColor(
r,
drawList->els[i].border.r,
drawList->els[i].border.g,
drawList->els[i].border.b,
drawList->els[i].border.a
);
SDL_RenderRect(r, &rect);
if (drawList->els[i].image != 0) {
SDL_RenderTexture(r, textures[drawList->els[i].image], NULL, &rect);
}
}
update(game, now, a);
if (SDL_GetAudioStreamQueued(stream) < (int) sizeof(musicBytes) / 8) {
SDL_PutAudioStreamData(stream, musicBytes, sizeof(musicBytes));
}
SDL_RenderPresent(r);
}
return 0;
}
#elif WASM
static Game *game;
static Arena perm;
__attribute((export_name("game_init")))
void game_init(void) {
static char heap[MEM_SIZE];
perm.start = heap;
perm.end = heap + MEM_SIZE;
game = new(&perm, 1, Game);
xmemcpy(&game->state.grid, &levels[0].grid, sizeof(game->state.grid));
game->state.goalx = levels[0].goalx;
game->state.goaly = levels[0].goaly;
game->state.playing = 0;
}
__attribute((export_name("game_render")))
DrawList *game_render(int width, int height, int mousex, int mousey) {
game->ui.width = width;
game->ui.height = height;
game->ui.mousex = mousex;
game->ui.mousey = mousey;
Arena scratch = perm;
return render(&game->state, &game->ui, &scratch);
}
__attribute((export_name("game_update")))
void game_update(int input, int mousex, int mousey, int now) {
game->input = input;
game->ui.mousex = mousex;
game->ui.mousey = mousey;
update(game, now, perm);
}
#endif
#endif
|