DungeonCrawl
Loading...
Searching...
No Matches
output_handler.h File Reference

Exposes functions for outputting to the console. More...

#include <notcurses/notcurses.h>
#include <stdbool.h>

Go to the source code of this file.

Macros

#define LAUNCH_SCREEN_MIN_DISPLAY_TIME_MS   2000

Functions

bool init_output_handler (void)
 Initialize the output handler.
void clear_screen (void)
 Clear the screen.
void print_text (int y, int x, const char *text, uint64_t ncchannel)
 Print text at a specific position.
void print_text_default (int y, int x, const char *text)
 Print text at a specific position with default colors.
void print_text_multi_line (int y, int x, const char *text, int max_width, u_int64_t ncchannel)
 Print multi-line text with word wrapping.
void print_text_multi_line_default (int y, int x, const char *text, int max_width)
 Print multi-line text with word wrapping and default colors.
void print_text_multi_strings (int y, int x, const char *text[], int count, uint64_t ncchannel)
 Print multiple strings on consecutive lines.
void print_text_multi_strings_default (int y, int x, const char *text[], int count)
 Print multiple strings on consecutive lines with default colors.
void print_menu (const char *title, const char **options, int option_count, int selected_index, int y, int x, uint64_t title_channel, uint64_t option_channel, uint64_t selected_channel)
 Print a menu with selection highlighting.
void print_menu_default (const char *title, const char **options, int option_count, int selected_index, int y, int x)
 Print a menu with selection highlighting using default colors.
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 show_message_screen (const char *message, const char *continue_message, int y, int x)
 Show a message screen.
bool render_frame (void)
 Render the current frame.
bool get_screen_dimensions (int *width, int *height)
 Get the dimensions of the standard plane.
void shutdown_output_handler (void)
 Shutdown the output handler.

Detailed Description

Exposes functions for outputting to the console.

Definition in file output_handler.h.

Macro Definition Documentation

◆ LAUNCH_SCREEN_MIN_DISPLAY_TIME_MS

#define LAUNCH_SCREEN_MIN_DISPLAY_TIME_MS   2000

Definition at line 12 of file output_handler.h.

Function Documentation

◆ clear_screen()

void clear_screen ( void )

Clear the screen.

Clears the entire standard plane.

Definition at line 39 of file output_handler.c.

39 {
40 if (!stdplane) {
41 log_msg(ERROR, "output_handler", "Output handler not initialized");
42 return;
43 }
44
45 // Clear the plane with the default colors
46 ncplane_set_base(stdplane, " ", 0, DEFAULT_COLORS);
47 ncplane_erase(stdplane);
48}
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

◆ get_screen_dimensions()

bool get_screen_dimensions ( int * width,
int * height )

Get the dimensions of the standard plane.

Parameters
[out]widthPointer to store the width
[out]heightPointer to store the height
Returns
true on success, false on failure

Definition at line 257 of file output_handler.c.

257 {
258 if (!stdplane || !width || !height) {
259 log_msg(ERROR, "output_handler", "Invalid parameters for get_screen_dimensions");
260 return false;
261 }
262
263 // Get the dimensions of the standard plane
264 *width = ncplane_dim_x(stdplane);
265 *height = ncplane_dim_y(stdplane);
266
267 return true;
268}

◆ get_text_input()

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.

Displays a prompt and gets a text input from the user.

Parameters
promptThe text prompt to display
bufferThe buffer to store the input in
buffer_sizeThe size of the buffer
confirm_msgThe text explaining that Enter confirms (can be NULL)
yThe Y coordinate (row) for the prompt
xThe X coordinate (column) for the prompt
Returns
true if input was confirmed, false if canceled

Definition at line 156 of file output_handler.c.

157 {
158 if (!stdplane || !buffer || buffer_size <= 0) {
159 log_msg(ERROR, "output_handler", "Invalid parameters for get_text_input");
160 return false;
161 }
162
163 // Clear the buffer
164 memset(buffer, 0, buffer_size);
165 int text_length = 0;
166 bool input_active = true;
167 bool confirmed = false;
168
169 while (input_active) {
170 // Clear screen
171 clear_screen();
172
173 // Display prompt
174 print_text_default(y, x, prompt);
175
176 // Display current input
177 print_text_default(y + 2, x, buffer);
178
179 // Display confirm message if provided
180 if (confirm_msg) {
181 print_text_default(y + 4, x, confirm_msg);
182 }
183
184 // Render the frame
185 render_frame();
186
187 // Get input
188 input_event_t input_event;
189 if (!get_input_blocking(&input_event)) {
190 continue;
191 }
192
193 // Process input
194 uint32_t key_id = input_event.raw_input.id;
195
196 if (input_event.type == INPUT_CONFIRM && text_length > 0) {
197 // Enter was pressed and we have input
198 input_active = false;
199 confirmed = true;
200 } else if (input_event.type == INPUT_CANCEL) {
201 // Escape was pressed
202 input_active = false;
203 confirmed = false;
204 } else if (key_id == NCKEY_BACKSPACE && text_length > 0) {
205 // Backspace was pressed and we have characters to delete
206 buffer[--text_length] = '\0';
207 } else if (key_id != 0 && text_length < buffer_size - 1 &&
208 !(input_event.type == INPUT_UP ||
209 input_event.type == INPUT_DOWN ||
210 input_event.type == INPUT_LEFT ||
211 input_event.type == INPUT_RIGHT)) {
212 // A printable character was typed
213 buffer[text_length++] = key_id;
214 buffer[text_length] = '\0';
215 }
216 }
217
218 return confirmed;
219}
bool get_input_blocking(input_event_t *event)
Get the next input event (blocking)
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.
Structure for advanced input events with context data.
Definition input_types.h:43

◆ init_output_handler()

bool init_output_handler ( void )

Initialize the output handler.

Sets up the output handling system. This function must be called before any other output functions.

Returns
true on success, false on failure

Definition at line 26 of file output_handler.c.

26 {
27 if (!nc) {
28 log_msg(ERROR, "output_handler", "Null Notcurses instance provided");
29 return false;
30 }
31
32 if (!stdplane) {
33 log_msg(ERROR, "output_handler", "Null standard plane provided");
34 return false;
35 }
36 return true;
37}

◆ print_menu()

void print_menu ( const char * title,
const char ** options,
int option_count,
int selected_index,
int y,
int x,
uint64_t title_channel,
uint64_t option_channel,
uint64_t selected_channel )

Print a menu with selection highlighting.

Renders a menu with the specified options, highlighting the selected option.

Parameters
titleThe menu title
optionsArray of option strings
option_countNumber of options
selected_indexIndex of the selected option
yThe Y coordinate (row) for the menu start
xThe X coordinate (column) for the menu start
title_channelThe color channel for the title
option_channelThe color channel for the options
selected_channelThe color channel for the selected option
Returns
true on success, false on failure

Definition at line 124 of file output_handler.c.

128 {
129 if (!stdplane || !title || !options || option_count <= 0) {
130 log_msg(ERROR, "output_handler", "Invalid menu parameters");
131 return;
132 }
133
134 // Print title
135 ncplane_set_channels(stdplane, title_channel);
136 ncplane_putstr_yx(stdplane, y, x, title);
137
138 // Print options
139 for (int i = 0; i < option_count; i++) {
140 if (i == selected_index) {
141 // Highlight selected option
142 ncplane_set_channels(stdplane, selected_channel);
143 } else {
144 ncplane_set_channels(stdplane, option_channel);
145 }
146 ncplane_putstr_yx(stdplane, y + i + 1, x, options[i]);
147 }
148}

◆ print_menu_default()

void print_menu_default ( const char * title,
const char ** options,
int option_count,
int selected_index,
int y,
int x )

Print a menu with selection highlighting using default colors.

Renders a menu with the specified options, highlighting the selected option. Uses default colors for normal options and inverted colors for the selected option.

Parameters
titleThe menu title
optionsArray of option strings
option_countNumber of options
selected_indexIndex of the selected option
yThe Y coordinate (row) for the menu start
xThe X coordinate (column) for the menu start

Definition at line 150 of file output_handler.c.

151 {
152 print_menu(title, options, option_count, selected_index, y, x,
153 DEFAULT_COLORS, DEFAULT_COLORS, INVERTED_COLORS);
154}
void print_menu(const char *title, const char **options, int option_count, int selected_index, int y, int x, uint64_t title_channel, uint64_t option_channel, uint64_t selected_channel)
Print a menu with selection highlighting.

◆ print_text()

void print_text ( int y,
int x,
const char * text,
uint64_t ncchannel )

Print text at a specific position.

Renders text at the specified coordinates with the given foreground and background colors.

Parameters
yThe Y coordinate (row)
xThe X coordinate (column)
textThe text to render
ncchannelThe color channel (foreground and background)
Returns
true on success, false on failure

Definition at line 50 of file output_handler.c.

50 {
51 if (!stdplane || !text) {
52 log_msg(ERROR, "output_handler", "Output handler not initialized or null text");
53 return;
54 }
55
56 // Set the channels and print the text
57 ncplane_set_channels(stdplane, ncchannel);
58 ncplane_putstr_yx(stdplane, y, x, text);
59}

◆ print_text_default()

void print_text_default ( int y,
int x,
const char * text )

Print text at a specific position with default colors.

Renders text at the specified coordinates with the default colors.

Parameters
yThe Y coordinate (row)
xThe X coordinate (column)
textThe text to render
Returns
true on success, false on failure

Definition at line 61 of file output_handler.c.

61 {
62 print_text(y, x, text, DEFAULT_COLORS);
63}
void print_text(int y, int x, const char *text, uint64_t ncchannel)
Print text at a specific position.

◆ print_text_multi_line()

void print_text_multi_line ( int y,
int x,
const char * text,
int max_width,
u_int64_t ncchannel )

Print multi-line text with word wrapping.

Renders text with word wrapping at the specified coordinates.

Parameters
yThe Y coordinate (row)
xThe X coordinate (column)
textThe text to render
max_widthThe maximum width for line wrapping
ncchannelThe color channel (foreground and background)

◆ print_text_multi_line_default()

void print_text_multi_line_default ( int y,
int x,
const char * text,
int max_width )

Print multi-line text with word wrapping and default colors.

Renders text with word wrapping at the specified coordinates using default colors.

Parameters
yThe Y coordinate (row)
xThe X coordinate (column)
textThe text to render
max_widthThe maximum width for line wrapping

Definition at line 99 of file output_handler.c.

99 {
100 print_text_multi_line(y, x, text, max_width, DEFAULT_COLORS);
101}

◆ print_text_multi_strings()

void print_text_multi_strings ( int y,
int x,
const char * text[],
int count,
uint64_t ncchannel )

Print multiple strings on consecutive lines.

Renders an array of strings starting at the specified coordinates.

Parameters
yThe Y coordinate (row) for the first string
xThe X coordinate (column)
textArray of strings to render
countNumber of strings in the array
ncchannelThe color channel (foreground and background)

Definition at line 103 of file output_handler.c.

103 {
104 if (!stdplane || !text || count <= 0) {
105 log_msg(ERROR, "output_handler", "Invalid parameters for print_text_multi_strings");
106 return;
107 }
108
109 // Set the channels
110 ncplane_set_channels(stdplane, ncchannel);
111
112 // Print each string on a new line
113 for (int i = 0; i < count; i++) {
114 if (text[i]) {
115 ncplane_putstr_yx(stdplane, y + i, x, text[i]);
116 }
117 }
118}

◆ print_text_multi_strings_default()

void print_text_multi_strings_default ( int y,
int x,
const char * text[],
int count )

Print multiple strings on consecutive lines with default colors.

Renders an array of strings starting at the specified coordinates using default colors.

Parameters
yThe Y coordinate (row) for the first string
xThe X coordinate (column)
textArray of strings to render
countNumber of strings in the array

Definition at line 120 of file output_handler.c.

120 {
121 print_text_multi_strings(y, x, text, count, DEFAULT_COLORS);
122}
void print_text_multi_strings(int y, int x, const char *text[], int count, uint64_t ncchannel)
Print multiple strings on consecutive lines.

◆ render_frame()

bool render_frame ( void )

Render the current frame.

Renders all changes to the screen.

Returns
true on success, false on failure

Definition at line 246 of file output_handler.c.

246 {
247 if (!nc) {
248 log_msg(ERROR, "output_handler", "Output handler not initialized");
249 return false;
250 }
251
252 // Render all changes directly
253 int ret = notcurses_render(nc);
254 return ret >= 0;
255}

◆ show_message_screen()

void show_message_screen ( const char * message,
const char * continue_message,
int y,
int x )

Show a message screen.

Displays a message on the screen and waits for user input.

Parameters
messageThe message to display
continue_messageOptional message explaining how to continue (can be NULL)
yThe Y coordinate (row) for the message
xThe X coordinate (column) for the message

Definition at line 221 of file output_handler.c.

221 {
222 if (!stdplane || !message) {
223 log_msg(ERROR, "output_handler", "Invalid parameters for show_message_screen");
224 return;
225 }
226
227 // Clear screen
228 clear_screen();
229
230 // Display message
231 print_text_default(y, x, message);
232
233 // Display continue message if provided
234 if (continue_message) {
235 print_text_default(y + 2, x, continue_message);
236 }
237
238 // Render the frame
239 render_frame();
240
241 // Wait for any input to continue
242 input_event_t input_event;
243 while (!get_input_blocking(&input_event));
244}

◆ shutdown_output_handler()

void shutdown_output_handler ( void )

Shutdown the output handler.

Cleans up resources.

Definition at line 270 of file output_handler.c.

270 {
271 // Reset the globals
272 nc = NULL;
273 stdplane = NULL;
274}