From bc869e1ac1cfdaed682ddef23de9c058eb779df3 Mon Sep 17 00:00:00 2001
From: matthew <matthew@owens.tech>
Date: Sat, 4 Aug 2018 21:01:40 +0100
Subject: [PATCH] implemented board_renderer, creating a bordered ncurses
 window for the board to be drawn to

---
 nogl/board_renderer.c | 38 ++++++++++++++++++++++++++++++++++
 nogl/board_renderer.h | 11 ++++++++++
 nogl/main.c           | 48 ++++++-------------------------------------
 3 files changed, 55 insertions(+), 42 deletions(-)
 create mode 100644 nogl/board_renderer.c
 create mode 100644 nogl/board_renderer.h

diff --git a/nogl/board_renderer.c b/nogl/board_renderer.c
new file mode 100644
index 0000000..9b02faa
--- /dev/null
+++ b/nogl/board_renderer.c
@@ -0,0 +1,38 @@
+#include "board_renderer.h"
+#include "point.h"
+
+static Board *b = NULL;
+static WINDOW *win = NULL;
+static Point2i winPos;
+static Point2i winSize;
+
+void boardRenderer_init()
+{
+	Point2i termsize;
+
+	winPos = point2i(0,0);
+	getmaxyx(stdscr, termsize.y, termsize.x);
+	winSize = termsize;
+	winSize.y -= 2;
+
+	win = newwin(winSize.y, winSize.x, winPos.y, winPos.x);
+}
+
+void boardRenderer_render()
+{
+	wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
+	wrefresh(win);
+}
+
+void boardRenderer_set_board(Board *board)
+{
+	b = board;
+}
+
+void boardRenderer_cleanup()
+{
+	// clearing the border
+	wborder(win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
+	b = NULL;
+	delwin(win);
+}
diff --git a/nogl/board_renderer.h b/nogl/board_renderer.h
new file mode 100644
index 0000000..68ee1fa
--- /dev/null
+++ b/nogl/board_renderer.h
@@ -0,0 +1,11 @@
+#ifndef BOARD_RENDERER_H
+#define BOARD_RENDERER_H
+#include <ncurses.h>
+#include "board.h"
+
+void boardRenderer_init();
+void boardRenderer_render();
+void boardRenderer_cleanup();
+void boardRenderer_set_board(Board *board);
+
+#endif//BOARD_RENDERER_H
diff --git a/nogl/main.c b/nogl/main.c
index 4d6e839..fd2a7c1 100644
--- a/nogl/main.c
+++ b/nogl/main.c
@@ -3,6 +3,7 @@
 #include <SDL2/SDL.h>
 #include "board.h"
 #include "point.h"
+#include "board_renderer.h"
 #include <ncurses.h>
 
 static Board* board = NULL;
@@ -15,6 +16,8 @@ bool init()
 	}
 
 	initscr();
+	boardRenderer_init();
+	curs_set(false);
 	return true;
 }
 
@@ -48,50 +51,11 @@ int main()
 		return -1;
 	}
 
-	Uint32 itime = SDL_GetTicks();
-	Uint32 ctime = 0;
-	Point2i loc;
-	Point2i bsize = board_get_size(board);
-	Uint32 cclock = 0;
-	Uint32 pclock = SDL_GetTicks();
-	Point2i rpos = point2i(10,10); // where to draw the arr
-
-	char *reprs = malloc(bsize.x * bsize.y * sizeof(char));
-	char *elevs = malloc(bsize.x * bsize.y * sizeof(char));
-	char *sel = NULL;
-
-	for(int y = 0; y < bsize.y; ++y){
-		for(int x = 0; x < bsize.x; ++x){
-			elevs[x + (bsize.x * y)] =
-				board_elevation_at(board, point2i(x, y)) + '0';
-			reprs[x + (bsize.x * y)] =
-				tile_repr(board_terrain_at(board, point2i(x, y)));
-		}
-	}
-
-	draw_borders();
-
-	// 'game' loop
-	while(ctime < itime + 5000){
-		ctime = SDL_GetTicks();
-		cclock = SDL_GetTicks();
-		
-		// Switching the selection if needed
-		if(cclock > pclock + 500 || pclock == 0){
-			sel = (sel == reprs) ? elevs : reprs;
-			pclock = cclock;
-
-			for(int y = 0; y < bsize.y; ++y){
-				for(int x = 0; x < bsize.x; ++x){
-					mvaddch(y + rpos.y, x + rpos.x , sel[x + bsize.x * y]);
-				}
-			}
-			refresh();
-		}
-	}
-
+	boardRenderer_render();
+	getchar();
 
 	SDL_Quit();
+	boardRenderer_cleanup();
 	endwin();
 
 	return 0;
-- 
2.20.1