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

Implementation of the main menu. More...

Go to the source code of this file.

Functions

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

Variables

bool menu_active
menu_result_t active_menu_state

Detailed Description

Implementation of the main menu.

Definition in file main_menu.c.

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.

◆ select_menu_option()

void select_menu_option ( int selected_index,
bool game_in_progress )

Process a selected menu option.

Select a menu option.

Parameters
selected_indexThe selected menu option index
game_in_progressIndicates whether there's an active game that can be continued
selected_indexThe index of the menu option that is to be selected.
game_in_progressA boolean which shows if the game is in progress or not.

Definition at line 115 of file main_menu.c.

115 {
116 //if the game is not in progress and the selected index is bigger than 0, we need to add 2 to the selected index
117 const int true_index = selected_index > 0 && !game_in_progress ? selected_index + 2 : selected_index;
118
119 switch (true_index) {
120 case 0:// New Game
121 if (!game_in_progress || show_confirmation(main_menu_strings[QUESTION_CONTINUE])) {
122 active_menu_state = MENU_START_GAME;
123 menu_active = false;
124 }
125 break;
126 case 1:// Continue
127 active_menu_state = MENU_CONTINUE;
128 menu_active = false;
129 break;
130 case 2:// Save Game
131 active_menu_state = show_save_game_menu();
132 if (active_menu_state == MENU_SAVE_GAME) {
133 menu_active = false;
134 }
135 break;
136 case 3:// Load Game
137 active_menu_state = show_load_game_menu(game_in_progress);
138 if (active_menu_state == MENU_LOAD_GAME) {
139 menu_active = false;
140 }
141 break;
142 case 4:// Change Language
143 active_menu_state = show_language_menu();
144 if (active_menu_state == MENU_CHANGE_LANGUAGE) {
145 menu_active = false;
146 }
147 break;
148 case 5:// Exit
149 if (!game_in_progress || show_confirmation(main_menu_strings[QUESTION_EXIT])) {
150 active_menu_state = MENU_EXIT;
151 menu_active = false;
152 }
153 break;
154 default:
155 log_msg(WARNING, "Main Menu", "Invalid menu option selected: %d", selected_index);
156 break;
157 }
158}
menu_result_t show_language_menu()
Draws the language menu to the screen.
void log_msg(const log_level_t level, const char *module, const char *format,...)
Logs a formatted message with a specified log level and module.
Definition logger.c:246
bool show_confirmation(const char *message)
Shows a confirmation dialog with the given message.
Definition menu.c:39
menu_result_t show_load_game_menu(bool game_in_progress)
Show load game interface to select a save file.
Definition save_menu.c:82
menu_result_t show_save_game_menu(void)
Show save game interface to get save name from user.
Definition save_menu.c:51

◆ 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}

Variable Documentation

◆ active_menu_state

menu_result_t active_menu_state

Definition at line 26 of file main_menu.c.

◆ menu_active

bool menu_active

Definition at line 25 of file main_menu.c.