From 7b3fee4b4ff00d2c4a1daf8976e80dc1db1af4ab Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Wed, 24 Jun 2020 02:51:30 +0100 Subject: [PATCH] Migrated overlay/input handling functions to game(.h/.c). --- game.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ game.h | 18 ++++++++++++++++++ main.c | 55 ++++++++----------------------------------------------- 3 files changed, 79 insertions(+), 47 deletions(-) create mode 100644 game.c create mode 100644 game.h diff --git a/game.c b/game.c new file mode 100644 index 0000000..a84799f --- /dev/null +++ b/game.c @@ -0,0 +1,53 @@ +#include "game.h" +#include "grid.h" +#include "vect.h" + +static Grid grid; + +static bool running = true; +static bool do_step = true; +static Vect2i cursor = {0, 0}; + +void initGame() +{ +} + +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; + } +} + +void showCurPos() +{ + attron(COLOR_PAIR(2)); + mvprintw(1, 0, "curpos: %i, %i", cursor.x, cursor.y); + attron(COLOR_PAIR(3)); + mvaddch(cursor.y, cursor.x, ' '); + attroff(COLOR_PAIR(3)); + attroff(COLOR_PAIR(2)); +} + +void endGame() +{ +} diff --git a/game.h b/game.h new file mode 100644 index 0000000..6065475 --- /dev/null +++ b/game.h @@ -0,0 +1,18 @@ +#include + +#ifndef GAME_H +#define GAME_H + +void initGame(); + +bool isRunning(); + +void handleInput(char ch); + +// Overlay +// Draws cursor and cursor position +void showCurPos(); + +bool endgame(); + +#endif diff --git a/main.c b/main.c index 6d434c3..21f65f6 100644 --- a/main.c +++ b/main.c @@ -1,21 +1,18 @@ -#include #include +// Delay and timing. #include -#include #include +// math +#include + +// game +#include "game.h" #include "grid.h" #include "vect.h" -static bool running = true; -static bool do_step = true; -static Vect2i cursor = {0, 0}; - -void handleInput(char ch); void showLastPressed(char ch); -void showCurPos(); - int main() { // init @@ -56,7 +53,7 @@ int main() float t = 0; - while (running) { + while (isRunning()) { clock_t start_t = clock(); char ch = getch(); @@ -67,14 +64,9 @@ int main() // draw overlays showLastPressed(ch); showCurPos(); - // cursor - attron(COLOR_PAIR(3)); - mvaddch(cursor.y, cursor.x, ' '); - attroff(COLOR_PAIR(3)); - refresh(); - if (do_step) updateGrid(&grid); + if (true) updateGrid(&grid); usleep(pow(10,6)*(FRAME_TIME-t)); float t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC; @@ -94,34 +86,3 @@ void showLastPressed(char ch) attroff(COLOR_PAIR(2)); } -void showCurPos() -{ - attron(COLOR_PAIR(2)); - mvprintw(1, 0, "curpos: %i, %i", cursor.x, cursor.y); - attroff(COLOR_PAIR(2)); -} - -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; - } - -}