Files
minigame-menu/grid_game_engine.c

154 lines
3.4 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 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 *rp) {
show_grid_on_offset(rp, 0, 0);
}
void update_grid(rooster *rp, char c, int x, int y) {
if (rooster_plaats(rp, 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(void) {
clear();
mvprintw(2,5, "YOU WON!!!!!");
refresh();
}
/*
* Shows the GAME OVER screen.
*
* Side Effect:
* Clears the console and prints the GAME OVER message.
*/
static void display_loss(void) {
clear();
mvprintw(2,5, "GAME OVER");
refresh();
}
/*
* Shows the quit screen
*
* Side Effect:
* Clears the console and prints the quit message.
*/
static void display_quit(void) {
clear();
mvprintw(2,5, "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(void) {
clear();
mvprintw(2,5, "The hacker man strikes again...");
refresh();
}
void graceful_exit(void) {
mvprintw(5, 5, "Press 'q' to exit.");
while (1) {
switch (getch()) {
case 'q':
case 'Q':
return;
}
}
}
void game_exit_screen(rooster *gp) {
toestand current_state = rooster_vraag_toestand(gp);
switch (current_state) {
case STATE_GEWONNEN:
display_victory();
break;
case STATE_VERLOREN:
display_loss();
break;
case STATE_QUIT:
display_quit();
break;
default:
display_hackerman();
}
graceful_exit();
}
static void init_ncurses() {
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);
}
static void cleanup_ncurses() {
endwin();
}
void init_engine(void) {
init_ncurses();
}
void cleanup_engine(void) {
cleanup_ncurses();
}