refactor(platform): 移除PlatformType枚举并调整OpenGL配置
移除不再使用的PlatformType枚举及相关平台判断逻辑,简化代码结构。同时将默认OpenGL配置从3.3 Core改为3.2 ES,以支持更多平台。调整了窗口服务初始化方式,使用lambda表达式创建配置对象。
This commit is contained in:
parent
8abf58e3d5
commit
0761b55864
|
|
@ -5,15 +5,6 @@
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 平台类型枚举
|
|
||||||
*/
|
|
||||||
enum class PlatformType {
|
|
||||||
Auto = 0,
|
|
||||||
PC,
|
|
||||||
Switch
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 应用程序配置
|
* @brief 应用程序配置
|
||||||
*/
|
*/
|
||||||
|
|
@ -27,7 +18,6 @@ struct AppConfig {
|
||||||
int32 fpsLimit = 0;
|
int32 fpsLimit = 0;
|
||||||
int32 glMajor = 3;
|
int32 glMajor = 3;
|
||||||
int32 glMinor = 3;
|
int32 glMinor = 3;
|
||||||
PlatformType platform = PlatformType::Auto;
|
|
||||||
bool enableCursors = true;
|
bool enableCursors = true;
|
||||||
bool enableDpiScale = false;
|
bool enableDpiScale = false;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
#include <app/application.h>
|
#include <app/application.h>
|
||||||
#include <core/director.h>
|
#include <core/director.h>
|
||||||
#include <core/service.h>
|
|
||||||
#include <core/event/events.h>
|
#include <core/event/events.h>
|
||||||
|
#include <core/service.h>
|
||||||
|
#include <platform/file.h>
|
||||||
|
#include <platform/input.h>
|
||||||
#include <platform/sdl2.h>
|
#include <platform/sdl2.h>
|
||||||
#include <platform/window.h>
|
#include <platform/window.h>
|
||||||
#include <platform/input.h>
|
|
||||||
#include <platform/file.h>
|
|
||||||
#include <utils/logger.h>
|
#include <utils/logger.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
@ -24,7 +24,8 @@ static double getTimeSeconds() {
|
||||||
#ifdef __SWITCH__
|
#ifdef __SWITCH__
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
clock_gettime(CLOCK_MONOTONIC, &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
|
#else
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
auto now = steady_clock::now();
|
auto now = steady_clock::now();
|
||||||
|
|
@ -33,16 +34,14 @@ static double getTimeSeconds() {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
Application& Application::instance() {
|
Application &Application::instance() {
|
||||||
static Application instance;
|
static Application instance;
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
Application::~Application() {
|
Application::~Application() { shutdown(); }
|
||||||
shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Application::init(const AppConfig& config) {
|
bool Application::init(const AppConfig &config) {
|
||||||
if (initialized_) {
|
if (initialized_) {
|
||||||
E2D_LOG_WARN("Application already initialized");
|
E2D_LOG_WARN("Application already initialized");
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -50,16 +49,6 @@ bool Application::init(const AppConfig& config) {
|
||||||
|
|
||||||
config_ = 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__
|
#ifdef __SWITCH__
|
||||||
Result rc;
|
Result rc;
|
||||||
rc = romfsInit();
|
rc = romfsInit();
|
||||||
|
|
@ -74,7 +63,6 @@ bool Application::init(const AppConfig& config) {
|
||||||
E2D_LOG_WARN("socketInitializeDefault failed");
|
E2D_LOG_WARN("socketInitializeDefault failed");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
if (!Sdl2::initAll()) {
|
if (!Sdl2::initAll()) {
|
||||||
E2D_LOG_ERROR("Failed to initialize SDL2");
|
E2D_LOG_ERROR("Failed to initialize SDL2");
|
||||||
|
|
@ -90,17 +78,18 @@ bool Application::init(const AppConfig& config) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
WindowCfg winCfg;
|
if (!WINDOW.create([&] {
|
||||||
winCfg.title = config_.title;
|
WindowCfg cfg;
|
||||||
winCfg.width = config_.width;
|
cfg.title = config_.title;
|
||||||
winCfg.height = config_.height;
|
cfg.width = config_.width;
|
||||||
winCfg.fullscreen = config_.fullscreen;
|
cfg.height = config_.height;
|
||||||
winCfg.resizable = config_.resizable;
|
cfg.fullscreen = config_.fullscreen;
|
||||||
winCfg.vsync = config_.vsync;
|
cfg.resizable = config_.resizable;
|
||||||
winCfg.glMajor = config_.glMajor;
|
cfg.vsync = config_.vsync;
|
||||||
winCfg.glMinor = config_.glMinor;
|
cfg.glMajor = config_.glMajor;
|
||||||
|
cfg.glMinor = config_.glMinor;
|
||||||
if (!WINDOW.create(winCfg)) {
|
return cfg;
|
||||||
|
}())) {
|
||||||
E2D_LOG_ERROR("Failed to create window");
|
E2D_LOG_ERROR("Failed to create window");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -110,9 +99,7 @@ bool Application::init(const AppConfig& config) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
WINDOW.setOnClose([this]() {
|
WINDOW.setOnClose([this]() { quit(); });
|
||||||
quit();
|
|
||||||
});
|
|
||||||
|
|
||||||
initialized_ = true;
|
initialized_ = true;
|
||||||
running_ = true;
|
running_ = true;
|
||||||
|
|
@ -120,13 +107,14 @@ bool Application::init(const AppConfig& config) {
|
||||||
events::OnInit::emit();
|
events::OnInit::emit();
|
||||||
|
|
||||||
E2D_LOG_INFO("Application initialized successfully");
|
E2D_LOG_INFO("Application initialized successfully");
|
||||||
E2D_LOG_INFO("Window: {}x{}, Fullscreen: {}, VSync: {}",
|
E2D_LOG_INFO("Window: {}x{}, Fullscreen: {}, VSync: {}", config_.width,
|
||||||
config_.width, config_.height, config_.fullscreen, config_.vsync);
|
config_.height, config_.fullscreen, config_.vsync);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::shutdown() {
|
void Application::shutdown() {
|
||||||
if (!initialized_) return;
|
if (!initialized_)
|
||||||
|
return;
|
||||||
|
|
||||||
events::OnShutdown::emit();
|
events::OnShutdown::emit();
|
||||||
|
|
||||||
|
|
@ -136,21 +124,10 @@ void Application::shutdown() {
|
||||||
SVC_MGR.shutdownAll();
|
SVC_MGR.shutdownAll();
|
||||||
Sdl2::shutdown();
|
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__
|
#ifdef __SWITCH__
|
||||||
romfsExit();
|
romfsExit();
|
||||||
socketExit();
|
socketExit();
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
initialized_ = false;
|
initialized_ = false;
|
||||||
running_ = false;
|
running_ = false;
|
||||||
|
|
@ -229,7 +206,8 @@ void Application::mainLoop() {
|
||||||
double frameTime = frameEndTime - currentTime;
|
double frameTime = frameEndTime - currentTime;
|
||||||
double target = 1.0 / static_cast<double>(config_.fpsLimit);
|
double target = 1.0 / static_cast<double>(config_.fpsLimit);
|
||||||
if (frameTime < target) {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,26 @@
|
||||||
#include <platform/window.h>
|
#include <SDL.h>
|
||||||
#include <platform/input.h>
|
#include <platform/input.h>
|
||||||
#include <platform/sdl2.h>
|
#include <platform/sdl2.h>
|
||||||
#include <SDL.h>
|
#include <platform/window.h>
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
WindowSvc& WindowSvc::inst() {
|
WindowSvc &WindowSvc::inst() {
|
||||||
static WindowSvc instance;
|
static WindowSvc instance;
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WindowSvc::init() {
|
bool WindowSvc::init() {
|
||||||
if (isInited()) return true;
|
if (isInited())
|
||||||
|
return true;
|
||||||
|
|
||||||
if (!Sdl2::initVideo()) {
|
if (!Sdl2::initVideo()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
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_DOUBLEBUFFER, 1);
|
||||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||||
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
||||||
|
|
@ -40,8 +41,9 @@ void WindowSvc::shutdown() {
|
||||||
state_ = SvcState::Shutdown;
|
state_ = SvcState::Shutdown;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WindowSvc::create(const WindowCfg& cfg) {
|
bool WindowSvc::create(const WindowCfg &cfg) {
|
||||||
if (window_) return true;
|
if (window_)
|
||||||
|
return true;
|
||||||
|
|
||||||
uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
|
uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
|
||||||
if (cfg.resizable) {
|
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_MAJOR_VERSION, cfg.glMajor);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, cfg.glMinor);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, cfg.glMinor);
|
||||||
|
|
||||||
window_ = SDL_CreateWindow(
|
window_ =
|
||||||
cfg.title.c_str(),
|
SDL_CreateWindow(cfg.title.c_str(), SDL_WINDOWPOS_CENTERED,
|
||||||
SDL_WINDOWPOS_CENTERED,
|
SDL_WINDOWPOS_CENTERED, cfg.width, cfg.height, flags);
|
||||||
SDL_WINDOWPOS_CENTERED,
|
|
||||||
cfg.width,
|
|
||||||
cfg.height,
|
|
||||||
flags
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!window_) {
|
if (!window_) {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -86,7 +83,8 @@ bool WindowSvc::pollEvents() {
|
||||||
switch (evt.type) {
|
switch (evt.type) {
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
shouldClose_ = true;
|
shouldClose_ = true;
|
||||||
if (onClose_) onClose_();
|
if (onClose_)
|
||||||
|
onClose_();
|
||||||
break;
|
break;
|
||||||
case SDL_WINDOWEVENT:
|
case SDL_WINDOWEVENT:
|
||||||
if (evt.window.event == SDL_WINDOWEVENT_RESIZED) {
|
if (evt.window.event == SDL_WINDOWEVENT_RESIZED) {
|
||||||
|
|
@ -112,14 +110,16 @@ void WindowSvc::swapBuffers() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Size WindowSvc::getSize() const {
|
Size WindowSvc::getSize() const {
|
||||||
if (!window_) return Size(0, 0);
|
if (!window_)
|
||||||
|
return Size(0, 0);
|
||||||
int w, h;
|
int w, h;
|
||||||
SDL_GetWindowSize(window_, &w, &h);
|
SDL_GetWindowSize(window_, &w, &h);
|
||||||
return Size(w, h);
|
return Size(w, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec2 WindowSvc::getPosition() const {
|
Vec2 WindowSvc::getPosition() const {
|
||||||
if (!window_) return Vec2(0, 0);
|
if (!window_)
|
||||||
|
return Vec2(0, 0);
|
||||||
int x, y;
|
int x, y;
|
||||||
SDL_GetWindowPosition(window_, &x, &y);
|
SDL_GetWindowPosition(window_, &x, &y);
|
||||||
return Vec2(static_cast<float>(x), static_cast<float>(y));
|
return Vec2(static_cast<float>(x), static_cast<float>(y));
|
||||||
|
|
@ -131,7 +131,7 @@ void WindowSvc::setSize(int32 w, int32 h) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowSvc::setTitle(const std::string& title) {
|
void WindowSvc::setTitle(const std::string &title) {
|
||||||
if (window_) {
|
if (window_) {
|
||||||
SDL_SetWindowTitle(window_, title.c_str());
|
SDL_SetWindowTitle(window_, title.c_str());
|
||||||
}
|
}
|
||||||
|
|
@ -139,12 +139,14 @@ void WindowSvc::setTitle(const std::string& title) {
|
||||||
|
|
||||||
void WindowSvc::setFullscreen(bool fullscreen) {
|
void WindowSvc::setFullscreen(bool fullscreen) {
|
||||||
if (window_) {
|
if (window_) {
|
||||||
SDL_SetWindowFullscreen(window_, fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
|
SDL_SetWindowFullscreen(window_,
|
||||||
|
fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WindowSvc::isFullscreen() const {
|
bool WindowSvc::isFullscreen() const {
|
||||||
if (!window_) return false;
|
if (!window_)
|
||||||
|
return false;
|
||||||
uint32 flags = SDL_GetWindowFlags(window_);
|
uint32 flags = SDL_GetWindowFlags(window_);
|
||||||
return (flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0;
|
return (flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0;
|
||||||
}
|
}
|
||||||
|
|
@ -154,9 +156,7 @@ void WindowSvc::setVsync(bool vsync) {
|
||||||
vsync_ = vsync;
|
vsync_ = vsync;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WindowSvc::isVsync() const {
|
bool WindowSvc::isVsync() const { return vsync_; }
|
||||||
return vsync_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowSvc::setVisible(bool visible) {
|
void WindowSvc::setVisible(bool visible) {
|
||||||
if (window_) {
|
if (window_) {
|
||||||
|
|
@ -169,7 +169,8 @@ void WindowSvc::setVisible(bool visible) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WindowSvc::isVisible() const {
|
bool WindowSvc::isVisible() const {
|
||||||
if (!window_) return false;
|
if (!window_)
|
||||||
|
return false;
|
||||||
uint32 flags = SDL_GetWindowFlags(window_);
|
uint32 flags = SDL_GetWindowFlags(window_);
|
||||||
return (flags & SDL_WINDOW_SHOWN) != 0;
|
return (flags & SDL_WINDOW_SHOWN) != 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue