Toroidal mapping now works properly.
This commit is contained in:
parent
fada7a7af3
commit
d788232e72
1
game.c
1
game.c
@ -127,7 +127,6 @@ void endGame()
|
|||||||
// free stuff
|
// free stuff
|
||||||
endwin();
|
endwin();
|
||||||
destroyGrid(grid);
|
destroyGrid(grid);
|
||||||
printf("game destroyed\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// locally used
|
// locally used
|
||||||
|
18
grid.c
18
grid.c
@ -24,10 +24,25 @@ void randomizeGrid(Grid* grid)
|
|||||||
grid->state[i] = rand()%2;
|
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
|
// maps x, y coordinate to array index of grid
|
||||||
unsigned int toIndex(Grid* grid, int x, int y)
|
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)
|
bool getPixel(Grid* grid, int x, int y)
|
||||||
@ -84,7 +99,6 @@ void destroyGrid(Grid* grid)
|
|||||||
free(grid->state);
|
free(grid->state);
|
||||||
free(grid->next_state);
|
free(grid->next_state);
|
||||||
free(grid);
|
free(grid);
|
||||||
printf("Grid destroyed\n");
|
|
||||||
}
|
}
|
||||||
// locally used
|
// locally used
|
||||||
// check if cell's next state is alive
|
// check if cell's next state is alive
|
||||||
|
3
main.c
3
main.c
@ -7,6 +7,7 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
// game
|
// game
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@ -15,6 +16,7 @@ int main()
|
|||||||
// framerate of the game
|
// framerate of the game
|
||||||
const int FRAME_RATE = 30;
|
const int FRAME_RATE = 30;
|
||||||
const float FRAME_TIME = 1.f/(float)FRAME_RATE;
|
const float FRAME_TIME = 1.f/(float)FRAME_RATE;
|
||||||
|
startLog("log.txt");
|
||||||
|
|
||||||
initGame();
|
initGame();
|
||||||
|
|
||||||
@ -39,6 +41,7 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
endGame();
|
endGame();
|
||||||
|
endLog();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user