refactor(platform): 移除PlatformType枚举并调整OpenGL配置

移除不再使用的PlatformType枚举及相关平台判断逻辑,简化代码结构。同时将默认OpenGL配置从3.3 Core改为3.2 ES,以支持更多平台。调整了窗口服务初始化方式,使用lambda表达式创建配置对象。
This commit is contained in:
ChestnutYueyue 2026-02-27 23:08:49 +08:00
parent 8abf58e3d5
commit 0761b55864
3 changed files with 281 additions and 312 deletions

View File

@ -5,15 +5,6 @@
namespace extra2d {
/**
* @brief
*/
enum class PlatformType {
Auto = 0,
PC,
Switch
};
/**
* @brief
*/
@ -27,7 +18,6 @@ struct AppConfig {
int32 fpsLimit = 0;
int32 glMajor = 3;
int32 glMinor = 3;
PlatformType platform = PlatformType::Auto;
bool enableCursors = true;
bool enableDpiScale = false;
};

View File

@ -1,11 +1,11 @@
#include <app/application.h>
#include <core/director.h>
#include <core/service.h>
#include <core/event/events.h>
#include <core/service.h>
#include <platform/file.h>
#include <platform/input.h>
#include <platform/sdl2.h>
#include <platform/window.h>
#include <platform/input.h>
#include <platform/file.h>
#include <utils/logger.h>
#include <chrono>
@ -24,7 +24,8 @@ static double getTimeSeconds() {
#ifdef __SWITCH__
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return static_cast<double>(ts.tv_sec) + static_cast<double>(ts.tv_nsec) / 1000000000.0;
return static_cast<double>(ts.tv_sec) +
static_cast<double>(ts.tv_nsec) / 1000000000.0;
#else
using namespace std::chrono;
auto now = steady_clock::now();
@ -38,9 +39,7 @@ Application& Application::instance() {
return instance;
}
Application::~Application() {
shutdown();
}
Application::~Application() { shutdown(); }
bool Application::init(const AppConfig &config) {
if (initialized_) {
@ -50,16 +49,6 @@ bool Application::init(const AppConfig& config) {
config_ = config;
PlatformType platform = config_.platform;
if (platform == PlatformType::Auto) {
#ifdef __SWITCH__
platform = PlatformType::Switch;
#else
platform = PlatformType::PC;
#endif
}
if (platform == PlatformType::Switch) {
#ifdef __SWITCH__
Result rc;
rc = romfsInit();
@ -74,7 +63,6 @@ bool Application::init(const AppConfig& config) {
E2D_LOG_WARN("socketInitializeDefault failed");
}
#endif
}
if (!Sdl2::initAll()) {
E2D_LOG_ERROR("Failed to initialize SDL2");
@ -90,17 +78,18 @@ bool Application::init(const AppConfig& config) {
return false;
}
WindowCfg winCfg;
winCfg.title = config_.title;
winCfg.width = config_.width;
winCfg.height = config_.height;
winCfg.fullscreen = config_.fullscreen;
winCfg.resizable = config_.resizable;
winCfg.vsync = config_.vsync;
winCfg.glMajor = config_.glMajor;
winCfg.glMinor = config_.glMinor;
if (!WINDOW.create(winCfg)) {
if (!WINDOW.create([&] {
WindowCfg cfg;
cfg.title = config_.title;
cfg.width = config_.width;
cfg.height = config_.height;
cfg.fullscreen = config_.fullscreen;
cfg.resizable = config_.resizable;
cfg.vsync = config_.vsync;
cfg.glMajor = config_.glMajor;
cfg.glMinor = config_.glMinor;
return cfg;
}())) {
E2D_LOG_ERROR("Failed to create window");
return false;
}
@ -110,9 +99,7 @@ bool Application::init(const AppConfig& config) {
return false;
}
WINDOW.setOnClose([this]() {
quit();
});
WINDOW.setOnClose([this]() { quit(); });
initialized_ = true;
running_ = true;
@ -120,13 +107,14 @@ bool Application::init(const AppConfig& config) {
events::OnInit::emit();
E2D_LOG_INFO("Application initialized successfully");
E2D_LOG_INFO("Window: {}x{}, Fullscreen: {}, VSync: {}",
config_.width, config_.height, config_.fullscreen, config_.vsync);
E2D_LOG_INFO("Window: {}x{}, Fullscreen: {}, VSync: {}", config_.width,
config_.height, config_.fullscreen, config_.vsync);
return true;
}
void Application::shutdown() {
if (!initialized_) return;
if (!initialized_)
return;
events::OnShutdown::emit();
@ -136,21 +124,10 @@ void Application::shutdown() {
SVC_MGR.shutdownAll();
Sdl2::shutdown();
PlatformType platform = config_.platform;
if (platform == PlatformType::Auto) {
#ifdef __SWITCH__
platform = PlatformType::Switch;
#else
platform = PlatformType::PC;
#endif
}
if (platform == PlatformType::Switch) {
#ifdef __SWITCH__
romfsExit();
socketExit();
#endif
}
initialized_ = false;
running_ = false;
@ -229,7 +206,8 @@ void Application::mainLoop() {
double frameTime = frameEndTime - currentTime;
double target = 1.0 / static_cast<double>(config_.fpsLimit);
if (frameTime < target) {
std::this_thread::sleep_for(std::chrono::duration<double>(target - frameTime));
std::this_thread::sleep_for(
std::chrono::duration<double>(target - frameTime));
}
}
}

View File

@ -1,7 +1,7 @@
#include <platform/window.h>
#include <SDL.h>
#include <platform/input.h>
#include <platform/sdl2.h>
#include <SDL.h>
#include <platform/window.h>
namespace extra2d {
@ -11,15 +11,16 @@ WindowSvc& WindowSvc::inst() {
}
bool WindowSvc::init() {
if (isInited()) return true;
if (isInited())
return true;
if (!Sdl2::initVideo()) {
return false;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
@ -41,7 +42,8 @@ void WindowSvc::shutdown() {
}
bool WindowSvc::create(const WindowCfg &cfg) {
if (window_) return true;
if (window_)
return true;
uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
if (cfg.resizable) {
@ -54,14 +56,9 @@ bool WindowSvc::create(const WindowCfg& cfg) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, cfg.glMajor);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, cfg.glMinor);
window_ = SDL_CreateWindow(
cfg.title.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
cfg.width,
cfg.height,
flags
);
window_ =
SDL_CreateWindow(cfg.title.c_str(), SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, cfg.width, cfg.height, flags);
if (!window_) {
return false;
@ -86,7 +83,8 @@ bool WindowSvc::pollEvents() {
switch (evt.type) {
case SDL_QUIT:
shouldClose_ = true;
if (onClose_) onClose_();
if (onClose_)
onClose_();
break;
case SDL_WINDOWEVENT:
if (evt.window.event == SDL_WINDOWEVENT_RESIZED) {
@ -112,14 +110,16 @@ void WindowSvc::swapBuffers() {
}
Size WindowSvc::getSize() const {
if (!window_) return Size(0, 0);
if (!window_)
return Size(0, 0);
int w, h;
SDL_GetWindowSize(window_, &w, &h);
return Size(w, h);
}
Vec2 WindowSvc::getPosition() const {
if (!window_) return Vec2(0, 0);
if (!window_)
return Vec2(0, 0);
int x, y;
SDL_GetWindowPosition(window_, &x, &y);
return Vec2(static_cast<float>(x), static_cast<float>(y));
@ -139,12 +139,14 @@ void WindowSvc::setTitle(const std::string& title) {
void WindowSvc::setFullscreen(bool fullscreen) {
if (window_) {
SDL_SetWindowFullscreen(window_, fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
SDL_SetWindowFullscreen(window_,
fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
}
}
bool WindowSvc::isFullscreen() const {
if (!window_) return false;
if (!window_)
return false;
uint32 flags = SDL_GetWindowFlags(window_);
return (flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0;
}
@ -154,9 +156,7 @@ void WindowSvc::setVsync(bool vsync) {
vsync_ = vsync;
}
bool WindowSvc::isVsync() const {
return vsync_;
}
bool WindowSvc::isVsync() const { return vsync_; }
void WindowSvc::setVisible(bool visible) {
if (window_) {
@ -169,7 +169,8 @@ void WindowSvc::setVisible(bool visible) {
}
bool WindowSvc::isVisible() const {
if (!window_) return false;
if (!window_)
return false;
uint32 flags = SDL_GetWindowFlags(window_);
return (flags & SDL_WINDOW_SHOWN) != 0;
}