forked from snapshot112/minigame-menu
		
	
		
			
				
	
	
		
			158 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			158 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /*
 | |
|  * 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
 |