DungeonCrawl
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
5#include "main.h"
6
8#include "common.h"
10#include "game.h"
11#include "game_data.h"
13#include "io/io_handler.h"
15#include "local/local_handler.h"
16#include "logging/logger.h"
17#include "map/map_mode.h"
18#include "menu/language_menu.h"
19#include "menu/main_menu.h"
20#include "menu/save_menu.h"
21
22#ifndef _WIN32
23 #include <unistd.h>// for usleep
24#endif
25
27
28#include <time.h>
29
30// Global flag to signal when initialization is complete
31volatile int init_done = 0;
32
36static void display_launch_screen_thread(void) {
38
39 // Track if we've displayed for minimum time
40 time_t start_time = time(NULL);
41 bool min_time_elapsed = false;
42
43 // Draw the launch screen for LAUNCH_SCREEN_MIN_DISPLAY_TIME_MS milliseconds
44 while (!min_time_elapsed) {
45 // Call the draw_launch_screen function from wait_output.c
47
48 // Check if we can exit the loop (both conditions met)
49 time_t current_time = time(NULL);
50 double elapsed_s = difftime(current_time, start_time);
51 long elapsed_ms = (long) (elapsed_s * 1000);
52
53 min_time_elapsed = (elapsed_ms >= LAUNCH_SCREEN_MIN_DISPLAY_TIME_MS);
54 }
55
56 // Clear the screen and render one more time to ensure it's cleared
59
61}
62
66void shutdown_game(void);
67
75int init() {
76 // Initialize the main memory pool
77 main_memory_pool = init_memory_pool(STANDARD_MEMORY_POOL_SIZE);
78 NULL_PTR_HANDLER_RETURN(main_memory_pool, FAIL_MEM_POOL_INIT, "Main", "Main memory pool is NULL");
79
80 // Seed random function
81 srand(time(NULL));
82
83 // Initialize logger
85
86 // First initialize the IO handler (needed for the splash screen)
88 log_msg(ERROR, "Main", "Failed to initialize IO handler");
89 return FAIL_IO_HANDLER_INIT;
90 }
91
92 // Start the launch screen in a background thread
93 // don't need multithreading anymore, init is too fast
94 display_launch_screen_thread();
95
96
97 // Initialize database connection
98 if (db_open_multiple_access(&db_connection, DB_GAME) != DB_OPEN_STATUS_SUCCESS) {
99 log_msg(ERROR, "Game", "Failed to open database");
100 return 1;
101 }
102 create_tables_game_state(&db_connection);
103
104 // Initialize localization
105 if (init_local_handler(LANGE_EN) != COMMON_SUCCESS) {
106 log_msg(ERROR, "Main", "Failed to initialize local handler");
107 return FAIL_LOCAL_INIT;
108 }
109
110 // Initialize map mode
112
113 // Initialize main menu
115 log_msg(ERROR, "Main", "Failed to initialize main menu");
116 return FAIL_MAIN_MENU_INIT;
117 }
119 log_msg(ERROR, "Main", "Failed to initialize save menu");
120 return FAIL_SAVE_MENU_INIT;
121 }
123 log_msg(ERROR, "Main", "Failed to initialize language menu");
124 return FAIL_LANGUAGE_INIT;
125 }
126 // Initialize combat mode
128 log_msg(ERROR, "Main", "Failed to initialize combat mode");
129 return FAIL_GAME_MODE_INIT;
130 }
131
132 // Initialize inventory mode
134 log_msg(ERROR, "Main", "Failed to initialize inventory mode");
135 return FAIL_INVENTORY_MODE_INIT;
136 }
137
138 // Initialize game data
140 log_msg(ERROR, "Game", "Failed to initialize game components");
141 return FAIL_GAME_ENTITY_INIT;
142 }
143
144 // Initialize stats mode
146 log_msg(ERROR, "Stats", "Failed to initialize stats components");
147 return FAIL_STATS_MODE_INIT;
148 }
149
150 // When all initialization is done, switch back to game mode
151 init_done = 1;// Set the global flag to signal completion
152 return COMMON_SUCCESS;
153}
154
161 // close database connection in game.c
162 db_close(&db_connection);
163
171
172 //shutdown the main memory pool
176}
177
178int main(void) {
179 const int exit_code = init();
180 if (exit_code != COMMON_SUCCESS) {
182 return exit_code;
183 }
184 run_game();
186 return exit_code;
187}
void shutdown_combat_mode()
Shuts down the combat mode and frees allocated memory resources.
int init_combat_mode()
Initialize the combat mode.
Definition combat_mode.c:67
Declares combat mode state machine, including menus and combat operations.
memory_pool_t * main_memory_pool
Global memory pool for the application.
Definition common.c:7
Defines common macros, types, and global variables for color schemes and utilities.
#define COMMON_SUCCESS
Common success return value.
Definition common.h:146
int db_open_multiple_access(db_connection_t *db_connection, db_type_t type)
This function is for the opening of the database with multiple access.
Definition database.c:34
void db_close(db_connection_t *db_connection)
This function is for the closing of the database.
Definition database.c:22
void run_game()
Starts the game loop.
Definition game.c:62
Declares core game states, global database connection, and main game control functions.
int free_game_data()
Frees game-related data structures and resources, such as ability tables, characters,...
Definition game_data.c:37
int init_game_data()
Initializes game data for the application.
Definition game_data.c:23
Declares functions and globals for initializing, resetting, and freeing game data such as player,...
void create_tables_game_state(const db_connection_t *db_connection)
Create the tables for the game state.
Declares functions to create, save, load, and manage game state data in the SQLite database.
void shutdown_inventory_mode(void)
Shuts down the inventory mode and frees allocated resources.
int init_inventory_mode()
Initialize the inventory mode.
Exposes functions for working with the inventory mode.
int init_io_handler(void)
Initialize the IO handler.
Definition io_handler.c:29
void shutdown_io_handler(void)
Shutdown the IO handler.
Definition io_handler.c:84
Exposes functions for the IO-Handler.
int init_language_menu()
Initializes the language menu with the needed data.
void shutdown_language_menu()
Shuts down the language menu and frees all associated data.
Exposes functions for the language menu.
void shutdown_local_handler(void)
Shut down the local language handler by releasing resources and closing the resource file.
int init_local_handler(const local_lang_t lang)
Initialize the local language handler by setting up the language and opening the corresponding resour...
Exposes public functions for the localization handler.
void shutdown_logger(void)
Shuts down the logging system for the application.
Definition logger.c:278
void init_logger(void)
Initializes the logging system for the application.
Definition logger.c:232
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.
int init()
Initializes all necessary parts and subsystems for the game.
Definition main.c:75
void shutdown_game(void)
Frees all allocated resources and performs cleanup tasks for the game.
Definition main.c:158
int main(void)
Main entry point for the application.
Definition main.c:178
Declares application initialization, shutdown, and main entry point.
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 init_map_mode(void)
Initializes the map mode.
Definition map_mode.c:139
void shutdown_map_mode(void)
Frees any resources associated with the map mode.
Definition map_mode.c:147
Defines and manages functions for map exploration, player movement, and map interactions in map mode.
memory_pool_t * init_memory_pool(size_t size)
Initialize a memory pool of the given size.
void shutdown_memory_pool(memory_pool_t *pool)
Shuts down the memory pool.
bool render_frame(void)
Render the current frame.
void clear_screen(void)
Clear the screen.
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
Exposes functions for the save menu.
void shutdown_stats_mode()
Shutdown the stats mode.
int init_stats_mode()
Initialize the stats mode.
Exposes functions for outputing to the screen in stats mode.
void draw_welcome_screen(void)
Draw the welcome screen with a message.
void draw_launch_screen(void)
Draw the launch screen with title and animation.
Definition wait_output.c:66
Exposes functions for drawing the laoding screen.