Toroidal mapping now works properly.

This commit is contained in:
Sheldon Lee 2020-07-01 02:01:52 +01:00
parent fada7a7af3
commit d788232e72
3 changed files with 19 additions and 3 deletions

1
game.c
View File

@ -127,7 +127,6 @@ void endGame()
// free stuff
endwin();
destroyGrid(grid);
printf("game destroyed\n");
}
// locally used

18
grid.c
View File

@ -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

3
main.c
View File

@ -7,6 +7,7 @@
#include <math.h>
// 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;
}