McRogueFace/src/ImGuiConsole.h
John McCardle c5cc022aa2 Add SDL2+OpenGL ES 2 renderer backend for Emscripten/WebGL
Implements Phase 1 of renderer abstraction plan:
- SDL2Types.h: SFML-compatible type stubs in sf:: namespace
- SDL2Renderer.h/cpp: OpenGL ES 2 rendering implementation
- EmscriptenStubs.cpp: Stubs for missing POSIX functions (wait3, wait4, wcsftime)

Build system changes:
- Add MCRF_SDL2 compile-time backend selection
- Add Emscripten SDL2 link options (-sUSE_SDL=2, -sFULL_ES2=1)
- Fix LONG_BIT mismatch for Emscripten in pyport.h

Code changes for SDL2/headless compatibility:
- Guard ImGui includes with !MCRF_HEADLESS && !MCRF_SDL2
- Defer GL shader initialization until after context creation

Current status: Python runs in browser, rendering WIP (canvas sizing issues)

Build commands:
  emcmake cmake -DMCRF_SDL2=ON -B build-emscripten
  emmake make -C build-emscripten

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 11:13:15 -05:00

78 lines
2.3 KiB
C++

#pragma once
// ImGuiConsole - excluded from headless and SDL2 builds (ImGui-SFML only)
#if !defined(MCRF_HEADLESS) && !defined(MCRF_SDL2)
#include <string>
#include <vector>
#include <deque>
/**
* @brief ImGui-based debug console for Python REPL
*
* Provides an overlay console that can execute Python code
* without blocking the main game loop. Activated by grave/tilde key.
*/
class ImGuiConsole {
public:
ImGuiConsole();
// Core functionality
void render(); // Render the console UI
void toggle(); // Toggle visibility
bool isVisible() const { return visible; }
void setVisible(bool v) { visible = v; }
// Configuration (for Python API)
static bool isEnabled() { return enabled; }
static void setEnabled(bool e) { enabled = e; }
// Input handling
bool wantsKeyboardInput() const; // Returns true if ImGui wants keyboard
// Font management - reload ImGui font at specified pixel size
static void reloadFont(float size);
static float getCurrentFontSize() { return s_currentFontSize; }
// Settings persistence via imgui.ini
static void registerSettingsHandler();
// Allow settings handler callbacks to access font size
static float s_currentFontSize; // Track current loaded font size
private:
void executeCommand(const std::string& command);
void addOutput(const std::string& text, bool isError = false);
void renderCodeEditor(); // Separate multi-line code editor window
// State
bool visible = false;
static bool enabled; // Global enable/disable (for shipping games)
// UI state
bool editorVisible = false; // Multi-line editor window
bool consoleLocked = false; // Prevent console dragging
bool editorLocked = false; // Prevent editor dragging
// Input buffers
char inputBuffer[1024] = {0};
char codeBuffer[16384] = {0}; // 16KB for multi-line code
// Output history
struct OutputLine {
std::string text;
bool isError;
bool isInput; // True if this was user input (for styling)
};
std::deque<OutputLine> outputHistory;
static constexpr size_t MAX_HISTORY = 500;
// Command history for up/down navigation
std::vector<std::string> commandHistory;
int historyIndex = -1;
// Scroll state
bool scrollToBottom = true;
};
#endif // MCRF_HEADLESS