Support Windows Server GUI startup with Direct3D and WARP (by Codex)
This commit is contained in:
1 parent
d9d05f83b4
commit
856d38cc0c
20 files changed
+532
-172
No files matched your search
+3
-21
@@ -31,6 +31,7 @@
|
||||
#include "launch/file_dialog.hpp"
|
||||
#include "launch/game_data.hpp"
|
||||
#include "launch/launcher.hpp"
|
||||
#include "launch/rml_video.hpp"
|
||||
#include "content/package.hpp"
|
||||
#include "version.hpp"
|
||||
#include "app/live_matchday_events.hpp"
|
||||
@@ -75,8 +76,7 @@ using namespace bmh;
|
||||
|
||||
// Why the game cannot start: a message box, and the same text on stderr.
|
||||
static void show_start_error(const std::string& text) {
|
||||
std::fprintf(stderr, "%s\n", text.c_str());
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "OpenBMH", text.c_str(), nullptr);
|
||||
rml::report_start_error("OpenBMH", text);
|
||||
}
|
||||
|
||||
// Reattach the native editor when an in-game filename field takes focus. Cocoa
|
||||
@@ -91,26 +91,8 @@ static void focus_filename_input(SDL_Renderer* renderer) {
|
||||
SDL_StartTextInput();
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
// The game is a Windows GUI program (no console window on double-click). Started from a
|
||||
// console, it writes --version, --help and start errors there (kernel32 AttachConsole).
|
||||
extern "C" __declspec(dllimport) int __stdcall AttachConsole(unsigned long process_id);
|
||||
static void attach_parent_console() {
|
||||
// (an output redirected to a file or pipe stays there; a GUI program without one has no
|
||||
// valid stream, fileno < 0)
|
||||
const bool out_free = _fileno(stdout) < 0, err_free = _fileno(stderr) < 0;
|
||||
if ((out_free || err_free) && AttachConsole(static_cast<unsigned long>(-1))) { // ATTACH_PARENT_PROCESS
|
||||
std::FILE* f = nullptr;
|
||||
if (out_free) freopen_s(&f, "CONOUT$", "w", stdout);
|
||||
if (err_free) freopen_s(&f, "CONOUT$", "w", stderr);
|
||||
}
|
||||
}
|
||||
#else
|
||||
static void attach_parent_console() {}
|
||||
#endif
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
attach_parent_console();
|
||||
rml::attach_parent_console();
|
||||
const std::vector<std::string> args = launch::utf8_arguments(argc, argv); // (file names beyond the code page)
|
||||
fs::path data, save_directory;
|
||||
uint32_t seed = uint32_t(std::chrono::system_clock::now().time_since_epoch().count());
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
include(FetchContent)
|
||||
set(OPENBMH_EDITOR_VERSION "0.5.2")
|
||||
set(OPENBMH_EDITOR_VERSION "0.5.3")
|
||||
file(WRITE "${CMAKE_BINARY_DIR}/editor-version.json" "{\"version\": \"${OPENBMH_EDITOR_VERSION}\"}\n")
|
||||
set(JSON_BuildTests OFF CACHE BOOL "" FORCE)
|
||||
FetchContent_Declare(editor_json GIT_REPOSITORY https://github.com/nlohmann/json.git
|
||||
|
||||
+12
-2
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "editor/ui.hpp"
|
||||
#include "editor/version.hpp"
|
||||
#include "launch/rml_video.hpp"
|
||||
#include <SDL.h>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
@@ -14,7 +15,14 @@ void stop(int) { stopped = true; }
|
||||
} // namespace
|
||||
int main(int argc, char **argv) {
|
||||
using namespace bmh::editor;
|
||||
bmh::rml::attach_parent_console();
|
||||
std::string language = "de";
|
||||
bool graphical = true;
|
||||
for (int i = 1; i < argc; ++i)
|
||||
if (std::string_view(argv[i]) == "--headless" || std::string_view(argv[i]) == "--serve" ||
|
||||
std::string_view(argv[i]) == "--stdio-mcp" || std::string_view(argv[i]) == "--version" ||
|
||||
std::string_view(argv[i]) == "--help")
|
||||
graphical = false;
|
||||
try {
|
||||
bool headless = false, serve = false, stdio = false, demo = false, blank = false, validate = false,
|
||||
print = false;
|
||||
@@ -153,10 +161,12 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
return has_errors ? 2 : 0;
|
||||
} catch (const Error &e) {
|
||||
std::cerr << Json{{"error", e.code}, {"message", tr(language, e.code)}, {"details", e.details}}.dump() << '\n';
|
||||
const auto message =
|
||||
Json{{"error", e.code}, {"message", tr(language, e.code)}, {"details", e.details}}.dump();
|
||||
bmh::rml::report_start_error("OpenBMHEditor", message, graphical);
|
||||
return 1;
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << tr(language, "error") << ": " << e.what() << '\n';
|
||||
bmh::rml::report_start_error("OpenBMHEditor", tr(language, "error") + ": " + e.what(), graphical);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,9 @@
|
||||
#include <map>
|
||||
#include <random>
|
||||
#ifdef _WIN32
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
@@ -268,8 +270,7 @@ std::string tr(const std::string &language, const std::string &key) {
|
||||
"Drag a rectangle on the image or enter crop values. Without cropping, "
|
||||
"the original resolution is retained."}},
|
||||
{"video_failed",
|
||||
{"Die Grafikausgabe benötigt OpenGL 3.3 (macOS: 4.1).",
|
||||
"The interface requires OpenGL 3.3 (macOS: 4.1)."}},
|
||||
{"Die Grafikausgabe konnte nicht initialisiert werden.", "Graphics initialization failed."}},
|
||||
{"ui_assets_missing",
|
||||
{"Editor-Oberfläche fehlt. Bitte das vollständige Paket entpacken.",
|
||||
"Editor interface files are missing. Please extract the complete "
|
||||
|
||||
+15
-66
@@ -3,11 +3,11 @@
|
||||
#include "editor/images.hpp"
|
||||
#include "editor/version.hpp"
|
||||
#include "launch/rml_path.hpp"
|
||||
#include "launch/rml_video.hpp"
|
||||
#include <RmlUi/Core.h>
|
||||
#include <RmlUi/Core/Elements/ElementFormControl.h>
|
||||
#include <RmlUi/Core/Elements/ElementFormControlInput.h>
|
||||
#include <RmlUi_Platform_SDL.h>
|
||||
#include <RmlUi_Renderer_GL3.h>
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
@@ -31,58 +31,13 @@ std::string button(const std::string &action, const std::string &label, const st
|
||||
return "<button data-action=\"" + escaped(action) + "\" class=\"" + css + "\" " + attributes + ">" +
|
||||
escaped(label) + "</button>";
|
||||
}
|
||||
struct Video {
|
||||
SDL_Window *window = nullptr;
|
||||
SDL_GLContext gl = nullptr;
|
||||
bool loader = false;
|
||||
Video() {
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
|
||||
throw Error("video_failed");
|
||||
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
#ifdef __APPLE__
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
||||
#else
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||
#endif
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
window =
|
||||
SDL_CreateWindow("OpenBMH Editor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1440, 900,
|
||||
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN);
|
||||
if (!window)
|
||||
throw Error("video_failed");
|
||||
SDL_SetWindowMinimumSize(window, 1080, 720);
|
||||
gl = SDL_GL_CreateContext(window);
|
||||
if (!gl)
|
||||
throw Error("video_failed");
|
||||
SDL_GL_MakeCurrent(window, gl);
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
std::string error;
|
||||
if (!RmlGL3::Initialize(&error))
|
||||
throw Error("video_failed", {{"detail", error}});
|
||||
loader = true;
|
||||
SDL_StartTextInput();
|
||||
}
|
||||
~Video() {
|
||||
if (loader)
|
||||
RmlGL3::Shutdown();
|
||||
if (gl)
|
||||
SDL_GL_DeleteContext(gl);
|
||||
if (window)
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
}
|
||||
};
|
||||
class Renderer final : public RenderInterface_GL3 {
|
||||
class Renderer final : public bmh::rml::Renderer {
|
||||
public:
|
||||
Session &session;
|
||||
std::map<std::string, std::pair<std::string, std::string>> memory;
|
||||
std::map<std::string, std::string> logical;
|
||||
uint64_t serial = 0;
|
||||
explicit Renderer(Session &s) : session(s) {}
|
||||
Renderer(Session &s, bmh::rml::Video &video) : bmh::rml::Renderer(video), session(s) {}
|
||||
std::string asset(const std::string &key) {
|
||||
auto token = "world-image-" + std::to_string(serial++) + ".png";
|
||||
logical[token] = key;
|
||||
@@ -101,7 +56,7 @@ class Renderer final : public RenderInterface_GL3 {
|
||||
Rml::TextureHandle LoadTexture(Rml::Vector2i &dimensions, const Rml::String &source) override {
|
||||
const auto token = utf8(path(source).filename());
|
||||
if (!memory.contains(token) && !logical.contains(token))
|
||||
return RenderInterface_GL3::LoadTexture(dimensions, source);
|
||||
return bmh::rml::Renderer::LoadTexture(dimensions, source);
|
||||
try {
|
||||
auto value = memory.contains(token) ? memory.at(token) : session.asset_bytes(logical.at(token));
|
||||
auto raster = images::decode(value.first, value.second, token.starts_with("icon-") ? 64 : 512);
|
||||
@@ -129,7 +84,7 @@ class EditorUi final : public Rml::EventListener {
|
||||
Options &options_;
|
||||
McpServer &server_;
|
||||
UiConfig config_;
|
||||
Video video_;
|
||||
bmh::rml::Video video_{"OpenBMH Editor", 1440, 900, 1080, 720};
|
||||
bmh::rml::AssetFiles files_;
|
||||
SystemInterface_SDL system_{video_.window};
|
||||
Renderer renderer_;
|
||||
@@ -299,7 +254,7 @@ class EditorUi final : public Rml::EventListener {
|
||||
void resize() {
|
||||
int w, h, pw, ph;
|
||||
SDL_GetWindowSize(video_.window, &w, &h);
|
||||
SDL_GL_GetDrawableSize(video_.window, &pw, &ph);
|
||||
video_.drawable_size(pw, ph);
|
||||
if (!w || !h)
|
||||
return;
|
||||
renderer_.SetViewport(pw, ph);
|
||||
@@ -1775,23 +1730,16 @@ class EditorUi final : public Rml::EventListener {
|
||||
if (config_.screenshot.empty())
|
||||
return;
|
||||
int w, h;
|
||||
SDL_GL_GetDrawableSize(video_.window, &w, &h);
|
||||
images::Raster raster{w, h, std::vector<uint8_t>(size_t(w) * h * 4)};
|
||||
using ReadPixels = void (*)(int, int, int, int, unsigned, unsigned, void *);
|
||||
auto read = reinterpret_cast<ReadPixels>(SDL_GL_GetProcAddress("glReadPixels"));
|
||||
if (!read)
|
||||
throw Error("video_failed");
|
||||
read(0, 0, w, h, 0x1908 /* RGBA */, 0x1401 /* UNSIGNED_BYTE */, raster.pixels.data());
|
||||
for (int y = 0; y < h / 2; ++y)
|
||||
for (int x = 0; x < w * 4; ++x)
|
||||
std::swap(raster.pixels[size_t(y) * w * 4 + x], raster.pixels[size_t(h - y - 1) * w * 4 + x]);
|
||||
auto pixels = video_.capture(w, h);
|
||||
images::Raster raster{w, h, std::move(pixels)};
|
||||
write_atomic(config_.screenshot, images::encode_png(raster));
|
||||
config_.screenshot.clear();
|
||||
}
|
||||
|
||||
public:
|
||||
EditorUi(Session &s, Options &o, McpServer &server, UiConfig config)
|
||||
: session_(s), options_(o), server_(server), config_(std::move(config)), renderer_(s) {
|
||||
: session_(s), options_(o), server_(server), config_(std::move(config)), renderer_(s, video_) {
|
||||
SDL_StartTextInput();
|
||||
page_ = config_.page;
|
||||
Rml::SetSystemInterface(&system_);
|
||||
Rml::SetFileInterface(&files_);
|
||||
@@ -1801,10 +1749,11 @@ class EditorUi final : public Rml::EventListener {
|
||||
if (!initialized_)
|
||||
throw Error("video_failed");
|
||||
for (auto weight : {"Regular", "Bold"})
|
||||
if (!Rml::LoadFontFace(utf8(config_.assets / (std::string("LatoLatin-") + weight + ".ttf"))))
|
||||
if (!Rml::LoadFontFace(
|
||||
bmh::rml::asset_path(config_.assets / (std::string("LatoLatin-") + weight + ".ttf"))))
|
||||
throw Error("ui_assets_missing");
|
||||
context_ = Rml::CreateContext("editor", {1440, 900});
|
||||
doc_ = context_->LoadDocument(utf8(config_.assets / "editor.rml"));
|
||||
doc_ = context_->LoadDocument(bmh::rml::asset_path(config_.assets / "editor.rml"));
|
||||
if (!doc_)
|
||||
throw Error("ui_assets_missing");
|
||||
for (auto event : {"click", "change", "mousedown", "mouseup", "mousemove"})
|
||||
@@ -1915,7 +1864,7 @@ class EditorUi final : public Rml::EventListener {
|
||||
ime_.HandleEdit(event.edit);
|
||||
int w, h, pw, ph;
|
||||
SDL_GetWindowSize(video_.window, &w, &h);
|
||||
SDL_GL_GetDrawableSize(video_.window, &pw, &ph);
|
||||
video_.drawable_size(pw, ph);
|
||||
if (w && h) {
|
||||
if (event.type == SDL_MOUSEMOTION) {
|
||||
event.motion.x = int(double(event.motion.x) * pw / w);
|
||||
@@ -1978,7 +1927,7 @@ class EditorUi final : public Rml::EventListener {
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_).count();
|
||||
if (!busy_ && elapsed > 1200 && !config_.screenshot.empty())
|
||||
screenshot();
|
||||
SDL_GL_SwapWindow(video_.window);
|
||||
video_.present();
|
||||
SDL_Delay(10);
|
||||
if (config_.quit_after_ms > 0 && elapsed >= config_.quit_after_ms)
|
||||
closed_ = true;
|
||||
|
||||
@@ -33,6 +33,9 @@ target_link_libraries(bmh_launcher PUBLIC bmh_content)
|
||||
include(../launch/rmlui.cmake)
|
||||
target_sources(bmh_launcher PRIVATE ../launch/launcher_rmlui.cpp)
|
||||
target_link_libraries(bmh_launcher PRIVATE bmh_rmlui nfd)
|
||||
add_executable(launcher_gui_smoke ../../tests/launcher_gui_smoke.cpp)
|
||||
target_link_libraries(launcher_gui_smoke PRIVATE bmh_launcher bmh_rmlui)
|
||||
set_target_properties(launcher_gui_smoke PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
|
||||
add_executable(launcher_document_tests ../../tests/launcher_document_tests.cpp)
|
||||
target_include_directories(launcher_document_tests PRIVATE ..)
|
||||
target_link_libraries(launcher_document_tests PRIVATE RmlUi::Core)
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "launch/launcher_ui.hpp"
|
||||
#include "launch/rml_path.hpp"
|
||||
#include "launch/archive.hpp"
|
||||
#include "launch/launcher_ui.hpp"
|
||||
#include "launch/process.hpp"
|
||||
#include "launch/rml_path.hpp"
|
||||
#include "launch/rml_video.hpp"
|
||||
#include "version.hpp"
|
||||
#include <RmlUi/Core.h>
|
||||
#include <RmlUi/Core/Elements/ElementFormControl.h>
|
||||
#include <RmlUi/Core/Elements/ElementFormControlInput.h>
|
||||
#include <RmlUi_Platform_SDL.h>
|
||||
#include <RmlUi_Renderer_GL3.h>
|
||||
#include <nfd.hpp>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <nfd.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace bmh::launch {
|
||||
@@ -41,9 +41,6 @@ std::string escape(std::string_view value) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
[[noreturn]] void sdl_error(const char *operation) {
|
||||
throw std::runtime_error(std::string(operation) + ": " + SDL_GetError());
|
||||
}
|
||||
int modifiers(Uint16 state) {
|
||||
int result = 0;
|
||||
if (state & KMOD_CTRL)
|
||||
@@ -65,62 +62,11 @@ int modifiers(Uint16 state) {
|
||||
result |= Rml::Input::KM_NUMLOCK;
|
||||
return result;
|
||||
}
|
||||
struct Video {
|
||||
SDL_Window *window = nullptr;
|
||||
SDL_GLContext gl = nullptr;
|
||||
bool loader = false;
|
||||
Video() {
|
||||
try {
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
|
||||
sdl_error("Launcher video initialization");
|
||||
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
#ifdef __APPLE__
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
||||
#else
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||
#endif
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
window = SDL_CreateWindow("OpenBMH", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1100, 688,
|
||||
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI |
|
||||
SDL_WINDOW_HIDDEN);
|
||||
if (!window)
|
||||
sdl_error("Launcher window");
|
||||
SDL_SetWindowMinimumSize(window, 800, 540);
|
||||
gl = SDL_GL_CreateContext(window);
|
||||
if (!gl)
|
||||
sdl_error("Launcher requires OpenGL 3.3 (macOS: 4.1)");
|
||||
SDL_GL_MakeCurrent(window, gl);
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
std::string error;
|
||||
if (!RmlGL3::Initialize(&error))
|
||||
throw std::runtime_error("Launcher graphics initialization: " + error);
|
||||
loader = true;
|
||||
SDL_StopTextInput();
|
||||
} catch (...) {
|
||||
close();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
void close() {
|
||||
if (loader)
|
||||
RmlGL3::Shutdown();
|
||||
if (gl)
|
||||
SDL_GL_DeleteContext(gl);
|
||||
if (window)
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER);
|
||||
}
|
||||
~Video() { close(); }
|
||||
};
|
||||
class RmlLauncher final : public LauncherUI, public Rml::EventListener {
|
||||
Video video_;
|
||||
rml::Video video_{"OpenBMH", 1100, 688, 800, 540};
|
||||
rml::AssetFiles files_;
|
||||
SystemInterface_SDL system_{video_.window};
|
||||
RenderInterface_GL3 renderer_;
|
||||
rml::Renderer renderer_{video_};
|
||||
TextInputMethodEditor_SDL ime_;
|
||||
Rml::Context *context_ = nullptr;
|
||||
Rml::ElementDocument *document_ = nullptr;
|
||||
@@ -165,7 +111,7 @@ class RmlLauncher final : public LauncherUI, public Rml::EventListener {
|
||||
void resize() {
|
||||
int w = 0, h = 0, pixels_w = 0, pixels_h = 0;
|
||||
SDL_GetWindowSize(video_.window, &w, &h);
|
||||
SDL_GL_GetDrawableSize(video_.window, &pixels_w, &pixels_h);
|
||||
video_.drawable_size(pixels_w, pixels_h);
|
||||
if (w <= 0 || h <= 0 || pixels_w <= 0 || pixels_h <= 0)
|
||||
return;
|
||||
renderer_.SetViewport(pixels_w, pixels_h);
|
||||
@@ -350,7 +296,7 @@ class RmlLauncher final : public LauncherUI, public Rml::EventListener {
|
||||
// SDL2 reports mouse coordinates in window units on Retina/HiDPI.
|
||||
int w = 0, h = 0, pw = 0, ph = 0;
|
||||
SDL_GetWindowSize(video_.window, &w, &h);
|
||||
SDL_GL_GetDrawableSize(video_.window, &pw, &ph);
|
||||
video_.drawable_size(pw, ph);
|
||||
if (w > 0 && h > 0) {
|
||||
if (event.type == SDL_MOUSEMOTION) {
|
||||
event.motion.x = int(double(event.motion.x) * pw / w);
|
||||
@@ -377,7 +323,7 @@ class RmlLauncher final : public LauncherUI, public Rml::EventListener {
|
||||
renderer_.Clear();
|
||||
context_->Render();
|
||||
renderer_.EndFrame();
|
||||
SDL_GL_SwapWindow(video_.window);
|
||||
video_.present();
|
||||
dirty_ = false;
|
||||
}
|
||||
}
|
||||
@@ -390,8 +336,9 @@ class RmlLauncher final : public LauncherUI, public Rml::EventListener {
|
||||
|
||||
public:
|
||||
RmlLauncher() {
|
||||
SDL_StopTextInput();
|
||||
if (!renderer_)
|
||||
throw std::runtime_error("RmlUI could not create the OpenGL renderer");
|
||||
throw std::runtime_error("RmlUI could not create the graphics renderer");
|
||||
try {
|
||||
Rml::SetSystemInterface(&system_);
|
||||
Rml::SetFileInterface(&files_);
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "launch/rml_video.hpp"
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <string_view>
|
||||
#ifdef _WIN32
|
||||
#include <SDL_syswm.h>
|
||||
#endif
|
||||
|
||||
namespace bmh::rml {
|
||||
namespace {
|
||||
[[noreturn]] void sdl_error(const char *operation) {
|
||||
throw std::runtime_error(std::string(operation) + ": " + SDL_GetError());
|
||||
}
|
||||
#ifdef _WIN32
|
||||
void check(HRESULT result, const char *operation) {
|
||||
if (FAILED(result)) {
|
||||
char code[24];
|
||||
std::snprintf(code, sizeof(code), "0x%08lX", static_cast<unsigned long>(result));
|
||||
throw std::runtime_error(std::string(operation) + ": " + code);
|
||||
}
|
||||
}
|
||||
std::wstring wide(const std::string &text) {
|
||||
const int length = MultiByteToWideChar(CP_UTF8, 0, text.data(), int(text.size()), nullptr, 0);
|
||||
std::wstring result(size_t(length), L' ');
|
||||
MultiByteToWideChar(CP_UTF8, 0, text.data(), int(text.size()), result.data(), length);
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
} // namespace
|
||||
|
||||
Video::Video(const char *title, int width, int height, int minimum_width, int minimum_height) {
|
||||
try {
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
|
||||
sdl_error("Video initialization");
|
||||
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
|
||||
Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN;
|
||||
#ifndef _WIN32
|
||||
flags |= SDL_WINDOW_OPENGL;
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
#ifdef __APPLE__
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
||||
#else
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||
#endif
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
#endif
|
||||
window =
|
||||
SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
|
||||
if (!window)
|
||||
sdl_error("Window creation");
|
||||
SDL_SetWindowMinimumSize(window, minimum_width, minimum_height);
|
||||
#ifdef _WIN32
|
||||
SDL_SysWMinfo info{};
|
||||
SDL_VERSION(&info.version);
|
||||
if (!SDL_GetWindowWMInfo(window, &info))
|
||||
sdl_error("Native window handle");
|
||||
DXGI_SWAP_CHAIN_DESC desc{};
|
||||
desc.BufferCount = 2;
|
||||
desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
desc.OutputWindow = info.info.win.window;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Windowed = TRUE;
|
||||
desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
|
||||
const D3D_FEATURE_LEVEL levels[] = {D3D_FEATURE_LEVEL_11_0};
|
||||
auto create = [&](D3D_DRIVER_TYPE driver) {
|
||||
return D3D11CreateDeviceAndSwapChain(nullptr, driver, nullptr, 0, levels, 1, D3D11_SDK_VERSION,
|
||||
&desc, swapchain_.ReleaseAndGetAddressOf(),
|
||||
device_.ReleaseAndGetAddressOf(), nullptr,
|
||||
context_.ReleaseAndGetAddressOf());
|
||||
};
|
||||
const auto mode = std::getenv("OPENBMH_RML_DRIVER");
|
||||
const bool force_warp = mode && std::string_view(mode) == "warp";
|
||||
HRESULT result = force_warp ? E_FAIL : create(D3D_DRIVER_TYPE_HARDWARE);
|
||||
bool software = false;
|
||||
if (FAILED(result)) {
|
||||
result = create(D3D_DRIVER_TYPE_WARP);
|
||||
software = true;
|
||||
}
|
||||
check(result, "Direct3D 11 initialization (hardware and WARP)");
|
||||
// Upstream's full-featured renderer requires the 11.1 runtime interfaces.
|
||||
Microsoft::WRL::ComPtr<ID3D11Device1> device1;
|
||||
Microsoft::WRL::ComPtr<ID3D11DeviceContext1> context1;
|
||||
check(device_.As(&device1), "Direct3D 11.1 device");
|
||||
check(context_.As(&context1), "Direct3D 11.1 context");
|
||||
std::fprintf(stderr, "RmlUI renderer: Direct3D 11 (%s)\n", software ? "WARP software" : "hardware");
|
||||
int pw, ph;
|
||||
drawable_size(pw, ph);
|
||||
resize(pw, ph);
|
||||
#else
|
||||
gl_ = SDL_GL_CreateContext(window);
|
||||
if (!gl_)
|
||||
sdl_error("OpenGL 3.3 initialization (macOS: 4.1)");
|
||||
if (SDL_GL_MakeCurrent(window, gl_) != 0)
|
||||
sdl_error("OpenGL context activation");
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
std::string error;
|
||||
if (!RmlGL3::Initialize(&error))
|
||||
throw std::runtime_error("OpenGL renderer initialization: " + error);
|
||||
loader_ = true;
|
||||
#endif
|
||||
} catch (...) {
|
||||
close();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
Video::~Video() { close(); }
|
||||
void Video::close() noexcept {
|
||||
#ifdef _WIN32
|
||||
if (context_)
|
||||
context_->ClearState();
|
||||
target_.Reset();
|
||||
swapchain_.Reset();
|
||||
context_.Reset();
|
||||
device_.Reset();
|
||||
#else
|
||||
if (loader_)
|
||||
RmlGL3::Shutdown();
|
||||
if (gl_)
|
||||
SDL_GL_DeleteContext(gl_);
|
||||
#endif
|
||||
if (window)
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER);
|
||||
}
|
||||
void Video::drawable_size(int &width, int &height) const {
|
||||
#ifdef _WIN32
|
||||
SDL_GetWindowSizeInPixels(window, &width, &height);
|
||||
#else
|
||||
SDL_GL_GetDrawableSize(window, &width, &height);
|
||||
#endif
|
||||
}
|
||||
#ifdef _WIN32
|
||||
void Video::resize(int width, int height) {
|
||||
if (width < 1 || height < 1 || (width == width_ && height == height_ && target_))
|
||||
return;
|
||||
context_->OMSetRenderTargets(0, nullptr, nullptr);
|
||||
target_.Reset();
|
||||
check(swapchain_->ResizeBuffers(0, UINT(width), UINT(height), DXGI_FORMAT_UNKNOWN, 0),
|
||||
"Swapchain resize");
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> buffer;
|
||||
check(swapchain_->GetBuffer(0, IID_PPV_ARGS(&buffer)), "Swapchain buffer");
|
||||
check(device_->CreateRenderTargetView(buffer.Get(), nullptr, &target_), "Render target");
|
||||
width_ = width;
|
||||
height_ = height;
|
||||
}
|
||||
#endif
|
||||
void Video::present() {
|
||||
#ifdef _WIN32
|
||||
check(swapchain_->Present(1, 0), "Direct3D presentation");
|
||||
#else
|
||||
SDL_GL_SwapWindow(window);
|
||||
#endif
|
||||
}
|
||||
std::vector<uint8_t> Video::capture(int &width, int &height) {
|
||||
drawable_size(width, height);
|
||||
std::vector<uint8_t> pixels(size_t(width) * size_t(height) * 4);
|
||||
#ifdef _WIN32
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> buffer, staging;
|
||||
check(swapchain_->GetBuffer(0, IID_PPV_ARGS(&buffer)), "Screenshot buffer");
|
||||
D3D11_TEXTURE2D_DESC desc{};
|
||||
buffer->GetDesc(&desc);
|
||||
desc.Usage = D3D11_USAGE_STAGING;
|
||||
desc.BindFlags = 0;
|
||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
desc.MiscFlags = 0;
|
||||
check(device_->CreateTexture2D(&desc, nullptr, &staging), "Screenshot staging texture");
|
||||
context_->CopyResource(staging.Get(), buffer.Get());
|
||||
D3D11_MAPPED_SUBRESOURCE data{};
|
||||
check(context_->Map(staging.Get(), 0, D3D11_MAP_READ, 0, &data), "Screenshot readback");
|
||||
for (int row = 0; row < height; ++row)
|
||||
std::memcpy(pixels.data() + size_t(row) * width * 4,
|
||||
static_cast<const uint8_t *>(data.pData) + size_t(row) * data.RowPitch,
|
||||
size_t(width) * 4);
|
||||
context_->Unmap(staging.Get(), 0);
|
||||
#else
|
||||
using ReadPixels = void (*)(int, int, int, int, unsigned, unsigned, void *);
|
||||
auto read = reinterpret_cast<ReadPixels>(SDL_GL_GetProcAddress("glReadPixels"));
|
||||
if (!read)
|
||||
sdl_error("Screenshot readback");
|
||||
read(0, 0, width, height, 0x1908, 0x1401, pixels.data());
|
||||
for (int row = 0; row < height / 2; ++row)
|
||||
for (int col = 0; col < width * 4; ++col)
|
||||
std::swap(pixels[size_t(row) * width * 4 + col],
|
||||
pixels[size_t(height - row - 1) * width * 4 + col]);
|
||||
#endif
|
||||
return pixels;
|
||||
}
|
||||
void report_start_error(const char *product, const std::string &message, bool dialog) noexcept {
|
||||
std::fprintf(stderr, "%s: %s\n", product, message.c_str());
|
||||
#ifdef _WIN32
|
||||
try {
|
||||
wchar_t directory[32768]{};
|
||||
const auto length = GetEnvironmentVariableW(L"LOCALAPPDATA", directory, 32768);
|
||||
auto root = (length > 0 && length < 32768 ? std::filesystem::path(directory)
|
||||
: std::filesystem::temp_directory_path()) /
|
||||
L"OpenBMH";
|
||||
std::filesystem::create_directories(root);
|
||||
std::ofstream log(root / (std::string(product) + "-startup.log"), std::ios::app);
|
||||
log << message << '\n';
|
||||
if (dialog && !std::getenv("OPENBMH_NO_ERROR_DIALOG"))
|
||||
MessageBoxW(nullptr, wide(message).c_str(), wide(product).c_str(), MB_OK | MB_ICONERROR);
|
||||
} catch (...) {
|
||||
if (dialog && !std::getenv("OPENBMH_NO_ERROR_DIALOG"))
|
||||
MessageBoxW(nullptr, L"The application could not start.", L"OpenBMH", MB_OK | MB_ICONERROR);
|
||||
}
|
||||
#else
|
||||
if (dialog && !std::getenv("OPENBMH_NO_ERROR_DIALOG"))
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, product, message.c_str(), nullptr);
|
||||
#endif
|
||||
}
|
||||
void attach_parent_console() noexcept {
|
||||
#ifdef _WIN32
|
||||
const bool out_free = _fileno(stdout) < 0, err_free = _fileno(stderr) < 0;
|
||||
if ((out_free || err_free) && AttachConsole(ATTACH_PARENT_PROCESS)) {
|
||||
FILE *stream = nullptr;
|
||||
if (out_free)
|
||||
freopen_s(&stream, "CONOUT$", "w", stdout);
|
||||
if (err_free)
|
||||
freopen_s(&stream, "CONOUT$", "w", stderr);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
} // namespace bmh::rml
|
||||
@@ -0,0 +1,74 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#pragma once
|
||||
#include <SDL.h>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#ifdef _WIN32
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <RmlUi_Renderer_DX11.h>
|
||||
#include <wrl/client.h>
|
||||
#else
|
||||
#include <RmlUi_Renderer_GL3.h>
|
||||
#endif
|
||||
|
||||
namespace bmh::rml {
|
||||
// The native Windows renderer needs no OpenGL ICD in a Remote Desktop session.
|
||||
// The device falls back to Microsoft's built-in WARP when hardware is absent.
|
||||
class Video {
|
||||
public:
|
||||
SDL_Window *window = nullptr;
|
||||
Video(const char *title, int width, int height, int minimum_width, int minimum_height);
|
||||
~Video();
|
||||
Video(const Video &) = delete;
|
||||
Video &operator=(const Video &) = delete;
|
||||
void drawable_size(int &width, int &height) const;
|
||||
void present();
|
||||
// RGBA rows in top-to-bottom order, independent of the graphics API.
|
||||
std::vector<uint8_t> capture(int &width, int &height);
|
||||
#ifdef _WIN32
|
||||
ID3D11Device *device() const { return device_.Get(); }
|
||||
ID3D11RenderTargetView *target() const { return target_.Get(); }
|
||||
void resize(int width, int height);
|
||||
#endif
|
||||
private:
|
||||
void close() noexcept;
|
||||
#ifdef _WIN32
|
||||
Microsoft::WRL::ComPtr<ID3D11Device> device_;
|
||||
Microsoft::WRL::ComPtr<ID3D11DeviceContext> context_;
|
||||
Microsoft::WRL::ComPtr<IDXGISwapChain> swapchain_;
|
||||
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> target_;
|
||||
int width_ = 0, height_ = 0;
|
||||
#else
|
||||
SDL_GLContext gl_ = nullptr;
|
||||
bool loader_ = false;
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
class Renderer : public RenderInterface_DX11 {
|
||||
Video &video_;
|
||||
|
||||
public:
|
||||
explicit Renderer(Video &video) : RenderInterface_DX11(video.device()), video_(video) {}
|
||||
void SetViewport(int width, int height) {
|
||||
video_.resize(width, height);
|
||||
RenderInterface_DX11::SetViewport(width, height);
|
||||
}
|
||||
void Clear() { RenderInterface_DX11::Clear(video_.target()); }
|
||||
void EndFrame() { RenderInterface_DX11::EndFrame(video_.target()); }
|
||||
explicit operator bool() const { return video_.device() != nullptr; }
|
||||
};
|
||||
#else
|
||||
class Renderer : public RenderInterface_GL3 {
|
||||
public:
|
||||
explicit Renderer(Video &) {}
|
||||
};
|
||||
#endif
|
||||
// Also used before SDL initialization, so an unavailable graphics driver cannot
|
||||
// prevent reporting the actual startup error. No preferences or tokens are logged.
|
||||
void report_start_error(const char *product, const std::string &message, bool dialog = true) noexcept;
|
||||
void attach_parent_console() noexcept;
|
||||
} // namespace bmh::rml
|
||||
@@ -22,8 +22,15 @@ FetchContent_Declare(rmlui
|
||||
FetchContent_MakeAvailable(rmlui)
|
||||
add_library(bmh_rmlui STATIC
|
||||
${rmlui_SOURCE_DIR}/Backends/RmlUi_Platform_SDL.cpp
|
||||
${rmlui_SOURCE_DIR}/Backends/RmlUi_Renderer_GL3.cpp)
|
||||
target_include_directories(bmh_rmlui PUBLIC ${rmlui_SOURCE_DIR}/Backends)
|
||||
${CMAKE_SOURCE_DIR}/src/launch/rml_video.cpp)
|
||||
if(WIN32)
|
||||
target_sources(bmh_rmlui PRIVATE ${rmlui_SOURCE_DIR}/Backends/RmlUi_Renderer_DX11.cpp)
|
||||
target_link_libraries(bmh_rmlui PUBLIC d3d11 dxgi d3dcompiler)
|
||||
target_compile_definitions(bmh_rmlui PUBLIC NUM_MSAA_SAMPLES=1 NOMINMAX)
|
||||
else()
|
||||
target_sources(bmh_rmlui PRIVATE ${rmlui_SOURCE_DIR}/Backends/RmlUi_Renderer_GL3.cpp)
|
||||
endif()
|
||||
target_include_directories(bmh_rmlui PUBLIC ${rmlui_SOURCE_DIR}/Backends ${CMAKE_SOURCE_DIR}/src)
|
||||
target_compile_definitions(bmh_rmlui PUBLIC RMLUI_SDL_VERSION_MAJOR=2 RMLUI_NUM_MSAA_SAMPLES=0)
|
||||
target_link_libraries(bmh_rmlui PUBLIC RmlUi::Core ${BMH_SDL_TARGET} PRIVATE ${CMAKE_DL_LIBS})
|
||||
|
||||
|
||||
Reference in new issue
Block a user