DungeonCrawl
Loading...
Searching...
No Matches
save_menu.c
Go to the documentation of this file.
1
5#include "save_menu.h"
6
7#include "../common.h"
14#include "src/menu/menu.h"
15
16#include <stdio.h>
17#include <string.h>
18#include <sys/types.h>
19
20
21int selected_save_file_id = -1;
22char last_save_name[50] = {0};
23
25 save_menu_strings = malloc(sizeof(char*) * MAX_SAVE_MENU_STRINGS);
26 RETURN_WHEN_NULL(save_menu_strings, 1, "Save Menu", "Failed to allocate memory for save menu strings.");
27
28 for (int i = 0; i < MAX_SAVE_MENU_STRINGS; i++) {
29 save_menu_strings[i] = NULL;
30 }
31
32 // update local once, so the strings are initialized
34 // add update local function to the observer list
36 return 0;
37}
38
39
41 return selected_save_file_id;
42}
43
44const char* get_save_name(void) {
45 if (last_save_name[0] == '\0') {
46 return NULL;
47 }
48 return last_save_name;
49}
50
51menu_result_t show_save_game_menu(void) {
52 char save_name[50] = {0};
53 menu_result_t result = MENU_CONTINUE;
54
55 // Get save name from user using the output handler
56 bool confirmed = get_text_input(
57 save_menu_strings[SAVE_NAME_REQUEST],
58 save_name,
59 sizeof(save_name),
60 save_menu_strings[PRESS_ENTER_CONFIRM],
61 MENU_START_Y,
62 MENU_START_X);
63
64 if (confirmed) {
65 // Store the save name for later use
66 strncpy(last_save_name, save_name, sizeof(last_save_name) - 1);
67 last_save_name[sizeof(last_save_name) - 1] = '\0';// Ensure null termination
68
69 // Show saving message
71 save_menu_strings[SAVING],
72 NULL,
73 MENU_START_Y,
74 MENU_START_X);
75
76 result = MENU_SAVE_GAME;
77 }
78
79 return result;
80}
81
82menu_result_t show_load_game_menu(bool game_in_progress) {
83 menu_result_t result = MENU_CONTINUE;
84
85 if (game_in_progress && !show_confirmation(save_menu_strings[CONFIRM_QUESTION])) {
86 // User declined, return to continue
87 return MENU_CONTINUE;
88 }
89
90 // Use the global database connection from game.h instead of creating a new one
91 extern db_connection_t db_connection;
92
93 save_info_container_t* save_infos = get_save_infos(&db_connection);
94 if (save_infos == NULL) {
95 log_msg(ERROR, "Menu", "Failed to get save files");
96 return MENU_CONTINUE;
97 }
98
99 if (save_infos->count == 0) {
100 // No saves available - show message and return
102 save_menu_strings[SAVES_NOT_FOUND],
103 save_menu_strings[PRESS_ANY_RETURN],
104 MENU_START_Y,
105 MENU_START_X);
106
107 free_save_infos(save_infos);
108 return MENU_CONTINUE;
109 }
110
111 // Prepare the save file options for the menu
112 const char** save_options = malloc(save_infos->count * sizeof(char*));
113 if (!save_options) {
114 log_msg(ERROR, "Menu", "Failed to allocate memory for save options");
115 free_save_infos(save_infos);
116 return MENU_CONTINUE;
117 }
118
119 // Format save info strings for the menu
120 for (int i = 0; i < save_infos->count; i++) {
121 save_options[i] = malloc(MAX_STRING_LENGTH + TIMESTAMP_LENGTH + 5);
122 if (!save_options[i]) {
123 // Clean up previously allocated memory
124 for (int j = 0; j < i; j++) {
125 free((void*) save_options[j]);
126 }
127 free(save_options);
128 free_save_infos(save_infos);
129 log_msg(ERROR, "Menu", "Failed to allocate memory for save option");
130 return MENU_CONTINUE;
131 }
132 snprintf((char*) save_options[i], MAX_STRING_LENGTH + TIMESTAMP_LENGTH + 5,
133 "%s (%s)", save_infos->infos[i].name, save_infos->infos[i].timestamp);
134 }
135
136 // Display the save files and let the user select one
137 int selected_save_index = 0;
138 bool selection_active = true;
139
140 while (selection_active) {
141 // Clear the screen
142 clear_screen();
143
144 // Print the title and options
145 print_text_default(MENU_START_Y, MENU_START_X, save_menu_strings[SELECT_SAVE]);
146
147 // Print the save options with highlighting
148 for (int i = 0; i < save_infos->count; i++) {
149 if (i == selected_save_index) {
150 // Highlight selected option with inverted colors
151 print_text(MENU_START_Y + 2 + (i * MENU_ITEM_SPACING),
152 MENU_START_X,
153 save_options[i],
154 INVERTED_COLORS);
155 } else {
156 // Normal option
157 print_text_default(MENU_START_Y + 2 + (i * MENU_ITEM_SPACING),
158 MENU_START_X,
159 save_options[i]);
160 }
161 }
162
163 // Print the navigation instructions
164 print_text_default(MENU_START_Y + 2 + (save_infos->count * MENU_ITEM_SPACING) + 2,
165 MENU_START_X,
166 save_menu_strings[NAVIGATE_INSTRUCTIONS]);
167
168 // Render the frame
169 render_frame();
170
171 // Get input
172 input_event_t input_event;
173 if (!get_input_blocking(&input_event)) {
174 continue;
175 }
176
177 // Use our logical input types
178 switch (input_event.type) {
179 case INPUT_UP:
180 selected_save_index = (selected_save_index - 1 + save_infos->count) % save_infos->count;
181 break;
182 case INPUT_DOWN:
183 selected_save_index = (selected_save_index + 1) % save_infos->count;
184 break;
185 case INPUT_CONFIRM:
186 // Set the selected save file ID for loading
187 result = MENU_LOAD_GAME;
188 selected_save_file_id = save_infos->infos[selected_save_index].id;
189 selection_active = false;
190 break;
191 case INPUT_CANCEL:
192 selection_active = false;
193 break;
194 default:
195 break;
196 }
197 }
198
199 // Clean up resources
200 for (int i = 0; i < save_infos->count; i++) {
201 free((void*) save_options[i]);
202 }
203 free(save_options);
204 free_save_infos(save_infos);
205
206 return result;
207}
208
210 if (save_menu_strings != NULL) {
211 for (int i = 0; i < MAX_SAVE_MENU_STRINGS; i++) {
212 if (save_menu_strings[i] != NULL) {
213 free(save_menu_strings[i]);
214 }
215 }
216 free(save_menu_strings);
217 }
218}
Defines common macros, types, and global variables for color schemes and utilities.
Exposes functions for working with the database.
save_info_container_t * get_save_infos(const db_connection_t *db_connection)
Get the info of the saves.
void free_save_infos(save_info_container_t *save_infos)
Free the resources associated with a save_info_container.
Declares functions to create, save, load, and manage game state data in the SQLite database.
bool get_input_blocking(input_event_t *event)
Get the next input event (blocking)
Exposes functions for working with input.
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
bool show_confirmation(const char *message)
Shows a confirmation dialog with the given message.
Definition menu.c:39
Exposes helper functions to work with menus.
void print_text(int y, int x, const char *text, uint64_t ncchannel)
Print text at a specific position.
bool get_text_input(const char *prompt, char *buffer, int buffer_size, const char *confirm_msg, int y, int x)
Get a text input from the user.
void print_text_default(int y, int x, const char *text)
Print text at a specific position with default colors.
bool render_frame(void)
Render the current frame.
void clear_screen(void)
Clear the screen.
void show_message_screen(const char *message, const char *continue_message, int y, int x)
Show a message screen.
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
int init_save_menu()
Initialize the save menu with the needed data.
Definition save_menu.c:24
void shutdown_save_menu(void)
Shuts down the save menu and frees associated data.
Definition save_menu.c:209
int get_selected_save_file_id(void)
Get the ID of the save file selected by the user.
Definition save_menu.c:40
const char * get_save_name(void)
Get the name of the latest save entered by the user.
Definition save_menu.c:44
Exposes functions for the save menu.
void update_save_menu_local(void)
Updates the localized strings for the save menu.
Declares functions and data for localized save menu UI.
This struct is used for the database connection in SQLite.
Definition database.h:22
Structure for advanced input events with context data.
Definition input_types.h:43