From e80d2a31862110c8913358027905cbcf905274ce Mon Sep 17 00:00:00 2001 From: elioat Date: Mon, 15 Apr 2024 14:36:16 -0400 Subject: c --- c/sand/NOTES.txt | 1 + c/sand/build.sh | 3 ++ c/sand/index.html | 12 ++++++++ c/sand/sand.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 c/sand/NOTES.txt create mode 100755 c/sand/build.sh create mode 100644 c/sand/index.html create mode 100644 c/sand/sand.c (limited to 'c') diff --git a/c/sand/NOTES.txt b/c/sand/NOTES.txt new file mode 100644 index 0000000..7a3e5c8 --- /dev/null +++ b/c/sand/NOTES.txt @@ -0,0 +1 @@ +...this doesn't actually compile, yet. BUT I'm working on it. \ No newline at end of file diff --git a/c/sand/build.sh b/c/sand/build.sh new file mode 100755 index 0000000..4accc08 --- /dev/null +++ b/c/sand/build.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +emcc sand.c -o sand.js -s WASM=1 -O3 \ No newline at end of file diff --git a/c/sand/index.html b/c/sand/index.html new file mode 100644 index 0000000..3171c47 --- /dev/null +++ b/c/sand/index.html @@ -0,0 +1,12 @@ + + + + + + wasm sand + + + + + + diff --git a/c/sand/sand.c b/c/sand/sand.c new file mode 100644 index 0000000..2c31f26 --- /dev/null +++ b/c/sand/sand.c @@ -0,0 +1,85 @@ +#include +#include + +#define CANVAS_WIDTH 800 +#define CANVAS_HEIGHT 600 +#define GRID_SIZE 10 +#define GRID_WIDTH (CANVAS_WIDTH / GRID_SIZE) +#define GRID_HEIGHT (CANVAS_HEIGHT / GRID_SIZE) + +int grid[GRID_HEIGHT][GRID_WIDTH] = {{0}}; + +EM_BOOL mouseDown = EM_FALSE; + +EM_BOOL canvasMouseDown(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData) { + mouseDown = EM_TRUE; + int x = mouseEvent->canvasX / GRID_SIZE; + int y = mouseEvent->canvasY / GRID_SIZE; + if (x >= 0 && x < GRID_WIDTH && y >= 0 && y < GRID_HEIGHT) { + grid[y][x] = 1; + } + return EM_TRUE; +} + +EM_BOOL canvasMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData) { + if (mouseDown) { + int x = mouseEvent->canvasX / GRID_SIZE; + int y = mouseEvent->canvasY / GRID_SIZE; + if (x >= 0 && x < GRID_WIDTH && y >= 0 && y < GRID_HEIGHT) { + grid[y][x] = 1; + } + } + return EM_TRUE; +} + +EM_BOOL canvasMouseUp(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData) { + mouseDown = EM_FALSE; + return EM_TRUE; +} + +void addPlayer() { + for (int y = 0; y < GRID_HEIGHT; y++) { + for (int x = 0; x < GRID_WIDTH; x++) { + if (grid[y][x] == 1) { + grid[y - 10 >= 0 ? y - 10 : 0][x] = 3; + return; + } + } + } +} + +EM_BOOL keyCallback(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData) { + if (eventType == EMSCRIPTEN_EVENT_KEYDOWN) { + if (keyEvent->keyCode == 32) { + addPlayer(); + } else if (keyEvent->keyCode == 82) { // 'r' key + for (int y = 0; y < GRID_HEIGHT; y++) { + for (int x = 0; x < GRID_WIDTH; x++) { + grid[y][x] = 0; + } + } + } + } + return EM_TRUE; +} + +void updateGrid() { + // Update grid logic +} + +void drawGrid() { + // Draw grid logic +} + +int main() { + // Set event listeners + emscripten_set_mousemove_callback("#sand", NULL, EM_TRUE, canvasMouseMove); + emscripten_set_mousedown_callback("#sand", NULL, EM_TRUE, canvasMouseDown); + emscripten_set_mouseup_callback("#sand", NULL, EM_TRUE, canvasMouseUp); + emscripten_set_keydown_callback(NULL, NULL, EM_TRUE, keyCallback); + + // Main loop + emscripten_set_main_loop(updateGrid, 60, 1); + + return 0; +} -- cgit 1.4.1-2-gfad0