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>
This commit is contained in:
John McCardle 2026-01-31 11:13:15 -05:00
commit c5cc022aa2
13 changed files with 2665 additions and 34 deletions

View file

@ -7,7 +7,8 @@
#include "Animation.h"
#include "Timer.h"
#include "BenchmarkLogger.h"
#ifndef MCRF_HEADLESS
// ImGui is only available for SFML builds (not headless, not SDL2)
#if !defined(MCRF_HEADLESS) && !defined(MCRF_SDL2)
#include "imgui.h"
#include "imgui-SFML.h"
#endif
@ -86,8 +87,8 @@ GameEngine::GameEngine(const McRogueFaceConfig& cfg)
window->setFramerateLimit(60);
render_target = window.get();
#ifndef MCRF_HEADLESS
// Initialize ImGui for the window
#if !defined(MCRF_HEADLESS) && !defined(MCRF_SDL2)
// Initialize ImGui for the window (SFML builds only)
if (ImGui::SFML::Init(*window)) {
imguiInitialized = true;
// Register settings handler before .ini is loaded (happens on first frame)
@ -199,7 +200,7 @@ void GameEngine::cleanup()
}
// Shutdown ImGui AFTER window is closed to avoid X11 BadCursor errors
#ifndef MCRF_HEADLESS
#if !defined(MCRF_HEADLESS) && !defined(MCRF_SDL2)
if (imguiInitialized) {
ImGui::SFML::Shutdown();
imguiInitialized = false;
@ -361,8 +362,8 @@ void GameEngine::doFrame()
if (!headless) {
sUserInput();
#ifndef MCRF_HEADLESS
// Update ImGui
#if !defined(MCRF_HEADLESS) && !defined(MCRF_SDL2)
// Update ImGui (SFML builds only)
if (imguiInitialized) {
ImGui::SFML::Update(*window, clock.getElapsedTime());
}
@ -405,8 +406,8 @@ void GameEngine::doFrame()
profilerOverlay->render(*render_target);
}
#ifndef MCRF_HEADLESS
// Render ImGui overlays (console and scene explorer)
#if !defined(MCRF_HEADLESS) && !defined(MCRF_SDL2)
// Render ImGui overlays (console and scene explorer) - SFML builds only
if (imguiInitialized && !headless) {
console.render();
sceneExplorer.render(*this);
@ -593,8 +594,8 @@ void GameEngine::sUserInput()
sf::Event event;
while (window && window->pollEvent(event))
{
#ifndef MCRF_HEADLESS
// Process event through ImGui first
#if !defined(MCRF_HEADLESS) && !defined(MCRF_SDL2)
// Process event through ImGui first (SFML builds only)
if (imguiInitialized) {
ImGui::SFML::ProcessEvent(*window, event);
}