diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2025-04-06 16:42:26 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2025-04-06 16:42:26 +0100 |
commit | 775d80fe2ea7c541eb3f1180e02a48ae5498743e (patch) | |
tree | eb0c905006fb207404f6676f05ba6393ffa7a0fd /src | |
parent | 8c4d5838e67d62775983fb46a64813d8b6bceb28 (diff) | |
download | ldjam57-775d80fe2ea7c541eb3f1180e02a48ae5498743e.tar |
Improves how click creates stuff
Diffstat (limited to 'src')
-rw-r--r-- | src/all.c | 109 | ||||
-rw-r--r-- | src/index.html.in | 16 | ||||
-rw-r--r-- | src/types.c | 3 |
3 files changed, 110 insertions, 18 deletions
@@ -99,9 +99,32 @@ static const Button buttons[N_BUTTONS] = { [BUTTON_BACK] = {.x = BUTTON_SPACING, .y = BUTTON_SPACER(BUTTON_BACK), .w = BUTTON_SIZE, .h = BUTTON_SIZE}, }; -static DrawList *render(State *state, UI *ui, Arena *a) { - (void) ui; +static int getPlaceAction(int currentColor, int cellx, int celly, float cellWidth, float cellHeight) { + int hoverColor = EMPTY; + + switch (currentColor) { + case BLACK: + if (cellx < cellWidth / 4 && cellx < celly && cellx < cellHeight - celly) { + hoverColor = RED_LEFT; + } else if (celly < cellHeight / 4 && celly < cellx && celly < cellWidth - cellx) { + hoverColor = RED_UP; + } else if (cellx > cellWidth / 4 * 3 && cellWidth - cellx < celly && cellWidth - cellx < cellHeight - celly) { + hoverColor = RED_RIGHT; + } else if (celly > cellHeight / 4 * 3 && cellHeight - celly < cellx && cellHeight - celly < cellWidth - cellx) { + hoverColor = RED_DOWN; + } else { + hoverColor = RED; + } + break; + case EMPTY: + hoverColor = BLACK; + break; + } + return hoverColor; +} + +static DrawList *render(State *state, UI *ui, Arena *a) { DrawList *drawList = new(a, 1, DrawList); float cellWidth = (float) (ui->width - GRID_OFFSET_X) / GRIDWIDTH; @@ -145,6 +168,7 @@ static DrawList *render(State *state, UI *ui, Arena *a) { }; } + // Goal drawList->els[drawList->len++] = (DrawElement) { .x = cellWidth * state->goalx + GRID_OFFSET_X, .y = cellHeight * state->goaly, @@ -154,6 +178,42 @@ static DrawList *render(State *state, UI *ui, Arena *a) { .border = {255, 0, 0, 255}, }; + // Hover + if (ui->mousex >= GRID_OFFSET_X) { + int hoverx = (ui->mousex - GRID_OFFSET_X) * GRIDWIDTH / (ui->width - GRID_OFFSET_X); + int hovery = ui->mousey * GRIDHEIGHT / ui->height; + + int cellx = ui->mousex - GRID_OFFSET_X - hoverx * cellWidth; + int celly = ui->mousey - hovery * cellHeight; + + int hoverColor = getPlaceAction( + state->grid[hoverx + hovery * GRIDWIDTH], + cellx, + celly, + cellWidth, + cellHeight + ); + + if (hoverColor != EMPTY) { + int subCellWidth = cellWidth / 2; + int subCellHeight = cellHeight / 2; + for (int subx = 0; subx < 2; subx++) { + for (int suby = 0; suby < 2; suby++) { + Color fill = colors[hoverColor][subx + 2 * suby]; + fill.a = 127; + drawList->els[drawList->len++] = (DrawElement) { + .x = cellWidth * hoverx + GRID_OFFSET_X + (subx * subCellWidth), + .y = cellHeight * hovery + (suby * subCellHeight), + .w = subCellWidth, + .h = subCellHeight, + .fill = fill, + }; + } + } + } + } + + // Side panel drawList->els[drawList->len++] = (DrawElement) { .x = 0, .y = 0, @@ -195,13 +255,25 @@ static void update(Game *game, uint64_t now, Arena a) { switch (game->input) { int x, y; case INPUT_CLICK: - x = (game->mousex - GRID_OFFSET_X) * 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])); + x = (game->ui.mousex - GRID_OFFSET_X) * GRIDWIDTH / offset_width; + y = game->ui.mousey * GRIDHEIGHT / game->ui.height; + float cellWidth = (float) (game->ui.width - GRID_OFFSET_X) / GRIDWIDTH; + float cellHeight = (float) game->ui.height / GRIDHEIGHT; + int cellx = game->ui.mousex - GRID_OFFSET_X - x * cellWidth; + int celly = game->ui.mousey - y * cellHeight; + // Keeping this around for testing purposes + // game->state.grid[x + y * GRIDWIDTH] = (game->state.grid[x + y * GRIDWIDTH] + 1) % (sizeof(colors) / sizeof(colors[0])); + game->state.grid[x + y * GRIDWIDTH] = getPlaceAction( + game->state.grid[x + y * GRIDWIDTH], + cellx, + celly, + cellWidth, + cellHeight + ); for (int i = 0; i < N_BUTTONS; i++) { if ( - game->mousex > buttons[i].x && game->mousex < buttons[i].x + buttons[i].w && - game->mousey > buttons[i].y && game->mousey < buttons[i].y + buttons[i].h + game->ui.mousex > buttons[i].x && game->ui.mousex < buttons[i].x + buttons[i].w && + game->ui.mousey > buttons[i].y && game->ui.mousey < buttons[i].y + buttons[i].h ) { // TODO - CLICK THINGS //game->state.buttonStates[i] = BUTTON_STATE_PRESSED; @@ -209,8 +281,8 @@ static void update(Game *game, uint64_t now, Arena a) { } break; case INPUT_RCLICK: - x = game->mousex * GRIDWIDTH / game->ui.width; - y = game->mousey * GRIDHEIGHT / game->ui.height; + x = (game->ui.mousex - GRID_OFFSET_X) * GRIDWIDTH / (game->ui.width - GRID_OFFSET_X); + y = game->ui.mousey * GRIDHEIGHT / game->ui.height; game->state.grid[x + y * GRIDWIDTH] = EMPTY; break; case INPUT_PAUSE_PLAY: @@ -263,6 +335,8 @@ int main(int argc, char **argv) { game->ui = (UI) { .width = 640, .height = 480, + .mousex = 0, + .mousey = 0, }; for (int i = 0; i < N_BUTTONS; i++) { game->state.buttonStates[i] = BUTTON_STATE_IDLE; @@ -317,14 +391,19 @@ int main(int argc, char **argv) { case SDL_EVENT_QUIT: return 0; case SDL_EVENT_MOUSE_BUTTON_DOWN: - game->mousex = (int) e.button.x; - game->mousey = (int) e.button.y; + game->ui.mousex = (int) e.button.x; + game->ui.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_MOUSE_MOTION: + game->ui.mousex = (int) e.motion.x; + game->ui.mousey = (int) e.motion.y; + game->input = INPUT_MOVE; + break; case SDL_EVENT_KEY_DOWN: switch (e.key.key) { case SDLK_SPACE: @@ -405,9 +484,11 @@ void game_init(void) { } __attribute((export_name("game_render"))) -DrawList *game_render(int width, int height) { +DrawList *game_render(int width, int height, int mousex, int mousey) { game->ui.width = width; game->ui.height = height; + game->ui.mousex = mousex; + game->ui.mousey = mousey; Arena scratch = perm; return render(&game->state, &game->ui, &scratch); } @@ -415,8 +496,8 @@ DrawList *game_render(int width, int height) { __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; + game->ui.mousex = mousex; + game->ui.mousey = mousey; update(game, now, perm); } diff --git a/src/index.html.in b/src/index.html.in index c239f6e..6147475 100644 --- a/src/index.html.in +++ b/src/index.html.in @@ -32,6 +32,7 @@ const INPUT_NONE = 0; const INPUT_CLICK = 1; const INPUT_RCLICK = 2; const INPUT_PAUSE_PLAY = 3; +const INPUT_MOVE = 4; const WASM = #include "../build/main.wasm.b64" @@ -56,6 +57,9 @@ async function main() { let ctx = canvas.getContext("2d"); let memory = exports.memory; + let mousex = 0; + let mousey = 0; + const audio = new Audio(); audio.src = MUSIC; audio.volume = 0.2; @@ -85,7 +89,7 @@ async function main() { function render() { let width = canvas.width = min(html.clientWidth, max_width()); let height = canvas.height = width; - let ptr = exports.game_render(width, height); + let ptr = exports.game_render(width, height, mousex, mousey); let dl = new Int32Array(memory.buffer, ptr); let len = dl[0]; let ops = dl.subarray(1); @@ -109,9 +113,15 @@ async function main() { window.addEventListener("resize", onresize); onresize(); + canvas.addEventListener("mousemove", function(e) { + mousex = e.clientX; + mousey = e.clientY; + exports.game_update(INPUT_MOVE, mousex, mousey, now()); + }); + canvas.addEventListener("mousedown", function(e) { - const mousex = e.clientX; - const mousey = e.clientY; + mousex = e.clientX; + mousey = e.clientY; if (e.button == 0) { exports.game_update(INPUT_CLICK, mousex, mousey, now()); } else if (e.button == 2) { diff --git a/src/types.c b/src/types.c index bfe0d7d..b50228c 100644 --- a/src/types.c +++ b/src/types.c @@ -56,6 +56,7 @@ enum { typedef struct { int width, height; + int mousex, mousey; } UI; typedef enum { @@ -82,13 +83,13 @@ enum { INPUT_CLICK, INPUT_RCLICK, INPUT_PAUSE_PLAY, + INPUT_MOVE, }; 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))) |