libctru  v1.6.0
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  int fg; ///< Foreground color
98  int 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 
116 /// Console debug devices supported by libnds.
117 typedef enum {
118  debugDevice_NULL, ///< Swallows prints to stderr
119  debugDevice_SVC, ///< Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive debuggers
120  debugDevice_CONSOLE, ///< Directs stderr debug statements to 3DS console window
121  debugDevice_3DMOO = debugDevice_SVC,
122 } debugDevice;
123 
124 /**
125  * @brief Loads the font into the console.
126  * @param console Pointer to the console to update, if NULL it will update the current console.
127  * @param font The font to load.
128  */
129 void consoleSetFont(PrintConsole* console, ConsoleFont* font);
130 
131 /**
132  * @brief Sets the print window.
133  * @param console Console to set, if NULL it will set the current console window.
134  * @param x X location of the window.
135  * @param y Y location of the window.
136  * @param width Width of the window.
137  * @param height Height of the window.
138  */
139 void consoleSetWindow(PrintConsole* console, int x, int y, int width, int height);
140 
141 /**
142  * @brief Gets a pointer to the console with the default values.
143  * This should only be used when using a single console or without changing the console that is returned, otherwise use consoleInit().
144  * @return A pointer to the console with the default values.
145  */
147 
148 /**
149  * @brief Make the specified console the render target.
150  * @param console A pointer to the console struct (must have been initialized with consoleInit(PrintConsole* console)).
151  * @return A pointer to the previous console.
152  */
154 
155 /**
156  * @brief Initialise the console.
157  * @param screen The screen to use for the console.
158  * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
159  * @return A pointer to the current console.
160  */
162 
163 /**
164  * @brief Initializes debug console output on stderr to the specified device.
165  * @param device The debug device (or devices) to output debug print statements to.
166  */
167 void consoleDebugInit(debugDevice device);
168 
169 /// Clears the screan by using iprintf("\x1b[2J");
170 void consoleClear(void);
171 
172 #ifdef __cplusplus
173 }
174 #endif
A font struct for the console.
Definition: console.h:41
PrintConsole * consoleSelect(PrintConsole *console)
Make the specified console the render target.
bool consoleInitialised
True if the console is initialized.
Definition: console.h:103
PrintConsole * consoleInit(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
ConsoleFont font
Font of the console.
Definition: console.h:78
u8 * gfx
A pointer to the font graphics.
Definition: console.h:43
Various system types.
bool(* ConsolePrint)(void *con, int c)
A callback for printing a character.
Definition: console.h:38
uint16_t u16
16-bit unsigned integer
Definition: types.h:22
int consoleHeight
Height of the console hardware layer in characters.
Definition: console.h:89
int cursorX
Current X location of the cursor (as a tile offset by default)
Definition: console.h:82
int windowWidth
Window width in characters (not implemented)
Definition: console.h:93
int cursorY
Current Y location of the cursor (as a tile offset by default)
Definition: console.h:83
int windowHeight
Window height in characters (not implemented)
Definition: console.h:94
int flags
Reverse/bright flags.
Definition: console.h:99
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
int windowY
Window Y location in characters (not implemented)
Definition: console.h:92
u16 asciiOffset
Offset to the first valid character in the font table.
Definition: console.h:44
Directs stderr debug statements to 3DS console window.
Definition: console.h:120
LCD Screens manipulation.
Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive...
Definition: console.h:119
debugDevice
Console debug devices supported by libnds.
Definition: console.h:117
void consoleDebugInit(debugDevice device)
Initializes debug console output on stderr to the specified device.
ConsolePrint PrintChar
Callback for printing a character. Should return true if it has handled rendering the graphics (else ...
Definition: console.h:101
int prevCursorY
Internal state.
Definition: console.h:86
void consoleSetWindow(PrintConsole *console, int x, int y, int width, int height)
Sets the print window.
int windowX
Window X location in characters (not implemented)
Definition: console.h:91
void consoleSetFont(PrintConsole *console, ConsoleFont *font)
Loads the font into the console.
Swallows prints to stderr.
Definition: console.h:118
int fg
Foreground color.
Definition: console.h:97
int prevCursorX
Internal state.
Definition: console.h:85
gfxScreen_t
Available screens.
Definition: gfx.h:20
int consoleWidth
Width of the console hardware layer in characters.
Definition: console.h:88
int tabSize
Size of a tab.
Definition: console.h:96
u16 numChars
Number of characters in the font graphics.
Definition: console.h:45
void consoleClear(void)
Clears the screan by using iprintf("\x1b[2J");.
u16 * frameBuffer
Framebuffer address.
Definition: console.h:80
PrintConsole * consoleGetDefault(void)
Gets a pointer to the console with the default values.
Console structure used to store the state of a console render context.
Definition: console.h:76
int bg
Background color.
Definition: console.h:98