From 912fcda40233fd4400e33ee91b0dcedc28b78d98 Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Sat, 5 Apr 2025 18:21:28 +0100 Subject: Add a level --- src/types.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/types.c (limited to 'src/types.c') diff --git a/src/types.c b/src/types.c new file mode 100644 index 0000000..fe72e13 --- /dev/null +++ b/src/types.c @@ -0,0 +1,90 @@ +#include "all.c" + +#define MEM_SIZE (1<<24) +#define GRIDWIDTH 20 +#define GRIDHEIGHT 16 +#define TICK_LENGTH 200 + +typedef struct { + unsigned char r, g, b, a; +} Color; + +typedef struct { + int x, y, w, h; + Color fill; + Color border; +} DrawElement; + +typedef struct { + int len; + DrawElement els[2 * GRIDWIDTH * GRIDHEIGHT * 4]; +} DrawList; + +typedef struct { + char *start; + char *end; +} Arena; + +enum { + EMPTY, + BLACK, + RED, + YELLOW, + RED_UP, + RED_DOWN, + RED_LEFT, + RED_RIGHT, + BLUE, + BLUE_UP, + BLUE_DOWN, + BLUE_LEFT, + BLUE_RIGHT, + N_COLORS, +}; + +typedef struct { + int width, height; +} UI; + +typedef struct { + uint64_t lastTick; + char playing; + int grid[GRIDWIDTH * GRIDHEIGHT]; + int goalx, goaly; +} State; + +// Mirror these in src/index.html.in +enum { + INPUT_NONE, + INPUT_CLICK, + INPUT_RCLICK, + INPUT_PAUSE_PLAY, +}; + +typedef struct { + State state; + UI ui; + int input; + int mousex, mousey; +} Game; + +#define new(a, c, t) ((t *) alloc(a, c, sizeof(t), _Alignof(t))) +#define affirm(c) while (!(c)) *(volatile int *)0 = 0 + +static void xmemcpy(void *dst, void *src, ptrdiff_t size) { + for (ptrdiff_t i = 0; i < size; i++) { + ((char *) dst)[i] = ((char *) src)[i]; + } +} + +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; +} -- cgit v1.2.3