wip refactoring file structure

This commit is contained in:
2025-10-22 15:07:52 +02:00
parent 4f6bbdd775
commit 40a3668049
55 changed files with 5309 additions and 73 deletions

305
engine/grid/grid.c Normal file
View File

@@ -0,0 +1,305 @@
/*
* Created by snapshot112 on 2/10/2025
*/
#include "grid.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/*
* The grid type this program is build around.
*/
typedef struct grid_data {
char *locations;
int height;
int width;
state state;
} grid;
/*
* Translate x and y coordinates to a location index on the grid.
*/
static int internal_location(const grid *gp, const int x, const int y) {
return y * (gp->width + 1) + x;
}
grid *grid_create_from_string(const char* input) {
int width = 0;
int height = 0;
const size_t len = strlen(input) / sizeof(char);
if (input == NULL || len == 0) {
printf("invalid input\n");
return NULL;
}
for (int i = 0; input[i] != '\n'; i++) {
width++;
}
height = (int)len / (width + 1);
for (int i = 0; i < height; i = i + width + 1) {
if ((int)strcspn(&input[i], "\n") != width) {
printf("line %d was not %d wide\n", i, width);
return NULL;
}
}
const int grid_size = (width + 1) * height + 1;
grid *gp = malloc(sizeof(grid));
if (gp == NULL) {
return NULL;
}
gp->locations = malloc(grid_size * sizeof(char));
gp->height = height;
gp->width = width;
gp->state = STATE_BEGIN;
strcpy(gp->locations, input);
return gp;
}
/*
* Sets a grids width and height
*
* Input:
* fh: The stream to read the grid from.
* gp: A pointer to the grid whose width and height you want to set.
*
* Side effects:
* the grid gets its width and height set
*
* Output:
* 1 if the file width and height seem to match with its size
* 0 otherwise
*/
static int get_grid_sizes(FILE *fh, grid *gp) {
while (getc(fh) != '\n') {
if (feof(fh)) {
return 0;
}
gp->width++;
}
if (gp->width == 0) {
return 0;
}
fseek(fh, 0, SEEK_END);
// Get file size (- 1 for the blank newline and EOF at the end)
if (ftell(fh) % (gp->width + 1) != 0) {
// Not all lines are the same width
return 0;
}
gp->height = (int)ftell(fh) / (int)sizeof(char) / (gp->width + 1);
fseek(fh, 0, SEEK_SET);
return 1;
}
grid *grid_create_from_file(FILE *fh) {
if (fh == NULL) {
return NULL;
}
grid temp_grid = {
NULL,
0,
0,
STATE_BEGIN
};
// Sets the width and height of the grid.
if (get_grid_sizes(fh, &temp_grid) != 1) {
// Unlogical file structure.
return NULL;
}
const int grid_size = (temp_grid.width + 1) * temp_grid.height + 1;
temp_grid.locations = malloc(grid_size * sizeof(char));
if (temp_grid.locations == NULL) {
return NULL;
}
// This makes the strncat() work.
temp_grid.locations[0] = '\0';
char *line = malloc((temp_grid.width + 2) * sizeof(char));
if (line == NULL) {
return NULL;
}
for (int i = 0; i < temp_grid.height; i++) {
if (fgets(line, temp_grid.width + 2, fh) == NULL) {
free(temp_grid.locations);
free(line);
return NULL;
}
// Validate that the line length is correct
if ((int)strcspn(line, "\n") != temp_grid.width) {
free(temp_grid.locations);
free(line);
return NULL;
}
// Width is without the newline at the end.
strncat(temp_grid.locations, line, temp_grid.width + 1);
}
free(line);
grid *return_grid = malloc(sizeof(temp_grid));
if (return_grid == NULL) {
return NULL;
}
memcpy(return_grid, &temp_grid, sizeof(temp_grid));
return return_grid;
}
state grid_fetch_state(const grid *gp) {
if (gp != NULL) {
return gp->state;
}
return STATE_VERLOREN;
}
void grid_put_state(grid *gp, const state t) {
if (gp != NULL) {
switch (t) {
case STATE_BEGIN:
gp->state = STATE_BEGIN;
break;
case STATE_AAN_HET_SPELEN:
gp->state = STATE_AAN_HET_SPELEN;
break;
case STATE_GEWONNEN:
gp->state = STATE_GEWONNEN;
break;
case STATE_VERLOREN:
gp->state = STATE_VERLOREN;
break;
case STATE_QUIT:
gp->state = STATE_QUIT;
break;
}
}
}
void grid_cleanup(grid *gp) {
if (gp != NULL) {
if (gp->locations != NULL)
{
free(gp->locations);
}
free(gp);
}
}
int grid_width(const grid *gp) {
if (gp == NULL) {
return 0;
}
return gp->width;
}
int grid_height(const grid *gp) {
if (gp == NULL) {
return 0;
}
return gp->height;
}
int grid_contains(const grid *gp, const int x, const int y) {
if (gp != NULL && gp->locations != NULL) {
if (x >= 0 && y >= 0 && x < gp->width && y < gp->height)
{
return 1;
}
}
return 0;
}
char grid_fetch(const grid *gp, const int x, const int y) {
if (gp != NULL && gp->locations != NULL && grid_contains(gp, x, y) == 1) {
return gp->locations[internal_location(gp, x, y)];
}
return '\0';
}
int grid_put(grid *gp, const int x, const int y, const char c) {
if (gp != NULL && gp->locations != NULL && grid_contains(gp, x, y) == 1) {
gp->locations[internal_location(gp, x, y)] = c;
return 1;
}
return 0;
}
char *grid_fetch_row(const grid *gp, const int y) {
if (gp != NULL && gp->locations != NULL && grid_contains(gp, 0, y) == 1) {
// we're going to remove the newline so this is long enough
char *row = malloc((gp->width + 1) * sizeof(char));
memcpy(row, &gp->locations[internal_location(gp, 0, y)], gp->width * sizeof(char));
row[gp->width] = '\0';
return row;
}
return NULL;
}
grid *grid_copy(const grid *gp) {
if (gp != NULL && gp->locations != NULL) {
const size_t grid_memory = ((gp->width + 1) * gp->height + 1) * sizeof(char);
char *locations = malloc(grid_memory);
if (locations == NULL) {
return NULL;
}
grid *new_grid = malloc(sizeof(*gp));
if (new_grid == NULL) {
return NULL;
}
memcpy(locations, gp->locations, grid_memory);
memcpy(new_grid, gp, sizeof(*gp));
new_grid->locations = locations;
return new_grid;
}
return NULL;
}
void grid_find(const grid *gp, const char c, int *x, int *y) {
if (gp == NULL || gp->locations == NULL) {
*x = -1;
*y = -1;
return;
}
const char search[2] = {c};
const int char_index = (int)strcspn(gp->locations, search);
if (gp->locations[char_index] == '\0')
{
*x = -1;
*y = -1;
return;
}
*x = char_index % (gp->width + 1);
*y = char_index / (gp->width + 1);
}

174
engine/grid/grid.h Normal file
View File

@@ -0,0 +1,174 @@
/*
* Created by snapshot112 on 2/10/2025
*
* This module provides a grid api.
*
* A grid is a rectangular grid of objects. Every object in this grid is a char.
*/
#ifndef _GRID_H
#define _GRID_H
#include <stdio.h>
struct grid_data;
typedef struct grid_data grid;
typedef enum {
STATE_BEGIN,
STATE_AAN_HET_SPELEN,
STATE_GEWONNEN,
STATE_VERLOREN,
STATE_QUIT
} state;
/* Maak een rooster op basis van de data in de gegeven stream.
*
* fh: de stream waaruit het doolhof gelezen moet worden.
*
* Uitvoer: als alles goed gaat, een pointer naar een rooster (die op de heap is
* gealloceerd), dat overeenkomt met de gegeven beschrijving.
* De begintoestand is BEGIN.
*
* Als de beschrijving niet consistent is (bijvoorbeeld
* niet alle rijen zijn even lang, of er klopt iets anders niet), of
* als niet voldoende geheugen kan worden gereserveerd, dan wordt
* NULL teruggegeven. (In dat geval blijft geen gereserveerd geheugen
* achter.)
*/
grid *grid_create_from_file(FILE *fh);
/*
* Maak een rooster op basis van een gegeven string.
*
* Uitvoer: als alles goed gaat, een pointer naar een rooster (die op de heap is
* gealloceerd), dat overeenkomt met de gegeven beschrijving.
* De begintoestand is BEGIN.
*
* Als de beschrijving niet consistent is (bijvoorbeeld
* niet alle rijen zijn even lang, of er klopt iets anders niet), of
* als niet voldoende geheugen kan worden gereserveerd, dan wordt
* NULL teruggegeven. (In dat geval blijft geen gereserveerd geheugen
* achter.)
*/
grid *grid_create_from_string(const char* input);
/*
* Haal een rij uit het rooster op.
*
* Input:
* gp: een pointer naar het rooster
* y: de y-coordinaat van de rij die je wilt hebben.
*
* Output:
* Een pointer naar een nieuwe string met daarin alle karakters in die rij.
*/
char *grid_fetch_row(const grid *gp, int y);
/*
* Maak een kopie van een rooster
*
* Input:
* gp: Een pointer naar het rooster.
*
* Output:
* Een pointer naar het kopie.
*/
grid *grid_copy(const grid *gp);
/* Vraag de huidige toestand van het spel op.
*
* Input:
* gp: een pointer naar het rooster.
*
* Uitvoer: de toestand.
*/
state grid_fetch_state(const grid *gp);
/* Verander de huidige toestand van het spel.
*
* gp: een pointer naar het rooster.
* t: de nieuwe toestand.
*/
void grid_put_state(grid *gp, state t);
/* Geef alle resources vrij die zijn gealloceerd voor een rooster.
* De rooster pointer is na aanroep van deze functie niet meer bruikbaar.
*
* gp: een pointer naar het rooster.
*/
void grid_cleanup(grid *gp);
/* Vraag de breedte van het rooster op, dat wil zeggen, het aantal kolommen.
*
* gp: een pointer naar het rooster.
*
* Uitvoer: de breedte.
*/
int grid_width(const grid *gp);
/* Vraag de hoogte van het rooster op, dat wil zeggen, het aantal rijen.
*
* gp: een pointer naar het rooster.
*
* Uitvoer: de hoogte.
*/
int grid_height(const grid *gp);
/* Kijk of de gegeven positie binnen het rooster valt.
*
* gp: een pointer naar het rooster.
* x,y: de positie.
*
* Uitvoer: 1 als de positie binnen het rooster valt, anders 0.
*/
int grid_contains(const grid *gp, int x, int y);
/* Kijk welk object er staat op een bepaalde positie in het rooster.
*
* gp : een pointer naar het rooster
* x,y: de betreffende positie.
*
* Uitvoer: het object op die positie, of '\0' als de positie buiten het
* rooster valt.
*/
char grid_fetch(const grid *gp, int x, int y);
/* Schrijf een bepaald object op een bepaalde positie in het rooster.
*
* gp : een pointer naar het rooster
* x,y: de betreffende positie.
* c : het object.
*
* Effect: als (x,y) binnen het rooster ligt, wordt het object op
* de opgegeven positie geplaatst, anders verandert er niets.
*
* Uitvoer: 1 als het object is geplaatst, of 0 als het buiten de grenzen lag.
*/
int grid_put(grid *gp, int x, int y, char c);
/* Zoek een bepaald object in het rooster, en geef de coordinaten van het
* object terug via de gegeven pointers. Let op: als er meerdere objecten van
* het gezochte soort in het rooster voorkomen, is niet gedefinieerd van welke
* de positie wordt gevonden.
*
* gp : een pointer naar het rooster
* c : het object dat moet worden gezocht
* x,y: pointers naar de geheugenlocaties waar de gevonden positie moet worden
* geschreven.
*
* Uitvoer: via de pointers x en y. Als het object niet wordt gevonden worden
* *x en *y op -1 gezet.
*/
void grid_find(const grid *gp, char c, int *x, int *y);
#endif //_GRID_H

160
engine/grid_game_engine.c Normal file
View File

@@ -0,0 +1,160 @@
/*
* Created by snapshot112 on 8/10/2025
*/
#include "grid_game_engine.h"
#include <locale.h>
#include <ncurses.h>
#include <stdlib.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 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 = grid_fetch_row(gp, y);
mvprintw(starting_y + y, starting_x, "%s", rij);
free(rij);
}
refresh();
}
void show_grid(const grid *gp) {
show_grid_on_offset(gp, 0, 0);
}
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);
}
}
/*
* Shows the victory screen.
*
* Side Effect:
* Clears the console and prints the victory message.
*/
static void display_victory(const coordinate location) {
mvaddstr(location.y, location.x, "YOU WON!!!!!");
}
/*
* Shows the GAME OVER screen.
*
* Side Effect:
* Clears the console and prints the GAME OVER message.
*/
static void display_loss(const coordinate location) {
mvaddstr(location.y, location.x, "GAME OVER");
}
/*
* Shows the quit screen
*
* Side Effect:
* Clears the console and prints the quit message.
*/
static void display_quit(const coordinate location) {
mvaddstr(location.y, location.x, "You quit the game");
}
/*
* Shows the hacker man screen
*
* Side Effect:
* Clears the console and prints the hacker man message.
*/
static void display_hackerman(const coordinate location) {
mvaddstr(location.y, location.x, "The hacker man strikes again...");
}
void graceful_exit(const coordinate message_location) {
mvaddstr(message_location.y, message_location.x, "Press ENTER to exit.");
while (1) {
switch (getch()) {
case KEY_BACKSPACE:
case KEY_ESCAPE:
case KEY_ENTER:
case '\n':
return;
}
}
}
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);
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);
refresh();
}
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();
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.
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();
}
void init_engine(void) {
init_ncurses();
}
void cleanup_engine(void) {
endwin();
}

157
engine/grid_game_engine.h Normal file
View File

@@ -0,0 +1,157 @@
/*
* Created by snapshot112 on 15/10/2025.
*
* A game engine build on top of a grid api to run and display games using ncurses.
*
* Please make sure to initialize the game engine before running any games.
*/
#ifndef MINIGAME_MENU_GRID_GAME_ENGINE_H
#define MINIGAME_MENU_GRID_GAME_ENGINE_H
#include "grid/grid.h"
#define KEY_ESCAPE 27
typedef struct {
int x;
int y;
} coordinate;
// Start at 1 since the color 0 is reserved for no_color.
typedef enum {
BLACK = 1,
WHITE = 2,
BLUE = 3,
GREEN = 4,
CYAN = 5,
MAGENTA = 6,
YELLOW = 7,
RED = 8
} game_colors;
typedef struct {
char name[100];
grid *game_map;
} game_maps;
/*
* Checks if 2 coordinates are the same.
*
* Input:
* a: coordinate 1
* b: coordinate 2
*
* Returns:
* If they point to the same location: 1
* Otherwise: 0
*/
int same_coordinate(coordinate a, coordinate b);
/*
* A proper modulo function.
* The one provided by c: '%' doesn't work according to the mathematical definition.
*/
int modulo(int number, int mod);
/*
* Displays the given grid with the given offset using ncurses.
*
* Input:
* gp: A pointer to the grid.
*
* Side effect:
* The console is cleared and the grid is printed.
*/
void show_grid_on_offset(const grid *gp, int starting_x, int starting_y);
/*
* Displays the given grid with ncurses.
*
* Input:
* gp: A pointer to the grid.
*
* Side effect:
* The console is cleared and the grid is printed.
*/
void show_grid(const grid *gp);
/*
* Turn on color highlighting for ncurses
*
* Input:
* color: The color you want the highlight to be.
*
* Side Effects:
* Enables color highlighting in ncurses.
*
* Note:
* This should always be disabled with disable_highlights before being called again.
* Not disabling the current highlight before enabling another one is undefined behaviour.
*/
void enable_highlight(game_colors color);
/*
* Turn off color highlighting for ncurses
*
* Input:
* color: The color highlighting you want to turn off.
*
* Side Effects:
* Enables color highlighting in ncurses.
*
* Note:
* This should always be enabled with enable_highlights before being called.
* Disabling the highlight before enabling it is undefined behaviour.
*/
void disable_highlight(game_colors color);
/*
* Updates a single location in the grid.
*
* Input:
* gp: A pointer to the grid.
* c: The character to update the location with.
* x: The x-coordinate of the spot you want to update.
* y: The y-coordinate of the spot you want to update.
*
* Side effect:
* The update gets applied both on the grid and on the screen.
*/
void update_grid(grid *gp, char c, int x, int y);
/*
* Display the ending screen that matches the end state of the grid.
*
* Input:
* gp: A pointer to the grid.
* coordinate: The location to show the ending screen.
*
* Side Effects:
* The end of game screen gets displayed with a graceful exit.
*/
void game_exit_message(const grid *gp, coordinate location);
/*
* Waits for you to press ENTER before exiting the game.
*
* Input:
* coordinate: The location to show the message.
*
* Side effect: Prints "Press ENTER to exit." game to the console.
*/
void graceful_exit(coordinate message_location);
/*
* 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.
*/
void init_engine(void);
/*
* Cleanup ncurses.
*/
void cleanup_engine(void);
#endif //MINIGAME_MENU_GRID_GAME_ENGINE_H