libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
console.hpp
Go to the documentation of this file.
1/**
2 * @file console.hpp
3 * @brief OpenGL-backed in-game terminal console for libmx2.
4 *
5 * Provides four related classes:
6 * - console::CommandQueue – thread-safe command dispatch queue
7 * - console::ConsoleChars – per-character SDL_Surface glyph cache
8 * - console::Console – core text-entry / output widget (SDL_Surface)
9 * - console::GLConsole – OpenGL wrapper that renders Console via GLSprite
10 *
11 * Depends on mx.hpp, gl.hpp (forward-declared) and SDL2/SDL_ttf.
12 * On Emscripten the threading primitives are replaced with no-op stubs.
13 */
14#ifndef __CONSOLE__H__MX2
15#define __CONSOLE__H__MX2
16
17#include<iostream>
18#include<string>
19#include<sstream>
20#include<unordered_map>
21#include<memory>
22#include<vector>
23#include<functional>
24#include<mutex>
25#include<queue>
26#include<atomic>
27#include<condition_variable>
28#include<thread>
29#include"mx.hpp"
30
31namespace gl {
32 class GLWindow;
33 class GLSprite;
34 class ShaderProgram;
35}
36
37#ifndef GL_UNSIGNED_INT
38typedef unsigned int GLuint;
39#endif
40
41#if defined(__EMSCRIPTEN__)
42 class EmscriptenMutex {
43 public:
44 void lock() {}
45 void unlock() {}
46 };
47
48 class EmscriptenGuard {
49 public:
50 EmscriptenGuard(EmscriptenMutex&) {}
51 };
52
53 #define THREAD_MUTEX EmscriptenMutex
54 #define THREAD_GUARD(mutex) EmscriptenGuard guard(mutex)
55 #define THREAD_ENABLED 0
56#else
57 #define THREAD_MUTEX std::recursive_mutex
58 #define THREAD_GUARD(mutex) std::lock_guard<std::recursive_mutex> guard(mutex)
59 #define THREAD_ENABLED 1
60#endif
61
62
63namespace console {
64
65 /**
66 * @class CommandQueue
67 * @brief Thread-safe FIFO queue for dispatching console commands to a worker thread.
68 *
69 * Commands are pushed from the rendering / event thread and consumed by a dedicated
70 * background thread that invokes the associated callback.
71 */
73 public:
74 /** @brief A single command entry with its text and completion callback. */
75 struct Command {
76 std::string text; ///< Raw command string.
77 std::function<void(const std::string&, std::ostream&)> callback; ///< Handler invoked on the worker thread.
78 };
79
80 /** @brief Push a command onto the queue (thread-safe). */
81 void push(const Command& cmd);
82
83 /**
84 * @brief Pop the next command (blocking on native, non-blocking on Emscripten).
85 * @param cmd Output: the dequeued command.
86 * @return @c true if a command was retrieved; @c false if the queue was shut down.
87 */
88 bool pop(Command& cmd);
89
90 /** @brief Signal the worker to drain and stop. */
91 void shutdown();
92
93 private:
94 std::queue<Command> queue;
95#if THREAD_ENABLED
96 std::mutex queue_mutex;
97 std::condition_variable cv;
98#else
99 EmscriptenMutex queue_mutex;
100#endif
101 std::atomic<bool> active{true};
102 };
103
104 /**
105 * @class ConsoleChars
106 * @brief Glyph cache that maps each ASCII character to a pre-rendered SDL_Surface.
107 *
108 * Surfaces are produced via SDL_ttf and stored in an unordered_map keyed by char.
109 * Recreate by calling load() after font changes.
110 */
112 public:
113 ConsoleChars();
115
116 ConsoleChars(const ConsoleChars&) = delete;
120
121 /**
122 * @brief Load/reload the glyph cache from a font file.
123 * @param fnt Path to a TTF font file.
124 * @param size Point size.
125 * @param c Colour applied to each glyph surface.
126 */
127 void load(const std::string &fnt, int size, const SDL_Color &c);
128
129 /** @brief Release all cached SDL_Surface objects. */
130 void clear();
131
132 std::unordered_map<char, SDL_Surface *> characters; ///< Glyph surfaces keyed by char.
133 protected:
134 std::unique_ptr<mx::Font> font;
135 };
136
142
143 class GLConsole;
144
145 /**
146 * @class Console
147 * @brief Core text-console widget backed by an SDL_Surface.
148 *
149 * Manages an output buffer, an editable input line, command history,
150 * optional fade-in/out animation, and a thread-safe message queue.
151 * Rendered to an SDL_Surface by drawText(); GLConsole uploads that surface
152 * to a GL texture each frame.
153 */
154 class Console {
155 public:
156 Console();
157 ~Console();
158
159 Console(const Console&) = delete;
160 Console& operator=(const Console&) = delete;
161 Console(Console&&) = delete;
163
164 friend class GLConsole;
165 mx::mxUtil *util; ///< Pointer to asset-path utilities (not owned).
166
167 /**
168 * @brief Set position and size of the console rectangle.
169 * @param x,y Top-left corner in pixels.
170 * @param w,h Width and height in pixels.
171 */
172 void create(int x, int y, int w, int h);
173
174 /**
175 * @brief Load font and rebuild the glyph cache.
176 * @param fnt Path to a TTF font file.
177 * @param size Point size.
178 * @param col Glyph render colour.
179 */
180 void load(const std::string &fnt, int size, const SDL_Color &col);
181
182 /** @brief Clear the output buffer and reset scroll. */
183 void clear();
184
185 /**
186 * @brief Append text to the output buffer.
187 * @param str String to append (may contain newlines).
188 */
189 void print(const std::string &str);
190
191 /**
192 * @brief Append text from any thread (uses the internal message queue).
193 * @param str String to append.
194 */
195 void thread_safe_print(const std::string &str);
196
197 /**
198 * @brief Handle a single character key press for the input line.
199 * @param c Character received from SDL_TEXTINPUT or SDL_KEYDOWN.
200 */
201 void keypress(char c);
202
203 /**
204 * @brief Render the console contents to an internal SDL_Surface.
205 * @return Pointer to the freshly rendered surface (owned by Console).
206 */
207 SDL_Surface *drawText();
208
209 /** @brief Return the current input-line text. */
210 std::string getText() const;
211
212 /** @brief Replace the input-line text. */
213 void setText(const std::string& text);
214
215 /** @return Current caret position within the input line. */
216 size_t getCursorPos() const;
217
218 /** @brief Move the caret to @p pos within the input line. */
219 void setCursorPos(size_t pos);
220
221 /**
222 * @brief Tokenise and dispatch a command string.
223 * @param cmd Raw command text (may include arguments).
224 */
225 void procCmd(const std::string &cmd);
226
227 /** @brief Change the prompt prefix (default: "$ "). */
228 void setPrompt(const std::string &text);
229
230 /** @brief Jump the scroll view to the most recent output. */
231 void scrollToBottom();
232
233 /**
234 * @brief Register a command callback invoked when the user presses Enter.
235 * @param window Parent GL window.
236 * @param callback Handler receives tokenised arguments; return @c true to suppress default handling.
237 */
239 std::function<bool(gl::GLWindow *win, const std::vector<std::string> &)> callback);
240
241 void moveCursorLeft(); ///< Move caret one character to the left.
242 void moveCursorRight(); ///< Move caret one character to the right.
243 void moveCursorHome(); ///< Move caret to start of input line.
244 void moveCursorEnd(); ///< Move caret to end of input line.
245 void moveHistoryUp(); ///< Recall the previous command from history.
246 void moveHistoryDown(); ///< Move forward through command history.
247 void updateInputScrollOffset(); ///< Recalculate horizontal scroll for long input lines.
248
249 int getHeight() const { return console_rect.h; } ///< @return Pixel height of the console area.
250 int getWidth() const { return console_rect.w; } ///< @return Pixel width of the console area.
251
252 std::string promptText = "$ "; ///< Prompt string displayed before the input line.
253 Uint32 cursorBlinkTime = 0; ///< SDL_GetTicks() value of the last cursor blink toggle.
254 bool cursorVisible = true; ///< Whether the cursor is currently shown.
255 gl::GLWindow *window = nullptr;///< Owning GL window (weak reference).
256
257 void startFadeIn(); ///< Begin a fade-in animation.
258 void startFadeOut(); ///< Begin a fade-out animation.
259 bool isFading() const { return fadeState != FADE_NONE; } ///< @return @c true while animating.
260 bool isVisible() const { return alpha > 0; } ///< @return @c true if at least partially visible.
261 void show(); ///< Make the console fully visible immediately.
262 void hide(); ///< Make the console invisible immediately.
263 void reload(); ///< Reload the glyph cache with the current font settings.
264
265 /** @return @c true if the glyph cache has been populated. */
266 bool loaded() const {
268 return c_chars.characters.size() > 0;
269 }
270
271 bool isScrollDragging() const { return scrollDragging; } ///< @return Whether the scrollbar thumb is being dragged.
272
273 /**
274 * @brief Change font size and colour without reloading the widget.
275 * @param size New point size.
276 * @param col New colour.
277 */
278 void setTextAttrib(const int size, const SDL_Color &col);
279
280 /**
281 * @brief Access the internal output buffer directly as an ostream.
282 * @return Reference to the internal ostringstream.
283 */
284 std::ostringstream &bufferData();
285
286 /**
287 * @brief Register a callback for raw input-line submission (Enter key).
288 * @param callback Handler receives the raw input string; return value is unused.
289 */
290 void setInputCallback(std::function<int(gl::GLWindow *win, const std::string &)> callback);
291
292 std::string inputBuffer; ///< Current content of the input line (mirrors internal state).
293 bool needsRedraw = true; ///< Set to trigger an SDL_Surface redraw.
294 bool needsReflow = true; ///< Set to trigger line-wrapping recalculation.
295 FadeState fadeState = FADE_NONE; ///< Current fade animation state.
296
297 /** @brief Process pending messages from the thread-safe print queue (call each frame). */
299
300#if defined(__EMSCRIPTEN__)
301 /** @brief Single-threaded command queue drain (Emscripten only). */
302 void processCommandQueue();
303#endif
304
305 void scrollUp(); ///< Scroll output up by one line.
306 void scrollDown(); ///< Scroll output down by one line.
307 void resetScroll(); ///< Reset scroll to the bottom.
308 void scrollPageUp(); ///< Scroll up by one page.
309 void scrollPageDown(); ///< Scroll down by one page.
310
311 /**
312 * @brief Handle a mouse-wheel scroll event.
313 * @param mouseX,mouseY Cursor position.
314 * @param wheelY Scroll delta (positive = up).
315 */
316 void handleMouseScroll(int mouseX, int mouseY, int wheelY);
317
318 /**
319 * @brief Test whether the mouse is over the scrollbar thumb.
320 * @return @c true if the cursor is inside the scrollbar rectangle.
321 */
322 bool isMouseOverScrollbar(int mouseX, int mouseY) const;
323
324 void beginScrollDrag(int mouseY); ///< Start a scrollbar thumb drag.
325 void updateScrollDrag(int mouseY); ///< Continue a scrollbar thumb drag.
326 void endScrollDrag(); ///< Release the scrollbar thumb.
327 protected:
329 std::queue<std::string> message_queue;
331 bool userScrolling = false;
332 std::string font;
333 int font_size = 16;
334 SDL_Color color = {255, 255, 255, 255};
335 std::ostringstream data;
336 std::string input_text;
337 SDL_Surface *surface = nullptr;
338 SDL_Rect console_rect;
339 size_t cursorPos = 0;
340 size_t stopPosition = 0;
341 size_t inputCursorPos = 0;
342 int cursorX = 0;
343 int cursorY = 0;
344 bool showInput = true;
345 void checkScroll();
347 void checkForLineWrap();
348 std::function<bool(gl::GLWindow *win, const std::vector<std::string> &)> callback = nullptr;
349 std::function<int(gl::GLWindow *window, const std::string &)> callbackEnter = nullptr;
350 bool callbackSet = false;
351 std::vector<std::string> commandHistory;
352 int historyIndex = -1;
353 std::string tempBuffer;
354 unsigned char alpha = 188;
355 Uint32 fadeStartTime = 0;
356 unsigned int fadeDuration = 500;
358 struct TextLine {
359 std::string text;
360 size_t startPos;
361 size_t length;
362 };
363 std::vector<TextLine> lines;
366 std::string multilineBuffer;
367 std::string originalPrompt;
368 std::string continuationPrompt;
369 void processMultilineInput(const std::string& cmd);
370 bool checkBraceBalance(const std::string& text);
371 void calculateLines();
372 std::unique_ptr<std::thread> worker_thread;
374 std::atomic<bool> worker_active{true};
375 void worker_thread_func();
376 bool enterCallbackSet = false;
377 void clearText();
378
380 int scrollOffset = 0;
381 bool scrollDragging = false;
384 SDL_Rect scrollbarRect = {0, 0, 0, 0};
385 };
386
387 /**
388 * @class GLConsole
389 * @brief OpenGL-rendered overlay console built on top of Console.
390 *
391 * GLConsole manages a gl::GLSprite and uploads the Console's SDL_Surface
392 * to a GL texture every frame. It handles window resize, fade animation,
393 * and optional full-height stretching — making it easy to drop an interactive
394 * terminal into any GLWindow-based application.
395 */
396 class GLConsole {
397 public:
398 GLConsole();
399 ~GLConsole();
400
401 GLConsole(const GLConsole&) = delete;
402 GLConsole& operator=(const GLConsole&) = delete;
403 GLConsole(GLConsole&&) = delete;
405
406 /** @brief Destroy the GL texture and sprite resources. */
407 void release();
408
409 friend class Console;
410
411 /**
412 * @brief Load and position the console inside a specific rectangle.
413 * @param win Parent GL window.
414 * @param rc Desired console rectangle in window-local pixels.
415 * @param fnt TTF font path.
416 * @param size Font point size.
417 * @param col Text colour.
418 */
419 void load(gl::GLWindow *win, const SDL_Rect &rc, const std::string &fnt, int size, const SDL_Color &col);
420
421 /**
422 * @brief Load the console spanning the full window width.
423 * @param win Parent GL window.
424 * @param fnt TTF font path.
425 * @param size Font point size.
426 * @param col Text colour.
427 */
428 void load(gl::GLWindow *win, const std::string &fnt, int size, const SDL_Color &col);
429
430 /**
431 * @brief Upload SDL_Surface and blit the GL texture each frame.
432 * @param win Current GL window (provides gl context).
433 */
434 void draw(gl::GLWindow *win);
435
436 /**
437 * @brief Low-level SDL event handler (keyboard + mouse).
438 * @param e SDL event reference.
439 */
440 void handleEvent(SDL_Event &e);
441
442 /**
443 * @brief High-level event handler with window context.
444 * @param win Parent GL window.
445 * @param e SDL event reference.
446 */
447 void event(gl::GLWindow *win, SDL_Event &e);
448
449 /** @brief Append text to the console output. */
450 void print(const std::string &data);
451
452 /** @brief Append text followed by a newline. */
453 void println(const std::string &data);
454
455 /** @brief Thread-safe variant of print(). */
456 void thread_safe_print(const std::string &data);
457
458 /**
459 * @brief Handle window resize — rebuilds GL texture at new dimensions.
460 * @param win Parent GL window.
461 * @param w,h New viewport size in pixels.
462 */
463 void resize(gl::GLWindow *win, int w, int h);
464
465 /** @brief Change the prompt prefix string. */
466 void setPrompt(const std::string &prompt);
467
468 /** @brief Register a command callback (same semantics as Console::setCallback()). */
469 void setCallback(gl::GLWindow *window,
470 std::function<bool(gl::GLWindow *win, const std::vector<std::string> &)> callback);
471
472 /** @brief Register a raw-input callback (same semantics as Console::setInputCallback()). */
473 void setInputCallback(std::function<int(gl::GLWindow *window, const std::string &)> callback);
474
475 void show(); ///< Make console visible.
476 void hide(); ///< Hide console.
477
478 /** @brief Enable/disable vertical stretch to fill the window height. */
479 void setStretch(bool stretch) { stretch_value = stretch; }
480
481 /** @brief Change font size/colour (propagates to inner Console). */
482 void setTextAttrib(const int size, const SDL_Color &col);
483
484 int getWidth() const; ///< @return Console pixel width.
485 int getHeight() const; ///< @return Console pixel height.
486
487 /** @brief Update the inner Console's window pointer. */
488 void setWindow(gl::GLWindow *win) { console.window = win; }
489
490 /**
491 * @brief Handle built-in commands (clear, quit, help, …).
492 * @param cmd Tokenised command vector.
493 * @return @c true if the command was consumed.
494 */
495 bool procDefaultCommands(const std::vector<std::string> &cmd);
496
497 /** @brief Reset all registered callbacks to no-ops. */
499 console.callback = [](gl::GLWindow *window, const std::vector<std::string> &args) -> bool {
500 return false;
501 };
502 console.callbackEnter = [](gl::GLWindow *window, const std::string &text) -> int {
503 return 0;
504 };
505 console.callbackSet = false;
506 console.enterCallbackSet = false;
507 }
508
509 /**
510 * @brief printf-style helper that formats a string and calls print().
511 * @tparam Args Variadic format arguments.
512 * @param format printf format string.
513 * @param args Format arguments.
514 */
515 template<typename... Args>
516 void printf(const char *format, Args... args) {
517 if (format == nullptr) return;
518 int size = std::snprintf(nullptr, 0, format, args...);
519 if (size < 0) {
520 print("[printf formatting error]\n");
521 return;
522 }
523 std::vector<char> buffer(static_cast<size_t>(size) + 1);
524 std::snprintf(buffer.data(), buffer.size(), format, args...);
525 print(std::string(buffer.data()));
526 }
527
528 /** @return Stream reference to the inner output buffer. */
529 std::ostream &bufferData() { return console.bufferData(); }
530
531 /** @return Stream containing the last submitted input line. */
532 std::istream &inputData();
533
534 /** @brief Flush the thread-safe message queue into the output buffer (call each frame). */
536
537 bool isVisible() const { return console.isVisible(); } ///< @return @c true if the console is at least partially visible.
538 bool isFading() const { return console.isFading(); } ///< @return @c true while a fade animation is running.
539
540 /** @brief Set an absolute height override for the stretch mode. */
541 void setStretchHeight(int value);
542
543 /** @brief Clear the entire output buffer. */
544 void clearText();
545
546 /** @brief Drain the command queue and process pending messages (call each frame). */
547 void refresh() {
548#if defined(__EMSCRIPTEN__)
549 console.processCommandQueue();
550#endif
551 console.process_message_queue(); }
552 protected:
553 mutable std::mutex gl_mutex;
555 bool stretch_value = true;
556 SDL_Rect rc;
558 std::unique_ptr<gl::GLSprite> sprite = nullptr;
559 std::unique_ptr<gl::ShaderProgram> shader;
561 GLuint loadTextFromSurface(SDL_Surface *surf);
562 void updateTexture(GLuint tex, SDL_Surface *surf);
563 std::string font;
564 int font_size = 16;
565 SDL_Color color = {255, 255, 255, 255};
566 private:
567 std::istringstream inputStream;
568 };
569}
570
571#endif
Thread-safe FIFO queue for dispatching console commands to a worker thread.
Definition console.hpp:72
std::mutex queue_mutex
Definition console.hpp:96
bool pop(Command &cmd)
Pop the next command (blocking on native, non-blocking on Emscripten).
Definition console.cpp:80
std::queue< Command > queue
Definition console.hpp:94
void push(const Command &cmd)
Push a command onto the queue (thread-safe).
Definition console.cpp:70
std::atomic< bool > active
Definition console.hpp:101
std::condition_variable cv
Definition console.hpp:97
void shutdown()
Signal the worker to drain and stop.
Definition console.cpp:101
Glyph cache that maps each ASCII character to a pre-rendered SDL_Surface.
Definition console.hpp:111
std::unordered_map< char, SDL_Surface * > characters
Glyph surfaces keyed by char.
Definition console.hpp:132
ConsoleChars & operator=(const ConsoleChars &)=delete
ConsoleChars & operator=(ConsoleChars &&)=delete
ConsoleChars(const ConsoleChars &)=delete
void load(const std::string &fnt, int size, const SDL_Color &c)
Load/reload the glyph cache from a font file.
Definition console.cpp:41
std::unique_ptr< mx::Font > font
Definition console.hpp:134
ConsoleChars(ConsoleChars &&)=delete
void clear()
Release all cached SDL_Surface objects.
Definition console.cpp:57
std::vector< TextLine > lines
Definition console.hpp:363
Console(const Console &)=delete
FadeState fadeState
Current fade animation state.
Definition console.hpp:295
void moveCursorLeft()
Move caret one character to the left.
Definition console.cpp:821
void show()
Make the console fully visible immediately.
Definition console.cpp:1063
void resetScroll()
Reset scroll to the bottom.
Definition console.cpp:195
std::string input_text
Definition console.hpp:336
unsigned char alpha
Definition console.hpp:354
void scrollPageDown()
Scroll down by one page.
Definition console.cpp:1382
bool isVisible() const
Definition console.hpp:260
std::string font
Definition console.hpp:332
SDL_Surface * drawText()
Render the console contents to an internal SDL_Surface.
Definition console.cpp:622
void procCmd(const std::string &cmd)
Tokenise and dispatch a command string.
Definition console.cpp:975
void moveCursorEnd()
Move caret to end of input line.
Definition console.cpp:849
void calculateLines()
Definition console.cpp:550
std::unique_ptr< std::thread > worker_thread
Definition console.hpp:372
std::recursive_mutex console_mutex
Definition console.hpp:328
bool loaded() const
Definition console.hpp:266
SDL_Rect console_rect
Definition console.hpp:338
Console & operator=(Console &&)=delete
std::function< int(gl::GLWindow *window, const std::string &)> callbackEnter
Definition console.hpp:349
void updateCursorPosition()
Definition console.cpp:464
void create(int x, int y, int w, int h)
Set position and size of the console rectangle.
Definition console.cpp:142
unsigned int fadeDuration
Definition console.hpp:356
std::ostringstream & bufferData()
Access the internal output buffer directly as an ostream.
Definition console.cpp:916
void setText(const std::string &text)
Replace the input-line text.
void scrollToBottom()
Jump the scroll view to the most recent output.
Definition console.cpp:810
SDL_Rect scrollbarRect
Definition console.hpp:384
CommandQueue command_queue
Definition console.hpp:373
std::string continuationPrompt
Definition console.hpp:368
std::vector< std::string > commandHistory
Definition console.hpp:351
std::string getText() const
Return the current input-line text.
int getHeight() const
Definition console.hpp:249
SDL_Color color
Definition console.hpp:334
gl::GLWindow * window
Owning GL window (weak reference).
Definition console.hpp:255
void print(const std::string &str)
Append text to the output buffer.
Definition console.cpp:205
void endScrollDrag()
Release the scrollbar thumb.
Definition console.cpp:1429
void setPrompt(const std::string &text)
Change the prompt prefix (default: "$ ").
Uint32 cursorBlinkTime
SDL_GetTicks() value of the last cursor blink toggle.
Definition console.hpp:253
mx::mxUtil * util
Pointer to asset-path utilities (not owned).
Definition console.hpp:165
bool isFading() const
Definition console.hpp:259
std::string originalPrompt
Definition console.hpp:367
std::queue< std::string > message_queue
Definition console.hpp:329
std::function< bool(gl::GLWindow *win, const std::vector< std::string > &)> callback
Definition console.hpp:348
void processMultilineInput(const std::string &cmd)
void moveHistoryUp()
Recall the previous command from history.
Definition console.cpp:855
void scrollDown()
Scroll output down by one line.
Definition console.cpp:186
void checkForLineWrap()
Definition console.cpp:424
void moveCursorHome()
Move caret to start of input line.
Definition console.cpp:843
void thread_safe_print(const std::string &str)
Append text from any thread (uses the internal message queue).
Definition console.cpp:920
size_t stopPosition
Definition console.hpp:340
ConsoleChars c_chars
Definition console.hpp:330
void updateScrollDrag(int mouseY)
Continue a scrollbar thumb drag.
Definition console.cpp:1404
void beginScrollDrag(int mouseY)
Start a scrollbar thumb drag.
Definition console.cpp:1397
void reload()
Reload the glyph cache with the current font settings.
Definition console.cpp:159
void process_message_queue()
Process pending messages from the thread-safe print queue (call each frame).
Definition console.cpp:952
bool needsReflow
Set to trigger line-wrapping recalculation.
Definition console.hpp:294
size_t getCursorPos() const
void setCursorPos(size_t pos)
Move the caret to pos within the input line.
void scrollPageUp()
Scroll up by one page.
Definition console.cpp:1374
std::string promptText
Prompt string displayed before the input line.
Definition console.hpp:252
void clear()
Clear the output buffer and reset scroll.
Definition console.cpp:171
std::atomic< bool > worker_active
Definition console.hpp:374
void handleMouseScroll(int mouseX, int mouseY, int wheelY)
Handle a mouse-wheel scroll event.
Definition console.cpp:1434
void setCallback(gl::GLWindow *window, std::function< bool(gl::GLWindow *win, const std::vector< std::string > &)> callback)
Register a command callback invoked when the user presses Enter.
Definition console.cpp:214
void updateInputScrollOffset()
Recalculate horizontal scroll for long input lines.
Definition console.cpp:219
void scrollUp()
Scroll output up by one line.
Definition console.cpp:175
Console & operator=(const Console &)=delete
int scrollDragStartOffset
Definition console.hpp:383
bool isMouseOverScrollbar(int mouseX, int mouseY) const
Test whether the mouse is over the scrollbar thumb.
Definition console.cpp:1389
bool cursorVisible
Whether the cursor is currently shown.
Definition console.hpp:254
friend class GLConsole
Definition console.hpp:164
void keypress(char c)
Handle a single character key press for the input line.
Definition console.cpp:279
void setTextAttrib(const int size, const SDL_Color &col)
Change font size and colour without reloading the widget.
Definition console.cpp:1071
int getWidth() const
Definition console.hpp:250
Uint32 fadeStartTime
Definition console.hpp:355
void moveHistoryDown()
Move forward through command history.
Definition console.cpp:869
bool checkBraceBalance(const std::string &text)
SDL_Surface * surface
Definition console.hpp:337
void worker_thread_func()
Definition console.cpp:927
std::ostringstream data
Definition console.hpp:335
void moveCursorRight()
Move caret one character to the right.
Definition console.cpp:835
void startFadeOut()
Begin a fade-out animation.
Definition console.cpp:1058
size_t inputCursorPos
Definition console.hpp:341
bool needsRedraw
Set to trigger an SDL_Surface redraw.
Definition console.hpp:293
std::string inputBuffer
Current content of the input line (mirrors internal state).
Definition console.hpp:292
bool isScrollDragging() const
Definition console.hpp:271
std::string multilineBuffer
Definition console.hpp:366
void setInputCallback(std::function< int(gl::GLWindow *win, const std::string &)> callback)
Register a callback for raw input-line submission (Enter key).
Definition console.cpp:969
void load(const std::string &fnt, int size, const SDL_Color &col)
Load font and rebuild the glyph cache.
Definition console.cpp:163
Console(Console &&)=delete
std::string tempBuffer
Definition console.hpp:353
void startFadeIn()
Begin a fade-in animation.
Definition console.cpp:1042
void hide()
Make the console invisible immediately.
Definition console.cpp:1067
std::istringstream inputStream
Definition console.hpp:567
std::string font
Definition console.hpp:563
void updateTexture(GLuint tex, SDL_Surface *surf)
Definition console.cpp:1177
std::unique_ptr< gl::GLSprite > sprite
Definition console.hpp:558
GLConsole & operator=(const GLConsole &)=delete
int getWidth() const
Definition console.cpp:1335
void printf(const char *format, Args... args)
printf-style helper that formats a string and calls print().
Definition console.hpp:516
void setPrompt(const std::string &prompt)
Change the prompt prefix string.
Definition console.cpp:1332
void draw(gl::GLWindow *win)
Upload SDL_Surface and blit the GL texture each frame.
Definition console.cpp:1184
void release()
Destroy the GL texture and sprite resources.
Definition console.cpp:893
std::ostream & bufferData()
Definition console.hpp:529
void clearText()
Clear the entire output buffer.
Definition console.cpp:1094
void clear_callbacks()
Reset all registered callbacks to no-ops.
Definition console.hpp:498
void process_message_queue()
Flush the thread-safe message queue into the output buffer (call each frame).
Definition console.cpp:1102
int getHeight() const
Definition console.cpp:1336
void print(const std::string &data)
Append text to the console output.
Definition console.cpp:1214
void println(const std::string &data)
Append text followed by a newline.
Definition console.cpp:1220
bool isVisible() const
Definition console.hpp:537
void show()
Make console visible.
Definition console.cpp:1078
void setStretchHeight(int value)
Set an absolute height override for the stretch mode.
Definition console.cpp:1307
GLConsole(GLConsole &&)=delete
bool procDefaultCommands(const std::vector< std::string > &cmd)
Handle built-in commands (clear, quit, help, …).
Definition console.cpp:1338
GLConsole & operator=(GLConsole &&)=delete
std::mutex gl_mutex
Definition console.hpp:553
std::istream & inputData()
Definition console.cpp:887
bool isFading() const
Definition console.hpp:538
GLuint loadTextFromSurface(SDL_Surface *surf)
Definition console.cpp:1110
void event(gl::GLWindow *win, SDL_Event &e)
High-level event handler with window context.
Definition console.cpp:1224
void resize(gl::GLWindow *win, int w, int h)
Handle window resize — rebuilds GL texture at new dimensions.
Definition console.cpp:1311
GLConsole(const GLConsole &)=delete
std::unique_ptr< gl::ShaderProgram > shader
Definition console.hpp:559
friend class Console
Definition console.hpp:409
void setTextAttrib(const int size, const SDL_Color &col)
Change font size/colour (propagates to inner Console).
Definition console.cpp:1156
void refresh()
Drain the command queue and process pending messages (call each frame).
Definition console.hpp:547
void setInputCallback(std::function< int(gl::GLWindow *window, const std::string &)> callback)
Register a raw-input callback (same semantics as Console::setInputCallback()).
Definition console.cpp:1106
void load(gl::GLWindow *win, const SDL_Rect &rc, const std::string &fnt, int size, const SDL_Color &col)
Load and position the console inside a specific rectangle.
Definition console.cpp:1127
void handleEvent(SDL_Event &e)
Low-level SDL event handler (keyboard + mouse).
void setCallback(gl::GLWindow *window, std::function< bool(gl::GLWindow *win, const std::vector< std::string > &)> callback)
Register a command callback (same semantics as Console::setCallback()).
Definition console.cpp:1328
void hide()
Hide console.
Definition console.cpp:1082
void setStretch(bool stretch)
Enable/disable vertical stretch to fill the window height.
Definition console.hpp:479
void thread_safe_print(const std::string &data)
Thread-safe variant of print().
Definition console.cpp:1098
void setWindow(gl::GLWindow *win)
Update the inner Console's window pointer.
Definition console.hpp:488
A textured screen-space quad drawn with an OpenGL shader.
Definition gl.hpp:255
SDL2 + OpenGL/ES window with event loop and console support.
Definition gl.hpp:411
GLSL shader program wrapper (shared-ownership).
Definition gl.hpp:57
Utility bag for SDL-based applications.
Definition util.hpp:33
#define THREAD_GUARD(mutex)
Definition console.hpp:58
#define THREAD_MUTEX
Definition console.hpp:57
unsigned int GLuint
Definition console.hpp:38
Central SDL2 application window class and related includes.
@ FADE_NONE
Definition console.hpp:138
Definition console.hpp:31
A single command entry with its text and completion callback.
Definition console.hpp:75
std::string text
Raw command string.
Definition console.hpp:76
std::function< void(const std::string &, std::ostream &)> callback
Handler invoked on the worker thread.
Definition console.hpp:77