libctru  v2.3.1
console.h
Go to the documentation of this file.
1 /**
2  * @file console.h
3  * @brief 3ds stdio support.
4  *
5  * Provides stdio integration for printing to the 3DS screen as well as debug print
6  * functionality provided by stderr.
7  *
8  * General usage is to initialize the console by:
9  * @code
10  * consoleDemoInit()
11  * @endcode
12  * or to customize the console usage by:
13  * @code
14  * consoleInit()
15  * @endcode
16  */
17 #pragma once
18 
19 #include <3ds/types.h>
20 #include <3ds/gfx.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #define CONSOLE_ESC(x) "\x1b[" #x
27 #define CONSOLE_RESET CONSOLE_ESC(0m)
28 #define CONSOLE_BLACK CONSOLE_ESC(30m)
29 #define CONSOLE_RED CONSOLE_ESC(31;1m)
30 #define CONSOLE_GREEN CONSOLE_ESC(32;1m)
31 #define CONSOLE_YELLOW CONSOLE_ESC(33;1m)
32 #define CONSOLE_BLUE CONSOLE_ESC(34;1m)
33 #define CONSOLE_MAGENTA CONSOLE_ESC(35;1m)
34 #define CONSOLE_CYAN CONSOLE_ESC(36;1m)
35 #define CONSOLE_WHITE CONSOLE_ESC(37;1m)
36 
37 /// A callback for printing a character.
38 typedef bool(*ConsolePrint)(void* con, int c);
39 
40 /// A font struct for the console.
41 typedef struct ConsoleFont
42 {
43  u8* gfx; ///< A pointer to the font graphics
44  u16 asciiOffset; ///< Offset to the first valid character in the font table
45  u16 numChars; ///< Number of characters in the font graphics
47 
48 /**
49  * @brief Console structure used to store the state of a console render context.
50  *
51  * Default values from consoleGetDefault();
52  * @code
53  * PrintConsole defaultConsole =
54  * {
55  * //Font:
56  * {
57  * (u8*)default_font_bin, //font gfx
58  * 0, //first ascii character in the set
59  * 128, //number of characters in the font set
60  * },
61  * 0,0, //cursorX cursorY
62  * 0,0, //prevcursorX prevcursorY
63  * 40, //console width
64  * 30, //console height
65  * 0, //window x
66  * 0, //window y
67  * 32, //window width
68  * 24, //window height
69  * 3, //tab size
70  * 0, //font character offset
71  * 0, //print callback
72  * false //console initialized
73  * };
74  * @endcode
75  */
76 typedef struct PrintConsole
77 {
78  ConsoleFont font; ///< Font of the console
79 
80  u16 *frameBuffer; ///< Framebuffer address
81 
82  int cursorX; ///< Current X location of the cursor (as a tile offset by default)
83  int cursorY; ///< Current Y location of the cursor (as a tile offset by default)
84 
85  int prevCursorX; ///< Internal state
86  int prevCursorY; ///< Internal state
87 
88  int consoleWidth; ///< Width of the console hardware layer in characters
89  int consoleHeight; ///< Height of the console hardware layer in characters
90 
91  int windowX; ///< Window X location in characters (not implemented)
92  int windowY; ///< Window Y location in characters (not implemented)
93  int windowWidth; ///< Window width in characters (not implemented)
94  int windowHeight; ///< Window height in characters (not implemented)
95 
96  int tabSize; ///< Size of a tab
97  u16 fg; ///< Foreground color
98  u16 bg; ///< Background color
99  int flags; ///< Reverse/bright flags
100 
101  ConsolePrint PrintChar; ///< Callback for printing a character. Should return true if it has handled rendering the graphics (else the print engine will attempt to render via tiles).
102 
103  bool consoleInitialised; ///< True if the console is initialized
104 }PrintConsole;
105 
106 #define CONSOLE_COLOR_BOLD (1<<0) ///< Bold text
107 #define CONSOLE_COLOR_FAINT (1<<1) ///< Faint text
108 #define CONSOLE_ITALIC (1<<2) ///< Italic text
109 #define CONSOLE_UNDERLINE (1<<3) ///< Underlined text
110 #define CONSOLE_BLINK_SLOW (1<<4) ///< Slow blinking text
111 #define CONSOLE_BLINK_FAST (1<<5) ///< Fast blinking text
112 #define CONSOLE_COLOR_REVERSE (1<<6) ///< Reversed color text
113 #define CONSOLE_CONCEAL (1<<7) ///< Concealed text
114 #define CONSOLE_CROSSED_OUT (1<<8) ///< Crossed out text
115 #define CONSOLE_FG_CUSTOM (1<<9) ///< Foreground custom color
116 #define CONSOLE_BG_CUSTOM (1<<10) ///< Background custom color
117 
118 /// Console debug devices supported by libnds.
119 typedef enum {
120  debugDevice_NULL, ///< Swallows prints to stderr
121  debugDevice_SVC, ///< Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive debuggers
122  debugDevice_CONSOLE, ///< Directs stderr debug statements to 3DS console window
123  debugDevice_3DMOO = debugDevice_SVC,
124 } debugDevice;
125 
126 /**
127  * @brief Loads the font into the console.
128  * @param console Pointer to the console to update, if NULL it will update the current console.
129  * @param font The font to load.
130  */
132 
133 /**
134  * @brief Sets the print window.
135  * @param console Console to set, if NULL it will set the current console window.
136  * @param x X location of the window.
137  * @param y Y location of the window.
138  * @param width Width of the window.
139  * @param height Height of the window.
140  */
141 void consoleSetWindow(PrintConsole* console, int x, int y, int width, int height);
142 
143 /**
144  * @brief Gets a pointer to the console with the default values.
145  * This should only be used when using a single console or without changing the console that is returned, otherwise use consoleInit().
146  * @return A pointer to the console with the default values.
147  */
149 
150 /**
151  * @brief Make the specified console the render target.
152  * @param console A pointer to the console struct (must have been initialized with consoleInit(PrintConsole* console)).
153  * @return A pointer to the previous console.
154  */
156 
157 /**
158  * @brief Initialise the console.
159  * @param screen The screen to use for the console.
160  * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
161  * @return A pointer to the current console.
162  */
164 
165 /**
166  * @brief Initializes debug console output on stderr to the specified device.
167  * @param device The debug device (or devices) to output debug print statements to.
168  */
170 
171 /// Clears the screen by using iprintf("\x1b[2J");
172 void consoleClear(void);
173 
174 #ifdef __cplusplus
175 }
176 #endif
bool(* ConsolePrint)(void *con, int c)
A callback for printing a character.
Definition: console.h:38
PrintConsole * consoleGetDefault(void)
Gets a pointer to the console with the default values.
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(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
debugDevice
Console debug devices supported by libnds.
Definition: console.h:119
@ debugDevice_SVC
Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive...
Definition: console.h:121
@ debugDevice_CONSOLE
Directs stderr debug statements to 3DS console window.
Definition: console.h:122
@ debugDevice_NULL
Swallows prints to stderr.
Definition: console.h:120
PrintConsole * consoleSelect(PrintConsole *console)
Make the specified console the render target.
void consoleDebugInit(debugDevice device)
Initializes debug console output on stderr to the specified device.
void consoleClear(void)
Clears the screen by using iprintf("\x1b[2J");.
Simple framebuffer API.
gfxScreen_t
Screen IDs.
Definition: gfx.h:25
A font struct for the console.
Definition: console.h:42
u16 asciiOffset
Offset to the first valid character in the font table.
Definition: console.h:44
u8 * gfx
A pointer to the font graphics.
Definition: console.h:43
u16 numChars
Number of characters in the font graphics.
Definition: console.h:45
Console structure used to store the state of a console render context.
Definition: console.h:77
int cursorX
Current X location of the cursor (as a tile offset by default)
Definition: console.h:82
int consoleWidth
Width of the console hardware layer in characters.
Definition: console.h:88
int flags
Reverse/bright flags.
Definition: console.h:99
int windowX
Window X location in characters (not implemented)
Definition: console.h:91
u16 bg
Background color.
Definition: console.h:98
int tabSize
Size of a tab.
Definition: console.h:96
int cursorY
Current Y location of the cursor (as a tile offset by default)
Definition: console.h:83
int prevCursorX
Internal state.
Definition: console.h:85
int prevCursorY
Internal state.
Definition: console.h:86
int consoleHeight
Height of the console hardware layer in characters.
Definition: console.h:89
int windowWidth
Window width in characters (not implemented)
Definition: console.h:93
int windowHeight
Window height in characters (not implemented)
Definition: console.h:94
u16 * frameBuffer
Framebuffer address.
Definition: console.h:80
bool consoleInitialised
True if the console is initialized.
Definition: console.h:103
ConsolePrint PrintChar
Callback for printing a character. Should return true if it has handled rendering the graphics (else ...
Definition: console.h:101
u16 fg
Foreground color.
Definition: console.h:97
ConsoleFont font
Font of the console.
Definition: console.h:78
int windowY
Window Y location in characters (not implemented)
Definition: console.h:92
Various system types.
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
uint16_t u16
16-bit unsigned integer
Definition: types.h:22