14#ifndef __CONSOLE__H__MX2
15#define __CONSOLE__H__MX2
20#include<unordered_map>
27#include<condition_variable>
37#ifndef GL_UNSIGNED_INT
41#if defined(__EMSCRIPTEN__)
42 class EmscriptenMutex {
48 class EmscriptenGuard {
50 EmscriptenGuard(EmscriptenMutex&) {}
53 #define THREAD_MUTEX EmscriptenMutex
54 #define THREAD_GUARD(mutex) EmscriptenGuard guard(mutex)
55 #define THREAD_ENABLED 0
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
77 std::function<void(
const std::string&, std::ostream&)>
callback;
97 std::condition_variable
cv;
127 void load(
const std::string &fnt,
int size,
const SDL_Color &c);
134 std::unique_ptr<mx::Font>
font;
172 void create(
int x,
int y,
int w,
int h);
180 void load(
const std::string &fnt,
int size,
const SDL_Color &col);
189 void print(
const std::string &str);
225 void procCmd(
const std::string &cmd);
268 return c_chars.characters.size() > 0;
300#if defined(__EMSCRIPTEN__)
302 void processCommandQueue();
334 SDL_Color
color = {255, 255, 255, 255};
419 void load(
gl::GLWindow *win,
const SDL_Rect &
rc,
const std::string &fnt,
int size,
const SDL_Color &col);
428 void load(
gl::GLWindow *win,
const std::string &fnt,
int size,
const SDL_Color &col);
450 void print(
const std::string &data);
453 void println(
const std::string &data);
466 void setPrompt(
const std::string &prompt);
470 std::function<
bool(
gl::GLWindow *win,
const std::vector<std::string> &)> callback);
499 console.callback = [](
gl::GLWindow *window,
const std::vector<std::string> &args) ->
bool {
506 console.enterCallbackSet =
false;
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...);
520 print(
"[printf formatting error]\n");
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()));
548#if defined(__EMSCRIPTEN__)
551 console.process_message_queue(); }
558 std::unique_ptr<gl::GLSprite>
sprite =
nullptr;
559 std::unique_ptr<gl::ShaderProgram>
shader;
565 SDL_Color
color = {255, 255, 255, 255};
Thread-safe FIFO queue for dispatching console commands to a worker thread.
bool pop(Command &cmd)
Pop the next command (blocking on native, non-blocking on Emscripten).
std::queue< Command > queue
void push(const Command &cmd)
Push a command onto the queue (thread-safe).
std::atomic< bool > active
std::condition_variable cv
void shutdown()
Signal the worker to drain and stop.
Glyph cache that maps each ASCII character to a pre-rendered SDL_Surface.
std::unordered_map< char, SDL_Surface * > characters
Glyph surfaces keyed by char.
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.
std::unique_ptr< mx::Font > font
ConsoleChars(ConsoleChars &&)=delete
void clear()
Release all cached SDL_Surface objects.
std::vector< TextLine > lines
Console(const Console &)=delete
FadeState fadeState
Current fade animation state.
void moveCursorLeft()
Move caret one character to the left.
void show()
Make the console fully visible immediately.
void resetScroll()
Reset scroll to the bottom.
void scrollPageDown()
Scroll down by one page.
SDL_Surface * drawText()
Render the console contents to an internal SDL_Surface.
void procCmd(const std::string &cmd)
Tokenise and dispatch a command string.
void moveCursorEnd()
Move caret to end of input line.
std::unique_ptr< std::thread > worker_thread
std::recursive_mutex console_mutex
Console & operator=(Console &&)=delete
std::function< int(gl::GLWindow *window, const std::string &)> callbackEnter
void updateCursorPosition()
void create(int x, int y, int w, int h)
Set position and size of the console rectangle.
unsigned int fadeDuration
std::ostringstream & bufferData()
Access the internal output buffer directly as an ostream.
void setText(const std::string &text)
Replace the input-line text.
void scrollToBottom()
Jump the scroll view to the most recent output.
CommandQueue command_queue
std::string continuationPrompt
std::vector< std::string > commandHistory
std::string getText() const
Return the current input-line text.
gl::GLWindow * window
Owning GL window (weak reference).
void print(const std::string &str)
Append text to the output buffer.
void endScrollDrag()
Release the scrollbar thumb.
void setPrompt(const std::string &text)
Change the prompt prefix (default: "$ ").
Uint32 cursorBlinkTime
SDL_GetTicks() value of the last cursor blink toggle.
mx::mxUtil * util
Pointer to asset-path utilities (not owned).
std::string originalPrompt
std::queue< std::string > message_queue
std::function< bool(gl::GLWindow *win, const std::vector< std::string > &)> callback
void processMultilineInput(const std::string &cmd)
void moveHistoryUp()
Recall the previous command from history.
void scrollDown()
Scroll output down by one line.
void moveCursorHome()
Move caret to start of input line.
void thread_safe_print(const std::string &str)
Append text from any thread (uses the internal message queue).
void updateScrollDrag(int mouseY)
Continue a scrollbar thumb drag.
void beginScrollDrag(int mouseY)
Start a scrollbar thumb drag.
void reload()
Reload the glyph cache with the current font settings.
void process_message_queue()
Process pending messages from the thread-safe print queue (call each frame).
bool needsReflow
Set to trigger line-wrapping recalculation.
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.
std::string promptText
Prompt string displayed before the input line.
void clear()
Clear the output buffer and reset scroll.
std::atomic< bool > worker_active
void handleMouseScroll(int mouseX, int mouseY, int wheelY)
Handle a mouse-wheel scroll event.
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.
void updateInputScrollOffset()
Recalculate horizontal scroll for long input lines.
void scrollUp()
Scroll output up by one line.
Console & operator=(const Console &)=delete
int scrollDragStartOffset
bool isMouseOverScrollbar(int mouseX, int mouseY) const
Test whether the mouse is over the scrollbar thumb.
bool cursorVisible
Whether the cursor is currently shown.
void keypress(char c)
Handle a single character key press for the input line.
void setTextAttrib(const int size, const SDL_Color &col)
Change font size and colour without reloading the widget.
void moveHistoryDown()
Move forward through command history.
bool checkBraceBalance(const std::string &text)
void worker_thread_func()
void moveCursorRight()
Move caret one character to the right.
void startFadeOut()
Begin a fade-out animation.
bool needsRedraw
Set to trigger an SDL_Surface redraw.
std::string inputBuffer
Current content of the input line (mirrors internal state).
bool isScrollDragging() const
std::string multilineBuffer
void setInputCallback(std::function< int(gl::GLWindow *win, const std::string &)> callback)
Register a callback for raw input-line submission (Enter key).
void load(const std::string &fnt, int size, const SDL_Color &col)
Load font and rebuild the glyph cache.
Console(Console &&)=delete
void startFadeIn()
Begin a fade-in animation.
void hide()
Make the console invisible immediately.
std::istringstream inputStream
void updateTexture(GLuint tex, SDL_Surface *surf)
std::unique_ptr< gl::GLSprite > sprite
GLConsole & operator=(const GLConsole &)=delete
void printf(const char *format, Args... args)
printf-style helper that formats a string and calls print().
void setPrompt(const std::string &prompt)
Change the prompt prefix string.
void draw(gl::GLWindow *win)
Upload SDL_Surface and blit the GL texture each frame.
void release()
Destroy the GL texture and sprite resources.
std::ostream & bufferData()
void clearText()
Clear the entire output buffer.
void clear_callbacks()
Reset all registered callbacks to no-ops.
void process_message_queue()
Flush the thread-safe message queue into the output buffer (call each frame).
void print(const std::string &data)
Append text to the console output.
void println(const std::string &data)
Append text followed by a newline.
void show()
Make console visible.
void setStretchHeight(int value)
Set an absolute height override for the stretch mode.
GLConsole(GLConsole &&)=delete
bool procDefaultCommands(const std::vector< std::string > &cmd)
Handle built-in commands (clear, quit, help, …).
GLConsole & operator=(GLConsole &&)=delete
std::istream & inputData()
GLuint loadTextFromSurface(SDL_Surface *surf)
void event(gl::GLWindow *win, SDL_Event &e)
High-level event handler with window context.
void resize(gl::GLWindow *win, int w, int h)
Handle window resize — rebuilds GL texture at new dimensions.
GLConsole(const GLConsole &)=delete
std::unique_ptr< gl::ShaderProgram > shader
void setTextAttrib(const int size, const SDL_Color &col)
Change font size/colour (propagates to inner Console).
void refresh()
Drain the command queue and process pending messages (call each frame).
void setInputCallback(std::function< int(gl::GLWindow *window, const std::string &)> callback)
Register a raw-input callback (same semantics as Console::setInputCallback()).
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.
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()).
void setStretch(bool stretch)
Enable/disable vertical stretch to fill the window height.
void thread_safe_print(const std::string &data)
Thread-safe variant of print().
void setWindow(gl::GLWindow *win)
Update the inner Console's window pointer.
A textured screen-space quad drawn with an OpenGL shader.
SDL2 + OpenGL/ES window with event loop and console support.
GLSL shader program wrapper (shared-ownership).
Utility bag for SDL-based applications.
#define THREAD_GUARD(mutex)
Central SDL2 application window class and related includes.
A single command entry with its text and completion callback.
std::string text
Raw command string.
std::function< void(const std::string &, std::ostream &)> callback
Handler invoked on the worker thread.