DungeonCrawl
Loading...
Searching...
No Matches
io_handler.c
Go to the documentation of this file.
1
5#include "io_handler.h"
6
7#include "../common.h"// Added for COMMON_SUCCESS
8#include "../logging/logger.h"
10#include "input/input_handler.h"
15
16#include <stdbool.h>
17#include <stdint.h>
18#include <string.h>
19#include <time.h>
20
21#ifndef _WIN32
22 #include <unistd.h>// for usleep
23#endif
24
25// Global notcurses instance and standard plane
26struct notcurses* nc = NULL;
27struct ncplane* stdplane = NULL;
28
29int init_io_handler(void) {
30 //initialize the Notcurses instance and standard plane
31 notcurses_options ncopt;
32 memset(&ncopt, 0, sizeof(ncopt));
33
34 nc = notcurses_init(&ncopt, stdout);
35 if (nc == NULL) {
36 log_msg(ERROR, "io_handler", "Failed to initialize notcurses");
37 return 1;// Error code
38 }
39
40 stdplane = notcurses_stdplane(nc);
41 if (!stdplane) {
42 log_msg(ERROR, "io_handler", "Failed to get standard plane");
43 notcurses_stop(nc);
44 nc = NULL;
45 return 2;// Error code
46 }
47
48 ncplane_set_bg_rgb(stdplane, 0x281D10);
49
50 // Initialize input handler (which starts its own thread)
51 if (!init_input_handler(nc)) {
52 log_msg(ERROR, "io_handler", "Failed to initialize input handler");
53 return 3;// Error code
54 }
55
56 // Initialize common output handler
57 if (!init_output_handler()) {
58 log_msg(ERROR, "io_handler", "Failed to initialize output handler");
60 return 4;// Error code
61 }
62
63 // Initialize media output handler
64 if (!init_media_output()) {
65 log_msg(ERROR, "io_handler", "Failed to initialize media output handler");
66 // Continue without media support, not a fatal error
67 }
68
69 return COMMON_SUCCESS;// 0
70}
71
72// Execute a callback in a background thread
73bool run_background_task(void (*callback)(void)) {
74 if (!callback) {
75 log_msg(ERROR, "io_handler", "Invalid callback for background task");
76 return false;
77 }
78
79 // Start a thread to execute the callback
80 start_simple_thread(callback);
81 return true;
82}
83
85 // Shutdown subsystems in reverse order of initialization
88
89 // Shutdown notcurses
90 if (nc) {
91 notcurses_stop(nc);
92 nc = NULL;
93 stdplane = NULL;
94 }
95}
Defines common macros, types, and global variables for color schemes and utilities.
#define COMMON_SUCCESS
Common success return value.
Definition common.h:146
bool init_input_handler(struct notcurses *notcurses_ptr)
Initialize the input handler.
void shutdown_input_handler(void)
Shutdown the input handler.
Exposes functions for working with input.
bool run_background_task(void(*callback)(void))
Execute a function in a background thread.
Definition io_handler.c:73
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.
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.
Exposes functions for drawing to the screen.
bool init_media_output(void)
Initialize the media output handler.
Exposes functions for the media output handler.
void shutdown_output_handler(void)
Shutdown the output handler.
bool init_output_handler()
Initialize the output handler.
Exposes functions for outputting to the console.
void start_simple_thread(void(*thread_func)(void))
Starts a new thread with the given function.
Exposes functions for the thread_handler.
Exposes functions for drawing the laoding screen.