Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/src/all.c
blob: 1dbe9a1260014805790a7499d8c69dfce6afcc77 (plain)
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
#include <stddef.h>
#include <stdint.h>

#if SDL
#include <string.h>
#define xmemcpy memcpy
#else
#define xmemcpy __builtin_memcpy
#endif

typedef struct {
	unsigned char r, g, b, a;
} Color;

typedef enum {
	BUTTON_STATE_IDLE,
	BUTTON_STATE_HOVERED,
	BUTTON_STATE_PRESSED
} ButtonState;

typedef struct {
	int idle_colour;
	int hover_colour;
	int pressed_colour;
} ButtonStyle;

typedef struct {
	int x, y, w, h;
	ButtonState state;
} Button;

typedef struct {
	int x, y, w, h;
	Color fill;
	Color border;
} DrawElement;

typedef struct {
	DrawElement bg;
} SidePanel;

typedef struct {
	int len;
	DrawElement els[512];
	SidePanel panel;
} DrawList;

#define MEM_SIZE (1<<16)
#define GRIDWIDTH 16
#define GRIDHEIGHT 16
#define TICK_LENGTH 1000
#define GRID_OFFSET_X 256

typedef struct {
	char *start;
	char *end;
} Arena;

#define new(a, c, t) ((t *) alloc(a, c, sizeof(t), _Alignof(t)))
#define affirm(c) while (!(c)) *(volatile int *)0 = 0

static void *alloc(Arena *a, ptrdiff_t count, ptrdiff_t size, ptrdiff_t align) {
	ptrdiff_t pad = -(size_t) a->start & (align - 1);
	affirm(count < (a->end - a->start - pad) / size);
	char *r = a->start + pad;
	a->start += pad + size * count;
	for (ptrdiff_t i = 0; i < count * size; i++) {
		r[i] = 0;
	}

	return r;
}

typedef struct {
	int width, height;
} UI;

typedef struct {
	uint64_t lastTick;
	char playing;
	int grid[GRIDWIDTH * GRIDHEIGHT];
} State;

// Mirror these in src/index.html.in
enum {
	INPUT_NONE,
	INPUT_CLICK,
	INPUT_PAUSE_PLAY,
};

typedef struct {
	State state;
	UI ui;
	int input;
	int mousex, mousey;
} Game;

enum {
	EMPTY,
	BLACK,
	RED,
	YELLOW,
	N_COLORS,
};

static const Color colors[N_COLORS] = {
	{255, 255, 255, 255},
	{0, 0, 0, 255},
	{255, 0, 0, 255},
	{255, 255, 0, 255}
};

static const ButtonStyle button_style = {
	.idle_colour = BLACK,
	.hover_colour = RED,
	.pressed_colour = YELLOW
};

enum {
	BUTTON_RETRY,
	BUTTON_BACK,
	N_BUTTONS,
};

static Button buttons[N_BUTTONS] = {
	{.x = 64, .y = 64, .w = 128, .h = 32, .state = BUTTON_STATE_IDLE},
	{.x = 64, .y = 114, .w = 128, .h = 32, .state = BUTTON_STATE_IDLE},
};

static DrawList *render(State *state, UI *ui, Arena *a) {
	(void) ui;

	DrawList *drawList = new(a, 1, DrawList);

	int cellWidth = (ui->width - GRID_OFFSET_X) / GRIDWIDTH;
	int cellHeight = ui->height / GRIDHEIGHT;

	for (int x = 0; x < GRIDWIDTH; x++) {
		for (int y = 0; y < GRIDHEIGHT; y++) {
			drawList->els[drawList->len++] = (DrawElement) {
				.x = cellWidth * x + GRID_OFFSET_X,
				.y = cellHeight * y,
				.w = cellWidth,
				.h = cellHeight,
				.fill = colors[state->grid[x + GRIDWIDTH * y]],
				.border = {0, 0, 0, 255},
			};
		}
	}

	drawList->panel = (SidePanel) {
		.bg = (DrawElement) {
			.x = 0,
			.y = 0,
			.w = GRID_OFFSET_X,
			.h = ui->height,
			.fill = colors[YELLOW],
			.border = {0, 0, 0, 255}
		}
	};

	return drawList;
}

static void update_buttons(const int mx, const int my, const int mstate) {
	for (int i = 0; i < N_BUTTONS; i++) {
		if (
			mx > buttons[i].x && mx < buttons[i].x + buttons[i].w &&
			my > buttons[i].y && my < buttons[i].y + buttons[i].h
			) {
			if (mstate == 1) {
				buttons[i].state = BUTTON_STATE_PRESSED;
			}
			else
				buttons[i].state = BUTTON_STATE_HOVERED;
		}
		else {
			buttons[i].state = BUTTON_STATE_IDLE;
		}
	}
}

static void update(Game *game, uint64_t now, Arena a) {
	const int offset_width = game->ui.width - GRID_OFFSET_X;
	
	switch (game->input) {
		int x, y;
		case INPUT_CLICK:
			x = game->mousex * GRIDWIDTH / offset_width;
			y = game->mousey * GRIDHEIGHT / game->ui.height;
			game->state.grid[x + y * GRIDWIDTH] = (game->state.grid[x + y * GRIDWIDTH] + 1) % (sizeof(colors) / sizeof(colors[0]));
			break;
		case INPUT_PAUSE_PLAY:
			game->state.playing = !game->state.playing;
			break;
		default:
			break;
	}

	if (game->state.playing && game->state.lastTick + TICK_LENGTH <= now) {
		game->state.lastTick = now;
		State *lastState = new(&a, 1, State);
		xmemcpy(lastState, game, sizeof(State));

		for (int x = 0; x < GRIDWIDTH; x++) {
			for (int y = 0; y < GRIDHEIGHT; y++) {
				if (
					lastState->grid[x + y * GRIDWIDTH] == BLACK && (
						(x > 0 && lastState->grid[x - 1 + y * GRIDWIDTH] == RED) ||
						(x < GRIDWIDTH - 1 && lastState->grid[x + 1 + y * GRIDWIDTH] == RED) ||
						(y > 0 && lastState->grid[x + (y - 1) * GRIDWIDTH] == RED) ||
						(y < GRIDHEIGHT - 1 && lastState->grid[x + (y + 1) * GRIDWIDTH] == RED)
					)
				) {
					game->state.grid[x + y * GRIDWIDTH] = RED;
				}

				if (lastState->grid[x + y * GRIDWIDTH] == RED) {
					game->state.grid[x + y * GRIDWIDTH] = YELLOW;
				}

				if (lastState->grid[x + y * GRIDWIDTH] == YELLOW) {
					game->state.grid[x + y * GRIDWIDTH] = BLACK;
				}
			}
		}
	}
}

#if SDL

#include <SDL3/SDL.h>

void render_button(SDL_Renderer* r, const Button* b) {
	Color colour = colors[BLACK];
	switch (b->state) {
		case BUTTON_STATE_IDLE:
			colour = colors[button_style.idle_colour];
			break;
		case BUTTON_STATE_HOVERED:
			colour = colors[button_style.hover_colour];
			break;
		case BUTTON_STATE_PRESSED:
			colour = colors[button_style.pressed_colour];
			break;
	}
	SDL_SetRenderDrawColor(r, colour.r, colour.g, colour.b, colour.a);
	SDL_FRect rect = {
		.x = (float) b->x,
		.y = (float) b->y,
		.w = (float) b->w,
		.h = (float) b->h
	};
	SDL_RenderFillRect(r, &rect);
}

void render_side_panel(SDL_Renderer* r, SidePanel* panel) {
	Color* bg_colour = &panel->bg.fill;
	SDL_FRect rect = {
		.x = (float) panel->bg.x,
		.y = (float) panel->bg.y,
		.w = (float) panel->bg.w,
		.h = (float) panel->bg.h,
	};
	SDL_SetRenderDrawColor(r, bg_colour->r, bg_colour->g, bg_colour->b, bg_colour->a);
	SDL_RenderFillRect(r, &rect);

	for (int i = 0; i < N_BUTTONS; i++) {
		render_button(r, &buttons[i]);
	}
}

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.grid[0] = 0;
	game->state.playing = 0;
	game->ui = (UI) {
		.width = 640,
		.height = 480,
	};

	SDL_Init(SDL_INIT_VIDEO);
	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);

	int mousex = 0, mousey = 0, mousestate = 0;
	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->input = INPUT_CLICK;
					game->mousex = (int) e.button.x - GRID_OFFSET_X;
					game->mousey = (int) e.button.y;
					mousestate = SDL_BUTTON_MASK(e.motion.state);
					break;
				case SDL_EVENT_MOUSE_BUTTON_UP:
					mousestate = 0;
					break;
				case SDL_EVENT_MOUSE_MOTION:
					mousex = (int) e.motion.x;
					mousey = (int) e.motion.y;
					break;
				case SDL_EVENT_KEY_DOWN:
					switch (e.key.key) {
						case SDLK_SPACE:
							game->input = INPUT_PAUSE_PLAY;
							break;
					}
					break;
				case SDL_EVENT_WINDOW_RESIZED:
					SDL_GetWindowSize(w, &game->ui.width, &game->ui.height);
					break;
			}
			update_buttons(mousex, mousey, mousestate);
		}

		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);
		}
		render_side_panel(r, &drawList->panel);

		update(game, now, a);

		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);
	game->state.grid[0] = 1;
}

__attribute((export_name("game_render")))
DrawList *game_render(int width, int height) {
	game->ui.width = width;
	game->ui.height = height;
	Arena scratch = perm;
	return render(&game->state, &game->ui, &scratch);
}

__attribute((export_name("game_update")))
void game_update(int input, int mousex, int mousey) {
	game->input = input;
	game->mousex = mousex;
	game->mousey = mousey;
	update(game, perm);
}

#endif