mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(window): add video driver selection via config and --video-driver argument
This commit is contained in:
@@ -8,5 +8,6 @@ struct Argument {
|
||||
std::optional<std::string> player;
|
||||
std::optional<bool> no_debug;
|
||||
std::optional<std::string> language;
|
||||
std::optional<std::string> video_driver;
|
||||
};
|
||||
} // namespace Cubed
|
||||
14
include/Cubed/tools/env_tools.hpp
Normal file
14
include/Cubed/tools/env_tools.hpp
Normal 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
|
||||
@@ -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!
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user