#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;
}