outputting elevation and terrain
authormatthew <matthew@owens.tech>
Mon, 30 Jul 2018 16:28:50 +0000 (17:28 +0100)
committermatthew <matthew@owens.tech>
Mon, 30 Jul 2018 16:28:50 +0000 (17:28 +0100)
Makefile
common/board.c
common/board.h
nogl/main.c

index e2191e1..348dbd2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ GL_TARGET = game
 LIBS = -lm -D_REENTRANT -lGL -lGLEW -lSDL2 -lSDL2_image
 
 NOGL_TARGET = game-nogl
-NOGL_LIBS = -lm -D_REENTRANT -lSDL2 -lSDL2_image
+NOGL_LIBS = -lm -D_REENTRANT -lSDL2 -lSDL2_image -lncurses
 
 CC = gcc
 CFLAGS = -g -Wall -I/usr/include/SDL2 -Icommon/ -Inogl/ -Igl/
index c8feac7..6c4a4da 100644 (file)
@@ -1,25 +1,27 @@
 #include "board.h"
 #include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
 
 typedef struct {
        Tile **tiles;
        Point2i dimensions;
 } Board_t;
 
-Board *board_init(int sizex, int sizey)
+Board *board_init(Point2i size)
 {
        Board *ret = NULL;
        Board_t *b = malloc(sizeof(Board_t));
-       int sizet = sizex * sizey;
+       int sizet = size.x * size.y;
 
-       b->dimensions.x = sizex;
-       b->dimensions.y = sizey;
+       b->dimensions.x = size.x;
+       b->dimensions.y = size.y;
        b->tiles = malloc(sizet * sizeof(Tile));
 
-       for(int x = 0; x < sizex; ++x){
-               for(int y = 0; y < sizey; ++y){
+       for(int x = 0; x < size.x; ++x){
+               for(int y = 0; y < size.y; ++y){
 
-                       b->tiles[y * sizex + x] =
+                       b->tiles[y * size.x + x] =
                        tile_init(point3i(x,y,1), PLAINS);
                }
        }
@@ -44,13 +46,54 @@ void board_cleanup(Board *b)
        free(bt->tiles);
 }
 
+bool is_point_valid(Point2i d, Point2i p)
+{
+       return !(p.x > d.x || p.x < 0 ||
+                        p.y > d.y || p.y < 0 );
+}
+
 Tile* board_tile_at(Board* b, Point2i point)
 {
        Board_t *bt = (Board_t *) b;
-       if (point.x > bt->dimensions.x || point.x < 0 ||
-               point.y > bt->dimensions.y || point.y < 0 ){
+
+       if(is_point_valid(bt->dimensions, point)){
+               return bt->tiles[point.x + (point.y * bt->dimensions.y)];
+       } else {
                return NULL;
        }
+}
+
+enum Terrain board_terrain_at(Board* b, Point2i point)
+{
+       Board_t *bt = (Board_t *) b;
+
+       if(is_point_valid(bt->dimensions, point)){
+               return tile_terrain(bt->tiles[point.x + (point.y * bt->dimensions.y)]);
+       } else {
+               return IMPASSABLE;
+       }
+}
+
+int board_elevation_at(Board* b, Point2i point)
+{
+       Board_t *bt = (Board_t *) b;
+       if(b == NULL)
+               return -1;
+
+       if(is_point_valid(bt->dimensions, point)){
+               return tile_position(bt->tiles[point.x+(point.y*bt->dimensions.y)]).z;
+       } else { 
+               return -1;
+       }
+}
 
-       return bt->tiles[point.x + (point.y * bt->dimensions.y)];
+Point2i board_get_size(Board* b, const char *why)
+{
+       Board_t *bt = (Board_t *) b;
+       if(b == NULL){
+               fprintf(stderr, "error getting board size to %s, board is NULL!", why);
+               return point2i(0,0);
+       } else {
+               return bt->dimensions;
+       }
 }
index 9da2346..81d01ff 100644 (file)
@@ -5,7 +5,10 @@
 
 typedef struct {} Board;
 
-Board *board_init(int sizex, int sizey);
+Board *board_init(Point2i size);
 void board_cleanup(Board* b);
-Tile* board_tile_at(Board* b, Point2i point);
+Point2i board_get_size(Board* b, const char *why);
+
+enum Terrain board_terrain_at(Board* b, Point2i point);
+int board_elevation_at(Board* b, Point2i point);
 #endif//BOARD_H
index 902f4d9..7ff0e3e 100644 (file)
@@ -3,6 +3,7 @@
 #include <SDL2/SDL.h>
 #include "board.h"
 #include "point.h"
+#include "ncurses.h"
 
 static Board* board = NULL;
 
@@ -13,11 +14,7 @@ bool init()
                return false;
        }
 
-       board = board_init(5,3);
-       if(board == NULL){
-               printf("ERROR: board null!\n");
-               return false;
-       }
+       //initscr();
        return true;
 }
 
@@ -26,45 +23,68 @@ int main()
        if(!init())
                return -1;
 
-       char *str = malloc(10);
-       char *rot = malloc(4);
+       board = board_init(point2i(5,3));
+       if(board == NULL){
+               fprintf(stderr, "board null after init!\n");
+               return -1;
+       }
+
        Uint32 itime = SDL_GetTicks();
        Uint32 ctime = 0;
-       int i = 0;
        Point2i loc;
-       Tile* sel = NULL;
+       Point2i bsize = board_get_size(board, "storing size");
+       char *terrain = malloc(bsize.x * bsize.y);
+       char *elevation = malloc(bsize.x * bsize.y);
+       char **sel = &terrain;
+       Uint32 cclock = SDL_GetTicks();
+       Uint32 pclock = 0;
 
-       for(int y = 0; y < 3; ++y){
-               for(int x = 0; x < 5; ++x){
-                       loc.x = x;
-                       loc.y = y;
-                       sel = board_tile_at(board, loc);
-                       if(sel == NULL){
-                               printf("X");
+       for(int y = 0; y < bsize.y; ++y){
+               for(int x = 0; x < bsize.x; ++x){
+                       loc = point2i(x,y);
+                       elevation[y * bsize.x + x] = board_elevation_at(board, loc) + '0';
+                       //if(elevation[y * bsize.x + x] != 1){
+                       if(board_elevation_at(board, loc) != 1){
+                               printf("elevation error at (%d,%d)\n", loc.x, loc.y);
+                       }
+                       if(board_terrain_at(board, loc) == IMPASSABLE){
+                               terrain[y * bsize.x + x] = 'X';
                        } else {
-                               printf("#");
+                               terrain[y * bsize.x + x] = '.';
                        }
                }
-               printf("\n");
        }
+       printf("\n");
 
-       strcpy(str, "Hello... |");
-       strcpy(rot, "|/-\\");
-       printf("%s", str);
+       // 'game' loop, only stays open for 2 seconds
+       while(ctime < itime + 2000){
+               // checking if the display should change
+               if(cclock - pclock > 200){
+                       if(sel == &terrain){
+                               sel = &elevation;
+                       } else {
+                               sel = &terrain;
+                       }
+                       pclock = cclock;
 
-       while(itime < ctime + 2000){
-               str[9] = rot[i];
-               printf("\r%s", str);
+                       printf("elevation? %s", (sel == &elevation) ? "true\n" : "false\n");
 
-               ++i;
-               if(i > 3) i = 0;
-               itime = SDL_GetTicks();
+                       for(int y = 0; y < bsize.y; ++y){
+                               for(int x = 0; x < bsize.x; ++x){
+                                       printf("%c", (*sel)[y * bsize.x + x]);
+                               }
+                               printf("\n");
+                       }
+               }
+
+               ctime = SDL_GetTicks();
+               cclock = SDL_GetTicks();
        }
-       printf("\n");
 
-       free(str);
-       free(rot);
+       free(terrain);
+       free(elevation);
        SDL_Quit();
+       //endwin();
 
        return 0;
 }