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

53
game.c
View File

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

86
grid.c
View File

@ -3,12 +3,17 @@
#include <ncurses.h> #include <ncurses.h>
#include "grid.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->size = width*height;
grid->width = width; grid->width = width;
grid->state = (bool*)malloc(sizeof(bool)*grid->size); grid->state = (bool*)malloc(sizeof(bool)*grid->size);
grid->next_state = (bool*)malloc(sizeof(bool)*grid->size); grid->next_state = (bool*)malloc(sizeof(bool)*grid->size);
return grid;
} }
void randomizeGrid(Grid* 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; 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 // check if cell's next state is alive
static bool isAliveNext(Grid* grid, int x, int y) static bool isAliveNext(Grid* grid, int x, int y)
{ {
@ -71,42 +116,3 @@ static bool isAliveNext(Grid* grid, int x, int y)
return false; 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; bool* next_state;
} Grid; } Grid;
void initGrid(Grid* grid, unsigned int width, unsigned int height); Grid* initGrid(unsigned int width, unsigned int height);
void randomizeGrid(Grid* grid); void randomizeGrid(Grid* grid);
@ -22,7 +22,7 @@ void clearGrid(Grid* grid);
void updateGrid(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); void drawGrid(Grid* grid);

10
main.c
View File

@ -36,7 +36,7 @@ int main()
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;
//initGame(); initGame();
float t = 0; float t = 0;
while (isRunning()) { while (isRunning()) {
@ -45,9 +45,13 @@ int main()
handleInput(ch); handleInput(ch);
//drawGame(); drawGame();
drawLastPressed(ch);
drawCurPos();
refresh();
//updateGame(); updateGame();
usleep(pow(10,6)*(FRAME_TIME-t)); usleep(pow(10,6)*(FRAME_TIME-t));
float t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC; float t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC;