| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-10-18 21:35:01 +02:00
										 |  |  |  * Created by snapshot112 on 15/10/2025. | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-10-18 21:35:01 +02:00
										 |  |  |  * A game engine build on top of a grid api to run and display games using ncurses. | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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
 | 
					
						
							| 
									
										
										
										
											2025-10-22 15:07:52 +02:00
										 |  |  | #include "grid/grid.h"
 | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  | #define KEY_ESCAPE 27
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-17 11:15:53 +02:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     int x; | 
					
						
							|  |  |  |     int y; | 
					
						
							|  |  |  | } coordinate; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-17 14:29:22 +02:00
										 |  |  | // Start at 1 since the color 0 is reserved for no_color.
 | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 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]; | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     grid *game_map; | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | } game_maps; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-17 14:29:22 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * 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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | void show_grid_on_offset(const grid *gp, int starting_x, int starting_y); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Displays the given grid with ncurses. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Input: | 
					
						
							|  |  |  |  * gp: A pointer to the grid. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Side effect: | 
					
						
							|  |  |  |  * The console is cleared and the grid is printed. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | 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); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | void update_grid(grid *gp, char c, int x, int y); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Display the ending screen that matches the end state of the grid. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Input: | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |  * gp:          A pointer to the grid. | 
					
						
							|  |  |  |  * coordinate:  The location to show the ending screen. | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Side Effects: | 
					
						
							|  |  |  |  * The end of game screen gets displayed with a graceful exit. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | void game_exit_message(const grid *gp, coordinate location); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-10-18 21:35:01 +02:00
										 |  |  |  * Waits for you to press ENTER before exiting the game. | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Input: | 
					
						
							|  |  |  |  * coordinate: The location to show the message. | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-10-18 21:35:01 +02:00
										 |  |  |  * Side effect: Prints "Press ENTER to exit." game to the console. | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  | void graceful_exit(coordinate message_location); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Initialize ncurses. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This enables cbreak, noecho, hides the cursor and enables the extra keys. | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |  * This also creates the color pairs needed for game_colors and sets ESCDELAY to 0. | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | void init_engine(void); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Cleanup ncurses. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void cleanup_engine(void); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif //MINIGAME_MENU_GRID_GAME_ENGINE_H
 |