DungeonCrawl
Loading...
Searching...
No Matches
main_menu.c
Go to the documentation of this file.
1
5#include "main_menu.h"
6
7#include "../common.h"
10#include "../logging/logger.h"
11#include "language_menu.h"
13#include "save_menu.h"
15
16// === Internal Functions ===
22void select_menu_option(int selected_index, bool game_in_progress);
23
24// === Internal Global Variables ===
25bool menu_active;
26menu_result_t active_menu_state;
27
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}
42
43menu_result_t show_main_menu(const bool game_in_progress) {
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}
108
115void select_menu_option(const int selected_index, const bool game_in_progress) {
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}
159
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}
Defines common macros, types, and global variables for color schemes and utilities.
bool get_input_blocking(input_event_t *event)
Get the next input event (blocking)
Exposes functions for working with input.
menu_result_t show_language_menu()
Draws the language menu to the screen.
Exposes functions for the language menu.
void observe_local(void(*update_func)(void))
Registers an observer function to be notified of updates from the local handler.
Exposes public functions for the localization handler.
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
Header file for logging functionality of the game.
menu_result_t show_main_menu(const bool game_in_progress)
Display and handle the main menu.
Definition main_menu.c:43
void select_menu_option(int selected_index, bool game_in_progress)
Process a selected menu option.
Definition main_menu.c:115
int init_main_menu()
Initializes resources for the main menu, including memory allocation and observer registration for lo...
Definition main_menu.c:28
void shutdown_main_menu(void)
Shuts down the main menu and frees associated data.
Definition main_menu.c:160
Exposes functions for the main menu.
void update_main_menu_local(void)
Updates the localized strings for the main menu.
Exposes functionality for the localization of the main menu.
bool show_confirmation(const char *message)
Shows a confirmation dialog with the given message.
Definition menu.c:39
void draw_menu(const char **menu_options, int menu_count, int selected_index)
Draws a menu with the given options.
Definition menu.c:15
Exposes functions for outputting to the console.
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
Exposes functions for the save menu.
Structure for advanced input events with context data.
Definition input_types.h:43