From 2767d64bf8d21c7127a64d8d0f13f7e2d4b76375 Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Sat, 14 Feb 2026 17:49:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor(core):=20=E7=A7=BB=E9=99=A4=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E7=BC=96=E7=A0=81=E8=BD=AC=E6=8D=A2=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E5=B9=B6=E6=B8=85=E7=90=86=E5=A4=B4=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=8C=85=E5=90=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除不再使用的字符串编码转换工具文件 string.h,并清理相关头文件包含关系 调整部分头文件中的代码格式,优化枚举和结构体定义 --- Extra2D/include/extra2d/app/application.h | 15 +- Extra2D/include/extra2d/core/string.h | 209 ---------------------- Extra2D/include/extra2d/extra2d.h | 31 ++-- Extra2D/include/extra2d/platform/window.h | 207 ++++++++++----------- 4 files changed, 125 insertions(+), 337 deletions(-) delete mode 100644 Extra2D/include/extra2d/core/string.h diff --git a/Extra2D/include/extra2d/app/application.h b/Extra2D/include/extra2d/app/application.h index 24ebc71..09c4ab7 100644 --- a/Extra2D/include/extra2d/app/application.h +++ b/Extra2D/include/extra2d/app/application.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include namespace extra2d { @@ -22,11 +20,7 @@ class Camera; // Application 配置 // ============================================================================ -enum class PlatformType { - Auto = 0, - PC, - Switch -}; +enum class PlatformType { Auto = 0, PC, Switch }; struct AppConfig { std::string title = "Easy2D Application"; @@ -40,8 +34,8 @@ struct AppConfig { int msaaSamples = 0; PlatformType platform = PlatformType::Auto; // 窗口高级配置 - bool enableCursors = true; // 是否启用光标 - bool enableDpiScale = false; // 是否启用DPI缩放 + bool enableCursors = true; // 是否启用光标 + bool enableDpiScale = false; // 是否启用DPI缩放 }; // ============================================================================ @@ -90,7 +84,8 @@ public: // 便捷方法 // ------------------------------------------------------------------------ void enterScene(Ptr scene); - void enterScene(Ptr scene, Ptr transitionScene); + void enterScene(Ptr scene, + Ptr transitionScene); float deltaTime() const { return deltaTime_; } float totalTime() const { return totalTime_; } diff --git a/Extra2D/include/extra2d/core/string.h b/Extra2D/include/extra2d/core/string.h deleted file mode 100644 index ddd51f5..0000000 --- a/Extra2D/include/extra2d/core/string.h +++ /dev/null @@ -1,209 +0,0 @@ -#pragma once - -#include - -namespace extra2d { - -// ============================================================================ -// 字符串编码转换工具函数 -// 统一使用 std::string (UTF-8) 作为项目标准字符串类型 -// ============================================================================ - -// UTF-8 ↔ UTF-16 转换 -std::u16string utf8ToUtf16(const std::string& utf8); -std::string utf16ToUtf8(const std::u16string& utf16); - -// UTF-8 ↔ UTF-32 转换 -std::u32string utf8ToUtf32(const std::string& utf8); -std::string utf32ToUtf8(const std::u32string& utf32); - -// UTF-8 ↔ Wide String 转换 -std::wstring utf8ToWide(const std::string& utf8); -std::string wideToUtf8(const std::wstring& wide); - -// UTF-8 ↔ GBK/GB2312 转换(Windows 中文系统常用) -std::string utf8ToGbk(const std::string& utf8); -std::string gbkToUtf8(const std::string& gbk); - -// ============================================================================ -// 内联实现 -// ============================================================================ - -inline std::u16string utf8ToUtf16(const std::string& utf8) { - if (utf8.empty()) return std::u16string(); - - // UTF-8 → UTF-32 → UTF-16 (with surrogate pairs) - std::u32string u32 = utf8ToUtf32(utf8); - std::u16string result; - result.reserve(u32.size()); - - for (char32_t ch : u32) { - if (ch <= 0xFFFF) { - result.push_back(static_cast(ch)); - } else if (ch <= 0x10FFFF) { - // Surrogate pair - ch -= 0x10000; - result.push_back(static_cast(0xD800 | (ch >> 10))); - result.push_back(static_cast(0xDC00 | (ch & 0x3FF))); - } - } - - return result; -} - -inline std::string utf16ToUtf8(const std::u16string& utf16) { - if (utf16.empty()) return std::string(); - - // UTF-16 → UTF-32 → UTF-8 - std::u32string u32; - u32.reserve(utf16.size()); - - for (size_t i = 0; i < utf16.size(); ++i) { - char16_t cu = utf16[i]; - char32_t ch; - if (cu >= 0xD800 && cu <= 0xDBFF && i + 1 < utf16.size()) { - // High surrogate - char16_t cl = utf16[i + 1]; - if (cl >= 0xDC00 && cl <= 0xDFFF) { - ch = 0x10000 + ((static_cast(cu - 0xD800) << 10) | - (cl - 0xDC00)); - ++i; - } else { - ch = cu; // Invalid, pass through - } - } else { - ch = cu; - } - u32.push_back(ch); - } - - return utf32ToUtf8(u32); -} - -inline std::u32string utf8ToUtf32(const std::string& utf8) { - std::u32string result; - result.reserve(utf8.size()); - - const char* ptr = utf8.c_str(); - const char* end = ptr + utf8.size(); - - while (ptr < end) { - char32_t ch = 0; - unsigned char byte = static_cast(*ptr); - - if ((byte & 0x80) == 0) { - // 1-byte sequence - ch = byte; - ptr += 1; - } else if ((byte & 0xE0) == 0xC0) { - // 2-byte sequence - ch = (byte & 0x1F) << 6; - ch |= (static_cast(ptr[1]) & 0x3F); - ptr += 2; - } else if ((byte & 0xF0) == 0xE0) { - // 3-byte sequence - ch = (byte & 0x0F) << 12; - ch |= (static_cast(ptr[1]) & 0x3F) << 6; - ch |= (static_cast(ptr[2]) & 0x3F); - ptr += 3; - } else if ((byte & 0xF8) == 0xF0) { - // 4-byte sequence - ch = (byte & 0x07) << 18; - ch |= (static_cast(ptr[1]) & 0x3F) << 12; - ch |= (static_cast(ptr[2]) & 0x3F) << 6; - ch |= (static_cast(ptr[3]) & 0x3F); - ptr += 4; - } else { - // Invalid UTF-8, skip - ptr += 1; - continue; - } - - result.push_back(ch); - } - - return result; -} - -inline std::string utf32ToUtf8(const std::u32string& utf32) { - std::string result; - - for (char32_t ch : utf32) { - if (ch <= 0x7F) { - // 1-byte - result.push_back(static_cast(ch)); - } else if (ch <= 0x7FF) { - // 2-byte - result.push_back(static_cast(0xC0 | ((ch >> 6) & 0x1F))); - result.push_back(static_cast(0x80 | (ch & 0x3F))); - } else if (ch <= 0xFFFF) { - // 3-byte - result.push_back(static_cast(0xE0 | ((ch >> 12) & 0x0F))); - result.push_back(static_cast(0x80 | ((ch >> 6) & 0x3F))); - result.push_back(static_cast(0x80 | (ch & 0x3F))); - } else if (ch <= 0x10FFFF) { - // 4-byte - result.push_back(static_cast(0xF0 | ((ch >> 18) & 0x07))); - result.push_back(static_cast(0x80 | ((ch >> 12) & 0x3F))); - result.push_back(static_cast(0x80 | ((ch >> 6) & 0x3F))); - result.push_back(static_cast(0x80 | (ch & 0x3F))); - } - } - - return result; -} - -inline std::wstring utf8ToWide(const std::string& utf8) { - if (utf8.empty()) return std::wstring(); - - if constexpr (sizeof(wchar_t) == 4) { - // wchar_t is 32-bit (Linux/Switch): same as UTF-32 - std::u32string u32 = utf8ToUtf32(utf8); - return std::wstring(u32.begin(), u32.end()); - } else { - // wchar_t is 16-bit (Windows): same as UTF-16 - std::u16string u16 = utf8ToUtf16(utf8); - return std::wstring(u16.begin(), u16.end()); - } -} - -inline std::string wideToUtf8(const std::wstring& wide) { - if (wide.empty()) return std::string(); - - if constexpr (sizeof(wchar_t) == 4) { - std::u32string u32(wide.begin(), wide.end()); - return utf32ToUtf8(u32); - } else { - std::u16string u16(wide.begin(), wide.end()); - return utf16ToUtf8(u16); - } -} - -// GBK/GB2312 转换(Windows 平台实现) -// 注意:Windows 实现在 .cpp 文件中,避免头文件包含 windows.h 导致冲突 -#ifdef _WIN32 -// 前向声明,实现在 .cpp 文件中 -std::string utf8ToGbkImpl(const std::string& utf8); -std::string gbkToUtf8Impl(const std::string& gbk); - -inline std::string utf8ToGbk(const std::string& utf8) { - return utf8ToGbkImpl(utf8); -} - -inline std::string gbkToUtf8(const std::string& gbk) { - return gbkToUtf8Impl(gbk); -} -#else -// 非 Windows 平台,GBK 转换使用 iconv 或返回原字符串 -inline std::string utf8ToGbk(const std::string& utf8) { - // TODO: 使用 iconv 实现 - return utf8; -} - -inline std::string gbkToUtf8(const std::string& gbk) { - // TODO: 使用 iconv 实现 - return gbk; -} -#endif - -} // namespace extra2d diff --git a/Extra2D/include/extra2d/extra2d.h b/Extra2D/include/extra2d/extra2d.h index 32210b1..d0eb408 100644 --- a/Extra2D/include/extra2d/extra2d.h +++ b/Extra2D/include/extra2d/extra2d.h @@ -4,21 +4,20 @@ // 包含所有公共 API // Core -#include -#include #include #include +#include // Platform -#include #include +#include // Graphics -#include -#include -#include #include +#include +#include #include +#include #include #include @@ -26,20 +25,20 @@ // Scene #include #include -#include -#include #include -#include -#include -#include -#include -#include +#include +#include #include +#include +#include +#include +#include +#include // Event #include -#include #include +#include #include // Audio @@ -50,10 +49,10 @@ #include // Utils -#include -#include #include +#include #include +#include // Application #include diff --git a/Extra2D/include/extra2d/platform/window.h b/Extra2D/include/extra2d/platform/window.h index 234d5e7..e6be53f 100644 --- a/Extra2D/include/extra2d/platform/window.h +++ b/Extra2D/include/extra2d/platform/window.h @@ -1,8 +1,6 @@ #pragma once - -#include -#include #include +#include #include #include @@ -17,32 +15,33 @@ class Input; // 窗口配置 // ============================================================================ struct WindowConfig { - std::string title = "Extra2D Application"; - int width = 1280; - int height = 720; - bool fullscreen = true; - bool resizable = false; - bool vsync = true; - int msaaSamples = 0; - bool centerWindow = true; - bool enableCursors = true; - bool enableDpiScale = true; - bool fullscreenDesktop = true; // true: SDL_WINDOW_FULLSCREEN_DESKTOP, false: SDL_WINDOW_FULLSCREEN + std::string title = "Extra2D Application"; + int width = 1280; + int height = 720; + bool fullscreen = true; + bool resizable = false; + bool vsync = true; + int msaaSamples = 0; + bool centerWindow = true; + bool enableCursors = true; + bool enableDpiScale = true; + bool fullscreenDesktop = + true; // true: SDL_WINDOW_FULLSCREEN_DESKTOP, false: SDL_WINDOW_FULLSCREEN }; // ============================================================================ // 鼠标光标形状枚举 // ============================================================================ enum class CursorShape { - Arrow, - IBeam, - Crosshair, - Hand, - HResize, - VResize, - ResizeAll, - ResizeNWSE, - ResizeNESW + Arrow, + IBeam, + Crosshair, + Hand, + HResize, + VResize, + ResizeAll, + ResizeNWSE, + ResizeNESW }; // ============================================================================ @@ -51,103 +50,107 @@ enum class CursorShape { // ============================================================================ class Window { public: - Window(); - ~Window(); + Window(); + ~Window(); - // 创建窗口 - bool create(const WindowConfig& config); - void destroy(); + // 创建窗口 + bool create(const WindowConfig &config); + void destroy(); - // 窗口操作 - void pollEvents(); - void swapBuffers(); - bool shouldClose() const; - void setShouldClose(bool close); + // 窗口操作 + void pollEvents(); + void swapBuffers(); + bool shouldClose() const; + void setShouldClose(bool close); - // 窗口属性 - void setTitle(const std::string& title); - void setSize(int width, int height); - void setPosition(int x, int y); - void setFullscreen(bool fullscreen); - void setVSync(bool enabled); - void setResizable(bool resizable); + // 窗口属性 + void setTitle(const std::string &title); + void setSize(int width, int height); + void setPosition(int x, int y); + void setFullscreen(bool fullscreen); + void setVSync(bool enabled); + void setResizable(bool resizable); - // 获取窗口属性 - int getWidth() const { return width_; } - int getHeight() const { return height_; } - Size getSize() const { return Size(static_cast(width_), static_cast(height_)); } - Vec2 getPosition() const; - bool isFullscreen() const { return fullscreen_; } - bool isVSync() const { return vsync_; } + // 获取窗口属性 + int getWidth() const { return width_; } + int getHeight() const { return height_; } + Size getSize() const { + return Size(static_cast(width_), static_cast(height_)); + } + Vec2 getPosition() const; + bool isFullscreen() const { return fullscreen_; } + bool isVSync() const { return vsync_; } - // DPI 缩放 (PC 端自动检测,Switch 固定 1.0) - float getContentScaleX() const; - float getContentScaleY() const; - Vec2 getContentScale() const; + // DPI 缩放 (PC 端自动检测,Switch 固定 1.0) + float getContentScaleX() const; + float getContentScaleY() const; + Vec2 getContentScale() const; - // 窗口状态 - bool isFocused() const { return focused_; } - bool isMinimized() const; - bool isMaximized() const; + // 窗口状态 + bool isFocused() const { return focused_; } + bool isMinimized() const; + bool isMaximized() const; - // 获取 SDL2 窗口和 GL 上下文 - SDL_Window* getSDLWindow() const { return sdlWindow_; } - SDL_GLContext getGLContext() const { return glContext_; } + // 获取 SDL2 窗口和 GL 上下文 + SDL_Window *getSDLWindow() const { return sdlWindow_; } + SDL_GLContext getGLContext() const { return glContext_; } - // 设置/获取用户数据 - void setUserData(void* data) { userData_ = data; } - void* getUserData() const { return userData_; } + // 设置/获取用户数据 + void setUserData(void *data) { userData_ = data; } + void *getUserData() const { return userData_; } - // 事件队列 - void setEventQueue(EventQueue* queue) { eventQueue_ = queue; } - EventQueue* getEventQueue() const { return eventQueue_; } + // 事件队列 + void setEventQueue(EventQueue *queue) { eventQueue_ = queue; } + EventQueue *getEventQueue() const { return eventQueue_; } - // 获取输入管理器 - Input* getInput() const { return input_.get(); } + // 获取输入管理器 + Input *getInput() const { return input_.get(); } - // 光标操作 (PC 端有效,Switch 上为空操作) - void setCursor(CursorShape shape); - void resetCursor(); - void setMouseVisible(bool visible); + // 光标操作 (PC 端有效,Switch 上为空操作) + void setCursor(CursorShape shape); + void resetCursor(); + void setMouseVisible(bool visible); - // 窗口回调 - using ResizeCallback = std::function; - using FocusCallback = std::function; - using CloseCallback = std::function; + // 窗口回调 + using ResizeCallback = std::function; + using FocusCallback = std::function; + using CloseCallback = std::function; - void setResizeCallback(ResizeCallback callback) { resizeCallback_ = callback; } - void setFocusCallback(FocusCallback callback) { focusCallback_ = callback; } - void setCloseCallback(CloseCallback callback) { closeCallback_ = callback; } + void setResizeCallback(ResizeCallback callback) { + resizeCallback_ = callback; + } + void setFocusCallback(FocusCallback callback) { focusCallback_ = callback; } + void setCloseCallback(CloseCallback callback) { closeCallback_ = callback; } private: - // SDL2 状态 - SDL_Window* sdlWindow_; - SDL_GLContext glContext_; - SDL_Cursor* sdlCursors_[9]; // 光标缓存 - SDL_Cursor* currentCursor_; + // SDL2 状态 + SDL_Window *sdlWindow_; + SDL_GLContext glContext_; + SDL_Cursor *sdlCursors_[9]; // 光标缓存 + SDL_Cursor *currentCursor_; - int width_; - int height_; - bool vsync_; - bool shouldClose_; - bool fullscreen_; - bool focused_; - float contentScaleX_; - float contentScaleY_; - bool enableDpiScale_; - void* userData_; - EventQueue* eventQueue_; - UniquePtr input_; + int width_; + int height_; + bool vsync_; + bool shouldClose_; + bool fullscreen_; + bool focused_; + float contentScaleX_; + float contentScaleY_; + bool enableDpiScale_; + void *userData_; + EventQueue *eventQueue_; + UniquePtr input_; - ResizeCallback resizeCallback_; - FocusCallback focusCallback_; - CloseCallback closeCallback_; + ResizeCallback resizeCallback_; + FocusCallback focusCallback_; + CloseCallback closeCallback_; - bool initSDL(const WindowConfig& config); - void deinitSDL(); - void initCursors(); - void deinitCursors(); - void updateContentScale(); + bool initSDL(const WindowConfig &config); + void deinitSDL(); + void initCursors(); + void deinitCursors(); + void updateContentScale(); }; } // namespace extra2d