Doesn't work segfaults.

This commit is contained in:
Sheldon Lee 2020-06-24 04:05:49 +01:00
parent f4dc092e26
commit 1ad1660736
8 changed files with 74 additions and 54 deletions

76
game.c
View File

@ -1,15 +1,39 @@
#include <ncurses.h>
#include "game.h" #include "game.h"
#include "grid.h" #include "grid.h"
#include "vect.h" #include "vect.h"
static Grid grid; static Grid* grid = 0;
static bool running = true; static bool running = true;
static bool do_step = true; static bool do_step = true;
static Vect2i cursor = {0, 0}; static Vect2i cursor;
void initGame() void initGame()
{ {
int width = 0;
int height = 0;
// stdscr is screen created by initscr()
getmaxyx(stdscr, height, width);
initGrid(grid, height, width);
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);
}
void drawGame()
{
if (!grid) return;
drawGrid(grid);
refresh();
} }
bool isRunning() { return running; } bool isRunning() { return running; }
@ -17,27 +41,39 @@ bool isRunning() { return running; }
void handleInput(char ch) void handleInput(char ch)
{ {
switch (ch) { switch (ch) {
case 'q': case 'q':
running = false; running = false;
break; break;
case ' ': case ' ':
do_step ^= 1; do_step ^= 1;
break; break;
case 'h': case 'h':
moveVect2i(&cursor, -1, 0); moveVect2i(&cursor, -1, 0);
break; break;
case 'j': case 'j':
moveVect2i(&cursor, 0, 1); moveVect2i(&cursor, 0, 1);
break; break;
case 'k': case 'k':
moveVect2i(&cursor, 0, -1); moveVect2i(&cursor, 0, -1);
break; break;
case 'l': case 'l':
moveVect2i(&cursor, 1, 0); moveVect2i(&cursor, 1, 0);
break; break;
case 's':
step();
break;
} }
} }
void showLastPressed(char ch)
{
static char lastc = ' ';
if (ch != -1) lastc = ch;
attron(COLOR_PAIR(2));
mvprintw(0, 0, "Last Pressed: %c", lastc);
attroff(COLOR_PAIR(2));
}
void showCurPos() void showCurPos()
{ {
attron(COLOR_PAIR(2)); attron(COLOR_PAIR(2));

9
game.h
View File

@ -7,12 +7,19 @@ void initGame();
bool isRunning(); bool isRunning();
void updateGame();
void drawGame();
void handleInput(char ch); void handleInput(char ch);
// Overlay // Overlays
// Draws cursor and cursor position // Draws cursor and cursor position
void showLastPressed(char ch);
void showCurPos(); void showCurPos();
bool endgame(); bool endgame();
#endif #endif

2
grid.c
View File

@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <ncurses.h>
#include "grid.h" #include "grid.h"
void initGrid(Grid* grid, unsigned int width, unsigned int height) void initGrid(Grid* grid, unsigned int width, unsigned int height)
@ -108,3 +109,4 @@ void drawGrid(Grid* grid)
} }
} }
} }

2
grid.h
View File

@ -1,5 +1,4 @@
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
#ifndef GRID_H #ifndef GRID_H
#define GRID_H #define GRID_H
@ -28,3 +27,4 @@ void putPixel(Grid* grid, int x, int y);
void drawGrid(Grid* grid); void drawGrid(Grid* grid);
#endif #endif

36
main.c
View File

@ -4,22 +4,18 @@
#include <time.h> #include <time.h>
// math // math
#include <math.h> #include <math.h>
// game // game
#include "game.h" #include "game.h"
#include "grid.h" #include "grid.h"
#include "vect.h" #include "vect.h"
void showLastPressed(char ch);
int main() int main()
{ {
// init // init
initscr(); initscr();
raw(); raw();
noecho(); noecho();
// Colors // Colors
// allows for transparancy when color values in init_pair(); are set to -1 or, no pair specified // 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 // e.g init_pair(1, COLOR_WHITE, -1) would be transparent background but white text
@ -31,26 +27,16 @@ int main()
init_pair(2, COLOR_YELLOW, -1); init_pair(2, COLOR_YELLOW, -1);
// cursor // cursor
init_pair(3, COLOR_RED, COLOR_RED); init_pair(3, COLOR_RED, COLOR_RED);
// doesn't wait for user to input. // doesn't wait for user to input.
// timeout(100) waits for 100ms for input. // timeout(100) waits for 100ms for input.
timeout(0); timeout(0);
curs_set(FALSE); curs_set(FALSE);
int width = 0;
int height = 0;
// stdscr is screen created by initscr()
getmaxyx(stdscr, height, width);
// 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;
Grid grid; //initGame();
initGrid(&grid, width, height);
randomizeGrid(&grid);
float t = 0; float t = 0;
while (isRunning()) { while (isRunning()) {
@ -59,14 +45,9 @@ int main()
handleInput(ch); handleInput(ch);
// draw grid //drawGame();
drawGrid(&grid);
// draw overlays //updateGame();
showLastPressed(ch);
showCurPos();
refresh();
if (true) updateGrid(&grid);
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;
@ -77,12 +58,3 @@ int main()
return 0; return 0;
} }
void showLastPressed(char ch)
{
static char lastc = ' ';
if (ch != -1) lastc = ch;
attron(COLOR_PAIR(2));
mvprintw(0, 0, "Last Pressed: %c", lastc);
attroff(COLOR_PAIR(2));
}

View File

@ -2,3 +2,4 @@ LDFLAGS=-lncurses
main: *.c main: *.c
gcc *.c -o main ${LDFLAGS} gcc *.c -o main ${LDFLAGS}

1
vect.c
View File

@ -12,3 +12,4 @@ void moveVect2f(Vect2f* vect, float x, float y)
vect->x += x; vect->x += x;
vect->y += y; vect->y += y;
} }

1
vect.h
View File

@ -14,3 +14,4 @@ void moveVect2i(Vect2i* vect, int x, int y);
void moveVect2f(Vect2f* vect, float x, float y); void moveVect2f(Vect2f* vect, float x, float y);
#endif #endif