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
|
#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},
},
};
static DrawList *render(State *state, UI *ui, Arena *a) {
(void) ui;
DrawList *drawList = new(a, 1, DrawList);
int cellWidth = ui->width / 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,
.y = cellHeight * y,
.w = cellWidth,
.h = cellHeight,
.fill = {0, 0, 0, 0},
.border = {0, 0, 0, 255},
};
for (int subx = 0; subx < 2; subx++) {
for (int suby = 0; suby < 2; suby++) {
drawList->els[drawList->len++] = (DrawElement) {
.x = cellWidth * x + subx * cellWidth / 2,
.y = cellHeight * y + suby * cellHeight / 2,
.w = cellWidth / 2,
.h = cellHeight / 2,
.fill = colors[state->grid[x + GRIDWIDTH * y]][subx + 2 * suby],
.border = {0, 0, 0, 0},
};
}
}
}
}
drawList->els[drawList->len++] = (DrawElement) {
.x = cellWidth * state->goalx,
.y = cellHeight * state->goaly,
.w = cellWidth,
.h = cellHeight,
.fill = {255, 0, 0, 63},
.border = {255, 0, 0, 255},
};
return drawList;
}
static void update(Game *game, uint64_t now, Arena a) {
switch (game->input) {
int x, y;
case INPUT_CLICK:
x = game->mousex * GRIDWIDTH / game->ui.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_RCLICK:
x = game->mousex * GRIDWIDTH / game->ui.width;
y = game->mousey * GRIDHEIGHT / game->ui.height;
game->state.grid[x + y * GRIDWIDTH] = EMPTY;
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;
tick(game, a);
}
}
#if SDL
#include <SDL3/SDL.h>
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);
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;
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);
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_BLEND);
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->mousex = (int) e.button.x;
game->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_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;
}
}
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);
}
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, int now) {
game->input = input;
game->mousex = mousex;
game->mousey = mousey;
update(game, now, perm);
}
#endif
#endif
|