From 8cf6ad99e48743352100b499963fc68232271d6f Mon Sep 17 00:00:00 2001 From: matthew Date: Tue, 31 Jul 2018 20:27:18 +0100 Subject: [PATCH] array headaches --- common/board.c | 20 +++++++++++++------- common/board.h | 1 + common/tile.c | 11 +++++++++++ common/tile.h | 11 ++++++++--- nogl/main.c | 48 ++---------------------------------------------- 5 files changed, 35 insertions(+), 56 deletions(-) diff --git a/common/board.c b/common/board.c index 6c4a4da..31c178d 100644 --- a/common/board.c +++ b/common/board.c @@ -8,22 +8,28 @@ typedef struct { Point2i dimensions; } Board_t; +int flatten(Point2i coord, Point2i size) +{ + return coord.x + (size.x * coord.y); +} + Board *board_init(Point2i size) { Board *ret = NULL; Board_t *b = malloc(sizeof(Board_t)); - int sizet = size.x * size.y; + int itr; b->dimensions.x = size.x; b->dimensions.y = size.y; - b->tiles = malloc(sizet * sizeof(Tile)); - - for(int x = 0; x < size.x; ++x){ - for(int y = 0; y < size.y; ++y){ + b->tiles = malloc(size.x * size.y * sizeof(Tile)); - b->tiles[y * size.x + x] = - tile_init(point3i(x,y,1), PLAINS); + for(int y = 0; y < size.y; ++y){ + for(int x = 0; x < size.x; ++x){ + itr = flatten(point2i(x, y), size); + printf("%d\t", itr); + b->tiles[itr] = tile_init(point3i(x, y, itr), PLAIN); } + printf("\n"); } ret = (Board *) b; diff --git a/common/board.h b/common/board.h index 81d01ff..73965a9 100644 --- a/common/board.h +++ b/common/board.h @@ -11,4 +11,5 @@ 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); +Tile* board_tile_at(Board* b, Point2i point); #endif//BOARD_H diff --git a/common/tile.c b/common/tile.c index f804c37..4833414 100644 --- a/common/tile.c +++ b/common/tile.c @@ -7,6 +7,8 @@ typedef struct enum Terrain terrain; } Tile_t; +static const char repr[] = {'X', '.', '^', '\'', '~', '#', '*', '|', '0'}; + Tile *tile_init(Point3i pos, enum Terrain ter) { Tile_t *t = malloc(sizeof(Tile_t)); @@ -31,3 +33,12 @@ enum Terrain tile_terrain(Tile *t) Tile_t *tt = (Tile_t *) t; return tt->terrain; } + +char tile_repr(enum Terrain t) +{ + if(t < 0 || t > LAST){ + return '?'; + } else { + return repr[t]; + } +} diff --git a/common/tile.h b/common/tile.h index aee4a81..943c9be 100644 --- a/common/tile.h +++ b/common/tile.h @@ -3,20 +3,25 @@ #include "point.h" enum Terrain{ - IMPASSABLE = -1, - PLAINS, + IMPASSABLE = 0, + PLAIN, MOUNTAIN, S_WATER, D_WATER, WALL, FLOOR, - ROAD + ROAD, + LAST }; + typedef struct {} Tile; Tile *tile_init(Point3i pos, enum Terrain ter); void tile_cleanup(Tile *t); Point3i tile_position(Tile *t); enum Terrain tile_terrain(Tile *t); + +// TODO: move to nogl output c file +char tile_repr(enum Terrain t); #endif//TILE_H diff --git a/nogl/main.c b/nogl/main.c index 550c96a..cd15d73 100644 --- a/nogl/main.c +++ b/nogl/main.c @@ -33,58 +33,14 @@ int main() Uint32 ctime = 0; Point2i loc; 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 < bsize.y; ++y){ - for(int x = 0; x < bsize.x; ++x){ - loc = point2i(x,y); - //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); - elevation[y * bsize.x + x] = '?'; - } else { - elevation[y * bsize.x + x] = board_elevation_at(board, loc) + '0'; - } - if(board_terrain_at(board, loc) == IMPASSABLE){ - terrain[y * bsize.x + x] = 'X'; - } else { - terrain[y * bsize.x + x] = '.'; - } - } - } - printf("\n"); - - // '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; - - printf("elevation? %s", (sel == &elevation) ? "true\n" : "false\n"); - - 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"); - } - } - + // 'game' loop, only stays open for .5 seconds + while(ctime < itime + 500){ ctime = SDL_GetTicks(); - cclock = SDL_GetTicks(); } - free(terrain); - free(elevation); SDL_Quit(); //endwin(); -- 2.20.1