diff --git a/game.c b/game.c index a84799f..b416681 100644 --- a/game.c +++ b/game.c @@ -1,15 +1,39 @@ +#include #include "game.h" #include "grid.h" #include "vect.h" -static Grid grid; +static Grid* grid = 0; static bool running = true; static bool do_step = true; -static Vect2i cursor = {0, 0}; +static Vect2i cursor; void initGame() { + int width = 0; + int height = 0; + // stdscr is screen created by initscr() + getmaxyx(stdscr, height, width); + + initGrid(grid, height, width); + 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); +} + +void drawGame() +{ + if (!grid) return; + drawGrid(grid); + refresh(); } bool isRunning() { return running; } @@ -17,27 +41,39 @@ bool isRunning() { return running; } void handleInput(char ch) { switch (ch) { - case 'q': - running = false; - break; - case ' ': - do_step ^= 1; - break; - case 'h': - moveVect2i(&cursor, -1, 0); - break; - case 'j': - moveVect2i(&cursor, 0, 1); - break; - case 'k': - moveVect2i(&cursor, 0, -1); - break; - case 'l': - moveVect2i(&cursor, 1, 0); - break; + case 'q': + running = false; + break; + case ' ': + do_step ^= 1; + break; + case 'h': + moveVect2i(&cursor, -1, 0); + break; + case 'j': + moveVect2i(&cursor, 0, 1); + break; + case 'k': + moveVect2i(&cursor, 0, -1); + break; + case 'l': + moveVect2i(&cursor, 1, 0); + break; + case 's': + step(); + break; } } +void showLastPressed(char ch) +{ + static char lastc = ' '; + if (ch != -1) lastc = ch; + attron(COLOR_PAIR(2)); + mvprintw(0, 0, "Last Pressed: %c", lastc); + attroff(COLOR_PAIR(2)); +} + void showCurPos() { attron(COLOR_PAIR(2)); diff --git a/game.h b/game.h index 6065475..b692fee 100644 --- a/game.h +++ b/game.h @@ -7,12 +7,19 @@ void initGame(); bool isRunning(); +void updateGame(); + +void drawGame(); + void handleInput(char ch); -// Overlay +// Overlays // Draws cursor and cursor position +void showLastPressed(char ch); + void showCurPos(); bool endgame(); #endif + diff --git a/grid.c b/grid.c index 2608615..a4c7c27 100644 --- a/grid.c +++ b/grid.c @@ -1,5 +1,6 @@ #include #include +#include #include "grid.h" void initGrid(Grid* grid, unsigned int width, unsigned int height) @@ -108,3 +109,4 @@ void drawGrid(Grid* grid) } } } + diff --git a/grid.h b/grid.h index a97b919..946b84d 100644 --- a/grid.h +++ b/grid.h @@ -1,5 +1,4 @@ #include -#include #ifndef GRID_H #define GRID_H @@ -28,3 +27,4 @@ void putPixel(Grid* grid, int x, int y); void drawGrid(Grid* grid); #endif + diff --git a/main.c b/main.c index 21f65f6..7036369 100644 --- a/main.c +++ b/main.c @@ -4,22 +4,18 @@ #include // math #include - // game #include "game.h" #include "grid.h" #include "vect.h" -void showLastPressed(char ch); - int main() { // init initscr(); raw(); noecho(); - // Colors // allows for transparancy when color values in init_pair(); are set to -1 or, no pair specified // e.g init_pair(1, COLOR_WHITE, -1) would be transparent background but white text @@ -31,26 +27,16 @@ int main() init_pair(2, COLOR_YELLOW, -1); // cursor init_pair(3, COLOR_RED, COLOR_RED); - // doesn't wait for user to input. // timeout(100) waits for 100ms for input. timeout(0); curs_set(FALSE); - int width = 0; - int height = 0; - - // stdscr is screen created by initscr() - getmaxyx(stdscr, height, width); - // framerate of the game const int FRAME_RATE = 30; const float FRAME_TIME = 1.f/(float)FRAME_RATE; - Grid grid; - initGrid(&grid, width, height); - randomizeGrid(&grid); - + //initGame(); float t = 0; while (isRunning()) { @@ -59,14 +45,9 @@ int main() handleInput(ch); - // draw grid - drawGrid(&grid); - // draw overlays - showLastPressed(ch); - showCurPos(); - - refresh(); - if (true) updateGrid(&grid); + //drawGame(); + + //updateGame(); usleep(pow(10,6)*(FRAME_TIME-t)); float t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC; @@ -77,12 +58,3 @@ int main() return 0; } -void showLastPressed(char ch) -{ - static char lastc = ' '; - if (ch != -1) lastc = ch; - attron(COLOR_PAIR(2)); - mvprintw(0, 0, "Last Pressed: %c", lastc); - attroff(COLOR_PAIR(2)); -} - diff --git a/makefile b/makefile index 95cedcd..95b945e 100644 --- a/makefile +++ b/makefile @@ -2,3 +2,4 @@ LDFLAGS=-lncurses main: *.c gcc *.c -o main ${LDFLAGS} + diff --git a/vect.c b/vect.c index b2562a7..5e63040 100644 --- a/vect.c +++ b/vect.c @@ -12,3 +12,4 @@ void moveVect2f(Vect2f* vect, float x, float y) vect->x += x; vect->y += y; } + diff --git a/vect.h b/vect.h index 9907bfe..106da81 100644 --- a/vect.h +++ b/vect.h @@ -14,3 +14,4 @@ void moveVect2i(Vect2i* vect, int x, int y); void moveVect2f(Vect2f* vect, float x, float y); #endif +