Files
minigame-menu/grid_game_engine.c

164 lines
3.9 KiB
C

/*
* Created by snapshot112 on 10/15/25.
*
* A game engine that can run and display games in square grids.
*/
#include "grid_game_engine.h"
#include <locale.h>
#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;
}
int modulo(const int number, const int mod) {
int result = number % mod;
if (result < 0) {
result += mod; //This is not how math is supposed to work C, damnit.
}
return result;
}
void show_grid_on_offset(const rooster *gp, const int starting_x, const int starting_y) {
const int height = rooster_hoogte(gp);
for (int y = 0; y < height; y++) {
char *rij = rooster_vraag_rij(gp, y);
mvprintw(starting_y + y, starting_x, "%s", rij);
free(rij);
}
refresh();
}
void show_grid(const rooster *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) {
mvaddch(y, x, c);
}
}
/*
* Shows the victory screen.
*
* Side Effect:
* Clears the console and prints the victory message.
*/
static void display_victory(const coordinate location) {
erase();
mvprintw(location.y, location.x, "YOU WON!!!!!");
refresh();
}
/*
* Shows the GAME OVER screen.
*
* Side Effect:
* 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();
}
/*
* Shows the quit screen
*
* Side Effect:
* 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();
}
/*
* Shows the hacker man screen
*
* Side Effect:
* 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();
}
void graceful_exit(const coordinate message_location) {
mvprintw(message_location.y, message_location.x, "Press ENTER or SPACE to exit.");
while (1) {
switch (getch()) {
case KEY_ENTER:
case '\n':
case ' ':
return;
}
}
}
void game_exit_screen(const rooster *gp, coordinate location) {
const toestand current_state = rooster_vraag_toestand(gp);
switch (current_state) {
case STATE_GEWONNEN:
display_victory(location);
break;
case STATE_VERLOREN:
display_loss(location);
break;
case STATE_QUIT:
display_quit(location);
break;
default:
display_hackerman(location);
}
location.y += 2;
graceful_exit(location);
}
static void init_ncurses() {
ESCDELAY = 0;
setlocale(LC_ALL, "");
initscr();
cbreak(); // So you can cancel the game with ctrl + c.
keypad(stdscr, TRUE); // Enable extra keys like the arrow keys.
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);
init_pair(BLUE, COLOR_BLACK, COLOR_BLUE);
init_pair(GREEN, COLOR_BLACK, COLOR_GREEN);
init_pair(CYAN, COLOR_BLACK, COLOR_CYAN);
init_pair(MAGENTA, COLOR_BLACK, COLOR_MAGENTA);
init_pair(YELLOW, COLOR_BLACK, COLOR_YELLOW);
init_pair(RED, COLOR_BLACK, COLOR_RED);
clear();
}
static void cleanup_ncurses() {
endwin();
}
void init_engine(void) {
init_ncurses();
}
void cleanup_engine(void) {
cleanup_ncurses();
}