From beed00a805e9aa8e3166c9a90b50013f85f07093 Mon Sep 17 00:00:00 2001 From: matthew Date: Tue, 7 Aug 2018 19:48:42 +0000 Subject: [PATCH] implemented keybindings, quitting with q and viewing different layers with J,K --- nogl/input.c | 22 ++++++++++++++++++++++ nogl/input.h | 12 ++++++++++++ nogl/main.c | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 nogl/input.c create mode 100644 nogl/input.h diff --git a/nogl/input.c b/nogl/input.c new file mode 100644 index 0000000..53e5147 --- /dev/null +++ b/nogl/input.c @@ -0,0 +1,22 @@ +#include "input.h" +#include "stdio.h" +#include "err.h" + +static char keys[] = {'q', 'K', 'J', 'c'}; + +enum Action input_update() +{ + char c = getchar(); + enum Action ret = ACTION_LAST; + + for(int i = 0; i < ACTION_LAST; ++i){ + if(c == keys[i]){ + return i; + } + } + + char msg[80]; + sprintf(msg, "character %c not recognised!", c); + err_output(msg); + return ret; +} diff --git a/nogl/input.h b/nogl/input.h new file mode 100644 index 0000000..eabb7ce --- /dev/null +++ b/nogl/input.h @@ -0,0 +1,12 @@ +#ifndef INPUT_H +#define INPUT_H +enum Action{ + QUIT = 0, + LAYER_UP, + LAYER_DOWN, + ERR_CLEAR, + ACTION_LAST +}; + +enum Action input_update(); +#endif//INPUT_H diff --git a/nogl/main.c b/nogl/main.c index 0fb34d0..b8abb74 100644 --- a/nogl/main.c +++ b/nogl/main.c @@ -1,11 +1,12 @@ #include #include #include +#include #include "board.h" #include "point.h" #include "board_renderer.h" #include "err.h" -#include +#include "input.h" static Board* board = NULL; @@ -43,22 +44,47 @@ void draw_borders() int main() { + int layer = 0; + if(!init()) return -1; board = board_init(point2i(4,3)); + bool quit = false; if(board == NULL){ fprintf(stderr, "board null after init!\n"); return -1; } brndr_set_board(board); - brndr_set_rendered_layer(5); //TODO: implement way to change layer + brndr_set_rendered_layer(layer); brndr_render(); - getchar(); - err_clear(); - brndr_render(); - getchar(); + + while(!quit){ + switch(input_update()){ + case QUIT: + quit = true; + break; + case LAYER_UP: + //if(layer < 9){ + // layer++; + //} + layer++; + brndr_set_rendered_layer(layer); + brndr_render(); + break; + case LAYER_DOWN: + if(layer > 0){ + layer--; + } + brndr_set_rendered_layer(layer); + brndr_render(); + break; + case ERR_CLEAR: + err_clear(); + break; + } + } SDL_Quit(); brndr_cleanup(); -- 2.20.1