Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/src/types.c
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2025-04-05 18:21:28 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2025-04-05 18:21:28 +0100
commit912fcda40233fd4400e33ee91b0dcedc28b78d98 (patch)
tree8d46662e219d4db62de080be2d1f211ff69cd466 /src/types.c
parentd36583ad6ae53708f1ae11bb7e4f4939e6ac3b4d (diff)
downloadldjam57-912fcda40233fd4400e33ee91b0dcedc28b78d98.tar
Add a level
Diffstat (limited to 'src/types.c')
-rw-r--r--src/types.c90
1 files changed, 90 insertions, 0 deletions
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;
+}