From d788232e723929f352646d7416c2872a1b305f83 Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Wed, 1 Jul 2020 02:01:52 +0100 Subject: [PATCH] Toroidal mapping now works properly. --- game.c | 1 - grid.c | 18 ++++++++++++++++-- main.c | 3 +++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/game.c b/game.c index 3985e40..459651b 100644 --- a/game.c +++ b/game.c @@ -127,7 +127,6 @@ void endGame() // free stuff endwin(); destroyGrid(grid); - printf("game destroyed\n"); } // locally used diff --git a/grid.c b/grid.c index ad8c8fc..b65c417 100644 --- a/grid.c +++ b/grid.c @@ -24,10 +24,25 @@ void randomizeGrid(Grid* grid) grid->state[i] = rand()%2; } } + +static void contain(int* num, int min, int max) +{ + int delta = max - min; + while (true) { + if (*num < min) *num += delta; + else if (*num >= max) *num -= delta; + else break; + } +} // maps x, y coordinate to array index of grid unsigned int toIndex(Grid* grid, int x, int y) { - return (grid->width*y + x)%grid->size; + unsigned int width, height; + width = grid->width; + height = grid->size/width; + contain(&x, 0, width); + contain(&y, 0, height); + return (grid->width*y + x); } bool getPixel(Grid* grid, int x, int y) @@ -84,7 +99,6 @@ void destroyGrid(Grid* grid) free(grid->state); free(grid->next_state); free(grid); - printf("Grid destroyed\n"); } // locally used // check if cell's next state is alive diff --git a/main.c b/main.c index 33b5f1f..5055e84 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,7 @@ #include // game #include "game.h" +#include "log.h" int main() @@ -15,6 +16,7 @@ int main() // framerate of the game const int FRAME_RATE = 30; const float FRAME_TIME = 1.f/(float)FRAME_RATE; + startLog("log.txt"); initGame(); @@ -39,6 +41,7 @@ int main() } endGame(); + endLog(); return 0; }