refactor(core): 移除字符串编码转换工具并清理头文件包含
移除不再使用的字符串编码转换工具文件 string.h,并清理相关头文件包含关系 调整部分头文件中的代码格式,优化枚举和结构体定义
This commit is contained in:
parent
b949d1a3da
commit
2767d64bf8
|
|
@ -1,10 +1,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <extra2d/core/string.h>
|
|
||||||
#include <extra2d/core/types.h>
|
#include <extra2d/core/types.h>
|
||||||
#include <extra2d/graphics/render_backend.h>
|
#include <extra2d/graphics/render_backend.h>
|
||||||
#include <extra2d/platform/window.h>
|
#include <extra2d/platform/window.h>
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
|
|
@ -22,11 +20,7 @@ class Camera;
|
||||||
// Application 配置
|
// Application 配置
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
enum class PlatformType {
|
enum class PlatformType { Auto = 0, PC, Switch };
|
||||||
Auto = 0,
|
|
||||||
PC,
|
|
||||||
Switch
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AppConfig {
|
struct AppConfig {
|
||||||
std::string title = "Easy2D Application";
|
std::string title = "Easy2D Application";
|
||||||
|
|
@ -90,7 +84,8 @@ public:
|
||||||
// 便捷方法
|
// 便捷方法
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
void enterScene(Ptr<class Scene> scene);
|
void enterScene(Ptr<class Scene> scene);
|
||||||
void enterScene(Ptr<class Scene> scene, Ptr<class TransitionScene> transitionScene);
|
void enterScene(Ptr<class Scene> scene,
|
||||||
|
Ptr<class TransitionScene> transitionScene);
|
||||||
|
|
||||||
float deltaTime() const { return deltaTime_; }
|
float deltaTime() const { return deltaTime_; }
|
||||||
float totalTime() const { return totalTime_; }
|
float totalTime() const { return totalTime_; }
|
||||||
|
|
|
||||||
|
|
@ -1,209 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
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<char16_t>(ch));
|
|
||||||
} else if (ch <= 0x10FFFF) {
|
|
||||||
// Surrogate pair
|
|
||||||
ch -= 0x10000;
|
|
||||||
result.push_back(static_cast<char16_t>(0xD800 | (ch >> 10)));
|
|
||||||
result.push_back(static_cast<char16_t>(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<char32_t>(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<unsigned char>(*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<unsigned char>(ptr[1]) & 0x3F);
|
|
||||||
ptr += 2;
|
|
||||||
} else if ((byte & 0xF0) == 0xE0) {
|
|
||||||
// 3-byte sequence
|
|
||||||
ch = (byte & 0x0F) << 12;
|
|
||||||
ch |= (static_cast<unsigned char>(ptr[1]) & 0x3F) << 6;
|
|
||||||
ch |= (static_cast<unsigned char>(ptr[2]) & 0x3F);
|
|
||||||
ptr += 3;
|
|
||||||
} else if ((byte & 0xF8) == 0xF0) {
|
|
||||||
// 4-byte sequence
|
|
||||||
ch = (byte & 0x07) << 18;
|
|
||||||
ch |= (static_cast<unsigned char>(ptr[1]) & 0x3F) << 12;
|
|
||||||
ch |= (static_cast<unsigned char>(ptr[2]) & 0x3F) << 6;
|
|
||||||
ch |= (static_cast<unsigned char>(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<char>(ch));
|
|
||||||
} else if (ch <= 0x7FF) {
|
|
||||||
// 2-byte
|
|
||||||
result.push_back(static_cast<char>(0xC0 | ((ch >> 6) & 0x1F)));
|
|
||||||
result.push_back(static_cast<char>(0x80 | (ch & 0x3F)));
|
|
||||||
} else if (ch <= 0xFFFF) {
|
|
||||||
// 3-byte
|
|
||||||
result.push_back(static_cast<char>(0xE0 | ((ch >> 12) & 0x0F)));
|
|
||||||
result.push_back(static_cast<char>(0x80 | ((ch >> 6) & 0x3F)));
|
|
||||||
result.push_back(static_cast<char>(0x80 | (ch & 0x3F)));
|
|
||||||
} else if (ch <= 0x10FFFF) {
|
|
||||||
// 4-byte
|
|
||||||
result.push_back(static_cast<char>(0xF0 | ((ch >> 18) & 0x07)));
|
|
||||||
result.push_back(static_cast<char>(0x80 | ((ch >> 12) & 0x3F)));
|
|
||||||
result.push_back(static_cast<char>(0x80 | ((ch >> 6) & 0x3F)));
|
|
||||||
result.push_back(static_cast<char>(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
|
|
||||||
|
|
@ -4,21 +4,20 @@
|
||||||
// 包含所有公共 API
|
// 包含所有公共 API
|
||||||
|
|
||||||
// Core
|
// Core
|
||||||
#include <extra2d/core/types.h>
|
|
||||||
#include <extra2d/core/string.h>
|
|
||||||
#include <extra2d/core/color.h>
|
#include <extra2d/core/color.h>
|
||||||
#include <extra2d/core/math_types.h>
|
#include <extra2d/core/math_types.h>
|
||||||
|
#include <extra2d/core/types.h>
|
||||||
|
|
||||||
// Platform
|
// Platform
|
||||||
#include <extra2d/platform/window.h>
|
|
||||||
#include <extra2d/platform/input.h>
|
#include <extra2d/platform/input.h>
|
||||||
|
#include <extra2d/platform/window.h>
|
||||||
|
|
||||||
// Graphics
|
// Graphics
|
||||||
#include <extra2d/graphics/render_backend.h>
|
|
||||||
#include <extra2d/graphics/texture.h>
|
|
||||||
#include <extra2d/graphics/font.h>
|
|
||||||
#include <extra2d/graphics/camera.h>
|
#include <extra2d/graphics/camera.h>
|
||||||
|
#include <extra2d/graphics/font.h>
|
||||||
|
#include <extra2d/graphics/render_backend.h>
|
||||||
#include <extra2d/graphics/shader_system.h>
|
#include <extra2d/graphics/shader_system.h>
|
||||||
|
#include <extra2d/graphics/texture.h>
|
||||||
|
|
||||||
#include <extra2d/graphics/render_target.h>
|
#include <extra2d/graphics/render_target.h>
|
||||||
#include <extra2d/graphics/vram_manager.h>
|
#include <extra2d/graphics/vram_manager.h>
|
||||||
|
|
@ -26,20 +25,20 @@
|
||||||
// Scene
|
// Scene
|
||||||
#include <extra2d/scene/node.h>
|
#include <extra2d/scene/node.h>
|
||||||
#include <extra2d/scene/scene.h>
|
#include <extra2d/scene/scene.h>
|
||||||
#include <extra2d/scene/sprite.h>
|
|
||||||
#include <extra2d/scene/shape_node.h>
|
|
||||||
#include <extra2d/scene/scene_manager.h>
|
#include <extra2d/scene/scene_manager.h>
|
||||||
#include <extra2d/scene/transition_scene.h>
|
#include <extra2d/scene/shape_node.h>
|
||||||
#include <extra2d/scene/transition_fade_scene.h>
|
#include <extra2d/scene/sprite.h>
|
||||||
#include <extra2d/scene/transition_slide_scene.h>
|
|
||||||
#include <extra2d/scene/transition_scale_scene.h>
|
|
||||||
#include <extra2d/scene/transition_flip_scene.h>
|
|
||||||
#include <extra2d/scene/transition_box_scene.h>
|
#include <extra2d/scene/transition_box_scene.h>
|
||||||
|
#include <extra2d/scene/transition_fade_scene.h>
|
||||||
|
#include <extra2d/scene/transition_flip_scene.h>
|
||||||
|
#include <extra2d/scene/transition_scale_scene.h>
|
||||||
|
#include <extra2d/scene/transition_scene.h>
|
||||||
|
#include <extra2d/scene/transition_slide_scene.h>
|
||||||
|
|
||||||
// Event
|
// Event
|
||||||
#include <extra2d/event/event.h>
|
#include <extra2d/event/event.h>
|
||||||
#include <extra2d/event/event_queue.h>
|
|
||||||
#include <extra2d/event/event_dispatcher.h>
|
#include <extra2d/event/event_dispatcher.h>
|
||||||
|
#include <extra2d/event/event_queue.h>
|
||||||
#include <extra2d/event/input_codes.h>
|
#include <extra2d/event/input_codes.h>
|
||||||
|
|
||||||
// Audio
|
// Audio
|
||||||
|
|
@ -50,10 +49,10 @@
|
||||||
#include <extra2d/resource/resource_manager.h>
|
#include <extra2d/resource/resource_manager.h>
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
#include <extra2d/utils/logger.h>
|
|
||||||
#include <extra2d/utils/timer.h>
|
|
||||||
#include <extra2d/utils/data.h>
|
#include <extra2d/utils/data.h>
|
||||||
|
#include <extra2d/utils/logger.h>
|
||||||
#include <extra2d/utils/random.h>
|
#include <extra2d/utils/random.h>
|
||||||
|
#include <extra2d/utils/timer.h>
|
||||||
|
|
||||||
// Application
|
// Application
|
||||||
#include <extra2d/app/application.h>
|
#include <extra2d/app/application.h>
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <extra2d/core/types.h>
|
|
||||||
#include <extra2d/core/string.h>
|
|
||||||
#include <extra2d/core/math_types.h>
|
#include <extra2d/core/math_types.h>
|
||||||
|
#include <extra2d/core/types.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
|
@ -27,7 +25,8 @@ struct WindowConfig {
|
||||||
bool centerWindow = true;
|
bool centerWindow = true;
|
||||||
bool enableCursors = true;
|
bool enableCursors = true;
|
||||||
bool enableDpiScale = true;
|
bool enableDpiScale = true;
|
||||||
bool fullscreenDesktop = true; // true: SDL_WINDOW_FULLSCREEN_DESKTOP, false: SDL_WINDOW_FULLSCREEN
|
bool fullscreenDesktop =
|
||||||
|
true; // true: SDL_WINDOW_FULLSCREEN_DESKTOP, false: SDL_WINDOW_FULLSCREEN
|
||||||
};
|
};
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
@ -75,7 +74,9 @@ public:
|
||||||
// 获取窗口属性
|
// 获取窗口属性
|
||||||
int getWidth() const { return width_; }
|
int getWidth() const { return width_; }
|
||||||
int getHeight() const { return height_; }
|
int getHeight() const { return height_; }
|
||||||
Size getSize() const { return Size(static_cast<float>(width_), static_cast<float>(height_)); }
|
Size getSize() const {
|
||||||
|
return Size(static_cast<float>(width_), static_cast<float>(height_));
|
||||||
|
}
|
||||||
Vec2 getPosition() const;
|
Vec2 getPosition() const;
|
||||||
bool isFullscreen() const { return fullscreen_; }
|
bool isFullscreen() const { return fullscreen_; }
|
||||||
bool isVSync() const { return vsync_; }
|
bool isVSync() const { return vsync_; }
|
||||||
|
|
@ -115,7 +116,9 @@ public:
|
||||||
using FocusCallback = std::function<void(bool focused)>;
|
using FocusCallback = std::function<void(bool focused)>;
|
||||||
using CloseCallback = std::function<void()>;
|
using CloseCallback = std::function<void()>;
|
||||||
|
|
||||||
void setResizeCallback(ResizeCallback callback) { resizeCallback_ = callback; }
|
void setResizeCallback(ResizeCallback callback) {
|
||||||
|
resizeCallback_ = callback;
|
||||||
|
}
|
||||||
void setFocusCallback(FocusCallback callback) { focusCallback_ = callback; }
|
void setFocusCallback(FocusCallback callback) { focusCallback_ = callback; }
|
||||||
void setCloseCallback(CloseCallback callback) { closeCallback_ = callback; }
|
void setCloseCallback(CloseCallback callback) { closeCallback_ = callback; }
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue