From 38148a6c54192a834d54596895cecd7b74eb4d08 Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Sun, 15 Feb 2026 09:22:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0SDL2=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=E6=94=AF=E6=8C=81=E5=B9=B6=E5=AE=9E=E7=8E=B0=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E7=A4=BA=E4=BE=8B=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加SDL2作为平台后端支持,包括窗口创建、输入处理和GLAD初始化 实现基础示例程序展示引擎基本功能 重构平台配置代码以提高可读性 移除未使用的input_codes.h头文件 添加demo_basic构建目标到xmake配置 --- Extra2D/include/extra2d/extra2d.h | 1 - .../extra2d/platform/platform_module.h | 1 + Extra2D/src/app/application.cpp | 20 +- Extra2D/src/config/platform_config.cpp | 592 +++++++++--------- .../platform/backends/sdl2/sdl2_backend.cpp | 21 +- .../platform/backends/sdl2/sdl2_window.cpp | 11 + examples/basic/main.cpp | 59 ++ xmake.lua | 20 + 8 files changed, 433 insertions(+), 292 deletions(-) create mode 100644 examples/basic/main.cpp diff --git a/Extra2D/include/extra2d/extra2d.h b/Extra2D/include/extra2d/extra2d.h index ca25447..bffb894 100644 --- a/Extra2D/include/extra2d/extra2d.h +++ b/Extra2D/include/extra2d/extra2d.h @@ -48,7 +48,6 @@ #include #include #include -#include // Utils #include diff --git a/Extra2D/include/extra2d/platform/platform_module.h b/Extra2D/include/extra2d/platform/platform_module.h index 98ffaa6..faeb144 100644 --- a/Extra2D/include/extra2d/platform/platform_module.h +++ b/Extra2D/include/extra2d/platform/platform_module.h @@ -79,6 +79,7 @@ private: */ #define E2D_REG_BACKEND(name, WinClass, InClass) \ namespace { \ + __attribute__((used)) \ static struct E2D_BACKEND_REG_##name { \ E2D_BACKEND_REG_##name() { \ ::extra2d::BackendFactory::reg( \ diff --git a/Extra2D/src/app/application.cpp b/Extra2D/src/app/application.cpp index 9e3dfb5..86abfbe 100644 --- a/Extra2D/src/app/application.cpp +++ b/Extra2D/src/app/application.cpp @@ -16,10 +16,18 @@ #include #include +#include + #ifdef __SWITCH__ #include #endif +#ifdef E2D_BACKEND_SDL2 +namespace extra2d { +void initSDL2Backend(); +} +#endif + namespace extra2d { /** @@ -56,10 +64,16 @@ bool Application::init() { bool Application::init(const AppConfig& config) { if (initialized_) { - E2D_LOG_WARN("Application already initialized"); return true; } + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0) { + E2D_LOG_ERROR("Failed to initialize SDL: {}", SDL_GetError()); + return false; + } + + Logger::init(); + E2D_LOG_INFO("Initializing application with config..."); if (!ConfigManager::instance().initialize()) { @@ -92,6 +106,10 @@ bool Application::init(const std::string& configPath) { } bool Application::initImpl() { +#ifdef E2D_BACKEND_SDL2 + initSDL2Backend(); +#endif + auto& configMgr = ConfigManager::instance(); AppConfig& appConfig = configMgr.appConfig(); diff --git a/Extra2D/src/config/platform_config.cpp b/Extra2D/src/config/platform_config.cpp index c151709..262078a 100644 --- a/Extra2D/src/config/platform_config.cpp +++ b/Extra2D/src/config/platform_config.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #ifdef _WIN32 @@ -16,320 +16,328 @@ namespace { class WindowsPlatformConfig : public PlatformConfig { public: - WindowsPlatformConfig() { - caps_.supportsWindowed = true; - caps_.supportsFullscreen = true; - caps_.supportsBorderless = true; - caps_.supportsCursor = true; - caps_.supportsCursorHide = true; - caps_.supportsDPIAwareness = true; - caps_.supportsVSync = true; - caps_.supportsMultiMonitor = true; - caps_.supportsClipboard = true; - caps_.supportsGamepad = true; - caps_.supportsTouch = false; - caps_.supportsKeyboard = true; - caps_.supportsMouse = true; - caps_.supportsResize = true; - caps_.supportsHighDPI = true; - caps_.maxTextureSize = 16384; - caps_.preferredScreenWidth = 1920; - caps_.preferredScreenHeight = 1080; - caps_.defaultDPI = 96.0f; - } + WindowsPlatformConfig() { + caps_.supportsWindowed = true; + caps_.supportsFullscreen = true; + caps_.supportsBorderless = true; + caps_.supportsCursor = true; + caps_.supportsCursorHide = true; + caps_.supportsDPIAwareness = true; + caps_.supportsVSync = true; + caps_.supportsMultiMonitor = true; + caps_.supportsClipboard = true; + caps_.supportsGamepad = true; + caps_.supportsTouch = false; + caps_.supportsKeyboard = true; + caps_.supportsMouse = true; + caps_.supportsResize = true; + caps_.supportsHighDPI = true; + caps_.maxTextureSize = 16384; + caps_.preferredScreenWidth = 1920; + caps_.preferredScreenHeight = 1080; + caps_.defaultDPI = 96.0f; + } - PlatformType platformType() const override { return PlatformType::Windows; } - const char* platformName() const override { return "Windows"; } - const PlatformCapabilities& capabilities() const override { return caps_; } + PlatformType platformType() const override { return PlatformType::Windows; } + const char *platformName() const override { return "Windows"; } + const PlatformCapabilities &capabilities() const override { return caps_; } - void applyConstraints(AppConfig& config) const override { - if (config.window.width < 320) config.window.width = 320; - if (config.window.height < 240) config.window.height = 240; - if (config.window.width > caps_.maxTextureSize) config.window.width = caps_.maxTextureSize; - if (config.window.height > caps_.maxTextureSize) config.window.height = caps_.maxTextureSize; - } + void applyConstraints(AppConfig &config) const override { + if (config.window.width < 320) + config.window.width = 320; + if (config.window.height < 240) + config.window.height = 240; + if (config.window.width > caps_.maxTextureSize) + config.window.width = caps_.maxTextureSize; + if (config.window.height > caps_.maxTextureSize) + config.window.height = caps_.maxTextureSize; + } - void applyDefaults(AppConfig& config) const override { - config.window.highDPI = true; - config.window.resizable = true; - config.render.vsync = true; - config.render.targetFPS = 60; - } + void applyDefaults(AppConfig &config) const override { + config.window.highDPI = true; + config.window.resizable = true; + config.render.vsync = true; + config.render.targetFPS = 60; + } - bool validateConfig(AppConfig& config) const override { - if (config.window.width <= 0 || config.window.height <= 0) { - E2D_LOG_ERROR("Windows: Invalid window dimensions"); - return false; - } - return true; + bool validateConfig(AppConfig &config) const override { + if (config.window.width <= 0 || config.window.height <= 0) { + E2D_LOG_ERROR("Windows: Invalid window dimensions"); + return false; } + return true; + } - int getRecommendedWidth() const override { return 1920; } - int getRecommendedHeight() const override { return 1080; } - bool isResolutionSupported(int width, int height) const override { - return width >= 320 && height >= 240 && - width <= caps_.maxTextureSize && height <= caps_.maxTextureSize; - } + int getRecommendedWidth() const override { return 1920; } + int getRecommendedHeight() const override { return 1080; } + bool isResolutionSupported(int width, int height) const override { + return width >= 320 && height >= 240 && width <= caps_.maxTextureSize && + height <= caps_.maxTextureSize; + } private: - PlatformCapabilities caps_; + PlatformCapabilities caps_; }; class LinuxPlatformConfig : public PlatformConfig { public: - LinuxPlatformConfig() { - caps_.supportsWindowed = true; - caps_.supportsFullscreen = true; - caps_.supportsBorderless = true; - caps_.supportsCursor = true; - caps_.supportsCursorHide = true; - caps_.supportsDPIAwareness = true; - caps_.supportsVSync = true; - caps_.supportsMultiMonitor = true; - caps_.supportsClipboard = true; - caps_.supportsGamepad = true; - caps_.supportsTouch = false; - caps_.supportsKeyboard = true; - caps_.supportsMouse = true; - caps_.supportsResize = true; - caps_.supportsHighDPI = true; - caps_.maxTextureSize = 16384; - caps_.preferredScreenWidth = 1920; - caps_.preferredScreenHeight = 1080; - caps_.defaultDPI = 96.0f; - } + LinuxPlatformConfig() { + caps_.supportsWindowed = true; + caps_.supportsFullscreen = true; + caps_.supportsBorderless = true; + caps_.supportsCursor = true; + caps_.supportsCursorHide = true; + caps_.supportsDPIAwareness = true; + caps_.supportsVSync = true; + caps_.supportsMultiMonitor = true; + caps_.supportsClipboard = true; + caps_.supportsGamepad = true; + caps_.supportsTouch = false; + caps_.supportsKeyboard = true; + caps_.supportsMouse = true; + caps_.supportsResize = true; + caps_.supportsHighDPI = true; + caps_.maxTextureSize = 16384; + caps_.preferredScreenWidth = 1920; + caps_.preferredScreenHeight = 1080; + caps_.defaultDPI = 96.0f; + } - PlatformType platformType() const override { return PlatformType::Linux; } - const char* platformName() const override { return "Linux"; } - const PlatformCapabilities& capabilities() const override { return caps_; } + PlatformType platformType() const override { return PlatformType::Linux; } + const char *platformName() const override { return "Linux"; } + const PlatformCapabilities &capabilities() const override { return caps_; } - void applyConstraints(AppConfig& config) const override { - if (config.window.width < 320) config.window.width = 320; - if (config.window.height < 240) config.window.height = 240; - } + void applyConstraints(AppConfig &config) const override { + if (config.window.width < 320) + config.window.width = 320; + if (config.window.height < 240) + config.window.height = 240; + } - void applyDefaults(AppConfig& config) const override { - config.window.resizable = true; - config.render.vsync = true; - } + void applyDefaults(AppConfig &config) const override { + config.window.resizable = true; + config.render.vsync = true; + } - bool validateConfig(AppConfig& config) const override { - if (config.window.width <= 0 || config.window.height <= 0) { - E2D_LOG_ERROR("Linux: Invalid window dimensions"); - return false; - } - return true; + bool validateConfig(AppConfig &config) const override { + if (config.window.width <= 0 || config.window.height <= 0) { + E2D_LOG_ERROR("Linux: Invalid window dimensions"); + return false; } + return true; + } - int getRecommendedWidth() const override { return 1920; } - int getRecommendedHeight() const override { return 1080; } - bool isResolutionSupported(int width, int height) const override { - return width >= 320 && height >= 240; - } + int getRecommendedWidth() const override { return 1920; } + int getRecommendedHeight() const override { return 1080; } + bool isResolutionSupported(int width, int height) const override { + return width >= 320 && height >= 240; + } private: - PlatformCapabilities caps_; + PlatformCapabilities caps_; }; class MacOSPlatformConfig : public PlatformConfig { public: - MacOSPlatformConfig() { - caps_.supportsWindowed = true; - caps_.supportsFullscreen = true; - caps_.supportsBorderless = true; - caps_.supportsCursor = true; - caps_.supportsCursorHide = true; - caps_.supportsDPIAwareness = true; - caps_.supportsVSync = true; - caps_.supportsMultiMonitor = true; - caps_.supportsClipboard = true; - caps_.supportsGamepad = true; - caps_.supportsTouch = false; - caps_.supportsKeyboard = true; - caps_.supportsMouse = true; - caps_.supportsResize = true; - caps_.supportsHighDPI = true; - caps_.maxTextureSize = 16384; - caps_.preferredScreenWidth = 1920; - caps_.preferredScreenHeight = 1080; - caps_.defaultDPI = 144.0f; - } + MacOSPlatformConfig() { + caps_.supportsWindowed = true; + caps_.supportsFullscreen = true; + caps_.supportsBorderless = true; + caps_.supportsCursor = true; + caps_.supportsCursorHide = true; + caps_.supportsDPIAwareness = true; + caps_.supportsVSync = true; + caps_.supportsMultiMonitor = true; + caps_.supportsClipboard = true; + caps_.supportsGamepad = true; + caps_.supportsTouch = false; + caps_.supportsKeyboard = true; + caps_.supportsMouse = true; + caps_.supportsResize = true; + caps_.supportsHighDPI = true; + caps_.maxTextureSize = 16384; + caps_.preferredScreenWidth = 1920; + caps_.preferredScreenHeight = 1080; + caps_.defaultDPI = 144.0f; + } - PlatformType platformType() const override { return PlatformType::macOS; } - const char* platformName() const override { return "macOS"; } - const PlatformCapabilities& capabilities() const override { return caps_; } + PlatformType platformType() const override { return PlatformType::macOS; } + const char *platformName() const override { return "macOS"; } + const PlatformCapabilities &capabilities() const override { return caps_; } - void applyConstraints(AppConfig& config) const override { - if (config.window.width < 320) config.window.width = 320; - if (config.window.height < 240) config.window.height = 240; - } + void applyConstraints(AppConfig &config) const override { + if (config.window.width < 320) + config.window.width = 320; + if (config.window.height < 240) + config.window.height = 240; + } - void applyDefaults(AppConfig& config) const override { - config.window.highDPI = true; - config.window.resizable = true; - config.render.vsync = true; - } + void applyDefaults(AppConfig &config) const override { + config.window.highDPI = true; + config.window.resizable = true; + config.render.vsync = true; + } - bool validateConfig(AppConfig& config) const override { - if (config.window.width <= 0 || config.window.height <= 0) { - E2D_LOG_ERROR("macOS: Invalid window dimensions"); - return false; - } - return true; + bool validateConfig(AppConfig &config) const override { + if (config.window.width <= 0 || config.window.height <= 0) { + E2D_LOG_ERROR("macOS: Invalid window dimensions"); + return false; } + return true; + } - int getRecommendedWidth() const override { return 1920; } - int getRecommendedHeight() const override { return 1080; } - bool isResolutionSupported(int width, int height) const override { - return width >= 320 && height >= 240; - } + int getRecommendedWidth() const override { return 1920; } + int getRecommendedHeight() const override { return 1080; } + bool isResolutionSupported(int width, int height) const override { + return width >= 320 && height >= 240; + } private: - PlatformCapabilities caps_; + PlatformCapabilities caps_; }; #ifdef __SWITCH__ class SwitchPlatformConfig : public PlatformConfig { public: - SwitchPlatformConfig() { - caps_.supportsWindowed = false; - caps_.supportsFullscreen = true; - caps_.supportsBorderless = false; - caps_.supportsCursor = false; - caps_.supportsCursorHide = false; - caps_.supportsDPIAwareness = false; - caps_.supportsVSync = true; - caps_.supportsMultiMonitor = false; - caps_.supportsClipboard = false; - caps_.supportsGamepad = true; - caps_.supportsTouch = true; - caps_.supportsKeyboard = false; - caps_.supportsMouse = false; - caps_.supportsResize = false; - caps_.supportsHighDPI = false; - caps_.maxTextureSize = 8192; - caps_.preferredScreenWidth = 1920; - caps_.preferredScreenHeight = 1080; - caps_.defaultDPI = 96.0f; - } + SwitchPlatformConfig() { + caps_.supportsWindowed = false; + caps_.supportsFullscreen = true; + caps_.supportsBorderless = false; + caps_.supportsCursor = false; + caps_.supportsCursorHide = false; + caps_.supportsDPIAwareness = false; + caps_.supportsVSync = true; + caps_.supportsMultiMonitor = false; + caps_.supportsClipboard = false; + caps_.supportsGamepad = true; + caps_.supportsTouch = true; + caps_.supportsKeyboard = false; + caps_.supportsMouse = false; + caps_.supportsResize = false; + caps_.supportsHighDPI = false; + caps_.maxTextureSize = 8192; + caps_.preferredScreenWidth = 1920; + caps_.preferredScreenHeight = 1080; + caps_.defaultDPI = 96.0f; + } - PlatformType platformType() const override { return PlatformType::Switch; } - const char* platformName() const override { return "Nintendo Switch"; } - const PlatformCapabilities& capabilities() const override { return caps_; } + PlatformType platformType() const override { return PlatformType::Switch; } + const char *platformName() const override { return "Nintendo Switch"; } + const PlatformCapabilities &capabilities() const override { return caps_; } - void applyConstraints(AppConfig& config) const override { - config.window.width = 1920; - config.window.height = 1080; - config.window.mode = WindowMode::Fullscreen; - config.window.resizable = false; - config.window.borderless = false; - config.window.highDPI = false; - config.render.vsync = true; - config.render.targetFPS = 60; - config.input.enableVibration = true; - config.input.maxGamepads = 2; - } + void applyConstraints(AppConfig &config) const override { + config.window.width = 1920; + config.window.height = 1080; + config.window.mode = WindowMode::Fullscreen; + config.window.resizable = false; + config.window.borderless = false; + config.window.highDPI = false; + config.render.vsync = true; + config.render.targetFPS = 60; + config.input.enableVibration = true; + config.input.maxGamepads = 2; + } - void applyDefaults(AppConfig& config) const override { - config.window.width = 1920; - config.window.height = 1080; - config.window.mode = WindowMode::Fullscreen; - config.window.resizable = false; - config.render.vsync = true; - config.render.targetFPS = 60; - config.input.enableVibration = true; - } + void applyDefaults(AppConfig &config) const override { + config.window.width = 1920; + config.window.height = 1080; + config.window.mode = WindowMode::Fullscreen; + config.window.resizable = false; + config.render.vsync = true; + config.render.targetFPS = 60; + config.input.enableVibration = true; + } - bool validateConfig(AppConfig& config) const override { - if (config.window.mode != WindowMode::Fullscreen) { - E2D_LOG_WARN("Switch: Only fullscreen mode is supported"); - config.window.mode = WindowMode::Fullscreen; - } - return true; + bool validateConfig(AppConfig &config) const override { + if (config.window.mode != WindowMode::Fullscreen) { + E2D_LOG_WARN("Switch: Only fullscreen mode is supported"); + config.window.mode = WindowMode::Fullscreen; } + return true; + } - int getRecommendedWidth() const override { return 1920; } - int getRecommendedHeight() const override { return 1080; } - bool isResolutionSupported(int width, int height) const override { - return (width == 1920 && height == 1080) || - (width == 1280 && height == 720); - } + int getRecommendedWidth() const override { return 1920; } + int getRecommendedHeight() const override { return 1080; } + bool isResolutionSupported(int width, int height) const override { + return (width == 1920 && height == 1080) || + (width == 1280 && height == 720); + } private: - PlatformCapabilities caps_; + PlatformCapabilities caps_; }; #else class SwitchPlatformConfig : public PlatformConfig { public: - SwitchPlatformConfig() { - caps_.supportsWindowed = false; - caps_.supportsFullscreen = true; - caps_.supportsBorderless = false; - caps_.supportsCursor = false; - caps_.supportsCursorHide = false; - caps_.supportsDPIAwareness = false; - caps_.supportsVSync = true; - caps_.supportsMultiMonitor = false; - caps_.supportsClipboard = false; - caps_.supportsGamepad = true; - caps_.supportsTouch = true; - caps_.supportsKeyboard = false; - caps_.supportsMouse = false; - caps_.supportsResize = false; - caps_.supportsHighDPI = false; - caps_.maxTextureSize = 8192; - caps_.preferredScreenWidth = 1920; - caps_.preferredScreenHeight = 1080; - caps_.defaultDPI = 96.0f; - } + SwitchPlatformConfig() { + caps_.supportsWindowed = false; + caps_.supportsFullscreen = true; + caps_.supportsBorderless = false; + caps_.supportsCursor = false; + caps_.supportsCursorHide = false; + caps_.supportsDPIAwareness = false; + caps_.supportsVSync = true; + caps_.supportsMultiMonitor = false; + caps_.supportsClipboard = false; + caps_.supportsGamepad = true; + caps_.supportsTouch = true; + caps_.supportsKeyboard = false; + caps_.supportsMouse = false; + caps_.supportsResize = false; + caps_.supportsHighDPI = false; + caps_.maxTextureSize = 8192; + caps_.preferredScreenWidth = 1920; + caps_.preferredScreenHeight = 1080; + caps_.defaultDPI = 96.0f; + } - PlatformType platformType() const override { return PlatformType::Switch; } - const char* platformName() const override { return "Nintendo Switch"; } - const PlatformCapabilities& capabilities() const override { return caps_; } + PlatformType platformType() const override { return PlatformType::Switch; } + const char *platformName() const override { return "Nintendo Switch"; } + const PlatformCapabilities &capabilities() const override { return caps_; } - void applyConstraints(AppConfig& config) const override { - config.window.width = 1920; - config.window.height = 1080; - config.window.mode = WindowMode::Fullscreen; - config.window.resizable = false; - config.window.borderless = false; - config.window.highDPI = false; - config.render.vsync = true; - config.render.targetFPS = 60; - config.input.enableVibration = true; - config.input.maxGamepads = 2; - } + void applyConstraints(AppConfig &config) const override { + config.window.width = 1920; + config.window.height = 1080; + config.window.mode = WindowMode::Fullscreen; + config.window.resizable = false; + config.window.borderless = false; + config.window.highDPI = false; + config.render.vsync = true; + config.render.targetFPS = 60; + config.input.enableVibration = true; + config.input.maxGamepads = 2; + } - void applyDefaults(AppConfig& config) const override { - config.window.width = 1920; - config.window.height = 1080; - config.window.mode = WindowMode::Fullscreen; - config.window.resizable = false; - config.render.vsync = true; - config.render.targetFPS = 60; - config.input.enableVibration = true; - } + void applyDefaults(AppConfig &config) const override { + config.window.width = 1920; + config.window.height = 1080; + config.window.mode = WindowMode::Fullscreen; + config.window.resizable = false; + config.render.vsync = true; + config.render.targetFPS = 60; + config.input.enableVibration = true; + } - bool validateConfig(AppConfig& config) const override { - if (config.window.mode != WindowMode::Fullscreen) { - E2D_LOG_WARN("Switch: Only fullscreen mode is supported"); - } - return true; + bool validateConfig(AppConfig &config) const override { + if (config.window.mode != WindowMode::Fullscreen) { + E2D_LOG_WARN("Switch: Only fullscreen mode is supported"); } + return true; + } - int getRecommendedWidth() const override { return 1920; } - int getRecommendedHeight() const override { return 1080; } - bool isResolutionSupported(int width, int height) const override { - return (width == 1920 && height == 1080) || - (width == 1280 && height == 720); - } + int getRecommendedWidth() const override { return 1920; } + int getRecommendedHeight() const override { return 1080; } + bool isResolutionSupported(int width, int height) const override { + return (width == 1920 && height == 1080) || + (width == 1280 && height == 720); + } private: - PlatformCapabilities caps_; + PlatformCapabilities caps_; }; #endif -} +} // namespace /** * @brief 创建平台配置实例 @@ -338,37 +346,37 @@ private: * @return 平台配置的智能指针 */ UniquePtr createPlatformConfig(PlatformType type) { - if (type == PlatformType::Auto) { + if (type == PlatformType::Auto) { #ifdef _WIN32 - type = PlatformType::Windows; + type = PlatformType::Windows; #elif defined(__SWITCH__) - type = PlatformType::Switch; + type = PlatformType::Switch; #elif defined(__linux__) - type = PlatformType::Linux; + type = PlatformType::Linux; #elif defined(__APPLE__) - type = PlatformType::macOS; + type = PlatformType::macOS; #else - type = PlatformType::Windows; + type = PlatformType::Windows; #endif - } + } - switch (type) { - case PlatformType::Windows: - E2D_LOG_INFO("Creating Windows platform config"); - return makeUnique(); - case PlatformType::Switch: - E2D_LOG_INFO("Creating Nintendo Switch platform config"); - return makeUnique(); - case PlatformType::Linux: - E2D_LOG_INFO("Creating Linux platform config"); - return makeUnique(); - case PlatformType::macOS: - E2D_LOG_INFO("Creating macOS platform config"); - return makeUnique(); - default: - E2D_LOG_WARN("Unknown platform type, defaulting to Windows"); - return makeUnique(); - } + switch (type) { + case PlatformType::Windows: + E2D_LOG_INFO("Creating Windows platform config"); + return makeUnique(); + case PlatformType::Switch: + E2D_LOG_INFO("Creating Nintendo Switch platform config"); + return makeUnique(); + case PlatformType::Linux: + E2D_LOG_INFO("Creating Linux platform config"); + return makeUnique(); + case PlatformType::macOS: + E2D_LOG_INFO("Creating macOS platform config"); + return makeUnique(); + default: + E2D_LOG_WARN("Unknown platform type, defaulting to Windows"); + return makeUnique(); + } } /** @@ -377,15 +385,21 @@ UniquePtr createPlatformConfig(PlatformType type) { * @param type 平台类型枚举值 * @return 平台名称字符串 */ -const char* getPlatformTypeName(PlatformType type) { - switch (type) { - case PlatformType::Auto: return "Auto"; - case PlatformType::Windows: return "Windows"; - case PlatformType::Switch: return "Switch"; - case PlatformType::Linux: return "Linux"; - case PlatformType::macOS: return "macOS"; - default: return "Unknown"; - } +const char *getPlatformTypeName(PlatformType type) { + switch (type) { + case PlatformType::Auto: + return "Auto"; + case PlatformType::Windows: + return "Windows"; + case PlatformType::Switch: + return "Switch"; + case PlatformType::Linux: + return "Linux"; + case PlatformType::macOS: + return "macOS"; + default: + return "Unknown"; + } } -} +} // namespace extra2d diff --git a/Extra2D/src/platform/backends/sdl2/sdl2_backend.cpp b/Extra2D/src/platform/backends/sdl2/sdl2_backend.cpp index 770c2c1..3e82a54 100644 --- a/Extra2D/src/platform/backends/sdl2/sdl2_backend.cpp +++ b/Extra2D/src/platform/backends/sdl2/sdl2_backend.cpp @@ -4,6 +4,25 @@ namespace extra2d { -E2D_REG_BACKEND(sdl2, SDL2Window, SDL2Input) +namespace { + static bool s_sdl2BackendRegistered = false; +} + +void initSDL2Backend() { + if (s_sdl2BackendRegistered) { + return; + } + s_sdl2BackendRegistered = true; + + BackendFactory::reg( + "sdl2", + []() -> UniquePtr { + return makeUnique(); + }, + []() -> UniquePtr { + return makeUnique(); + } + ); +} } // namespace extra2d diff --git a/Extra2D/src/platform/backends/sdl2/sdl2_window.cpp b/Extra2D/src/platform/backends/sdl2/sdl2_window.cpp index 168817c..7fae4f3 100644 --- a/Extra2D/src/platform/backends/sdl2/sdl2_window.cpp +++ b/Extra2D/src/platform/backends/sdl2/sdl2_window.cpp @@ -1,6 +1,7 @@ #include "sdl2_window.h" #include "sdl2_input.h" #include +#include namespace extra2d { @@ -73,6 +74,16 @@ bool SDL2Window::create(const WindowConfigData& cfg) { return false; } + if (!gladLoadGLES2Loader((GLADloadproc)SDL_GL_GetProcAddress)) { + E2D_LOG_ERROR("Failed to initialize GLAD"); + SDL_GL_DeleteContext(glContext_); + glContext_ = nullptr; + SDL_DestroyWindow(sdlWindow_); + sdlWindow_ = nullptr; + deinitSDL(); + return false; + } + SDL_GL_SetSwapInterval(cfg.vsync ? 1 : 0); SDL_GetWindowSize(sdlWindow_, &width_, &height_); diff --git a/examples/basic/main.cpp b/examples/basic/main.cpp new file mode 100644 index 0000000..2cd8471 --- /dev/null +++ b/examples/basic/main.cpp @@ -0,0 +1,59 @@ +/** + * @file main.cpp + * @brief Extra2D 基础示例程序 + * + * 演示如何使用 Extra2D 引擎创建一个简单的窗口应用程序。 + */ + +#include +#include + +using namespace extra2d; + +/** + * @brief 主函数 + * + * 初始化应用程序,创建场景,运行主循环。 + */ +int main(int argc, char* argv[]) { + (void)argc; + (void)argv; + + std::cout << "Extra2D Demo - Starting..." << std::endl; + + AppConfig config = AppConfig::createDefault(); + config.appName = "Extra2D Demo"; + config.appVersion = "1.0.0"; + config.window.title = "Extra2D Demo"; + config.window.width = 800; + config.window.height = 600; + config.window.mode = WindowMode::Windowed; + config.window.resizable = true; + config.window.vsync = true; + config.render.targetFPS = 60; + + Application& app = Application::get(); + + if (!app.init(config)) { + std::cerr << "Failed to initialize application!" << std::endl; + return -1; + } + + std::cout << "Application initialized successfully!" << std::endl; + std::cout << "Window: " << app.window().width() << "x" << app.window().height() << std::endl; + std::cout << "Running main loop. Press ESC or close window to exit." << std::endl; + + auto scene = Scene::create(); + scene->setBackgroundColor(Colors::SkyBlue); + scene->setViewportSize(static_cast(config.window.width), + static_cast(config.window.height)); + app.enterScene(scene); + + app.run(); + + std::cout << "Shutting down..." << std::endl; + app.shutdown(); + + std::cout << "Goodbye!" << std::endl; + return 0; +} diff --git a/xmake.lua b/xmake.lua index 05ef2ef..da4ad34 100644 --- a/xmake.lua +++ b/xmake.lua @@ -95,3 +95,23 @@ includes("xmake/engine.lua") -- 定义引擎库 define_extra2d_engine() +-- ============================================== +-- 示例程序 +-- ============================================== + +-- 基础示例 +target("demo_basic") + set_kind("binary") + set_default(false) + + add_deps("extra2d") + add_files("examples/basic/main.cpp") + + -- 平台配置 + local target_plat = get_config("plat") or os.host() + if target_plat == "mingw" then + add_packages("glm", "nlohmann_json", "libsdl2") + add_syslinks("opengl32", "glu32", "winmm", "imm32", "version", "setupapi") + end +target_end() +