ncurses-gameoflife/game.c

170 lines
2.8 KiB
C
Raw Normal View History

2020-06-24 11:05:49 +08:00
#include <ncurses.h>
#include "game.h"
#include "grid.h"
#include "vect.h"
2020-07-04 07:26:34 +08:00
#include "log.h"
2020-06-24 11:05:49 +08:00
static Grid* grid = 0;
static bool running = true;
static bool stepping_mode = false;
static bool do_step = true;
2020-06-24 11:05:49 +08:00
static Vect2i cursor;
// declaration of locally used
static void toggleStepMode();
static void toggleCell();
void initGame()
{
2020-06-27 08:18:06 +08:00
// init
initscr();
raw();
noecho();
// Colors
// allows for transparancy when color values in init_pair(); are set to -1 or, no pair specified
// e.g init_pair(1, COLOR_WHITE, -1) would be transparent background but white text
use_default_colors();
start_color();
// cell
init_pair(1, COLOR_BLUE, COLOR_WHITE);
// text
init_pair(2, COLOR_YELLOW, -1);
// cursor
init_pair(3, COLOR_RED, COLOR_RED);
// doesn't wait for user to input.
// timeout(100) waits for 100ms for input.
timeout(0);
curs_set(FALSE);
2020-06-24 11:05:49 +08:00
int width = 0;
int height = 0;
// stdscr is screen created by initscr()
getmaxyx(stdscr, height, width);
grid = initGrid(width, height);
2020-06-27 08:18:06 +08:00
//randomizeGrid(grid);
2020-06-24 11:05:49 +08:00
cursor.x = width/2; cursor.y = height/2;
}
void updateGame()
{
if (!grid) return;
if (do_step) {
updateGrid(grid);
if (stepping_mode) do_step = false;
}
2020-06-24 11:05:49 +08:00
}
void drawGame()
{
if (!grid) return;
drawGrid(grid);
}
bool isRunning() { return running; }
void handleInput(char ch)
{
switch (ch) {
2020-06-24 11:05:49 +08:00
case 'q':
running = false;
break;
case ' ':
toggleStepMode();
2020-06-24 11:05:49 +08:00
break;
case 'h':
moveVect2i(&cursor, -1, 0);
break;
case 'j':
moveVect2i(&cursor, 0, 1);
break;
case 'k':
moveVect2i(&cursor, 0, -1);
break;
case 'l':
moveVect2i(&cursor, 1, 0);
break;
2020-06-27 08:18:06 +08:00
case 'r':
randomizeGrid(grid);
break;
case 'c':
clearGrid(grid);
break;
case '\n':
do_step = true;
break;
case 'i':
toggleCell();
2020-06-24 11:05:49 +08:00
break;
default:
if (ch != -1) do_step = true;
}
}
void drawLastPressed(char ch)
2020-06-24 11:05:49 +08:00
{
static char lastc = ' ';
if (ch != -1) lastc = ch;
attron(COLOR_PAIR(2));
mvprintw(0, 0, "Last Pressed: %c", lastc);
attroff(COLOR_PAIR(2));
}
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));
}
2020-07-04 07:26:34 +08:00
void saveGame(char* name)
{
unsigned int width, height;
getDimensions(grid, &width, &height);
FILE* file = fopen(name, "w");
if (file) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
fprintf(file, "%i", getPixel(grid, x, y));
}
fprintf(file, "\n");
}
fclose(file);
}
}
void loadGame(char* name)
{
}
void endGame()
{
// free stuff
2020-06-27 08:18:06 +08:00
endwin();
destroyGrid(grid);
}
// 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);
}