Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/src/all.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/all.c')
-rw-r--r--src/all.c67
1 files changed, 62 insertions, 5 deletions
diff --git a/src/all.c b/src/all.c
index 4fe3d5b..130bf27 100644
--- a/src/all.c
+++ b/src/all.c
@@ -19,6 +19,7 @@ typedef struct {
#define MEM_SIZE (1<<16)
#define GRIDWIDTH 16
#define GRIDHEIGHT 16
+#define TICK_LENGTH 1000
typedef struct {
char *start;
@@ -45,6 +46,8 @@ typedef struct {
} UI;
typedef struct {
+ uint64_t lastTick;
+ char playing;
int grid[GRIDWIDTH * GRIDHEIGHT];
} State;
@@ -52,6 +55,7 @@ typedef struct {
enum {
INPUT_NONE,
INPUT_CLICK,
+ INPUT_PAUSE_PLAY,
};
typedef struct {
@@ -61,9 +65,19 @@ typedef struct {
int mousex, mousey;
} Game;
-static const Color colors[] = {
+enum {
+ EMPTY,
+ BLACK,
+ RED,
+ YELLOW,
+ N_COLORS,
+};
+
+static const Color colors[N_COLORS] = {
{255, 255, 255, 255},
{0, 0, 0, 255},
+ {255, 0, 0, 255},
+ {255, 255, 0, 255}
};
static DrawList *render(State *state, UI *ui, Arena *a) {
@@ -90,7 +104,7 @@ static DrawList *render(State *state, UI *ui, Arena *a) {
return drawList;
}
-static void update(Game *game, Arena a) {
+static void update(Game *game, uint64_t now, Arena a) {
switch (game->input) {
int x, y;
case INPUT_CLICK:
@@ -98,9 +112,41 @@ static void update(Game *game, Arena a) {
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]));
break;
+ case INPUT_PAUSE_PLAY:
+ game->state.playing = !game->state.playing;
+ break;
default:
break;
}
+
+ if (game->state.playing && game->state.lastTick + TICK_LENGTH <= now) {
+ game->state.lastTick = now;
+ State *lastState = new(&a, 1, State);
+ __builtin_memcpy(lastState, game, sizeof(State));
+
+ for (int x = 0; x < GRIDWIDTH; x++) {
+ for (int y = 0; y < GRIDHEIGHT; y++) {
+ if (
+ lastState->grid[x + y * GRIDWIDTH] == BLACK && (
+ (x > 0 && lastState->grid[x - 1 + y * GRIDWIDTH] == RED) ||
+ (x < GRIDWIDTH - 1 && lastState->grid[x + 1 + y * GRIDWIDTH] == RED) ||
+ (y > 0 && lastState->grid[x + (y - 1) * GRIDWIDTH] == RED) ||
+ (y < GRIDHEIGHT - 1 && lastState->grid[x + (y + 1) * GRIDWIDTH] == RED)
+ )
+ ) {
+ game->state.grid[x + y * GRIDWIDTH] = RED;
+ }
+
+ if (lastState->grid[x + y * GRIDWIDTH] == RED) {
+ game->state.grid[x + y * GRIDWIDTH] = YELLOW;
+ }
+
+ if (lastState->grid[x + y * GRIDWIDTH] == YELLOW) {
+ game->state.grid[x + y * GRIDWIDTH] = BLACK;
+ }
+ }
+ }
+ }
}
#if SDL
@@ -118,7 +164,8 @@ int main(int argc, char **argv) {
};
Game *game = new(&a, 1, Game);
- game->state.grid[0] = 1;
+ game->state.grid[0] = 0;
+ game->state.playing = 0;
game->ui = (UI) {
.width = 640,
.height = 480,
@@ -138,6 +185,7 @@ int main(int argc, char **argv) {
SDL_DestroyProperties(renderProps);
for (;;) {
+ uint64_t now = SDL_GetTicks();
game->input = INPUT_NONE;
SDL_Event e = {0};
while (SDL_PollEvent(&e)) {
@@ -149,6 +197,13 @@ int main(int argc, char **argv) {
game->mousex = (int) e.button.x;
game->mousey = (int) e.button.y;
break;
+ case SDL_EVENT_KEY_DOWN:
+ switch (e.key.key) {
+ case SDLK_SPACE:
+ game->input = INPUT_PAUSE_PLAY;
+ break;
+ }
+ break;
}
}
@@ -183,7 +238,7 @@ int main(int argc, char **argv) {
SDL_RenderRect(r, &rect);
}
- update(game, a);
+ update(game, now, a);
SDL_RenderPresent(r);
}
@@ -215,8 +270,10 @@ DrawList *game_render(int width, int height) {
}
__attribute((export_name("game_update")))
-void game_update(int input) {
+void game_update(int input, int mousex, int mousey) {
game->input = input;
+ game->mousex = mousex;
+ game->mousey = mousey;
update(game, perm);
}