diff options
Diffstat (limited to 'src/all.c')
-rw-r--r-- | src/all.c | 83 |
1 files changed, 62 insertions, 21 deletions
@@ -1,4 +1,15 @@ -#include "all.h" +#include <stddef.h> +#include <stdint.h> + +typedef struct { + int x, y, w, h; + unsigned char r, g, b, a; +} DrawElement; + +typedef struct { + int len; + DrawElement els[256]; +} DrawList; #define MEM_SIZE (1<<16) @@ -30,6 +41,7 @@ typedef struct { int x, y; } State; +// Mirror these in src/index.html.in enum { INPUT_NONE, INPUT_UP, @@ -50,18 +62,14 @@ static DrawList *render(State state, UI *ui, Arena *a) { DrawList *drawList = new(a, 1, DrawList); drawList->len = 1; drawList->els[0] = (DrawElement) { - .rect = (SDL_Rect) { - .x = state.x, - .y = state.y, - .w = 100, - .h = 100, - }, - .color = (SDL_Color) { - .r = 255, - .g = 0, - .b = 0, - .a = 255, - }, + .x = state.x, + .y = state.y, + .w = 100, + .h = 100, + .r = 255, + .g = 0, + .b = 0, + .a = 255, }; return drawList; @@ -88,6 +96,8 @@ static void update(Game *game, Arena a) { #if SDL +#include <SDL3/SDL.h> + int main(int argc, char **argv) { (void) argc; (void) argv; @@ -154,16 +164,16 @@ int main(int argc, char **argv) { for (int i = 0; i < drawList->len; i++) { SDL_SetRenderDrawColor( r, - drawList->els[i].color.r, - drawList->els[i].color.g, - drawList->els[i].color.b, - drawList->els[i].color.a + drawList->els[i].r, + drawList->els[i].g, + drawList->els[i].b, + drawList->els[i].a ); SDL_FRect rect = { - .x = (float) drawList->els[i].rect.x, - .y = (float) drawList->els[i].rect.y, - .w = (float) drawList->els[i].rect.w, - .h = (float) drawList->els[i].rect.h + .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_RenderFillRect(r, &rect); } @@ -177,4 +187,35 @@ int main(int argc, char **argv) { } #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 = (State) { + .x = 100, + .y = 100, + }; +} + +__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) { + game->input = input; + update(game, perm); +} + #endif |