about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-04-15 14:36:16 -0400
committerelioat <elioat@tilde.institute>2024-04-15 14:36:16 -0400
commite80d2a31862110c8913358027905cbcf905274ce (patch)
tree4d55e65c5ba6d7f6f80fc649e5881cea9ec65b7f
parentbfc3d1d29278d286677aba7e75dbae3dc43bc193 (diff)
downloadtour-e80d2a31862110c8913358027905cbcf905274ce.tar.gz
c
-rw-r--r--c/sand/NOTES.txt1
-rwxr-xr-xc/sand/build.sh3
-rw-r--r--c/sand/index.html12
-rw-r--r--c/sand/sand.c85
4 files changed, 101 insertions, 0 deletions
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 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>wasm sand</title>
+</head>
+<body>
+    <canvas id="sand" width="800" height="600"></canvas>
+    <script src="sand.js"></script>
+</body>
+</html>
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 <stdio.h>
+#include <emscripten.h>
+
+#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;
+}