implemented rudimentary board
authormatthew <matthew@owens.tech>
Sun, 29 Jul 2018 16:10:05 +0000 (16:10 +0000)
committermatthew <matthew@owens.tech>
Sun, 29 Jul 2018 16:10:05 +0000 (16:10 +0000)
common/board.c [new file with mode: 0644]
common/board.h [new file with mode: 0644]
common/point.c [new file with mode: 0644]
common/point.h
nogl/main.c

diff --git a/common/board.c b/common/board.c
new file mode 100644 (file)
index 0000000..0ca4510
--- /dev/null
@@ -0,0 +1,54 @@
+#include "board.h"
+#include <stdlib.h>
+
+typedef struct {
+       Tile ***tiles;
+       Point2i dimensions;
+} Board_t;
+
+Board *board_init(int sizex, int sizey)
+{
+       Board *ret = NULL;
+       Board_t *b = malloc(sizeof(Board_t));
+       int sizet = sizex * sizey;
+
+       b->dimensions.x = sizex;
+       b->dimensions.y = sizey;
+       b->tiles = malloc(sizet * sizeof(Tile));
+
+       for(int i = 0; i < sizey; ++i){
+               b->tiles[i] = malloc(sizex * sizeof(Tile));
+               for(int j = 0; j < sizex; ++j){
+                       b->tiles[i][j] = tile_init(point3i(i,j,1), PLAINS);
+               }
+       }
+
+       return ret;
+}
+
+void board_cleanup(Board *b)
+{
+       Board_t *bt = (Board_t *) b;
+
+       for(int i = 0; i < bt->dimensions.x; ++i){
+               for(int j = 0; j < bt->dimensions.y; ++j){
+                       tile_cleanup(bt->tiles[i][j]);
+               }
+       }
+
+       for(int j = 0; j < bt->dimensions.y; ++j){
+               free(bt->tiles[j]);
+       }
+       free(bt->tiles);
+}
+
+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 ){
+               return NULL;
+       }
+
+       return bt->tiles[point.x][point.y];
+}
diff --git a/common/board.h b/common/board.h
new file mode 100644 (file)
index 0000000..9da2346
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef BOARD_H
+#define BOARD_H
+#include "point.h"
+#include "tile.h"
+
+typedef struct {} Board;
+
+Board *board_init(int sizex, int sizey);
+void board_cleanup(Board* b);
+Tile* board_tile_at(Board* b, Point2i point);
+#endif//BOARD_H
diff --git a/common/point.c b/common/point.c
new file mode 100644 (file)
index 0000000..0e480e8
--- /dev/null
@@ -0,0 +1,51 @@
+#include "point.h"
+
+Point2i point2i(int x, int y)
+{
+       Point2i p;
+       p.x = x;
+       p.y = y;
+       return p;
+}
+Point3i point3i(int x, int y, int z)
+{
+       Point3i p;
+       p.x = x;
+       p.y = y;
+       p.z = z;
+       return p;
+}
+Point4i point4i(int w, int x, int y, int z)
+{
+       Point4i p;
+       p.w = w;
+       p.x = x;
+       p.y = y;
+       p.z = z;
+       return p;
+}
+
+Point2f point2f(int x, int y)
+{
+       Point2f p;
+       p.x = x;
+       p.y = y;
+       return p;
+}
+Point3f point3f(int x, int y, int z)
+{
+       Point3f p;
+       p.x = x;
+       p.y = y;
+       p.z = z;
+       return p;
+}
+Point4f point4f(int w, int x, int y, int z)
+{
+       Point4f p;
+       p.w = w;
+       p.x = x;
+       p.y = y;
+       p.z = z;
+       return p;
+}
index 87dba3c..a5cb3a8 100644 (file)
@@ -13,4 +13,12 @@ typedef struct { int w, x, y , z; } Point4i;
 typedef struct { float x, y; } Point2f;
 typedef struct { float x, y, z; } Point3f;
 typedef struct { float w, x, y, z; } Point4f;
+
+Point2i point2i(int x, int y);
+Point3i point3i(int x, int y, int z);
+Point4i point4i(int w, int x, int y, int z);
+
+Point2f point2f(float x, float y);
+Point3f point3f(float x, float y, float z);
+Point4f point4f(float w, float x, float y, float z);
 #endif//POINT_H
index 5e62c25..3882ae9 100644 (file)
@@ -1,12 +1,10 @@
 #include <stdio.h>
 #include <stdbool.h>
 #include <SDL2/SDL.h>
-#include "tile.h"
+#include "board.h"
 #include "point.h"
 
-
-static Tile *tile = NULL;
-static Point3i pos;
+static Board* board = NULL;
 
 bool init()
 {
@@ -15,17 +13,9 @@ bool init()
                return false;
        }
 
-       pos.x = 1;
-       pos.y = 2;
-       pos.z = 3;
-
-       tile = tile_init(pos, D_WATER);
-
-       pos.x = pos.y = pos.z = 0;
-       pos = tile_position(tile);
-
-       if(tile == NULL){
-               printf("ERROR: tile null!");
+       board = board_init(5,3);
+       if(board == NULL){
+               printf("ERROR: board null!");
                return false;
        }
        return true;
@@ -41,9 +31,22 @@ int main()
        Uint32 itime = SDL_GetTicks();
        Uint32 ctime = 0;
        int i = 0;
+       Point2i loc;
+       Tile* sel = NULL;
+
+       for(int i = 0; i < 5; ++i){
+               for(int j = 0; j < 3; ++j){
+                       loc.x = i;
+                       loc.y = j;
+                       sel = board_tile_at(board, loc);
+                       if(sel == NULL){
+                               printf("N");
+                       } else {
+                               printf("#");
+                       }
+               }
+       }
 
-       printf("Tile pos - (%d,%d,%d), ter - %d\n",
-                       pos.x, pos.y, pos.z, tile_terrain(tile));
        strcpy(str, "Hello... |");
        strcpy(rot, "|/-\\");
        printf("%s", str);