2020-06-12 09:46:44 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ncurses.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <math.h>
|
2020-06-18 08:44:21 +08:00
|
|
|
#include <time.h>
|
2020-06-12 09:46:44 +08:00
|
|
|
#include "grid.h"
|
|
|
|
#include "vect.h"
|
|
|
|
|
2020-06-22 07:58:05 +08:00
|
|
|
static bool running = true;
|
2020-06-23 09:34:29 +08:00
|
|
|
static bool do_step = true;
|
|
|
|
static Vect2i cursor = {0, 0};
|
2020-06-22 07:58:05 +08:00
|
|
|
|
|
|
|
void handleInput(char ch);
|
|
|
|
|
|
|
|
void showLastPressed(char ch);
|
|
|
|
|
2020-06-23 09:34:29 +08:00
|
|
|
void showCurPos();
|
|
|
|
|
2020-06-12 09:46:44 +08:00
|
|
|
int main()
|
|
|
|
{
|
2020-06-18 08:44:21 +08:00
|
|
|
// init
|
2020-06-12 09:46:44 +08:00
|
|
|
initscr();
|
2020-06-18 08:44:21 +08:00
|
|
|
raw();
|
2020-06-23 09:34:29 +08:00
|
|
|
noecho();
|
2020-06-22 07:58:05 +08:00
|
|
|
|
|
|
|
// Colors
|
2020-06-19 05:26:14 +08:00
|
|
|
// 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();
|
2020-06-22 07:58:05 +08:00
|
|
|
// cell
|
|
|
|
init_pair(1, COLOR_BLUE, COLOR_WHITE);
|
|
|
|
// text
|
|
|
|
init_pair(2, COLOR_YELLOW, -1);
|
2020-06-23 09:34:29 +08:00
|
|
|
// cursor
|
|
|
|
init_pair(3, COLOR_RED, COLOR_RED);
|
2020-06-22 07:58:05 +08:00
|
|
|
|
2020-06-18 08:44:21 +08:00
|
|
|
// doesn't wait for user to input.
|
|
|
|
// timeout(100) waits for 100ms for input.
|
|
|
|
timeout(0);
|
2020-06-12 09:46:44 +08:00
|
|
|
curs_set(FALSE);
|
2020-06-18 08:44:21 +08:00
|
|
|
|
2020-06-12 09:46:44 +08:00
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
|
|
|
|
2020-06-18 08:44:21 +08:00
|
|
|
// stdscr is screen created by initscr()
|
2020-06-12 09:46:44 +08:00
|
|
|
getmaxyx(stdscr, height, width);
|
|
|
|
|
2020-06-19 05:26:14 +08:00
|
|
|
// framerate of the game
|
2020-06-23 09:34:29 +08:00
|
|
|
const int FRAME_RATE = 30;
|
2020-06-22 07:58:05 +08:00
|
|
|
const float FRAME_TIME = 1.f/(float)FRAME_RATE;
|
2020-06-12 09:46:44 +08:00
|
|
|
|
|
|
|
Grid grid;
|
|
|
|
initGrid(&grid, width, height);
|
2020-06-15 08:12:14 +08:00
|
|
|
randomizeGrid(&grid);
|
2020-06-12 09:46:44 +08:00
|
|
|
|
2020-06-23 09:34:29 +08:00
|
|
|
|
2020-06-22 07:58:05 +08:00
|
|
|
float t = 0;
|
2020-06-13 05:50:03 +08:00
|
|
|
while (running) {
|
2020-06-18 08:44:21 +08:00
|
|
|
clock_t start_t = clock();
|
|
|
|
char ch = getch();
|
|
|
|
|
2020-06-22 07:58:05 +08:00
|
|
|
handleInput(ch);
|
|
|
|
|
|
|
|
// draw grid
|
|
|
|
drawGrid(&grid);
|
|
|
|
// draw overlays
|
|
|
|
showLastPressed(ch);
|
2020-06-23 09:34:29 +08:00
|
|
|
showCurPos();
|
|
|
|
// cursor
|
|
|
|
attron(COLOR_PAIR(3));
|
|
|
|
mvaddch(cursor.y, cursor.x, ' ');
|
|
|
|
attroff(COLOR_PAIR(3));
|
|
|
|
|
2020-06-12 09:46:44 +08:00
|
|
|
|
2020-06-22 07:58:05 +08:00
|
|
|
refresh();
|
2020-06-23 09:34:29 +08:00
|
|
|
if (do_step) updateGrid(&grid);
|
2020-06-12 09:46:44 +08:00
|
|
|
|
2020-06-22 07:58:05 +08:00
|
|
|
usleep(pow(10,6)*(FRAME_TIME-t));
|
|
|
|
float t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC;
|
2020-06-12 09:46:44 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
endwin();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-22 07:58:05 +08:00
|
|
|
void showLastPressed(char ch)
|
|
|
|
{
|
|
|
|
static char lastc = ' ';
|
|
|
|
if (ch != -1) lastc = ch;
|
2020-06-23 09:34:29 +08:00
|
|
|
attron(COLOR_PAIR(2));
|
|
|
|
mvprintw(0, 0, "Last Pressed: %c", lastc);
|
|
|
|
attroff(COLOR_PAIR(2));
|
2020-06-22 07:58:05 +08:00
|
|
|
}
|
2020-06-23 09:34:29 +08:00
|
|
|
|
|
|
|
void showCurPos()
|
|
|
|
{
|
2020-06-23 09:36:44 +08:00
|
|
|
attron(COLOR_PAIR(2));
|
2020-06-23 09:34:29 +08:00
|
|
|
mvprintw(1, 0, "curpos: %i, %i", cursor.x, cursor.y);
|
2020-06-23 09:36:44 +08:00
|
|
|
attroff(COLOR_PAIR(2));
|
2020-06-23 09:34:29 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 07:58:05 +08:00
|
|
|
void handleInput(char ch)
|
|
|
|
{
|
2020-06-23 09:34:29 +08:00
|
|
|
switch (ch) {
|
|
|
|
case 'q':
|
|
|
|
running = false;
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
do_step ^= 1;
|
|
|
|
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-22 07:58:05 +08:00
|
|
|
}
|