Migrated handling of grid and game to game.(c/h).

This commit is contained in:
Sheldon Lee 2020-06-24 14:27:24 +01:00
parent 1ad1660736
commit 50eeaa8f0a
5 changed files with 98 additions and 59 deletions

51
game.c
View File

@ -6,9 +6,15 @@
static Grid* grid = 0;
static bool running = true;
static bool stepping_mode = false;
static bool do_step = true;
static Vect2i cursor;
// declaration of locally used
static void toggleStepMode();
static void toggleCell();
void initGame()
{
int width = 0;
@ -16,28 +22,29 @@ void initGame()
// stdscr is screen created by initscr()
getmaxyx(stdscr, height, width);
initGrid(grid, height, width);
grid = initGrid(width, height);
randomizeGrid(grid);
cursor.x = width/2; cursor.y = height/2;
}
static void step() { do_step ^= 1; }
void updateGame()
{
if (!grid) return;
if (do_step) updateGrid(grid);
if (do_step) {
updateGrid(grid);
if (stepping_mode) do_step = false;
}
}
void drawGame()
{
if (!grid) return;
drawGrid(grid);
refresh();
}
bool isRunning() { return running; }
void handleInput(char ch)
{
switch (ch) {
@ -45,7 +52,7 @@ void handleInput(char ch)
running = false;
break;
case ' ':
do_step ^= 1;
toggleStepMode();
break;
case 'h':
moveVect2i(&cursor, -1, 0);
@ -59,13 +66,18 @@ void handleInput(char ch)
case 'l':
moveVect2i(&cursor, 1, 0);
break;
case 's':
step();
case '\n':
do_step = true;
break;
case 'i':
toggleCell();
break;
default:
if (ch != -1) do_step = true;
}
}
void showLastPressed(char ch)
void drawLastPressed(char ch)
{
static char lastc = ' ';
if (ch != -1) lastc = ch;
@ -74,16 +86,33 @@ void showLastPressed(char ch)
attroff(COLOR_PAIR(2));
}
void showCurPos()
void drawCurPos()
{
attron(COLOR_PAIR(2));
mvprintw(1, 0, "curpos: %i, %i", cursor.x, cursor.y);
attroff(COLOR_PAIR(2));
attron(COLOR_PAIR(3));
mvaddch(cursor.y, cursor.x, ' ');
attroff(COLOR_PAIR(3));
attroff(COLOR_PAIR(2));
}
void endGame()
{
// free stuff
}
// locally used
static void toggleStepMode()
{
stepping_mode ^= 1;
do_step = true;
}
static void toggleCell()
{
if (!grid) return;
bool cell = getPixel(grid, cursor.x, cursor.y);
if (cell) setPixel(grid, cursor.x, cursor.y, 0);
else setPixel(grid, cursor.x, cursor.y, 1);
}

4
game.h
View File

@ -15,9 +15,9 @@ void handleInput(char ch);
// Overlays
// Draws cursor and cursor position
void showLastPressed(char ch);
void drawLastPressed(char ch);
void showCurPos();
void drawCurPos();
bool endgame();

86
grid.c
View File

@ -3,12 +3,17 @@
#include <ncurses.h>
#include "grid.h"
void initGrid(Grid* grid, unsigned int width, unsigned int height)
// locally used declaration
static bool isAliveNext(Grid* grid, int x, int y);
Grid* initGrid(unsigned int width, unsigned int height)
{
Grid* grid = (Grid*)malloc(sizeof(Grid));
grid->size = width*height;
grid->width = width;
grid->state = (bool*)malloc(sizeof(bool)*grid->size);
grid->next_state = (bool*)malloc(sizeof(bool)*grid->size);
return grid;
}
void randomizeGrid(Grid* grid)
@ -34,6 +39,46 @@ void clearGrid(Grid* grid)
for (int i = 0; i < grid->size; i++) grid->state[i]=false;
}
void updateGrid(Grid* grid)
{
unsigned int width = grid->width;
unsigned int height = grid->size/width;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
grid->next_state[toIndex(grid, x, y)] = isAliveNext(grid, x, y);
}
}
for (int i = 0; i < grid->size; i++) {
grid->state[i] = grid->next_state[i];
}
}
void setPixel(Grid* grid, int x, int y, bool on)
{
grid->state[toIndex(grid, x, y)] = on;
}
void drawGrid(Grid* grid)
{
unsigned int width, height;
width = grid->width;
height = grid->size/width;
// Init color pair init_pair(index, fg, bg);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (grid->state[toIndex(grid, x, y)]){
attron(COLOR_PAIR(1));
mvaddch(y, x, ' ');
attroff(COLOR_PAIR(1));
}
else mvaddch(y, x, ' ');
}
}
}
// locally used
// check if cell's next state is alive
static bool isAliveNext(Grid* grid, int x, int y)
{
@ -71,42 +116,3 @@ static bool isAliveNext(Grid* grid, int x, int y)
return false;
}
void updateGrid(Grid* grid)
{
unsigned int width = grid->width;
unsigned int height = grid->size/width;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
grid->next_state[toIndex(grid, x, y)] = isAliveNext(grid, x, y);
}
}
for (int i = 0; i < grid->size; i++) {
grid->state[i] = grid->next_state[i];
}
}
void putPixel(Grid* grid, int x, int y)
{
grid->state[toIndex(grid, x, y)] = true;
}
void drawGrid(Grid* grid)
{
unsigned int width, height;
width = grid->width;
height = grid->size/width;
// Init color pair init_pair(index, fg, bg);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (grid->state[toIndex(grid, x, y)]){
attron(COLOR_PAIR(1));
mvaddch(y, x, ' ');
attroff(COLOR_PAIR(1));
}
else mvaddch(y, x, ' ');
}
}
}

4
grid.h
View File

@ -10,7 +10,7 @@ typedef struct Grid {
bool* next_state;
} Grid;
void initGrid(Grid* grid, unsigned int width, unsigned int height);
Grid* initGrid(unsigned int width, unsigned int height);
void randomizeGrid(Grid* grid);
@ -22,7 +22,7 @@ void clearGrid(Grid* grid);
void updateGrid(Grid* grid);
void putPixel(Grid* grid, int x, int y);
void setPixel(Grid* grid, int x, int y, bool on);
void drawGrid(Grid* grid);

10
main.c
View File

@ -36,7 +36,7 @@ int main()
const int FRAME_RATE = 30;
const float FRAME_TIME = 1.f/(float)FRAME_RATE;
//initGame();
initGame();
float t = 0;
while (isRunning()) {
@ -45,9 +45,13 @@ int main()
handleInput(ch);
//drawGame();
drawGame();
drawLastPressed(ch);
drawCurPos();
//updateGame();
refresh();
updateGame();
usleep(pow(10,6)*(FRAME_TIME-t));
float t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC;