libnx v4.10.0
Loading...
Searching...
No Matches
console.h
Go to the documentation of this file.
1/**
2 * @file console.h
3 * @brief Framebuffer text console.
4 * @author yellows8
5 * @author WinterMute
6 * @copyright libnx Authors
7 *
8 * Provides stdio integration for printing to the Switch screen as well as debug print
9 * functionality provided by stderr.
10 *
11 * General usage is to initialize the console by:
12 * @code
13 * consoleInit(NULL)
14 * @endcode
15 * optionally customizing the console usage by passing a pointer to a custom PrintConsole struct.
16 */
17#pragma once
18#include "../../types.h"
19
20#define CONSOLE_ESC(x) "\x1b[" #x
21#define CONSOLE_RESET CONSOLE_ESC(0m)
22#define CONSOLE_BLACK CONSOLE_ESC(30m)
23#define CONSOLE_RED CONSOLE_ESC(31;1m)
24#define CONSOLE_GREEN CONSOLE_ESC(32;1m)
25#define CONSOLE_YELLOW CONSOLE_ESC(33;1m)
26#define CONSOLE_BLUE CONSOLE_ESC(34;1m)
27#define CONSOLE_MAGENTA CONSOLE_ESC(35;1m)
28#define CONSOLE_CYAN CONSOLE_ESC(36;1m)
29#define CONSOLE_WHITE CONSOLE_ESC(37;1m)
30
31// Forward declaration
32typedef struct PrintConsole PrintConsole;
33
34/// Renderer interface for the console.
35typedef struct ConsoleRenderer
36{
37 bool (*init)(PrintConsole* con);
38 void (*deinit)(PrintConsole* con);
39 void (*drawChar)(PrintConsole* con, int x, int y, int c);
40 void (*scrollWindow)(PrintConsole* con);
41 void (*flushAndSwap)(PrintConsole* con);
43
44/// A font struct for the console.
45typedef struct ConsoleFont
46{
47 const void* gfx; ///< A pointer to the font graphics
48 u16 asciiOffset; ///< Offset to the first valid character in the font table
49 u16 numChars; ///< Number of characters in the font graphics
50 u16 tileWidth;
51 u16 tileHeight;
53
54/**
55 * @brief Console structure used to store the state of a console render context.
56 *
57 * Default values from consoleGetDefault();
58 * @code
59 * PrintConsole defaultConsole =
60 * {
61 * //Font:
62 * {
63 * default_font_bin, //font gfx
64 * 0, //first ascii character in the set
65 * 256, //number of characters in the font set
66 * 16, //tile width
67 * 16, //tile height
68 * },
69 * NULL, //renderer
70 * 0,0, //cursorX cursorY
71 * 0,0, //prevcursorX prevcursorY
72 * 80, //console width
73 * 45, //console height
74 * 0, //window x
75 * 0, //window y
76 * 80, //window width
77 * 45, //window height
78 * 3, //tab size
79 * 7, // foreground color
80 * 0, // background color
81 * 0, // flags
82 * false //console initialized
83 * };
84 * @endcode
85 */
87{
88 ConsoleFont font; ///< Font of the console
89 ConsoleRenderer* renderer; ///< Renderer of the console
90
91 int cursorX; ///< Current X location of the cursor (as a tile offset by default)
92 int cursorY; ///< Current Y location of the cursor (as a tile offset by default)
93
94 int prevCursorX; ///< Internal state
95 int prevCursorY; ///< Internal state
96
97 int consoleWidth; ///< Width of the console hardware layer in characters
98 int consoleHeight; ///< Height of the console hardware layer in characters
99
100 int windowX; ///< Window X location in characters
101 int windowY; ///< Window Y location in characters
102 int windowWidth; ///< Window width in characters
103 int windowHeight; ///< Window height in characters
104
105 int tabSize; ///< Size of a tab
106 u16 fg; ///< Foreground color
107 u16 bg; ///< Background color
108 int flags; ///< Reverse/bright flags
109
110 bool consoleInitialised; ///< True if the console is initialized
111};
112
113#define CONSOLE_COLOR_BOLD (1<<0) ///< Bold text
114#define CONSOLE_COLOR_FAINT (1<<1) ///< Faint text
115#define CONSOLE_ITALIC (1<<2) ///< Italic text
116#define CONSOLE_UNDERLINE (1<<3) ///< Underlined text
117#define CONSOLE_BLINK_SLOW (1<<4) ///< Slow blinking text
118#define CONSOLE_BLINK_FAST (1<<5) ///< Fast blinking text
119#define CONSOLE_COLOR_REVERSE (1<<6) ///< Reversed color text
120#define CONSOLE_CONCEAL (1<<7) ///< Concealed text
121#define CONSOLE_CROSSED_OUT (1<<8) ///< Crossed out text
122#define CONSOLE_FG_CUSTOM (1<<9) ///< Foreground custom color
123#define CONSOLE_BG_CUSTOM (1<<10) ///< Background custom color
124#define CONSOLE_COLOR_FG_BRIGHT (1<<11) ///< Bright foreground color
125#define CONSOLE_COLOR_BG_BRIGHT (1<<12) ///< Bright background color
126
127/// Console debug devices supported by libnx.
128typedef enum {
129 debugDevice_NULL, ///< Swallows prints to stderr
130 debugDevice_SVC, ///< Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive debuggers
131 debugDevice_CONSOLE, ///< Directs stderr debug statements to Switch console window
133
134/**
135 * @brief Loads the font into the console.
136 * @param console Pointer to the console to update, if NULL it will update the current console.
137 * @param font The font to load.
138 */
140
141/**
142 * @brief Sets the print window.
143 * @param console Console to set, if NULL it will set the current console window.
144 * @param x X location of the window.
145 * @param y Y location of the window.
146 * @param width Width of the window.
147 * @param height Height of the window.
148 */
149void consoleSetWindow(PrintConsole* console, int x, int y, int width, int height);
150
151/**
152 * @brief Gets a pointer to the console with the default values.
153 * This should only be used when using a single console or without changing the console that is returned, otherwise use consoleInit().
154 * @return A pointer to the console with the default values.
155 */
157
158/**
159 * @brief Make the specified console the render target.
160 * @param console A pointer to the console struct (must have been initialized with consoleInit(PrintConsole* console)).
161 * @return A pointer to the previous console.
162 */
164
165/**
166 * @brief Initialise the console.
167 * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
168 * @return A pointer to the current console.
169 */
171
172/**
173 * @brief Deinitialise the console.
174 * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
175 */
177
178/**
179 * @brief Updates the console, submitting a new frame to the display.
180 * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
181 * @remark This function should be called periodically. Failure to call this function will result in lack of screen updating.
182 */
184
185/**
186 * @brief Initializes debug console output on stderr to the specified device.
187 * @param device The debug device (or devices) to output debug print statements to.
188 */
190
191/// Clears the screan by using printf("\x1b[2J");
192void consoleClear(void);
void consoleExit(PrintConsole *console)
Deinitialise the console.
void consoleSetWindow(PrintConsole *console, int x, int y, int width, int height)
Sets the print window.
void consoleSetFont(PrintConsole *console, ConsoleFont *font)
Loads the font into the console.
PrintConsole * consoleInit(PrintConsole *console)
Initialise the console.
PrintConsole * consoleGetDefault(void)
Gets a pointer to the console with the default values.
void consoleUpdate(PrintConsole *console)
Updates the console, submitting a new frame to the display.
PrintConsole * consoleSelect(PrintConsole *console)
Make the specified console the render target.
debugDevice
Console debug devices supported by libnx.
Definition console.h:128
@ debugDevice_SVC
Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive...
Definition console.h:130
@ debugDevice_CONSOLE
Directs stderr debug statements to Switch console window.
Definition console.h:131
@ debugDevice_NULL
Swallows prints to stderr.
Definition console.h:129
void consoleDebugInit(debugDevice device)
Initializes debug console output on stderr to the specified device.
void consoleClear(void)
Clears the screan by using printf("\x1b[2J");.
A font struct for the console.
Definition console.h:46
const void * gfx
A pointer to the font graphics.
Definition console.h:47
u16 asciiOffset
Offset to the first valid character in the font table.
Definition console.h:48
u16 numChars
Number of characters in the font graphics.
Definition console.h:49
Renderer interface for the console.
Definition console.h:36
Console structure used to store the state of a console render context.
Definition console.h:87
int cursorX
Current X location of the cursor (as a tile offset by default)
Definition console.h:91
int consoleWidth
Width of the console hardware layer in characters.
Definition console.h:97
int flags
Reverse/bright flags.
Definition console.h:108
int windowX
Window X location in characters.
Definition console.h:100
u16 bg
Background color.
Definition console.h:107
int tabSize
Size of a tab.
Definition console.h:105
int cursorY
Current Y location of the cursor (as a tile offset by default)
Definition console.h:92
int prevCursorX
Internal state.
Definition console.h:94
int prevCursorY
Internal state.
Definition console.h:95
int consoleHeight
Height of the console hardware layer in characters.
Definition console.h:98
ConsoleRenderer * renderer
Renderer of the console.
Definition console.h:89
int windowWidth
Window width in characters.
Definition console.h:102
int windowHeight
Window height in characters.
Definition console.h:103
bool consoleInitialised
True if the console is initialized.
Definition console.h:110
u16 fg
Foreground color.
Definition console.h:106
ConsoleFont font
Font of the console.
Definition console.h:88
int windowY
Window Y location in characters.
Definition console.h:101
uint16_t u16
16-bit unsigned integer.
Definition types.h:20