General refactor and minor improvements

This commit is contained in:
2025-10-18 18:56:28 +02:00
parent 103f85d07e
commit 6b0e858064
13 changed files with 616 additions and 466 deletions

View File

@@ -10,8 +10,6 @@
#include <ncurses.h>
#include <stdlib.h>
#include "rooster.h"
int same_coordinate(const coordinate a, const coordinate b) {
return a.x == b.x && a.y == b.y;
}
@@ -24,23 +22,23 @@ int modulo(const int number, const int mod) {
return result;
}
void show_grid_on_offset(const rooster *gp, const int starting_x, const int starting_y) {
const int height = rooster_hoogte(gp);
void show_grid_on_offset(const grid *gp, const int starting_x, const int starting_y) {
const int height = grid_height(gp);
for (int y = 0; y < height; y++) {
char *rij = rooster_vraag_rij(gp, y);
char *rij = grid_fetch_row(gp, y);
mvprintw(starting_y + y, starting_x, "%s", rij);
free(rij);
}
refresh();
}
void show_grid(const rooster *gp) {
void show_grid(const grid *gp) {
show_grid_on_offset(gp, 0, 0);
}
void update_grid(rooster *gp, const char c, const int x, const int y) {
if (rooster_plaats(gp, x, y, c) == 1) {
void update_grid(grid *gp, const char c, const int x, const int y) {
if (grid_put(gp, x, y, c) == 1) {
mvaddch(y, x, c);
}
}
@@ -52,9 +50,7 @@ void update_grid(rooster *gp, const char c, const int x, const int y) {
* Clears the console and prints the victory message.
*/
static void display_victory(const coordinate location) {
erase();
mvprintw(location.y, location.x, "YOU WON!!!!!");
refresh();
mvaddstr(location.y, location.x, "YOU WON!!!!!");
}
/*
@@ -64,9 +60,7 @@ static void display_victory(const coordinate location) {
* Clears the console and prints the GAME OVER message.
*/
static void display_loss(const coordinate location) {
erase();
mvprintw(location.y, location.x, "GAME OVER");
refresh();
mvaddstr(location.y, location.x, "GAME OVER");
}
/*
@@ -76,9 +70,7 @@ static void display_loss(const coordinate location) {
* Clears the console and prints the quit message.
*/
static void display_quit(const coordinate location) {
erase();
mvprintw(location.y, location.x, "You quit the game");
refresh();
mvaddstr(location.y, location.x, "You quit the game");
}
/*
@@ -88,15 +80,15 @@ static void display_quit(const coordinate location) {
* Clears the console and prints the hacker man message.
*/
static void display_hackerman(const coordinate location) {
erase();
mvprintw(location.y, location.x, "The hacker man strikes again...");
refresh();
mvaddstr(location.y, location.x, "The hacker man strikes again...");
}
void graceful_exit(const coordinate message_location) {
mvprintw(message_location.y, message_location.x, "Press ENTER or SPACE to exit.");
mvaddstr(message_location.y, message_location.x, "Press ENTER or SPACE to exit.");
while (1) {
switch (getch()) {
case KEY_BACKSPACE:
case KEY_ESCAPE:
case KEY_ENTER:
case '\n':
case ' ':
@@ -105,8 +97,8 @@ void graceful_exit(const coordinate message_location) {
}
}
void game_exit_screen(const rooster *gp, coordinate location) {
const toestand current_state = rooster_vraag_toestand(gp);
void game_exit_message(const grid *gp, coordinate location) {
const state current_state = grid_fetch_state(gp);
switch (current_state) {
case STATE_GEWONNEN:
display_victory(location);
@@ -123,9 +115,24 @@ void game_exit_screen(const rooster *gp, coordinate location) {
location.y += 2;
graceful_exit(location);
refresh();
}
static void init_ncurses() {
void enable_highlight(const game_colors color) {
attron(COLOR_PAIR(color));
}
void disable_highlight(const game_colors color) {
attroff(COLOR_PAIR(color));
}
/*
* Initialize ncurses.
*
* This enables cbreak, noecho, hides the cursor and enables the extra keys.
* This also creates the color pairs needed for game_colors and sets ESCDELAY to 0.
*/
static void init_ncurses(void) {
ESCDELAY = 0;
setlocale(LC_ALL, "");
initscr();
@@ -134,9 +141,6 @@ static void init_ncurses() {
noecho(); // Don't write the keyboard input to the console.
curs_set(0); // Hides the cursor.
// mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL); // Don't mask any mouse events
// printf("\033[?1003h\n"); // Makes the terminal report mouse movement events
start_color();
init_pair(BLACK, COLOR_BLACK, COLOR_BLACK);
init_pair(WHITE, COLOR_BLACK, COLOR_WHITE);
@@ -150,14 +154,10 @@ static void init_ncurses() {
clear();
}
static void cleanup_ncurses() {
endwin();
}
void init_engine(void) {
init_ncurses();
}
void cleanup_engine(void) {
cleanup_ncurses();
endwin();
}