From 50eeaa8f0a503acd9f10af1453717c1348c3e481 Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Wed, 24 Jun 2020 14:27:24 +0100 Subject: [PATCH] Migrated handling of grid and game to game.(c/h). --- game.c | 53 ++++++++++++++++++++++++++++-------- game.h | 4 +-- grid.c | 86 +++++++++++++++++++++++++++++++--------------------------- grid.h | 4 +-- main.c | 10 +++++-- 5 files changed, 98 insertions(+), 59 deletions(-) diff --git a/game.c b/game.c index b416681..c6039b5 100644 --- a/game.c +++ b/game.c @@ -6,38 +6,45 @@ static Grid* grid = 0; static bool running = true; +static bool stepping_mode = false; static bool do_step = true; static Vect2i cursor; +// declaration of locally used +static void toggleStepMode(); + +static void toggleCell(); + void initGame() { int width = 0; int height = 0; // stdscr is screen created by initscr() getmaxyx(stdscr, height, width); - - initGrid(grid, height, width); + + grid = initGrid(width, height); randomizeGrid(grid); cursor.x = width/2; cursor.y = height/2; } -static void step() { do_step ^= 1; } - void updateGame() { if (!grid) return; - if (do_step) updateGrid(grid); + if (do_step) { + updateGrid(grid); + if (stepping_mode) do_step = false; + } } void drawGame() { if (!grid) return; drawGrid(grid); - refresh(); } bool isRunning() { return running; } + void handleInput(char ch) { switch (ch) { @@ -45,7 +52,7 @@ void handleInput(char ch) running = false; break; case ' ': - do_step ^= 1; + toggleStepMode(); break; case 'h': moveVect2i(&cursor, -1, 0); @@ -59,13 +66,18 @@ void handleInput(char ch) case 'l': moveVect2i(&cursor, 1, 0); break; - case 's': - step(); + case '\n': + do_step = true; break; + case 'i': + toggleCell(); + break; + default: + if (ch != -1) do_step = true; } } -void showLastPressed(char ch) +void drawLastPressed(char ch) { static char lastc = ' '; if (ch != -1) lastc = ch; @@ -74,16 +86,33 @@ void showLastPressed(char ch) attroff(COLOR_PAIR(2)); } -void showCurPos() +void drawCurPos() { attron(COLOR_PAIR(2)); mvprintw(1, 0, "curpos: %i, %i", cursor.x, cursor.y); + attroff(COLOR_PAIR(2)); attron(COLOR_PAIR(3)); mvaddch(cursor.y, cursor.x, ' '); attroff(COLOR_PAIR(3)); - attroff(COLOR_PAIR(2)); } void endGame() { + // free stuff +} + +// locally used +static void toggleStepMode() +{ + stepping_mode ^= 1; + do_step = true; +} + +static void toggleCell() +{ + if (!grid) return; + bool cell = getPixel(grid, cursor.x, cursor.y); + if (cell) setPixel(grid, cursor.x, cursor.y, 0); + else setPixel(grid, cursor.x, cursor.y, 1); + } diff --git a/game.h b/game.h index b692fee..cb16f0f 100644 --- a/game.h +++ b/game.h @@ -15,9 +15,9 @@ void handleInput(char ch); // Overlays // Draws cursor and cursor position -void showLastPressed(char ch); +void drawLastPressed(char ch); -void showCurPos(); +void drawCurPos(); bool endgame(); diff --git a/grid.c b/grid.c index a4c7c27..a7cc0f9 100644 --- a/grid.c +++ b/grid.c @@ -3,12 +3,17 @@ #include #include "grid.h" -void initGrid(Grid* grid, unsigned int width, unsigned int height) +// locally used declaration +static bool isAliveNext(Grid* grid, int x, int y); + +Grid* initGrid(unsigned int width, unsigned int height) { + Grid* grid = (Grid*)malloc(sizeof(Grid)); grid->size = width*height; grid->width = width; grid->state = (bool*)malloc(sizeof(bool)*grid->size); grid->next_state = (bool*)malloc(sizeof(bool)*grid->size); + return grid; } void randomizeGrid(Grid* grid) @@ -34,6 +39,46 @@ void clearGrid(Grid* grid) for (int i = 0; i < grid->size; i++) grid->state[i]=false; } +void updateGrid(Grid* grid) +{ + unsigned int width = grid->width; + unsigned int height = grid->size/width; + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + grid->next_state[toIndex(grid, x, y)] = isAliveNext(grid, x, y); + } + } + + for (int i = 0; i < grid->size; i++) { + grid->state[i] = grid->next_state[i]; + } +} + +void setPixel(Grid* grid, int x, int y, bool on) +{ + grid->state[toIndex(grid, x, y)] = on; +} + +void drawGrid(Grid* grid) +{ + unsigned int width, height; + width = grid->width; + height = grid->size/width; + // Init color pair init_pair(index, fg, bg); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + if (grid->state[toIndex(grid, x, y)]){ + attron(COLOR_PAIR(1)); + mvaddch(y, x, ' '); + attroff(COLOR_PAIR(1)); + } + else mvaddch(y, x, ' '); + } + } +} + +// locally used // check if cell's next state is alive static bool isAliveNext(Grid* grid, int x, int y) { @@ -71,42 +116,3 @@ static bool isAliveNext(Grid* grid, int x, int y) return false; } -void updateGrid(Grid* grid) -{ - unsigned int width = grid->width; - unsigned int height = grid->size/width; - - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - grid->next_state[toIndex(grid, x, y)] = isAliveNext(grid, x, y); - } - } - - for (int i = 0; i < grid->size; i++) { - grid->state[i] = grid->next_state[i]; - } -} - -void putPixel(Grid* grid, int x, int y) -{ - grid->state[toIndex(grid, x, y)] = true; -} - -void drawGrid(Grid* grid) -{ - unsigned int width, height; - width = grid->width; - height = grid->size/width; - // Init color pair init_pair(index, fg, bg); - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - if (grid->state[toIndex(grid, x, y)]){ - attron(COLOR_PAIR(1)); - mvaddch(y, x, ' '); - attroff(COLOR_PAIR(1)); - } - else mvaddch(y, x, ' '); - } - } -} - diff --git a/grid.h b/grid.h index 946b84d..fa18e09 100644 --- a/grid.h +++ b/grid.h @@ -10,7 +10,7 @@ typedef struct Grid { bool* next_state; } Grid; -void initGrid(Grid* grid, unsigned int width, unsigned int height); +Grid* initGrid(unsigned int width, unsigned int height); void randomizeGrid(Grid* grid); @@ -22,7 +22,7 @@ void clearGrid(Grid* grid); void updateGrid(Grid* grid); -void putPixel(Grid* grid, int x, int y); +void setPixel(Grid* grid, int x, int y, bool on); void drawGrid(Grid* grid); diff --git a/main.c b/main.c index 7036369..34918f1 100644 --- a/main.c +++ b/main.c @@ -36,7 +36,7 @@ int main() const int FRAME_RATE = 30; const float FRAME_TIME = 1.f/(float)FRAME_RATE; - //initGame(); + initGame(); float t = 0; while (isRunning()) { @@ -45,9 +45,13 @@ int main() handleInput(ch); - //drawGame(); + drawGame(); + drawLastPressed(ch); + drawCurPos(); + + refresh(); - //updateGame(); + updateGame(); usleep(pow(10,6)*(FRAME_TIME-t)); float t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC;