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

Entry point handling game initialization, launch screen display, main loop start, and shutdown. More...

#include "main.h"
#include "combat/combat_mode.h"
#include "common.h"
#include "database/game/gamestate_database.h"
#include "game.h"
#include "game_data.h"
#include "inventory/inventory_mode.h"
#include "io/io_handler.h"
#include "io/output/specific/stats_output.h"
#include "local/local_handler.h"
#include "logging/logger.h"
#include "map/map_mode.h"
#include "menu/language_menu.h"
#include "menu/main_menu.h"
#include "menu/save_menu.h"
#include <unistd.h>
#include "io/output/specific/wait_output.h"
#include <time.h>

Go to the source code of this file.

Functions

void shutdown_game (void)
 Frees all allocated resources and performs cleanup tasks for the game.
int init ()
 Initializes all necessary parts and subsystems for the game.
int main (void)
 Main entry point for the application.

Variables

volatile int init_done = 0

Detailed Description

Entry point handling game initialization, launch screen display, main loop start, and shutdown.

Definition in file main.c.

Function Documentation

◆ init()

int init ( )

Initializes all necessary parts and subsystems for the game.

First initializes IO handler to show a splash screen, then initializes the rest of the systems sequentially.

Returns
An exit code indicating success or the specific failure that occurred.

Definition at line 75 of file main.c.

75 {
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}
int init_combat_mode()
Initialize the combat mode.
Definition combat_mode.c:67
memory_pool_t * main_memory_pool
Global memory pool for the application.
Definition common.c:7
#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
int init_game_data()
Initializes game data for the application.
Definition game_data.c:23
void create_tables_game_state(const db_connection_t *db_connection)
Create the tables for the game state.
int init_inventory_mode()
Initialize the inventory mode.
int init_io_handler(void)
Initialize the IO handler.
Definition io_handler.c:29
int init_language_menu()
Initializes the language menu with the needed data.
int init_local_handler(const local_lang_t lang)
Initialize the local language handler by setting up the language and opening the corresponding resour...
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
int init_main_menu()
Initializes resources for the main menu, including memory allocation and observer registration for lo...
Definition main_menu.c:28
void init_map_mode(void)
Initializes the map mode.
Definition map_mode.c:139
memory_pool_t * init_memory_pool(size_t size)
Initialize a memory pool of the given size.
int init_save_menu()
Initialize the save menu with the needed data.
Definition save_menu.c:24
int init_stats_mode()
Initialize the stats mode.

◆ main()

int main ( void )

Main entry point for the application.

This function initializes the game, runs the main game loop, and performs cleanup before exiting. It handles the overall flow of the application.

Returns
Exit code indicating success or failure.

Definition at line 178 of file main.c.

178 {
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 run_game()
Starts the game loop.
Definition game.c:62
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

◆ shutdown_game()

void shutdown_game ( void )

Frees all allocated resources and performs cleanup tasks for the game.

Shuts down the entire game and frees associated resources.

Definition at line 158 of file main.c.

158 {
161 // close database connection in game.c
162 db_close(&db_connection);
163
171
172 //shutdown the main memory pool
176}
void shutdown_combat_mode()
Shuts down the combat mode and frees allocated memory resources.
void db_close(db_connection_t *db_connection)
This function is for the closing of the database.
Definition database.c:22
int free_game_data()
Frees game-related data structures and resources, such as ability tables, characters,...
Definition game_data.c:37
void shutdown_inventory_mode(void)
Shuts down the inventory mode and frees allocated resources.
void shutdown_io_handler(void)
Shutdown the IO handler.
Definition io_handler.c:84
void shutdown_language_menu()
Shuts down the language menu and frees all associated data.
void shutdown_local_handler(void)
Shut down the local language handler by releasing resources and closing the resource file.
void shutdown_logger(void)
Shuts down the logging system for the application.
Definition logger.c:278
void shutdown_main_menu(void)
Shuts down the main menu and frees associated data.
Definition main_menu.c:160
void shutdown_map_mode(void)
Frees any resources associated with the map mode.
Definition map_mode.c:147
void shutdown_memory_pool(memory_pool_t *pool)
Shuts down the memory pool.
void shutdown_save_menu(void)
Shuts down the save menu and frees associated data.
Definition save_menu.c:209
void shutdown_stats_mode()
Shutdown the stats mode.

Variable Documentation

◆ init_done

volatile int init_done = 0

Definition at line 31 of file main.c.