DungeonCrawl
Loading...
Searching...
No Matches
main_menu.h File Reference

Exposes functions for the main menu. More...

#include "menu.h"

Go to the source code of this file.

Functions

int init_main_menu (void)
 Initializes resources for the main menu, including memory allocation and observer registration for localized menu strings.
menu_result_t show_main_menu (bool game_in_progress)
 Display and handle the main menu.
void shutdown_main_menu (void)
 Shuts down the main menu and frees associated data.

Detailed Description

Exposes functions for the main menu.

Definition in file main_menu.h.

Function Documentation

◆ init_main_menu()

int init_main_menu ( void )

Initializes resources for the main menu, including memory allocation and observer registration for localized menu strings.

Returns
0 if the main menu initializes successfully, non-zero value otherwise in case of failure.

Definition at line 28 of file main_menu.c.

28 {
29 main_menu_strings = (char**) malloc(sizeof(char*) * MAX_MAIN_MENU_STRINGS);
30 RETURN_WHEN_NULL(main_menu_strings, 1, "Main Menu", "Failed to allocate memory for main menu strings.");
31
32 for (int i = 0; i < MAX_MAIN_MENU_STRINGS; i++) {
33 main_menu_strings[i] = NULL;
34 }
35
36 // update local once, so the strings are initialized
38 // add update local function to the observer list
40 return 0;
41}
void observe_local(void(*update_func)(void))
Registers an observer function to be notified of updates from the local handler.
void update_main_menu_local(void)
Updates the localized strings for the main menu.

◆ show_main_menu()

menu_result_t show_main_menu ( bool game_in_progress)

Display and handle the main menu.

Parameters
game_in_progressindicates whether there's an active game that can be continued
Returns
MENU_START_GAME to start a new game, MENU_CONTINUE to continue the current game, MENU_SAVE_GAME to save the game, MENU_LOAD_GAME to load a game, MENU_EXIT to exit the game

Definition at line 43 of file main_menu.c.

43 {
44 const char* menu_options[6];
45 int menu_count;
46
47 // Always include New Game
48 menu_options[0] = main_menu_strings[NEW_GAME_STR];
49
50 if (game_in_progress) {
51 // If game is in progress, show all options
52 menu_options[1] = main_menu_strings[CONTINUE_STR];
53 menu_options[2] = main_menu_strings[SAVE_GAME_STR];
54 menu_options[3] = main_menu_strings[LOAD_GAME_STR];
55 menu_options[4] = main_menu_strings[CHANGE_LANGUAGE_STR];
56 menu_options[5] = main_menu_strings[EXIT_STR];
57 menu_count = 6;
58 } else {
59 // If no game in progress, only show New Game, Load Game and Exit
60 menu_options[1] = main_menu_strings[LOAD_GAME_STR];
61 menu_options[2] = main_menu_strings[CHANGE_LANGUAGE_STR];
62 menu_options[3] = main_menu_strings[EXIT_STR];
63 menu_count = 4;
64 }
65
66 int selected_index = 0;
67 active_menu_state = MENU_CONTINUE;
68 menu_active = true;
69
70 while (menu_active) {
71 draw_menu(menu_options, menu_count, selected_index);
72
73 input_event_t input_event;
74 if (!get_input_blocking(&input_event)) {
75 continue;
76 }
77
78 // Using our input type to navigate the menu
79 switch (input_event.type) {
80 case INPUT_UP:
81 selected_index = (selected_index - 1 + menu_count) % menu_count;
82 break;
83 case INPUT_DOWN:
84 selected_index = (selected_index + 1) % menu_count;
85 break;
86 case INPUT_CONFIRM: {
87 // Get the selected menu option
88 select_menu_option(selected_index, game_in_progress);
89 break;
90 }
91 case INPUT_QUIT:
92 active_menu_state = MENU_EXIT;
93 menu_active = false;
94 break;
95 case INPUT_CANCEL:
96 case INPUT_MENU:
97 active_menu_state = MENU_CONTINUE;
98 menu_active = false;
99 break;
100 default:
101 // do nothing for other keys
102 break;
103 }
104 }
105
106 return active_menu_state;
107}
bool get_input_blocking(input_event_t *event)
Get the next input event (blocking)
void select_menu_option(int selected_index, bool game_in_progress)
Process a selected menu option.
Definition main_menu.c:115
void draw_menu(const char **menu_options, int menu_count, int selected_index)
Draws a menu with the given options.
Definition menu.c:15
Structure for advanced input events with context data.
Definition input_types.h:43

◆ shutdown_main_menu()

void shutdown_main_menu ( void )

Shuts down the main menu and frees associated data.

Definition at line 160 of file main_menu.c.

160 {
161 if (main_menu_strings != NULL) {
162 for (int i = 0; i < MAX_MAIN_MENU_STRINGS; i++) {
163 if (main_menu_strings[i] != NULL) {
164 free(main_menu_strings[i]);
165 }
166 }
167 free(main_menu_strings);
168 }
169}