feat(window): add video driver selection via config and --video-driver argument

This commit is contained in:
2026-07-17 14:08:02 +08:00
parent 9617978dd5
commit 0c8073d71d
5 changed files with 39 additions and 3 deletions

View File

@@ -8,5 +8,6 @@ struct Argument {
std::optional<std::string> player; std::optional<std::string> player;
std::optional<bool> no_debug; std::optional<bool> no_debug;
std::optional<std::string> language; std::optional<std::string> language;
std::optional<std::string> video_driver;
}; };
} // namespace Cubed } // namespace Cubed

View File

@@ -0,0 +1,14 @@
#pragma once
#include <cstdlib> // 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

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "Cubed/argument.hpp"
#include "Cubed/config.hpp" #include "Cubed/config.hpp"
#include "Cubed/input/event.hpp" #include "Cubed/input/event.hpp"
@@ -15,7 +16,7 @@ public:
bool is_mouse_enable() const; bool is_mouse_enable() const;
const SDL_Window* get_window() const; const SDL_Window* get_window() const;
SDL_Window* get_window(); SDL_Window* get_window();
void init(); void init(const Argument& argument);
void imgui_init(); void imgui_init();
// end of frame to reload! // end of frame to reload!

View File

@@ -40,7 +40,7 @@ void App::init(int argc, char** argv) {
m_game_config.get("language", default_value)); m_game_config.get("language", default_value));
} }
m_window.init(); m_window.init(m_argument);
m_window.imgui_init(); m_window.imgui_init();
Logger::info("Window Init Success"); Logger::info("Window Init Success");
@@ -122,6 +122,11 @@ void App::handle_argument(int argc, char** argv) {
[&](ArgParser& p) { [&](ArgParser& p) {
auto arg = p.require_next("--language"); auto arg = p.require_next("--language");
m_argument.language = arg; m_argument.language = arg;
}},
{"--video-driver",
[&](ArgParser& p) {
auto arg = p.require_next("--video-driver");
m_argument.video_driver = arg;
} }
} }

View File

@@ -3,6 +3,7 @@
#include "Cubed/camera.hpp" #include "Cubed/camera.hpp"
#include "Cubed/gameplay/client_player.hpp" #include "Cubed/gameplay/client_player.hpp"
#include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/env_tools.hpp"
#include "Cubed/tools/font.hpp" #include "Cubed/tools/font.hpp"
#include "Cubed/tools/log.hpp" #include "Cubed/tools/log.hpp"
@@ -109,7 +110,21 @@ bool Window::handle_mouse_button_event(const MouseButtonEvent& e) {
return false; 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)) { if (!SDL_Init(SDL_INIT_VIDEO)) {
Logger::error("sdl3 init fail"); Logger::error("sdl3 init fail");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);