implemented keybindings, quitting with q and viewing different layers with J,K
authormatthew <matthew@owens.tech>
Tue, 7 Aug 2018 19:48:42 +0000 (19:48 +0000)
committermatthew <matthew@owens.tech>
Tue, 7 Aug 2018 19:48:42 +0000 (19:48 +0000)
nogl/input.c [new file with mode: 0644]
nogl/input.h [new file with mode: 0644]
nogl/main.c

diff --git a/nogl/input.c b/nogl/input.c
new file mode 100644 (file)
index 0000000..53e5147
--- /dev/null
@@ -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 (file)
index 0000000..eabb7ce
--- /dev/null
@@ -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
index 0fb34d0..b8abb74 100644 (file)
@@ -1,11 +1,12 @@
 #include <stdio.h>
 #include <stdbool.h>
 #include <SDL2/SDL.h>
+#include <ncurses.h>
 #include "board.h"
 #include "point.h"
 #include "board_renderer.h"
 #include "err.h"
-#include <ncurses.h>
+#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();