From 0c8073d71dd62a8eaa43b9c2b8f2cc7e304f65be Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Fri, 17 Jul 2026 14:08:02 +0800 Subject: [PATCH] feat(window): add video driver selection via config and --video-driver argument --- include/Cubed/argument.hpp | 1 + include/Cubed/tools/env_tools.hpp | 14 ++++++++++++++ include/Cubed/window.hpp | 3 ++- src/app.cpp | 7 ++++++- src/window.cpp | 17 ++++++++++++++++- 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 include/Cubed/tools/env_tools.hpp diff --git a/include/Cubed/argument.hpp b/include/Cubed/argument.hpp index 6cc9974..7124ceb 100644 --- a/include/Cubed/argument.hpp +++ b/include/Cubed/argument.hpp @@ -8,5 +8,6 @@ struct Argument { std::optional player; std::optional no_debug; std::optional language; + std::optional video_driver; }; } // namespace Cubed \ No newline at end of file diff --git a/include/Cubed/tools/env_tools.hpp b/include/Cubed/tools/env_tools.hpp new file mode 100644 index 0000000..fd22932 --- /dev/null +++ b/include/Cubed/tools/env_tools.hpp @@ -0,0 +1,14 @@ +#pragma once +#include // IWYU pragma: keep +namespace Cubed { +namespace Tools { + +inline void set_env(const char* name, const char* value) { +#ifdef _WIN32 + _putenv_s(name, value); +#else + setenv(name, value, 1); +#endif +} +} // namespace Tools +} // namespace Cubed \ No newline at end of file diff --git a/include/Cubed/window.hpp b/include/Cubed/window.hpp index 4285f73..28a959b 100644 --- a/include/Cubed/window.hpp +++ b/include/Cubed/window.hpp @@ -1,5 +1,6 @@ #pragma once +#include "Cubed/argument.hpp" #include "Cubed/config.hpp" #include "Cubed/input/event.hpp" @@ -15,7 +16,7 @@ public: bool is_mouse_enable() const; const SDL_Window* get_window() const; SDL_Window* get_window(); - void init(); + void init(const Argument& argument); void imgui_init(); // end of frame to reload! diff --git a/src/app.cpp b/src/app.cpp index 0b76649..77d7444 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -40,7 +40,7 @@ void App::init(int argc, char** argv) { m_game_config.get("language", default_value)); } - m_window.init(); + m_window.init(m_argument); m_window.imgui_init(); Logger::info("Window Init Success"); @@ -122,6 +122,11 @@ void App::handle_argument(int argc, char** argv) { [&](ArgParser& p) { auto arg = p.require_next("--language"); m_argument.language = arg; + }}, + {"--video-driver", + [&](ArgParser& p) { + auto arg = p.require_next("--video-driver"); + m_argument.video_driver = arg; } } diff --git a/src/window.cpp b/src/window.cpp index fcb2d3c..8d174d0 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,6 +3,7 @@ #include "Cubed/camera.hpp" #include "Cubed/gameplay/client_player.hpp" #include "Cubed/tools/cubed_assert.hpp" +#include "Cubed/tools/env_tools.hpp" #include "Cubed/tools/font.hpp" #include "Cubed/tools/log.hpp" @@ -109,7 +110,21 @@ bool Window::handle_mouse_button_event(const MouseButtonEvent& e) { return false; } -void Window::init() { +void Window::init(const Argument& argument) { + auto video_driver = m_config.get("video_driver", std::string("auto")); + if (argument.video_driver) { + video_driver = *argument.video_driver; + } + if (video_driver == "wayland") { + Tools::set_env("SDL_VIDEO_DRIVER", "wayland"); + } else if (video_driver == "x11") { + Tools::set_env("SDL_VIDEO_DRIVER", "x11"); + } else if (video_driver == "auto") { + + } else { + Logger::warn("Unknow Vider Driver {}", video_driver); + } + if (!SDL_Init(SDL_INIT_VIDEO)) { Logger::error("sdl3 init fail"); exit(EXIT_FAILURE);