From b41e96d87004921b10471918d63601db93806999 Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Fri, 20 Feb 2026 19:02:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(core):=20=E7=BB=9F=E4=B8=80=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E8=A7=84=E8=8C=83=E5=B9=B6=E4=BC=98=E5=8C=96=E6=A0=B8?= =?UTF-8?q?=E5=BF=83=E6=A8=A1=E5=9D=97=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构智能指针、基础类型和核心组件的命名,使用更简洁一致的命名风格: - 将 UniquePtr/SharedPtr 简化为 Unique/Ref - 基础类型使用 i32/u32/f32 等更短名称 - 方法名统一为小写驼峰,如 getServiceInfo() 改为 info() - 模块初始化方法 parallel() 替代 allowParallelInit() 新增核心组件: - 添加 ObjectPool 对象池模板类 - 添加 RingBuffer 环形缓冲区模板类 - 添加编译期类型ID生成机制 优化现有组件: - 事件队列改用 RingBuffer 实现 - 服务注册表改用数组存储提高缓存友好性 - 模块依赖管理算法优化 - 统一时间相关参数为 f32/f64 类型 移除冗余代码,简化接口设计,提升整体代码一致性和性能 --- Extra2D/.trae/documents/asset_service_plan.md | 199 +++++++++ Extra2D/include/extra2d/app/application.h | 20 +- Extra2D/include/extra2d/core/module.h | 4 +- Extra2D/include/extra2d/core/object_pool.h | 64 +++ Extra2D/include/extra2d/core/registry.h | 84 +++- Extra2D/include/extra2d/core/result.h | 398 ++++++++++++++++++ Extra2D/include/extra2d/core/ring_buffer.h | 103 +++++ .../include/extra2d/core/service_interface.h | 16 +- .../include/extra2d/core/service_locator.h | 111 ++--- .../include/extra2d/core/service_registry.h | 22 +- Extra2D/include/extra2d/core/types.h | 57 +-- Extra2D/include/extra2d/event/event.h | 64 +-- .../include/extra2d/event/event_dispatcher.h | 24 +- Extra2D/include/extra2d/event/event_queue.h | 37 +- .../extra2d/platform/glfw/glfw_window.h | 66 +-- .../include/extra2d/platform/window_module.h | 2 +- .../include/extra2d/services/event_service.h | 58 +-- .../include/extra2d/services/logger_service.h | 122 +++--- .../include/extra2d/services/timer_service.h | 36 +- Extra2D/include/extra2d/utils/random.h | 36 +- Extra2D/include/extra2d/utils/timer.h | 42 +- Extra2D/src/app/application.cpp | 32 +- Extra2D/src/core/registry.cpp | 123 +++--- Extra2D/src/core/service_locator.cpp | 74 ++-- Extra2D/src/core/service_registry.cpp | 6 +- Extra2D/src/event/event.cpp | 100 +---- Extra2D/src/event/event_dispatcher.cpp | 90 +--- Extra2D/src/event/event_queue.cpp | 98 +---- Extra2D/src/platform/glfw/glfw_window.cpp | 216 +++++----- Extra2D/src/platform/window_module.cpp | 4 +- Extra2D/src/services/event_service.cpp | 45 +- Extra2D/src/services/logger_service.cpp | 71 ++-- Extra2D/src/services/timer_service.cpp | 35 +- Extra2D/src/utils/random.cpp | 32 +- Extra2D/src/utils/timer.cpp | 111 +---- 35 files changed, 1580 insertions(+), 1022 deletions(-) create mode 100644 Extra2D/.trae/documents/asset_service_plan.md create mode 100644 Extra2D/include/extra2d/core/object_pool.h create mode 100644 Extra2D/include/extra2d/core/result.h create mode 100644 Extra2D/include/extra2d/core/ring_buffer.h diff --git a/Extra2D/.trae/documents/asset_service_plan.md b/Extra2D/.trae/documents/asset_service_plan.md new file mode 100644 index 0000000..93abfd2 --- /dev/null +++ b/Extra2D/.trae/documents/asset_service_plan.md @@ -0,0 +1,199 @@ +# Extra2D 资源服务设计计划 + +## 1. 概述 + +设计一个现代化的资源服务,支持高效的资源管理、存储和访问功能。采用设计模式确保代码的可扩展性、可维护性和性能。 + +## 2. 设计目标 + +- **高效缓存**: 避免重复加载相同资源 +- **类型安全**: 使用模板和强类型ID +- **异步加载**: 支持后台加载大资源 +- **内存管理**: 自动释放未使用资源 +- **扩展性**: 易于添加新资源类型 + +## 3. 核心组件设计 + +### 3.1 资源类型枚举 + +```cpp +enum class AssetType { + Texture, // 纹理/图片 + Font, // 字体 + Shader, // 着色器 + Audio, // 音频 + Data, // 通用数据 + Custom // 自定义类型 +}; +``` + +### 3.2 资源句柄 (Resource Handle) + +使用强类型句柄替代裸指针,提供类型安全和生命周期管理: + +```cpp +template +class AssetHandle { + AssetID id_; + Weak cacheRef_; +public: + bool valid() const; + Ref get() const; + void release(); +}; +``` + +### 3.3 资源基类 + +```cpp +class Asset { +public: + virtual ~Asset() = default; + virtual AssetType type() const = 0; + virtual bool loaded() const = 0; + virtual size_t memorySize() const = 0; + + const AssetID& id() const { return id_; } + const std::string& path() const { return path_; } + +protected: + AssetID id_; + std::string path_; + std::atomic loaded_{false}; +}; +``` + +### 3.4 具体资源类型 + +#### 纹理资源 +```cpp +class TextureAsset : public Asset { +public: + AssetType type() const override { return AssetType::Texture; } + + int width() const { return width_; } + int height() const { return height_; } + int channels() const { return channels_; } + const u8* data() const { return data_.get(); } + +private: + int width_ = 0; + int height_ = 0; + int channels_ = 0; + Unique data_; +}; +``` + +#### 字体资源 +```cpp +class FontAsset : public Asset { +public: + AssetType type() const override { return AssetType::Font; } + + const stbtt_fontinfo& info() const { return info_; } + float scaleForPixelHeight(float pixels) const; + +private: + std::vector data_; + stbtt_fontinfo info_; +}; +``` + +#### 着色器资源 +```cpp +class ShaderAsset : public Asset { +public: + AssetType type() const override { return AssetType::Shader; } + + const std::string& vertexSource() const { return vertexSrc_; } + const std::string& fragmentSource() const { return fragmentSrc_; } + +private: + std::string vertexSrc_; + std::string fragmentSrc_; +}; +``` + +## 4. 服务接口设计 + +### 4.1 资源服务接口 + +```cpp +class IAssetService : public IService { +public: + virtual ~IAssetService() = default; + + // 同步加载 + template + AssetHandle load(const std::string& path); + + // 异步加载 + template + void loadAsync(const std::string& path, AssetLoadCallback callback); + + // 获取已缓存资源 + template + AssetHandle get(const std::string& path); + + // 卸载资源 + virtual void unload(const AssetID& id) = 0; + virtual void unloadAll() = 0; + + // 缓存管理 + virtual void setCacheLimit(size_t maxBytes) = 0; + virtual size_t cacheSize() const = 0; + virtual void gc() = 0; // 垃圾回收 + + // 注册加载器 + template + void registerLoader(Unique> loader); +}; +``` + +### 4.2 资源加载器接口 + +使用**策略模式**支持不同资源类型的加载: + +```cpp +template +class AssetLoader { +public: + virtual ~AssetLoader() = default; + virtual Unique load(const std::string& path) = 0; + virtual bool canLoad(const std::string& path) const = 0; + virtual AssetType type() const = 0; +}; + +// 纹理加载器 +class TextureLoader : public AssetLoader { +public: + Unique load(const std::string& path) override; + bool canLoad(const std::string& path) const override; + AssetType type() const override { return AssetType::Texture; } +}; + +// 字体加载器 +class FontLoader : public AssetLoader { +public: + Unique load(const std::string& path) override; + bool canLoad(const std::string& path) const override; + AssetType type() const override { return AssetType::Font; } +}; +``` + +## 5. 实现架构 + +### 5.1 类图 + +``` + IService + │ + ▼ + IAssetService (接口) + │ + ▼ + AssetService (实现) + │ + ┌──────────────┼──────────────┐ + │ │ │ + ▼ ▼ ▼ diff --git a/Extra2D/include/extra2d/app/application.h b/Extra2D/include/extra2d/app/application.h index 2ffdfb6..52774e5 100644 --- a/Extra2D/include/extra2d/app/application.h +++ b/Extra2D/include/extra2d/app/application.h @@ -76,8 +76,8 @@ public: */ void resume(); - bool isPaused() const { return paused_; } - bool isRunning() const { return running_; } + bool paused() const { return paused_; } + bool running() const { return running_; } /** * @brief 获取窗口 @@ -85,9 +85,9 @@ public: */ GLFWWindow *window(); - float deltaTime() const { return deltaTime_; } - float totalTime() const { return totalTime_; } - int fps() const { return currentFps_; } + f32 dt() const { return dt_; } + f32 totalTime() const { return totalTime_; } + int fps() const { return fps_; } private: Application(); @@ -102,12 +102,12 @@ private: bool paused_ = false; bool shouldQuit_ = false; - float deltaTime_ = 0.0f; - float totalTime_ = 0.0f; - double lastFrameTime_ = 0.0; + f32 dt_ = 0.0f; + f32 totalTime_ = 0.0f; + f64 lastFrameTime_ = 0.0; int frameCount_ = 0; - float fpsTimer_ = 0.0f; - int currentFps_ = 0; + f32 fpsTimer_ = 0.0f; + int fps_ = 0; }; } // namespace extra2d diff --git a/Extra2D/include/extra2d/core/module.h b/Extra2D/include/extra2d/core/module.h index d1734b4..8eb5b17 100644 --- a/Extra2D/include/extra2d/core/module.h +++ b/Extra2D/include/extra2d/core/module.h @@ -56,7 +56,7 @@ public: * @brief 检查模块是否支持并行初始化 * @return 支持并行初始化返回 true */ - virtual bool allowParallelInit() const { return true; } + virtual bool parallel() const { return true; } /** * @brief 设置所属Application @@ -77,6 +77,6 @@ protected: /** * @brief 模块工厂函数类型 */ -using ModuleFactory = std::function()>; +using ModuleFactory = std::function()>; } // namespace extra2d diff --git a/Extra2D/include/extra2d/core/object_pool.h b/Extra2D/include/extra2d/core/object_pool.h new file mode 100644 index 0000000..db45a2b --- /dev/null +++ b/Extra2D/include/extra2d/core/object_pool.h @@ -0,0 +1,64 @@ +#pragma once + +#include +#include +#include + +namespace extra2d { + +/** + * @brief 固定大小对象池 + * @tparam T 对象类型 + * @tparam Size 池大小 + */ +template +class ObjectPool { +public: + ObjectPool() { + for (size_t i = 0; i < Size; ++i) { + available_.push(&pool_[i]); + } + } + + /** + * @brief 获取对象 + * @return 对象指针,池耗尽返回nullptr + */ + T* acquire() { + if (available_.empty()) { + return nullptr; + } + T* obj = available_.front(); + available_.pop(); + return obj; + } + + /** + * @brief 释放对象回池 + * @param obj 对象指针 + */ + void release(T* obj) { + if (obj >= pool_.data() && obj < pool_.data() + Size) { + obj->~T(); + new (obj) T(); + available_.push(obj); + } + } + + /** + * @brief 获取可用对象数量 + */ + size_t available() const { return available_.size(); } + + /** + * @brief 获取池总大小 + */ + static constexpr size_t capacity() { return Size; } + +private: + alignas(alignof(T)) std::array storage_; + T* pool_ = reinterpret_cast(storage_.data()); + std::queue available_; +}; + +} // namespace extra2d diff --git a/Extra2D/include/extra2d/core/registry.h b/Extra2D/include/extra2d/core/registry.h index 157c568..dd19253 100644 --- a/Extra2D/include/extra2d/core/registry.h +++ b/Extra2D/include/extra2d/core/registry.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include @@ -12,12 +12,32 @@ namespace extra2d { class Application; +/** + * @brief 编译期类型ID生成器 + */ +using TypeId = size_t; + +namespace detail { + inline TypeId nextTypeId() { + static TypeId id = 0; + return ++id; + } + + template + inline TypeId getTypeId() { + static TypeId id = nextTypeId(); + return id; + } +} + /** * @brief 模块注册表 * 管理模块的注册、拓扑排序和生命周期 */ class Registry { public: + static constexpr size_t MAX_MODULES = 64; + static Registry& instance(); Registry(const Registry&) = delete; @@ -33,16 +53,30 @@ public: template T* use(Args&&... args) { static_assert(std::is_base_of_v, "T must derive from Module"); - - auto typeIdx = std::type_index(typeid(T)); - if (modules_.count(typeIdx)) { - return static_cast(modules_[typeIdx].get()); + + TypeId typeId = detail::getTypeId(); + + // 数组查找,O(n) 但 n 很小,缓存友好 + for (size_t i = 0; i < moduleCount_; ++i) { + if (modules_[i].id == typeId) { + return static_cast(modules_[i].module.get()); + } } - - auto module = makeUnique(std::forward(args)...); + + // 添加新模块 + if (moduleCount_ >= MAX_MODULES) { + return nullptr; // 模块数量超过上限 + } + + auto module = ptr::makeUnique(std::forward(args)...); T* ptr = module.get(); module->setApp(app_); - modules_[typeIdx] = std::move(module); + + modules_[moduleCount_].id = typeId; + modules_[moduleCount_].module = std::move(module); + modules_[moduleCount_].valid = true; + ++moduleCount_; + return ptr; } @@ -53,10 +87,12 @@ public: */ template T* get() const { - auto typeIdx = std::type_index(typeid(T)); - auto it = modules_.find(typeIdx); - if (it != modules_.end()) { - return static_cast(it->second.get()); + TypeId typeId = detail::getTypeId(); + + for (size_t i = 0; i < moduleCount_; ++i) { + if (modules_[i].id == typeId && modules_[i].valid) { + return static_cast(modules_[i].module.get()); + } } return nullptr; } @@ -67,9 +103,12 @@ public: * @return 模块指针 */ Module* get(std::type_index typeIdx) const { - auto it = modules_.find(typeIdx); - if (it != modules_.end()) { - return it->second.get(); + // 这里仍然使用type_index作为后备方案 + for (size_t i = 0; i < moduleCount_; ++i) { + if (modules_[i].valid && + std::type_index(typeid(*modules_[i].module)) == typeIdx) { + return modules_[i].module.get(); + } } return nullptr; } @@ -98,26 +137,33 @@ public: /** * @brief 获取模块数量 */ - size_t size() const { return modules_.size(); } + size_t size() const { return moduleCount_; } private: Registry() = default; ~Registry() = default; + struct ModuleEntry { + TypeId id = 0; + Unique module; + bool valid = false; + }; + /** * @brief 拓扑排序模块 * @return 排序后的模块列表 */ - std::vector topologicalSort(); + std::vector sort(); /** * @brief 按层级对模块进行分组 * 同一层级的模块没有相互依赖,可以并行初始化 * @return 按层级分组的模块列表 */ - std::vector> groupByLevel(); + std::vector> group(); - std::unordered_map> modules_; + std::array modules_; + size_t moduleCount_ = 0; Application* app_ = nullptr; }; diff --git a/Extra2D/include/extra2d/core/result.h b/Extra2D/include/extra2d/core/result.h new file mode 100644 index 0000000..44385d6 --- /dev/null +++ b/Extra2D/include/extra2d/core/result.h @@ -0,0 +1,398 @@ +#pragma once + +#include +#include +#include + +namespace extra2d { + +/** + * @brief 错误码枚举 + */ +enum class ErrorCode { + None = 0, + Unknown = 1, + InvalidArgument = 2, + OutOfMemory = 3, + FileNotFound = 4, + PermissionDenied = 5, + NotImplemented = 6, + AlreadyExists = 7, + NotInitialized = 8, + AlreadyInitialized = 9, + OperationFailed = 10, + Timeout = 11, + Cancelled = 12, + InvalidState = 13, + ResourceExhausted = 14, + Unavailable = 15, + DataLoss = 16, + Unauthenticated = 17, + PermissionDenied2 = 18, + ResourceNotFound = 19, + Aborted = 20, + OutOfRange = 21, + Unimplemented = 22, + Internal = 23, + DataCorrupted = 24, + RequestTooLarge = 25, + ResourceBusy = 26, + QuotaExceeded = 27, + DeadlineExceeded = 28, + LoadBalancing = 29, + NetworkError = 30, + ProtocolError = 31, + ServiceUnavailable = 32, + GatewayError = 33, + RateLimited = 34, + BadRequest = 35, + Unauthorized = 36, + Forbidden = 37, + NotFound = 38, + MethodNotAllowed = 39, + Conflict = 40, + Gone = 41, + LengthRequired = 42, + PreconditionFailed = 43, + PayloadTooLarge = 44, + UriTooLong = 45, + UnsupportedMediaType = 46, + RangeNotSatisfiable = 47, + ExpectationFailed = 48, + ImATeapot = 49, + MisdirectedRequest = 50, + UnprocessableEntity = 51, + Locked = 52, + FailedDependency = 53, + TooEarly = 54, + UpgradeRequired = 55, + PreconditionRequired = 56, + TooManyRequests = 57, + RequestHeaderFieldsTooLarge = 58, + UnavailableForLegalReasons = 59 +}; + +/** + * @brief 错误信息结构 + */ +struct Error { + ErrorCode code = ErrorCode::None; + std::string message; + std::string file; + int line = 0; + + Error() = default; + Error(ErrorCode c, const std::string& msg) : code(c), message(msg) {} + Error(ErrorCode c, const std::string& msg, const std::string& f, int l) + : code(c), message(msg), file(f), line(l) {} + + bool ok() const { return code == ErrorCode::None; } + + static Error none() { return Error(); } + static Error unknown(const std::string& msg) { return Error(ErrorCode::Unknown, msg); } + static Error invalidArgument(const std::string& msg) { return Error(ErrorCode::InvalidArgument, msg); } + static Error outOfMemory(const std::string& msg) { return Error(ErrorCode::OutOfMemory, msg); } + static Error fileNotFound(const std::string& msg) { return Error(ErrorCode::FileNotFound, msg); } + static Error permissionDenied(const std::string& msg) { return Error(ErrorCode::PermissionDenied, msg); } + static Error notImplemented(const std::string& msg) { return Error(ErrorCode::NotImplemented, msg); } + static Error alreadyExists(const std::string& msg) { return Error(ErrorCode::AlreadyExists, msg); } + static Error notInitialized(const std::string& msg) { return Error(ErrorCode::NotInitialized, msg); } + static Error alreadyInitialized(const std::string& msg) { return Error(ErrorCode::AlreadyInitialized, msg); } + static Error operationFailed(const std::string& msg) { return Error(ErrorCode::OperationFailed, msg); } + static Error timeout(const std::string& msg) { return Error(ErrorCode::Timeout, msg); } + static Error cancelled(const std::string& msg) { return Error(ErrorCode::Cancelled, msg); } + static Error invalidState(const std::string& msg) { return Error(ErrorCode::InvalidState, msg); } + static Error resourceExhausted(const std::string& msg) { return Error(ErrorCode::ResourceExhausted, msg); } + static Error unavailable(const std::string& msg) { return Error(ErrorCode::Unavailable, msg); } + static Error dataLoss(const std::string& msg) { return Error(ErrorCode::DataLoss, msg); } + static Error unauthenticated(const std::string& msg) { return Error(ErrorCode::Unauthenticated, msg); } + static Error permissionDenied2(const std::string& msg) { return Error(ErrorCode::PermissionDenied2, msg); } + static Error resourceNotFound(const std::string& msg) { return Error(ErrorCode::ResourceNotFound, msg); } + static Error aborted(const std::string& msg) { return Error(ErrorCode::Aborted, msg); } + static Error outOfRange(const std::string& msg) { return Error(ErrorCode::OutOfRange, msg); } + static Error unimplemented(const std::string& msg) { return Error(ErrorCode::Unimplemented, msg); } + static Error internal(const std::string& msg) { return Error(ErrorCode::Internal, msg); } + static Error dataCorrupted(const std::string& msg) { return Error(ErrorCode::DataCorrupted, msg); } + static Error requestTooLarge(const std::string& msg) { return Error(ErrorCode::RequestTooLarge, msg); } + static Error resourceBusy(const std::string& msg) { return Error(ErrorCode::ResourceBusy, msg); } + static Error quotaExceeded(const std::string& msg) { return Error(ErrorCode::QuotaExceeded, msg); } + static Error deadlineExceeded(const std::string& msg) { return Error(ErrorCode::DeadlineExceeded, msg); } + static Error loadBalancing(const std::string& msg) { return Error(ErrorCode::LoadBalancing, msg); } + static Error networkError(const std::string& msg) { return Error(ErrorCode::NetworkError, msg); } + static Error protocolError(const std::string& msg) { return Error(ErrorCode::ProtocolError, msg); } + static Error serviceUnavailable(const std::string& msg) { return Error(ErrorCode::ServiceUnavailable, msg); } + static Error gatewayError(const std::string& msg) { return Error(ErrorCode::GatewayError, msg); } + static Error rateLimited(const std::string& msg) { return Error(ErrorCode::RateLimited, msg); } + static Error badRequest(const std::string& msg) { return Error(ErrorCode::BadRequest, msg); } + static Error unauthorized(const std::string& msg) { return Error(ErrorCode::Unauthorized, msg); } + static Error forbidden(const std::string& msg) { return Error(ErrorCode::Forbidden, msg); } + static Error notFound(const std::string& msg) { return Error(ErrorCode::NotFound, msg); } + static Error methodNotAllowed(const std::string& msg) { return Error(ErrorCode::MethodNotAllowed, msg); } + static Error conflict(const std::string& msg) { return Error(ErrorCode::Conflict, msg); } + static Error gone(const std::string& msg) { return Error(ErrorCode::Gone, msg); } + static Error lengthRequired(const std::string& msg) { return Error(ErrorCode::LengthRequired, msg); } + static Error preconditionFailed(const std::string& msg) { return Error(ErrorCode::PreconditionFailed, msg); } + static Error payloadTooLarge(const std::string& msg) { return Error(ErrorCode::PayloadTooLarge, msg); } + static Error uriTooLong(const std::string& msg) { return Error(ErrorCode::UriTooLong, msg); } + static Error unsupportedMediaType(const std::string& msg) { return Error(ErrorCode::UnsupportedMediaType, msg); } + static Error rangeNotSatisfiable(const std::string& msg) { return Error(ErrorCode::RangeNotSatisfiable, msg); } + static Error expectationFailed(const std::string& msg) { return Error(ErrorCode::ExpectationFailed, msg); } + static Error imATeapot(const std::string& msg) { return Error(ErrorCode::ImATeapot, msg); } + static Error misdirectedRequest(const std::string& msg) { return Error(ErrorCode::MisdirectedRequest, msg); } + static Error unprocessableEntity(const std::string& msg) { return Error(ErrorCode::UnprocessableEntity, msg); } + static Error locked(const std::string& msg) { return Error(ErrorCode::Locked, msg); } + static Error failedDependency(const std::string& msg) { return Error(ErrorCode::FailedDependency, msg); } + static Error tooEarly(const std::string& msg) { return Error(ErrorCode::TooEarly, msg); } + static Error upgradeRequired(const std::string& msg) { return Error(ErrorCode::UpgradeRequired, msg); } + static Error preconditionRequired(const std::string& msg) { return Error(ErrorCode::PreconditionRequired, msg); } + static Error tooManyRequests(const std::string& msg) { return Error(ErrorCode::TooManyRequests, msg); } + static Error requestHeaderFieldsTooLarge(const std::string& msg) { return Error(ErrorCode::RequestHeaderFieldsTooLarge, msg); } + static Error unavailableForLegalReasons(const std::string& msg) { return Error(ErrorCode::UnavailableForLegalReasons, msg); } +}; + +/** + * @brief Result类型,用于错误处理 + * @tparam T 成功时的值类型 + * @tparam E 错误类型,默认为Error + */ +template +class Result { +public: + Result() : hasValue_(false) { + new (&storage_.error) E(); + } + + ~Result() { + if (hasValue_) { + storage_.value.~T(); + } else { + storage_.error.~E(); + } + } + + Result(const Result& other) : hasValue_(other.hasValue_) { + if (hasValue_) { + new (&storage_.value) T(other.storage_.value); + } else { + new (&storage_.error) E(other.storage_.error); + } + } + + Result(Result&& other) noexcept : hasValue_(other.hasValue_) { + if (hasValue_) { + new (&storage_.value) T(std::move(other.storage_.value)); + } else { + new (&storage_.error) E(std::move(other.storage_.error)); + } + } + + Result& operator=(const Result& other) { + if (this != &other) { + this->~Result(); + hasValue_ = other.hasValue_; + if (hasValue_) { + new (&storage_.value) T(other.storage_.value); + } else { + new (&storage_.error) E(other.storage_.error); + } + } + return *this; + } + + Result& operator=(Result&& other) noexcept { + if (this != &other) { + this->~Result(); + hasValue_ = other.hasValue_; + if (hasValue_) { + new (&storage_.value) T(std::move(other.storage_.value)); + } else { + new (&storage_.error) E(std::move(other.storage_.error)); + } + } + return *this; + } + + static Result ok(T value) { + Result result; + result.hasValue_ = true; + new (&result.storage_.value) T(std::move(value)); + return result; + } + + static Result err(E error) { + Result result; + result.hasValue_ = false; + new (&result.storage_.error) E(std::move(error)); + return result; + } + + bool ok() const { return hasValue_; } + bool isOk() const { return hasValue_; } + bool isErr() const { return !hasValue_; } + + T& value() & { + return storage_.value; + } + + const T& value() const & { + return storage_.value; + } + + T&& value() && { + return std::move(storage_.value); + } + + E& error() & { + return storage_.error; + } + + const E& error() const & { + return storage_.error; + } + + E&& error() && { + return std::move(storage_.error); + } + + T valueOr(T defaultValue) const { + return hasValue_ ? storage_.value : std::move(defaultValue); + } + + template + Result map(F&& f) { + if (hasValue_) { + return Result::ok(f(storage_.value)); + } + return *this; + } + + template + Result mapErr(F&& f) { + if (!hasValue_) { + return Result::err(f(storage_.error)); + } + return *this; + } + + template + auto andThen(F&& f) -> decltype(f(std::declval())) { + if (hasValue_) { + return f(storage_.value); + } + return Result()))::ValueType, E>::err(storage_.error); + } + + template + Result orElse(F&& f) { + if (!hasValue_) { + return f(storage_.error); + } + return *this; + } + +private: + union Storage { + T value; + E error; + + Storage() {} + ~Storage() {} + } storage_; + + bool hasValue_; +}; + +// 特化void类型 +template +class Result { +public: + Result() : hasValue_(true) {} + + ~Result() { + if (!hasValue_) { + storage_.error.~E(); + } + } + + Result(const Result& other) : hasValue_(other.hasValue_) { + if (!hasValue_) { + new (&storage_.error) E(other.storage_.error); + } + } + + Result(Result&& other) noexcept : hasValue_(other.hasValue_) { + if (!hasValue_) { + new (&storage_.error) E(std::move(other.storage_.error)); + } + } + + Result& operator=(const Result& other) { + if (this != &other) { + this->~Result(); + hasValue_ = other.hasValue_; + if (!hasValue_) { + new (&storage_.error) E(other.storage_.error); + } + } + return *this; + } + + Result& operator=(Result&& other) noexcept { + if (this != &other) { + this->~Result(); + hasValue_ = other.hasValue_; + if (!hasValue_) { + new (&storage_.error) E(std::move(other.storage_.error)); + } + } + return *this; + } + + static Result ok() { + return Result(); + } + + static Result err(E error) { + Result result; + result.hasValue_ = false; + new (&result.storage_.error) E(std::move(error)); + return result; + } + + bool ok() const { return hasValue_; } + bool isOk() const { return hasValue_; } + bool isErr() const { return !hasValue_; } + + E& error() & { + return storage_.error; + } + + const E& error() const & { + return storage_.error; + } + + E&& error() && { + return std::move(storage_.error); + } + +private: + union Storage { + E error; + + Storage() {} + ~Storage() {} + } storage_; + + bool hasValue_; +}; + +// 便捷宏 +#define E2D_TRY(result) \ + do { \ + auto _res = (result); \ + if (!_res.ok()) { \ + return Result::err(_res.error()); \ + } \ + } while(0) + +} // namespace extra2d diff --git a/Extra2D/include/extra2d/core/ring_buffer.h b/Extra2D/include/extra2d/core/ring_buffer.h new file mode 100644 index 0000000..15ede7a --- /dev/null +++ b/Extra2D/include/extra2d/core/ring_buffer.h @@ -0,0 +1,103 @@ +#pragma once + +#include +#include +#include + +namespace extra2d { + +/** + * @brief 无锁环形缓冲区(单生产者单消费者) + * @tparam T 元素类型 + * @tparam Size 缓冲区大小(必须是2的幂) + */ +template +class RingBuffer { + static_assert((Size & (Size - 1)) == 0, "Size must be a power of 2"); + +public: + RingBuffer() = default; + + /** + * @brief 入队 + * @param item 元素 + * @return 成功返回true,缓冲区满返回false + */ + bool push(const T& item) { + const size_t currentHead = head_.load(std::memory_order_relaxed); + const size_t currentTail = tail_.load(std::memory_order_acquire); + + if ((currentHead - currentTail) >= Size) { + return false; // 缓冲区满 + } + + buffer_[currentHead & mask_] = item; + head_.store(currentHead + 1, std::memory_order_release); + return true; + } + + /** + * @brief 入队(移动语义) + * @param item 元素 + * @return 成功返回true,缓冲区满返回false + */ + bool push(T&& item) { + const size_t currentHead = head_.load(std::memory_order_relaxed); + const size_t currentTail = tail_.load(std::memory_order_acquire); + + if ((currentHead - currentTail) >= Size) { + return false; // 缓冲区满 + } + + buffer_[currentHead & mask_] = std::move(item); + head_.store(currentHead + 1, std::memory_order_release); + return true; + } + + /** + * @brief 出队 + * @param item 输出元素 + * @return 成功返回true,缓冲区空返回false + */ + bool pop(T& item) { + const size_t currentTail = tail_.load(std::memory_order_relaxed); + const size_t currentHead = head_.load(std::memory_order_acquire); + + if (currentTail == currentHead) { + return false; // 缓冲区空 + } + + item = std::move(buffer_[currentTail & mask_]); + tail_.store(currentTail + 1, std::memory_order_release); + return true; + } + + /** + * @brief 检查是否为空 + */ + bool empty() const { + return head_.load(std::memory_order_acquire) == + tail_.load(std::memory_order_acquire); + } + + /** + * @brief 获取当前大小 + */ + size_t size() const { + return head_.load(std::memory_order_acquire) - + tail_.load(std::memory_order_acquire); + } + + /** + * @brief 获取容量 + */ + static constexpr size_t capacity() { return Size; } + +private: + static constexpr size_t mask_ = Size - 1; + alignas(64) std::array buffer_; + alignas(64) std::atomic head_{0}; + alignas(64) std::atomic tail_{0}; +}; + +} // namespace extra2d diff --git a/Extra2D/include/extra2d/core/service_interface.h b/Extra2D/include/extra2d/core/service_interface.h index 0b77e69..dcc3416 100644 --- a/Extra2D/include/extra2d/core/service_interface.h +++ b/Extra2D/include/extra2d/core/service_interface.h @@ -9,7 +9,7 @@ namespace extra2d { * @brief 服务优先级枚举 * 定义服务的初始化顺序,数值越小越先初始化 */ -enum class ServicePriority : int { +enum class ServicePriority : i32 { Core = 0, Event = 100, Timer = 200, @@ -56,13 +56,13 @@ public: * @brief 获取服务信息 * @return 服务信息结构体 */ - virtual ServiceInfo getServiceInfo() const = 0; + virtual ServiceInfo info() const = 0; /** * @brief 初始化服务 * @return 初始化成功返回 true */ - virtual bool initialize() = 0; + virtual bool init() = 0; /** * @brief 关闭服务 @@ -87,15 +87,15 @@ public: /** * @brief 更新服务 - * @param deltaTime 帧间隔时间 + * @param dt 帧间隔时间 */ - virtual void update(float deltaTime) { } + virtual void update(f32 dt) { } /** * @brief 检查服务是否已初始化 * @return 已初始化返回 true */ - virtual bool isInitialized() const { + virtual bool initialized() const { return info_.state == ServiceState::Running || info_.state == ServiceState::Paused; } @@ -104,13 +104,13 @@ public: * @brief 获取服务状态 * @return 当前服务状态 */ - ServiceState getState() const { return info_.state; } + ServiceState state() const { return info_.state; } /** * @brief 获取服务名称 * @return 服务名称 */ - const std::string& getName() const { return info_.name; } + const std::string& name() const { return info_.name; } protected: ServiceInfo info_; diff --git a/Extra2D/include/extra2d/core/service_locator.h b/Extra2D/include/extra2d/core/service_locator.h index 798b2c6..565627e 100644 --- a/Extra2D/include/extra2d/core/service_locator.h +++ b/Extra2D/include/extra2d/core/service_locator.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -15,7 +16,7 @@ namespace extra2d { /** * @brief 服务工厂函数类型 */ -template using ServiceFactory = std::function()>; +template using ServiceFactory = Fn()>; /** * @brief 服务定位器 @@ -25,7 +26,7 @@ template using ServiceFactory = std::function()>; * - 类型安全的服务注册和获取 * - 支持服务工厂延迟创建 * - 支持服务依赖声明 - * - 线程安全 + * - 线程安全(读写锁) * - 支持 Mock 测试 */ class ServiceLocator { @@ -42,39 +43,39 @@ public: /** * @brief 注册服务实例 * @tparam T 服务接口类型 - * @param service 服务实例 + * @param svc 服务实例 */ - template void registerService(SharedPtr service) { + template void add(Ref svc) { static_assert(std::is_base_of_v, "T must derive from IService"); - std::lock_guard lock(mutex_); + std::unique_lock lock(mutex_); auto typeId = std::type_index(typeid(T)); - services_[typeId] = std::static_pointer_cast(service); - orderedServices_.push_back(service); - sortServices(); + services_[typeId] = std::static_pointer_cast(svc); + orderedServices_.push_back(svc); + sort(); } /** * @brief 注册服务工厂 * @tparam T 服务接口类型 - * @param factory 服务工厂函数 + * @param fn 服务工厂函数 */ - template void registerFactory(ServiceFactory factory) { + template void setFactory(ServiceFactory fn) { static_assert(std::is_base_of_v, "T must derive from IService"); - std::lock_guard lock(mutex_); + std::unique_lock lock(mutex_); auto typeId = std::type_index(typeid(T)); - factories_[typeId] = [factory]() -> SharedPtr { - return std::static_pointer_cast(factory()); + factories_[typeId] = [fn]() -> Ref { + return std::static_pointer_cast(fn()); }; // 立即创建服务实例并添加到有序列表 - auto service = factories_[typeId](); - services_[typeId] = service; - orderedServices_.push_back(service); - sortServices(); + auto svc = factories_[typeId](); + services_[typeId] = svc; + orderedServices_.push_back(svc); + sort(); } /** @@ -82,13 +83,15 @@ public: * @tparam T 服务接口类型 * @return 服务实例,不存在返回 nullptr */ - template SharedPtr getService() const { + template Ref get() const { static_assert(std::is_base_of_v, "T must derive from IService"); - std::lock_guard lock(mutex_); auto typeId = std::type_index(typeid(T)); + // 读锁查询 + std::shared_lock lock(mutex_); + auto it = services_.find(typeId); if (it != services_.end()) { return std::static_pointer_cast(it->second); @@ -96,9 +99,9 @@ public: auto factoryIt = factories_.find(typeId); if (factoryIt != factories_.end()) { - auto service = factoryIt->second(); - services_[typeId] = service; - return std::static_pointer_cast(service); + auto svc = factoryIt->second(); + services_[typeId] = svc; + return std::static_pointer_cast(svc); } return nullptr; @@ -109,12 +112,14 @@ public: * @tparam T 服务接口类型 * @return 服务实例,不存在返回 nullptr */ - template SharedPtr tryGetService() const { + template Ref tryGet() const { static_assert(std::is_base_of_v, "T must derive from IService"); - std::lock_guard lock(mutex_); auto typeId = std::type_index(typeid(T)); + + // 读锁查询 + std::shared_lock lock(mutex_); auto it = services_.find(typeId); if (it != services_.end()) { return std::static_pointer_cast(it->second); @@ -127,8 +132,8 @@ public: * @tparam T 服务接口类型 * @return 已注册返回 true */ - template bool hasService() const { - std::lock_guard lock(mutex_); + template bool has() const { + std::shared_lock lock(mutex_); auto typeId = std::type_index(typeid(T)); return services_.find(typeId) != services_.end() || factories_.find(typeId) != factories_.end(); @@ -138,17 +143,17 @@ public: * @brief 注销服务 * @tparam T 服务接口类型 */ - template void unregisterService() { - std::lock_guard lock(mutex_); + template void remove() { + std::unique_lock lock(mutex_); auto typeId = std::type_index(typeid(T)); auto it = services_.find(typeId); if (it != services_.end()) { - auto service = it->second; + auto svc = it->second; services_.erase(it); auto orderIt = - std::find(orderedServices_.begin(), orderedServices_.end(), service); + std::find(orderedServices_.begin(), orderedServices_.end(), svc); if (orderIt != orderedServices_.end()) { orderedServices_.erase(orderIt); } @@ -161,34 +166,34 @@ public: * @brief 初始化所有已注册的服务 * @return 所有服务初始化成功返回 true */ - bool initializeAll(); + bool init(); /** * @brief 关闭所有服务 */ - void shutdownAll(); + void shutdown(); /** * @brief 更新所有服务 - * @param deltaTime 帧间隔时间 + * @param dt 帧间隔时间 */ - void updateAll(float deltaTime); + void update(f32 dt); /** * @brief 暂停所有服务 */ - void pauseAll(); + void pause(); /** * @brief 恢复所有服务 */ - void resumeAll(); + void resume(); /** * @brief 获取所有服务(按优先级排序) * @return 服务列表 */ - std::vector> getAllServices() const; + std::vector> all() const; /** * @brief 清空所有服务和工厂 @@ -208,13 +213,13 @@ private: /** * @brief 按优先级排序服务 */ - void sortServices(); + void sort(); - mutable std::unordered_map> services_; - std::unordered_map()>> + mutable std::unordered_map> services_; + std::unordered_map()>> factories_; - std::vector> orderedServices_; - mutable std::recursive_mutex mutex_; + std::vector> orderedServices_; + mutable std::shared_mutex mutex_; }; /** @@ -223,13 +228,13 @@ private: */ template class ServiceRegistrar { public: - explicit ServiceRegistrar(ServiceFactory factory = nullptr) { - if (factory) { - ServiceLocator::instance().registerFactory(factory); + explicit ServiceRegistrar(ServiceFactory fn = nullptr) { + if (fn) { + ServiceLocator::instance().setFactory(fn); } else { - ServiceLocator::instance().registerFactory( - []() -> SharedPtr { - return makeShared(); + ServiceLocator::instance().setFactory( + []() -> Ref { + return ptr::make(); }); } } @@ -251,9 +256,9 @@ template struct ServiceAutoReg { * @return true 表示注册成功 */ static bool doRegister() { - ::extra2d::ServiceLocator::instance().registerFactory( - []() -> ::extra2d::SharedPtr { - return ::extra2d::makeShared(); + ::extra2d::ServiceLocator::instance().setFactory( + []() -> ::extra2d::Ref { + return ::extra2d::ptr::make(); }); return true; } @@ -271,8 +276,8 @@ template struct ServiceAutoRegFactory { template struct Impl { static const bool registered; - static bool doRegister(Factory factory) { - ::extra2d::ServiceLocator::instance().registerFactory(factory); + static bool doRegister(Factory fn) { + ::extra2d::ServiceLocator::instance().setFactory(fn); return true; } }; diff --git a/Extra2D/include/extra2d/core/service_registry.h b/Extra2D/include/extra2d/core/service_registry.h index 7ce4964..3a855f2 100644 --- a/Extra2D/include/extra2d/core/service_registry.h +++ b/Extra2D/include/extra2d/core/service_registry.h @@ -14,7 +14,7 @@ namespace extra2d { struct ServiceRegistration { std::string name; ServicePriority priority; - std::function()> factory; + std::function()> factory; bool enabled = true; }; @@ -41,7 +41,7 @@ public: * @param priority 服务优先级 */ template - void registerService(const std::string& name, ServicePriority priority) { + void add(const std::string& name, ServicePriority priority) { static_assert(std::is_base_of_v, "T must derive from IService"); static_assert(std::is_base_of_v, @@ -50,8 +50,8 @@ public: ServiceRegistration reg; reg.name = name; reg.priority = priority; - reg.factory = []() -> SharedPtr { - return std::static_pointer_cast(makeShared()); + reg.factory = []() -> Ref { + return std::static_pointer_cast(ptr::make()); }; registrations_.push_back(reg); } @@ -64,17 +64,17 @@ public: * @param factory 工厂函数 */ template - void registerServiceWithFactory( + void addWithFactory( const std::string& name, ServicePriority priority, - std::function()> factory) { + std::function()> factory) { static_assert(std::is_base_of_v, "T must derive from IService"); ServiceRegistration reg; reg.name = name; reg.priority = priority; - reg.factory = [factory]() -> SharedPtr { + reg.factory = [factory]() -> Ref { return std::static_pointer_cast(factory()); }; registrations_.push_back(reg); @@ -85,19 +85,19 @@ public: * @param name 服务名称 * @param enabled 是否启用 */ - void setServiceEnabled(const std::string& name, bool enabled); + void setEnabled(const std::string& name, bool enabled); /** * @brief 创建所有已注册的服务 * 并注册到 ServiceLocator */ - void createAllServices(); + void createAll(); /** * @brief 获取所有注册信息 * @return 注册信息列表 */ - const std::vector& getRegistrations() const { + const std::vector& all() const { return registrations_; } @@ -123,7 +123,7 @@ template class AutoServiceRegistrar { public: AutoServiceRegistrar(const std::string& name, ServicePriority priority) { - ServiceRegistry::instance().registerService( + ServiceRegistry::instance().add( name, priority); } }; diff --git a/Extra2D/include/extra2d/core/types.h b/Extra2D/include/extra2d/core/types.h index b3720eb..1afc74f 100644 --- a/Extra2D/include/extra2d/core/types.h +++ b/Extra2D/include/extra2d/core/types.h @@ -16,43 +16,46 @@ namespace extra2d { // --------------------------------------------------------------------------- // 智能指针别名 // --------------------------------------------------------------------------- -template using Ptr = std::shared_ptr; -template using SharedPtr = std::shared_ptr; +template using Ref = std::shared_ptr; +template using Unique = std::unique_ptr; +template using Weak = std::weak_ptr; -template using UniquePtr = std::unique_ptr; +/// 智能指针工厂函数命名空间 +namespace ptr { + /// 创建 Ref 的便捷函数 + template inline Ref make(Args &&...args) { + return std::make_shared(std::forward(args)...); + } -template using WeakPtr = std::weak_ptr; - -/// 创建 shared_ptr 的便捷函数 -template inline Ptr makePtr(Args &&...args) { - return std::make_shared(std::forward(args)...); -} - -template inline SharedPtr makeShared(Args &&...args) { - return std::make_shared(std::forward(args)...); -} - -/// 创建 unique_ptr 的便捷函数 -template -inline UniquePtr makeUnique(Args &&...args) { - return std::make_unique(std::forward(args)...); + /// 创建 Unique 的便捷函数 + template + inline Unique makeUnique(Args &&...args) { + return std::make_unique(std::forward(args)...); + } } // --------------------------------------------------------------------------- // 函数别名 // --------------------------------------------------------------------------- -template using Function = std::function; +template using Fn = std::function; // --------------------------------------------------------------------------- // 基础类型别名 // --------------------------------------------------------------------------- -using int8 = std::int8_t; -using int16 = std::int16_t; -using int32 = std::int32_t; -using int64 = std::int64_t; -using uint8 = std::uint8_t; -using uint16 = std::uint16_t; -using uint32 = std::uint32_t; -using uint64 = std::uint64_t; +using i8 = int8_t; +using i16 = int16_t; +using i32 = int32_t; +using i64 = int64_t; +using u8 = uint8_t; +using u16 = uint16_t; +using u32 = uint32_t; +using u64 = uint64_t; +using f32 = float; +using f64 = double; + +// --------------------------------------------------------------------------- +// ID 类型别名 +// --------------------------------------------------------------------------- +using ID = u32; } // namespace extra2d diff --git a/Extra2D/include/extra2d/event/event.h b/Extra2D/include/extra2d/event/event.h index 45eb403..92e00a6 100644 --- a/Extra2D/include/extra2d/event/event.h +++ b/Extra2D/include/extra2d/event/event.h @@ -59,70 +59,70 @@ enum class EventType { // 键盘事件数据 // ============================================================================ struct KeyEvent { - int keyCode; - int scancode; - int mods; // 修饰键 (Shift, Ctrl, Alt, etc.) + i32 key; + i32 scancode; + i32 mods; // 修饰键 (Shift, Ctrl, Alt, etc.) }; // ============================================================================ // 鼠标事件数据 // ============================================================================ struct MouseButtonEvent { - int button; - int mods; - Vec2 position; + i32 button; + i32 mods; + Vec2 pos; }; struct MouseMoveEvent { - Vec2 position; + Vec2 pos; Vec2 delta; }; struct MouseScrollEvent { Vec2 offset; - Vec2 position; + Vec2 pos; }; // ============================================================================ // 窗口事件数据 // ============================================================================ struct WindowResizeEvent { - int width; - int height; + i32 w; + i32 h; }; struct WindowMoveEvent { - int x; - int y; + i32 x; + i32 y; }; // ============================================================================ // 游戏手柄事件数据 // ============================================================================ struct GamepadButtonEvent { - int gamepadId; - int button; + i32 gamepadId; + i32 button; }; struct GamepadAxisEvent { - int gamepadId; - int axis; - float value; + i32 gamepadId; + i32 axis; + f32 value; }; // ============================================================================ // 触摸事件数据 // ============================================================================ struct TouchEvent { - int touchId; - Vec2 position; + i32 touchId; + Vec2 pos; }; // ============================================================================ // 自定义事件数据 // ============================================================================ struct CustomEvent { - uint32_t id; + u32 id; void *data; }; @@ -131,7 +131,7 @@ struct CustomEvent { // ============================================================================ struct Event { EventType type = EventType::None; - double timestamp = 0.0; + f64 timestamp = 0.0; bool handled = false; // 事件数据联合体 @@ -141,32 +141,32 @@ struct Event { data; // 便捷访问方法 - bool isWindowEvent() const { + bool window() const { return type == EventType::WindowClose || type == EventType::WindowResize || type == EventType::WindowFocus || type == EventType::WindowLostFocus || type == EventType::WindowMoved; } - bool isKeyboardEvent() const { + bool keyboard() const { return type == EventType::KeyPressed || type == EventType::KeyReleased || type == EventType::KeyRepeat; } - bool isMouseEvent() const { + bool mouse() const { return type == EventType::MouseButtonPressed || type == EventType::MouseButtonReleased || type == EventType::MouseMoved || type == EventType::MouseScrolled; } // 静态工厂方法 - static Event createWindowResize(int width, int height); - static Event createWindowClose(); - static Event createKeyPress(int keyCode, int scancode, int mods); - static Event createKeyRelease(int keyCode, int scancode, int mods); - static Event createMouseButtonPress(int button, int mods, const Vec2 &pos); - static Event createMouseButtonRelease(int button, int mods, const Vec2 &pos); - static Event createMouseMove(const Vec2 &pos, const Vec2 &delta); - static Event createMouseScroll(const Vec2 &offset, const Vec2 &pos); + static Event windowResize(i32 w, i32 h); + static Event windowClose(); + static Event keyPress(i32 key, i32 scancode, i32 mods); + static Event keyRelease(i32 key, i32 scancode, i32 mods); + static Event mousePress(i32 btn, i32 mods, Vec2 pos); + static Event mouseRelease(i32 btn, i32 mods, Vec2 pos); + static Event mouseMove(Vec2 pos, Vec2 delta); + static Event mouseScroll(Vec2 offset, Vec2 pos); }; } // namespace extra2d diff --git a/Extra2D/include/extra2d/event/event_dispatcher.h b/Extra2D/include/extra2d/event/event_dispatcher.h index 1ddff8a..4d01f25 100644 --- a/Extra2D/include/extra2d/event/event_dispatcher.h +++ b/Extra2D/include/extra2d/event/event_dispatcher.h @@ -11,46 +11,46 @@ namespace extra2d { // ============================================================================ // 事件监听器 ID // ============================================================================ -using ListenerId = uint64_t; +using ListenerID = u64; // ============================================================================ // 事件分发器 // ============================================================================ class EventDispatcher { public: - using EventCallback = std::function; + using EventFn = Fn; EventDispatcher(); ~EventDispatcher() = default; // 添加监听器 - ListenerId addListener(EventType type, EventCallback callback); + ListenerID on(EventType type, EventFn fn); // 移除监听器 - void removeListener(ListenerId id); - void removeAllListeners(EventType type); - void removeAllListeners(); + void off(ListenerID id); + void offAll(EventType type); + void offAll(); // 分发事件 void dispatch(Event &event); void dispatch(const Event &event); // 处理事件队列 - void processQueue(class EventQueue &queue); + void process(class EventQueue &queue); // 统计 - size_t getListenerCount(EventType type) const; - size_t getTotalListenerCount() const; + size_t listenerCount(EventType type) const; + size_t totalListeners() const; private: struct Listener { - ListenerId id; + ListenerID id; EventType type; - EventCallback callback; + EventFn fn; }; std::unordered_map> listeners_; - ListenerId nextId_; + ListenerID nextId_; }; } // namespace extra2d diff --git a/Extra2D/include/extra2d/event/event_queue.h b/Extra2D/include/extra2d/event/event_queue.h index 5ef03bd..2c01fab 100644 --- a/Extra2D/include/extra2d/event/event_queue.h +++ b/Extra2D/include/extra2d/event/event_queue.h @@ -1,9 +1,9 @@ #pragma once +#include #include #include #include -#include namespace extra2d { @@ -12,29 +12,32 @@ namespace extra2d { // ============================================================================ class EventQueue { public: - EventQueue(); - ~EventQueue() = default; + static constexpr size_t DEFAULT_CAPACITY = 1024; - // 添加事件到队列 - void push(const Event &event); - void push(Event &&event); + EventQueue(); + ~EventQueue() = default; - // 从队列取出事件 - bool poll(Event &event); + // 添加事件到队列 + bool push(const Event &event); + bool push(Event &&event); - // 查看队列头部事件(不移除) - bool peek(Event &event) const; + // 从队列取出事件 + bool poll(Event &event); - // 清空队列 - void clear(); + // 查看队列头部事件(不移除) + bool peek(Event &event) const; - // 队列状态 - bool empty() const; - size_t size() const; + // 清空队列 + void clear(); + + // 队列状态 + bool empty() const; + size_t size() const; + size_t capacity() const { return buffer_.capacity(); } private: - std::queue queue_; - mutable std::mutex mutex_; + RingBuffer buffer_; + mutable std::mutex mutex_; // 用于peek和clear的互斥 }; } // namespace extra2d diff --git a/Extra2D/include/extra2d/platform/glfw/glfw_window.h b/Extra2D/include/extra2d/platform/glfw/glfw_window.h index b6d43e8..9843118 100644 --- a/Extra2D/include/extra2d/platform/glfw/glfw_window.h +++ b/Extra2D/include/extra2d/platform/glfw/glfw_window.h @@ -26,9 +26,9 @@ enum class Cursor { */ class GLFWWindow { public: - using ResizeCb = std::function; - using CloseCb = std::function; - using FocusCb = std::function; + using ResizeFn = std::function; + using CloseFn = std::function; + using FocusFn = std::function; GLFWWindow(); ~GLFWWindow(); @@ -36,12 +36,12 @@ public: /** * @brief 创建窗口 * @param title 窗口标题 - * @param width 窗口宽度 - * @param height 窗口高度 + * @param w 窗口宽度 + * @param h 窗口高度 * @param vsync 是否启用垂直同步 * @return 创建是否成功 */ - bool create(const std::string& title, int width, int height, bool vsync = true); + bool create(const std::string& title, int w, int h, bool vsync = true); /** * @brief 销毁窗口 @@ -71,42 +71,42 @@ public: /** * @brief 设置窗口标题 */ - void setTitle(const std::string& title); + void title(const std::string& t); /** * @brief 设置窗口大小 */ - void setSize(int w, int h); + void size(int w, int h); /** * @brief 设置窗口位置 */ - void setPos(int x, int y); + void pos(int x, int y); /** * @brief 设置全屏模式 */ - void setFullscreen(bool fs); + void fullscreen(bool fs); /** * @brief 设置垂直同步 */ - void setVSync(bool vsync); + void vsync(bool v); /** * @brief 设置窗口可见性 */ - void setVisible(bool visible); + void visible(bool v); /** * @brief 获取窗口宽度 */ - int width() const { return width_; } + int w() const { return w_; } /** * @brief 获取窗口高度 */ - int height() const { return height_; } + int h() const { return h_; } /** * @brief 获取窗口大小 @@ -141,17 +141,17 @@ public: /** * @brief 获取内容缩放X */ - float scaleX() const { return scaleX_; } + f32 scaleX() const { return scaleX_; } /** * @brief 获取内容缩放Y */ - float scaleY() const { return scaleY_; } + f32 scaleY() const { return scaleY_; } /** * @brief 设置光标形状 */ - void setCursor(Cursor cursor); + void cursor(Cursor c); /** * @brief 显示/隐藏光标 @@ -166,17 +166,17 @@ public: /** * @brief 设置大小改变回调 */ - void onResize(ResizeCb cb) { resizeCb_ = cb; } + void onResize(ResizeFn fn) { resizeFn_ = fn; } /** * @brief 设置关闭回调 */ - void onClose(CloseCb cb) { closeCb_ = cb; } + void onClose(CloseFn fn) { closeFn_ = fn; } /** * @brief 设置焦点改变回调 */ - void onFocus(FocusCb cb) { focusCb_ = cb; } + void onFocus(FocusFn fn) { focusFn_ = fn; } /** * @brief 获取原生窗口句柄 @@ -186,7 +186,7 @@ public: /** * @brief 获取 GLFW 窗口句柄 */ - GLFWwindow* glfwWindow() const { return glfwWindow_; } + GLFWwindow* handle() const { return handle_; } private: bool initGLFW(); @@ -194,33 +194,33 @@ private: void updateContentScale(); // GLFW 回调函数(静态) - static void framebufferSizeCallback(GLFWwindow* window, int width, int height); + static void framebufferSizeCallback(GLFWwindow* window, int w, int h); static void windowCloseCallback(GLFWwindow* window); static void windowFocusCallback(GLFWwindow* window, int focused); static void windowIconifyCallback(GLFWwindow* window, int iconified); - static void cursorPosCallback(GLFWwindow* window, double xpos, double ypos); - static void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods); - static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset); + static void cursorPosCallback(GLFWwindow* window, double x, double y); + static void mouseButtonCallback(GLFWwindow* window, int btn, int action, int mods); + static void scrollCallback(GLFWwindow* window, double x, double y); static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); static void joystickCallback(int jid, int event); - GLFWwindow* glfwWindow_ = nullptr; + GLFWwindow* handle_ = nullptr; - int width_ = 1280; - int height_ = 720; + int w_ = 1280; + int h_ = 720; bool fullscreen_ = false; bool vsync_ = true; bool focused_ = true; bool minimized_ = false; bool shouldClose_ = false; - float scaleX_ = 1.0f; - float scaleY_ = 1.0f; + f32 scaleX_ = 1.0f; + f32 scaleY_ = 1.0f; bool cursorVisible_ = true; bool cursorLocked_ = false; - ResizeCb resizeCb_; - CloseCb closeCb_; - FocusCb focusCb_; + ResizeFn resizeFn_; + CloseFn closeFn_; + FocusFn focusFn_; }; } // namespace extra2d diff --git a/Extra2D/include/extra2d/platform/window_module.h b/Extra2D/include/extra2d/platform/window_module.h index b5407f7..e9b5dd9 100644 --- a/Extra2D/include/extra2d/platform/window_module.h +++ b/Extra2D/include/extra2d/platform/window_module.h @@ -52,7 +52,7 @@ public: private: WindowCfg cfg_; - UniquePtr win_; + Unique win_; bool initialized_ = false; }; diff --git a/Extra2D/include/extra2d/services/event_service.h b/Extra2D/include/extra2d/services/event_service.h index 7235aff..ae60d64 100644 --- a/Extra2D/include/extra2d/services/event_service.h +++ b/Extra2D/include/extra2d/services/event_service.h @@ -14,21 +14,21 @@ class IEventService : public IService { public: virtual ~IEventService() = default; - virtual void pushEvent(const Event& event) = 0; - virtual void pushEvent(Event&& event) = 0; - virtual bool pollEvent(Event& event) = 0; + virtual void push(const Event& event) = 0; + virtual void push(Event&& event) = 0; + virtual bool poll(Event& event) = 0; - virtual ListenerId addListener(EventType type, EventDispatcher::EventCallback callback) = 0; - virtual void removeListener(ListenerId id) = 0; - virtual void removeAllListeners(EventType type) = 0; - virtual void removeAllListeners() = 0; + virtual ListenerID on(EventType type, EventDispatcher::EventFn fn) = 0; + virtual void off(ListenerID id) = 0; + virtual void offAll(EventType type) = 0; + virtual void offAll() = 0; virtual void dispatch(Event& event) = 0; - virtual void processQueue() = 0; + virtual void process() = 0; - virtual size_t getListenerCount(EventType type) const = 0; - virtual size_t getTotalListenerCount() const = 0; - virtual size_t getQueueSize() const = 0; + virtual size_t listenerCount(EventType type) const = 0; + virtual size_t totalListeners() const = 0; + virtual size_t queueSize() const = 0; }; /** @@ -39,32 +39,32 @@ public: EventService(); ~EventService() override = default; - ServiceInfo getServiceInfo() const override; + ServiceInfo info() const override; - bool initialize() override; + bool init() override; void shutdown() override; - void update(float deltaTime) override; + void update(f32 dt) override; - void pushEvent(const Event& event) override; - void pushEvent(Event&& event) override; - bool pollEvent(Event& event) override; + void push(const Event& event) override; + void push(Event&& event) override; + bool poll(Event& event) override; - ListenerId addListener(EventType type, EventDispatcher::EventCallback callback) override; - void removeListener(ListenerId id) override; - void removeAllListeners(EventType type) override; - void removeAllListeners() override; + ListenerID on(EventType type, EventDispatcher::EventFn fn) override; + void off(ListenerID id) override; + void offAll(EventType type) override; + void offAll() override; void dispatch(Event& event) override; - void processQueue() override; + void process() override; - size_t getListenerCount(EventType type) const override; - size_t getTotalListenerCount() const override; - size_t getQueueSize() const override; + size_t listenerCount(EventType type) const override; + size_t totalListeners() const override; + size_t queueSize() const override; - EventQueue& getQueue() { return queue_; } - const EventQueue& getQueue() const { return queue_; } - EventDispatcher& getDispatcher() { return dispatcher_; } - const EventDispatcher& getDispatcher() const { return dispatcher_; } + EventQueue& queue() { return queue_; } + const EventQueue& queue() const { return queue_; } + EventDispatcher& dispatcher() { return dispatcher_; } + const EventDispatcher& dispatcher() const { return dispatcher_; } private: EventQueue queue_; diff --git a/Extra2D/include/extra2d/services/logger_service.h b/Extra2D/include/extra2d/services/logger_service.h index f40d8f0..a3333ad 100644 --- a/Extra2D/include/extra2d/services/logger_service.h +++ b/Extra2D/include/extra2d/services/logger_service.h @@ -13,10 +13,10 @@ namespace extra2d { * @brief 日志颜色结构 */ struct LogColor { - uint8_t r, g, b; + u8 r, g, b; constexpr LogColor() : r(255), g(255), b(255) {} - constexpr LogColor(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b) {} + constexpr LogColor(u8 r, u8 g, u8 b) : r(r), g(g), b(b) {} static constexpr LogColor White() { return LogColor(255, 255, 255); } static constexpr LogColor Gray() { return LogColor(128, 128, 128); } @@ -68,27 +68,27 @@ public: /** * @brief 设置日志级别 */ - virtual void setLevel(LogLevel level) = 0; + virtual void level(LogLevel lvl) = 0; /** * @brief 获取日志级别 */ - virtual LogLevel getLevel() const = 0; + virtual LogLevel level() const = 0; /** * @brief 检查日志级别是否启用 */ - virtual bool isEnabled(LogLevel level) const = 0; + virtual bool enabled(LogLevel lvl) const = 0; /** * @brief 记录日志(格式化) */ - virtual void log(LogLevel level, const char *fmt, ...) = 0; + virtual void log(LogLevel lvl, const char *fmt, ...) = 0; /** * @brief 记录日志(字符串) */ - virtual void log(LogLevel level, const std::string &msg) = 0; + virtual void log(LogLevel lvl, const std::string &msg) = 0; /** * @brief Trace级别日志 @@ -127,35 +127,35 @@ public: /** * @brief 设置日志级别颜色 - * @param level 日志级别 - * @param color 颜色 + * @param lvl 日志级别 + * @param c 颜色 */ - virtual void setLevelColor(LogLevel level, const LogColor &color) = 0; + virtual void levelColor(LogLevel lvl, const LogColor &c) = 0; /** * @brief 获取日志级别颜色 - * @param level 日志级别 + * @param lvl 日志级别 * @return 颜色 */ - virtual LogColor getLevelColor(LogLevel level) const = 0; + virtual LogColor levelColor(LogLevel lvl) const = 0; /** * @brief 启用/禁用颜色输出 - * @param enabled 是否启用 + * @param on 是否启用 */ - virtual void setColorEnabled(bool enabled) = 0; + virtual void colors(bool on) = 0; /** * @brief 是否启用颜色输出 */ - virtual bool isColorEnabled() const = 0; + virtual bool colors() const = 0; - ServiceInfo getServiceInfo() const override { - ServiceInfo info; - info.name = "Logger"; - info.priority = ServicePriority::Core; - info.enabled = true; - return info; + ServiceInfo info() const override { + ServiceInfo i; + i.name = "Logger"; + i.priority = ServicePriority::Core; + i.enabled = true; + return i; } }; @@ -167,15 +167,15 @@ public: ConsoleLogger(); ~ConsoleLogger() override; - bool initialize() override; + bool init() override; void shutdown() override; - void setLevel(LogLevel level) override; - LogLevel getLevel() const override; - bool isEnabled(LogLevel level) const override; + void level(LogLevel lvl) override; + LogLevel level() const override; + bool enabled(LogLevel lvl) const override; - void log(LogLevel level, const char *fmt, ...) override; - void log(LogLevel level, const std::string &msg) override; + void log(LogLevel lvl, const char *fmt, ...) override; + void log(LogLevel lvl, const std::string &msg) override; void trace(const char *fmt, ...) override; void debug(const char *fmt, ...) override; @@ -185,21 +185,21 @@ public: void error(const char *fmt, ...) override; void fatal(const char *fmt, ...) override; - void setLevelColor(LogLevel level, const LogColor &color) override; - LogColor getLevelColor(LogLevel level) const override; - void setColorEnabled(bool enabled) override; - bool isColorEnabled() const override; + void levelColor(LogLevel lvl, const LogColor &c) override; + LogColor levelColor(LogLevel lvl) const override; + void colors(bool on) override; + bool colors() const override; private: - void output(LogLevel level, const char *msg); - const char *getLevelString(LogLevel level); - std::string getAnsiColor(LogLevel level); + void output(LogLevel lvl, const char *msg); + const char *levelString(LogLevel lvl); + std::string ansiColor(LogLevel lvl); LogLevel level_; - bool colorEnabled_; + bool colors_; LogColor levelColors_[7]; class Impl; - UniquePtr impl_; + Unique impl_; // 服务注册元数据 E2D_AUTO_REGISTER_SERVICE(ILogger, ConsoleLogger); @@ -262,12 +262,12 @@ std::string format_str(const char *fmt, Args &&...args) { } // namespace extra2d // 便捷宏 - 自动获取日志服务 -#define E2D_LOG(level, ...) \ +#define E2D_LOG(lvl, ...) \ do { \ if (auto logService = ::extra2d::ServiceLocator::instance() \ - .tryGetService<::extra2d::ILogger>()) { \ - if (logService->isEnabled(level)) { \ - logService->log(level, ::extra2d::format_str(__VA_ARGS__)); \ + .tryGet<::extra2d::ILogger>()) { \ + if (logService->enabled(lvl)) { \ + logService->log(lvl, ::extra2d::format_str(__VA_ARGS__)); \ } \ } \ } while (0) @@ -281,30 +281,30 @@ std::string format_str(const char *fmt, Args &&...args) { #define E2D_FATAL(...) E2D_LOG(::extra2d::LogLevel::Fatal, __VA_ARGS__) // 带颜色参数的日志宏 -#define E2D_LOG_COLOR(level, color, ...) \ +#define E2D_LOG_COLOR(lvl, c, ...) \ do { \ if (auto logService = ::extra2d::ServiceLocator::instance() \ - .tryGetService<::extra2d::ILogger>()) { \ - if (logService->isEnabled(level)) { \ - auto prevColor = logService->getLevelColor(level); \ - logService->setLevelColor(level, color); \ - logService->log(level, ::extra2d::format_str(__VA_ARGS__)); \ - logService->setLevelColor(level, prevColor); \ + .tryGet<::extra2d::ILogger>()) { \ + if (logService->enabled(lvl)) { \ + auto prevColor = logService->levelColor(lvl); \ + logService->levelColor(lvl, c); \ + logService->log(lvl, ::extra2d::format_str(__VA_ARGS__)); \ + logService->levelColor(lvl, prevColor); \ } \ } \ } while (0) -#define E2D_TRACE_COLOR(color, ...) \ - E2D_LOG_COLOR(::extra2d::LogLevel::Trace, color, __VA_ARGS__) -#define E2D_DEBUG_COLOR(color, ...) \ - E2D_LOG_COLOR(::extra2d::LogLevel::Debug, color, __VA_ARGS__) -#define E2D_INFO_COLOR(color, ...) \ - E2D_LOG_COLOR(::extra2d::LogLevel::Info, color, __VA_ARGS__) -#define E2D_REGISTRY_COLOR(color, ...) \ - E2D_LOG_COLOR(::extra2d::LogLevel::Registry, color, __VA_ARGS__) -#define E2D_WARN_COLOR(color, ...) \ - E2D_LOG_COLOR(::extra2d::LogLevel::Warn, color, __VA_ARGS__) -#define E2D_ERROR_COLOR(color, ...) \ - E2D_LOG_COLOR(::extra2d::LogLevel::Error, color, __VA_ARGS__) -#define E2D_FATAL_COLOR(color, ...) \ - E2D_LOG_COLOR(::extra2d::LogLevel::Fatal, color, __VA_ARGS__) +#define E2D_TRACE_COLOR(c, ...) \ + E2D_LOG_COLOR(::extra2d::LogLevel::Trace, c, __VA_ARGS__) +#define E2D_DEBUG_COLOR(c, ...) \ + E2D_LOG_COLOR(::extra2d::LogLevel::Debug, c, __VA_ARGS__) +#define E2D_INFO_COLOR(c, ...) \ + E2D_LOG_COLOR(::extra2d::LogLevel::Info, c, __VA_ARGS__) +#define E2D_REGISTRY_COLOR(c, ...) \ + E2D_LOG_COLOR(::extra2d::LogLevel::Registry, c, __VA_ARGS__) +#define E2D_WARN_COLOR(c, ...) \ + E2D_LOG_COLOR(::extra2d::LogLevel::Warn, c, __VA_ARGS__) +#define E2D_ERROR_COLOR(c, ...) \ + E2D_LOG_COLOR(::extra2d::LogLevel::Error, c, __VA_ARGS__) +#define E2D_FATAL_COLOR(c, ...) \ + E2D_LOG_COLOR(::extra2d::LogLevel::Fatal, c, __VA_ARGS__) diff --git a/Extra2D/include/extra2d/services/timer_service.h b/Extra2D/include/extra2d/services/timer_service.h index bf2d65d..c7b112b 100644 --- a/Extra2D/include/extra2d/services/timer_service.h +++ b/Extra2D/include/extra2d/services/timer_service.h @@ -13,13 +13,13 @@ class ITimerService : public IService { public: virtual ~ITimerService() = default; - virtual uint32 addTimer(float delay, Timer::Callback callback) = 0; - virtual uint32 addRepeatingTimer(float interval, Timer::Callback callback) = 0; - virtual void cancelTimer(uint32 timerId) = 0; - virtual void pauseTimer(uint32 timerId) = 0; - virtual void resumeTimer(uint32 timerId) = 0; + virtual u32 add(f32 delay, Timer::Fn fn) = 0; + virtual u32 addRepeat(f32 interval, Timer::Fn fn) = 0; + virtual void cancel(u32 timerId) = 0; + virtual void pauseTimer(u32 timerId) = 0; + virtual void resumeTimer(u32 timerId) = 0; virtual void clear() = 0; - virtual size_t getTimerCount() const = 0; + virtual size_t count() const = 0; }; /** @@ -30,25 +30,25 @@ public: TimerService(); ~TimerService() override = default; - ServiceInfo getServiceInfo() const override; + ServiceInfo info() const override; - bool initialize() override; + bool init() override; void shutdown() override; - void update(float deltaTime) override; + void update(f32 dt) override; - uint32 addTimer(float delay, Timer::Callback callback) override; - uint32 addRepeatingTimer(float interval, Timer::Callback callback) override; - void cancelTimer(uint32 timerId) override; - void pauseTimer(uint32 timerId) override; - void resumeTimer(uint32 timerId) override; + u32 add(f32 delay, Timer::Fn fn) override; + u32 addRepeat(f32 interval, Timer::Fn fn) override; + void cancel(u32 timerId) override; + void pauseTimer(u32 timerId) override; + void resumeTimer(u32 timerId) override; void clear() override; - size_t getTimerCount() const override; + size_t count() const override; - TimerManager& getManager() { return manager_; } - const TimerManager& getManager() const { return manager_; } + TimerManager& mgr() { return mgr_; } + const TimerManager& mgr() const { return mgr_; } private: - TimerManager manager_; + TimerManager mgr_; // 服务注册元数据 E2D_AUTO_REGISTER_SERVICE(ITimerService, TimerService); diff --git a/Extra2D/include/extra2d/utils/random.h b/Extra2D/include/extra2d/utils/random.h index 55b76e8..3c5e768 100644 --- a/Extra2D/include/extra2d/utils/random.h +++ b/Extra2D/include/extra2d/utils/random.h @@ -14,34 +14,34 @@ public: static Random &get(); /// 设置随机种子 - void setSeed(uint32 seed); + void seed(u32 s); /// 使用当前时间作为种子 void randomize(); /// 获取 [0, 1) 范围内的随机浮点数 - float getFloat(); + f32 randomF32(); /// 获取 [min, max] 范围内的随机浮点数 - float getFloat(float min, float max); + f32 randomF32(f32 min, f32 max); /// 获取 [0, max] 范围内的随机整数 - int getInt(int max); + i32 randomI32(i32 max); /// 获取 [min, max] 范围内的随机整数 - int getInt(int min, int max); + i32 randomI32(i32 min, i32 max); /// 获取随机布尔值 - bool getBool(); + bool boolean(); /// 获取随机布尔值(带概率) - bool getBool(float probability); + bool boolean(f32 probability); /// 获取指定范围内的随机角度(弧度) - float getAngle(); + f32 angle(); /// 获取 [-1, 1] 范围内的随机数(用于方向) - float getSigned(); + f32 signedF32(); private: Random(); @@ -59,27 +59,27 @@ private: // ============================================================================ /// 获取 [0, 1) 范围内的随机浮点数 -inline float randomFloat() { return Random::get().getFloat(); } +inline f32 randF32() { return Random::get().randomF32(); } /// 获取 [min, max] 范围内的随机浮点数 -inline float randomFloat(float min, float max) { - return Random::get().getFloat(min, max); +inline f32 randF32(f32 min, f32 max) { + return Random::get().randomF32(min, max); } /// 获取 [0, max] 范围内的随机整数 -inline int randomInt(int max) { return Random::get().getInt(max); } +inline i32 randI32(i32 max) { return Random::get().randomI32(max); } /// 获取 [min, max] 范围内的随机整数 -inline int randomInt(int min, int max) { - return Random::get().getInt(min, max); +inline i32 randI32(i32 min, i32 max) { + return Random::get().randomI32(min, max); } /// 获取随机布尔值 -inline bool randomBool() { return Random::get().getBool(); } +inline bool randBool() { return Random::get().boolean(); } /// 获取随机布尔值(带概率) -inline bool randomBool(float probability) { - return Random::get().getBool(probability); +inline bool randBool(f32 probability) { + return Random::get().boolean(probability); } } // namespace extra2d diff --git a/Extra2D/include/extra2d/utils/timer.h b/Extra2D/include/extra2d/utils/timer.h index e220a8b..2bb0514 100644 --- a/Extra2D/include/extra2d/utils/timer.h +++ b/Extra2D/include/extra2d/utils/timer.h @@ -16,12 +16,12 @@ public: using Clock = std::chrono::steady_clock; using TimePoint = Clock::time_point; using Duration = Clock::duration; - using Callback = Function; + using Fn = std::function; - Timer(float interval, bool repeat, Callback callback); + Timer(f32 interval, bool repeat, Fn fn); /// 更新计时器,返回 true 如果触发了回调 - bool update(float deltaTime); + bool update(f32 dt); /// 重置计时器 void reset(); @@ -36,27 +36,27 @@ public: void cancel(); /// 是否有效 - bool isValid() const { return valid_; } + bool valid() const { return valid_; } /// 是否暂停 - bool isPaused() const { return paused_; } + bool paused() const { return paused_; } /// 获取剩余时间(秒) - float getRemaining() const; + f32 remaining() const; /// 获取唯一ID - uint32 getId() const { return id_; } + u32 id() const { return id_; } private: - uint32 id_; - float interval_; - float elapsed_; + u32 id_; + f32 interval_; + f32 elapsed_; bool repeat_; bool paused_; bool valid_; - Callback callback_; + Fn fn_; - static uint32 nextId_; + static u32 nextId_; }; // ============================================================================ @@ -68,32 +68,32 @@ public: ~TimerManager() = default; /// 创建单次计时器,返回计时器ID - uint32 addTimer(float delay, Timer::Callback callback); + u32 add(f32 delay, Timer::Fn fn); /// 创建重复计时器,返回计时器ID - uint32 addRepeatingTimer(float interval, Timer::Callback callback); + u32 addRepeat(f32 interval, Timer::Fn fn); /// 取消指定ID的计时器 - void cancelTimer(uint32 timerId); + void cancel(u32 timerId); /// 暂停指定ID的计时器 - void pauseTimer(uint32 timerId); + void pause(u32 timerId); /// 恢复指定ID的计时器 - void resumeTimer(uint32 timerId); + void resume(u32 timerId); /// 更新所有计时器(每帧调用) - void update(float deltaTime); + void update(f32 dt); /// 清除所有计时器 void clear(); /// 获取计时器数量 - size_t getTimerCount() const { return timers_.size(); } + size_t count() const { return timers_.size(); } private: - std::map> timers_; - std::vector timersToRemove_; + std::map> timers_; + std::vector timersToRemove_; }; } // namespace extra2d diff --git a/Extra2D/src/app/application.cpp b/Extra2D/src/app/application.cpp index 8cd5e87..7818fd4 100644 --- a/Extra2D/src/app/application.cpp +++ b/Extra2D/src/app/application.cpp @@ -8,17 +8,17 @@ namespace extra2d { -static double getTimeSeconds() { +static f64 getTimeSeconds() { #ifdef __SWITCH__ struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); - return static_cast(ts.tv_sec) + - static_cast(ts.tv_nsec) / 1000000000.0; + return static_cast(ts.tv_sec) + + static_cast(ts.tv_nsec) / 1000000000.0; #else using namespace std::chrono; auto now = steady_clock::now(); auto duration = now.time_since_epoch(); - return duration_cast>(duration).count(); + return duration_cast>(duration).count(); #endif } @@ -47,7 +47,7 @@ bool Application::init() { } // 初始化所有服务 - ServiceLocator::instance().initializeAll(); + ServiceLocator::instance().init(); initialized_ = true; running_ = true; @@ -58,7 +58,7 @@ void Application::shutdown() { if (!initialized_) return; - ServiceLocator::instance().shutdownAll(); + ServiceLocator::instance().shutdown(); ServiceLocator::instance().clear(); Registry::instance().shutdown(); Registry::instance().clear(); @@ -90,29 +90,29 @@ void Application::quit() { void Application::pause() { if (!paused_) { paused_ = true; - ServiceLocator::instance().pauseAll(); + ServiceLocator::instance().pause(); } } void Application::resume() { if (paused_) { paused_ = false; - ServiceLocator::instance().resumeAll(); + ServiceLocator::instance().resume(); lastFrameTime_ = getTimeSeconds(); } } void Application::mainLoop() { - double currentTime = getTimeSeconds(); - deltaTime_ = static_cast(currentTime - lastFrameTime_); + f64 currentTime = getTimeSeconds(); + dt_ = static_cast(currentTime - lastFrameTime_); lastFrameTime_ = currentTime; - totalTime_ += deltaTime_; + totalTime_ += dt_; frameCount_++; - fpsTimer_ += deltaTime_; + fpsTimer_ += dt_; if (fpsTimer_ >= 1.0f) { - currentFps_ = frameCount_; + fps_ = frameCount_; frameCount_ = 0; fpsTimer_ -= 1.0f; } @@ -122,9 +122,9 @@ void Application::mainLoop() { winMod->win()->poll(); } - auto eventService = ServiceLocator::instance().getService(); + auto eventService = ServiceLocator::instance().get(); if (eventService) { - eventService->processQueue(); + eventService->process(); } if (!paused_) { @@ -134,7 +134,7 @@ void Application::mainLoop() { render(); } -void Application::update() { ServiceLocator::instance().updateAll(deltaTime_); } +void Application::update() { ServiceLocator::instance().update(dt_); } void Application::render() { auto *winMod = get(); diff --git a/Extra2D/src/core/registry.cpp b/Extra2D/src/core/registry.cpp index c3b5c98..9ee33d2 100644 --- a/Extra2D/src/core/registry.cpp +++ b/Extra2D/src/core/registry.cpp @@ -11,8 +11,8 @@ Registry &Registry::instance() { } bool Registry::init() { - auto levels = groupByLevel(); - E2D_REGISTRY("正在初始化 {} 个模块,共 {} 个层级...", modules_.size(), + auto levels = group(); + E2D_REGISTRY("正在初始化 {} 个模块,共 {} 个层级...", moduleCount_, levels.size()); for (size_t level = 0; level < levels.size(); ++level) { @@ -21,7 +21,7 @@ bool Registry::init() { // 检查当前层级是否有支持并行初始化的模块 bool hasParallelModules = false; for (auto *module : modules) { - if (module->allowParallelInit()) { + if (module->parallel()) { hasParallelModules = true; break; } @@ -49,7 +49,7 @@ bool Registry::init() { // 分离支持并行和不支持并行的模块 for (auto *module : modules) { - if (module->allowParallelInit()) { + if (module->parallel()) { futures.push_back(std::async(std::launch::async, [module]() { return std::make_pair(module, module->init()); })); @@ -88,52 +88,67 @@ bool Registry::init() { } void Registry::shutdown() { - auto sorted = topologicalSort(); - - for (auto it = sorted.rbegin(); it != sorted.rend(); ++it) { - (*it)->shutdown(); + // 从后向前关闭模块 + for (size_t i = moduleCount_; i > 0; --i) { + if (modules_[i - 1].valid && modules_[i - 1].module) { + modules_[i - 1].module->shutdown(); + } } } void Registry::clear() { shutdown(); - modules_.clear(); + + // 销毁所有模块 + for (size_t i = 0; i < moduleCount_; ++i) { + modules_[i].module.reset(); + modules_[i].valid = false; + } + moduleCount_ = 0; } -std::vector Registry::topologicalSort() { +std::vector Registry::sort() { std::vector result; - std::unordered_map inDegree; - std::unordered_map> adj; + std::vector inDegree(moduleCount_, 0); + std::vector> adj(moduleCount_); - for (auto &[typeIdx, module] : modules_) { - inDegree[module.get()] = 0; - } - - for (auto &[typeIdx, module] : modules_) { - for (auto &depType : module->deps()) { - Module *dep = get(depType); - if (dep) { - adj[dep].push_back(module.get()); - inDegree[module.get()]++; + // 构建依赖图 + for (size_t i = 0; i < moduleCount_; ++i) { + if (!modules_[i].valid) + continue; + + auto deps = modules_[i].module->deps(); + for (auto &depType : deps) { + // 查找依赖模块的索引 + for (size_t j = 0; j < moduleCount_; ++j) { + if (modules_[j].valid && + std::type_index(typeid(*modules_[j].module)) == depType) { + adj[j].push_back(i); + inDegree[i]++; + break; + } } } } - auto cmp = [](Module *a, Module *b) { return a->priority() > b->priority(); }; - std::priority_queue, decltype(cmp)> pq(cmp); + // 使用优先队列,按优先级排序 + auto cmp = [this](size_t a, size_t b) { + return modules_[a].module->priority() > modules_[b].module->priority(); + }; + std::priority_queue, decltype(cmp)> pq(cmp); - for (auto &[mod, degree] : inDegree) { - if (degree == 0) { - pq.push(mod); + for (size_t i = 0; i < moduleCount_; ++i) { + if (inDegree[i] == 0) { + pq.push(i); } } while (!pq.empty()) { - Module *curr = pq.top(); + size_t curr = pq.top(); pq.pop(); - result.push_back(curr); + result.push_back(modules_[curr].module.get()); - for (Module *next : adj[curr]) { + for (size_t next : adj[curr]) { inDegree[next]--; if (inDegree[next] == 0) { pq.push(next); @@ -144,42 +159,44 @@ std::vector Registry::topologicalSort() { return result; } -std::vector> Registry::groupByLevel() { +std::vector> Registry::group() { std::vector> levels; - std::unordered_map inDegree; - std::unordered_map> adj; - std::unordered_map levelMap; - - // 初始化入度 - for (auto &[typeIdx, module] : modules_) { - inDegree[module.get()] = 0; - } + std::vector inDegree(moduleCount_, 0); + std::vector> adj(moduleCount_); // 构建依赖图 - for (auto &[typeIdx, module] : modules_) { - for (auto &depType : module->deps()) { - Module *dep = get(depType); - if (dep) { - adj[dep].push_back(module.get()); - inDegree[module.get()]++; + for (size_t i = 0; i < moduleCount_; ++i) { + if (!modules_[i].valid) + continue; + + auto deps = modules_[i].module->deps(); + for (auto &depType : deps) { + for (size_t j = 0; j < moduleCount_; ++j) { + if (modules_[j].valid && + std::type_index(typeid(*modules_[j].module)) == depType) { + adj[j].push_back(i); + inDegree[i]++; + break; + } } } } // 使用 BFS 按层级分组 - std::queue q; + std::queue q; + std::vector levelMap(moduleCount_, -1); // 找到所有入度为 0 的模块(第一层) - for (auto &[mod, degree] : inDegree) { - if (degree == 0) { - q.push(mod); - levelMap[mod] = 0; + for (size_t i = 0; i < moduleCount_; ++i) { + if (inDegree[i] == 0) { + q.push(i); + levelMap[i] = 0; } } // BFS 遍历 while (!q.empty()) { - Module *curr = q.front(); + size_t curr = q.front(); q.pop(); int currLevel = levelMap[curr]; @@ -188,10 +205,10 @@ std::vector> Registry::groupByLevel() { if (levels.size() <= static_cast(currLevel)) { levels.resize(currLevel + 1); } - levels[currLevel].push_back(curr); + levels[currLevel].push_back(modules_[curr].module.get()); // 处理依赖当前模块的其他模块 - for (Module *next : adj[curr]) { + for (size_t next : adj[curr]) { inDegree[next]--; if (inDegree[next] == 0) { q.push(next); diff --git a/Extra2D/src/core/service_locator.cpp b/Extra2D/src/core/service_locator.cpp index 24d1644..94ca13c 100644 --- a/Extra2D/src/core/service_locator.cpp +++ b/Extra2D/src/core/service_locator.cpp @@ -8,34 +8,34 @@ ServiceLocator& ServiceLocator::instance() { return instance; } -bool ServiceLocator::initializeAll() { - std::lock_guard lock(mutex_); +bool ServiceLocator::init() { + std::shared_lock lock(mutex_); - for (auto& service : orderedServices_) { - if (!service) continue; + for (auto& svc : orderedServices_) { + if (!svc) continue; - auto info = service->getServiceInfo(); + auto info = svc->info(); if (!info.enabled) continue; - if (!service->isInitialized()) { - service->setState(ServiceState::Initializing); - if (!service->initialize()) { - service->setState(ServiceState::Stopped); + if (!svc->initialized()) { + svc->setState(ServiceState::Initializing); + if (!svc->init()) { + svc->setState(ServiceState::Stopped); return false; } - service->setState(ServiceState::Running); + svc->setState(ServiceState::Running); } } return true; } -void ServiceLocator::shutdownAll() { - std::lock_guard lock(mutex_); +void ServiceLocator::shutdown() { + std::shared_lock lock(mutex_); for (auto it = orderedServices_.rbegin(); it != orderedServices_.rend(); ++it) { - if (*it && (*it)->isInitialized()) { + if (*it && (*it)->initialized()) { (*it)->setState(ServiceState::Stopping); (*it)->shutdown(); (*it)->setState(ServiceState::Stopped); @@ -43,50 +43,50 @@ void ServiceLocator::shutdownAll() { } } -void ServiceLocator::updateAll(float deltaTime) { - std::lock_guard lock(mutex_); +void ServiceLocator::update(f32 dt) { + std::shared_lock lock(mutex_); - for (auto& service : orderedServices_) { - if (service && service->isInitialized()) { - auto state = service->getState(); + for (auto& svc : orderedServices_) { + if (svc && svc->initialized()) { + auto state = svc->state(); if (state == ServiceState::Running) { - service->update(deltaTime); + svc->update(dt); } } } } -void ServiceLocator::pauseAll() { - std::lock_guard lock(mutex_); +void ServiceLocator::pause() { + std::shared_lock lock(mutex_); - for (auto& service : orderedServices_) { - if (service && service->isInitialized()) { - service->pause(); + for (auto& svc : orderedServices_) { + if (svc && svc->initialized()) { + svc->pause(); } } } -void ServiceLocator::resumeAll() { - std::lock_guard lock(mutex_); +void ServiceLocator::resume() { + std::shared_lock lock(mutex_); - for (auto& service : orderedServices_) { - if (service && service->isInitialized()) { - service->resume(); + for (auto& svc : orderedServices_) { + if (svc && svc->initialized()) { + svc->resume(); } } } -std::vector> ServiceLocator::getAllServices() const { - std::lock_guard lock(mutex_); +std::vector> ServiceLocator::all() const { + std::shared_lock lock(mutex_); return orderedServices_; } void ServiceLocator::clear() { - std::lock_guard lock(mutex_); + std::unique_lock lock(mutex_); for (auto it = orderedServices_.rbegin(); it != orderedServices_.rend(); ++it) { - if (*it && (*it)->isInitialized()) { + if (*it && (*it)->initialized()) { (*it)->setState(ServiceState::Stopping); (*it)->shutdown(); (*it)->setState(ServiceState::Stopped); @@ -98,12 +98,12 @@ void ServiceLocator::clear() { orderedServices_.clear(); } -void ServiceLocator::sortServices() { +void ServiceLocator::sort() { std::stable_sort(orderedServices_.begin(), orderedServices_.end(), - [](const SharedPtr& a, const SharedPtr& b) { + [](const Ref& a, const Ref& b) { if (!a || !b) return false; - return static_cast(a->getServiceInfo().priority) < - static_cast(b->getServiceInfo().priority); + return static_cast(a->info().priority) < + static_cast(b->info().priority); }); } diff --git a/Extra2D/src/core/service_registry.cpp b/Extra2D/src/core/service_registry.cpp index 752de31..f238a76 100644 --- a/Extra2D/src/core/service_registry.cpp +++ b/Extra2D/src/core/service_registry.cpp @@ -7,7 +7,7 @@ ServiceRegistry& ServiceRegistry::instance() { return instance; } -void ServiceRegistry::setServiceEnabled(const std::string& name, bool enabled) { +void ServiceRegistry::setEnabled(const std::string& name, bool enabled) { for (auto& reg : registrations_) { if (reg.name == name) { reg.enabled = enabled; @@ -16,7 +16,7 @@ void ServiceRegistry::setServiceEnabled(const std::string& name, bool enabled) { } } -void ServiceRegistry::createAllServices() { +void ServiceRegistry::createAll() { std::sort(registrations_.begin(), registrations_.end(), [](const ServiceRegistration& a, const ServiceRegistration& b) { return static_cast(a.priority) < static_cast(b.priority); @@ -29,7 +29,7 @@ void ServiceRegistry::createAllServices() { auto service = reg.factory(); if (service) { - ServiceLocator::instance().registerService(service); + ServiceLocator::instance().add(service); } } } diff --git a/Extra2D/src/event/event.cpp b/Extra2D/src/event/event.cpp index 3e01ed9..9908f3d 100644 --- a/Extra2D/src/event/event.cpp +++ b/Extra2D/src/event/event.cpp @@ -2,129 +2,55 @@ namespace extra2d { -/** - * @brief 创建窗口大小改变事件 - * - * 创建一个表示窗口尺寸变化的Event对象 - * - * @param width 新的窗口宽度(像素) - * @param height 新的窗口高度(像素) - * @return 包含窗口大小改变信息的Event对象 - */ -Event Event::createWindowResize(int width, int height) { +Event Event::windowResize(i32 w, i32 h) { Event event; event.type = EventType::WindowResize; - event.data = WindowResizeEvent{width, height}; + event.data = WindowResizeEvent{w, h}; return event; } -/** - * @brief 创建窗口关闭事件 - * - * 创建一个表示窗口请求关闭的Event对象 - * - * @return 表示窗口关闭请求的Event对象 - */ -Event Event::createWindowClose() { +Event Event::windowClose() { Event event; event.type = EventType::WindowClose; return event; } -/** - * @brief 创建键盘按键按下事件 - * - * 创建一个表示键盘按键被按下的Event对象 - * - * @param keyCode 按键码 - * @param scancode 扫描码 - * @param mods 修饰键状态(如Shift、Ctrl等) - * @return 包含按键按下信息的Event对象 - */ -Event Event::createKeyPress(int keyCode, int scancode, int mods) { +Event Event::keyPress(i32 key, i32 scancode, i32 mods) { Event event; event.type = EventType::KeyPressed; - event.data = KeyEvent{keyCode, scancode, mods}; + event.data = KeyEvent{key, scancode, mods}; return event; } -/** - * @brief 创建键盘按键释放事件 - * - * 创建一个表示键盘按键被释放的Event对象 - * - * @param keyCode 按键码 - * @param scancode 扫描码 - * @param mods 修饰键状态(如Shift、Ctrl等) - * @return 包含按键释放信息的Event对象 - */ -Event Event::createKeyRelease(int keyCode, int scancode, int mods) { +Event Event::keyRelease(i32 key, i32 scancode, i32 mods) { Event event; event.type = EventType::KeyReleased; - event.data = KeyEvent{keyCode, scancode, mods}; + event.data = KeyEvent{key, scancode, mods}; return event; } -/** - * @brief 创建鼠标按钮按下事件 - * - * 创建一个表示鼠标按钮被按下的Event对象 - * - * @param button 鼠标按钮编号 - * @param mods 修饰键状态 - * @param pos 鼠标按下时的位置坐标 - * @return 包含鼠标按钮按下信息的Event对象 - */ -Event Event::createMouseButtonPress(int button, int mods, const Vec2 &pos) { +Event Event::mousePress(i32 btn, i32 mods, Vec2 pos) { Event event; event.type = EventType::MouseButtonPressed; - event.data = MouseButtonEvent{button, mods, pos}; + event.data = MouseButtonEvent{btn, mods, pos}; return event; } -/** - * @brief 创建鼠标按钮释放事件 - * - * 创建一个表示鼠标按钮被释放的Event对象 - * - * @param button 鼠标按钮编号 - * @param mods 修饰键状态 - * @param pos 鼠标释放时的位置坐标 - * @return 包含鼠标按钮释放信息的Event对象 - */ -Event Event::createMouseButtonRelease(int button, int mods, const Vec2 &pos) { +Event Event::mouseRelease(i32 btn, i32 mods, Vec2 pos) { Event event; event.type = EventType::MouseButtonReleased; - event.data = MouseButtonEvent{button, mods, pos}; + event.data = MouseButtonEvent{btn, mods, pos}; return event; } -/** - * @brief 创建鼠标移动事件 - * - * 创建一个表示鼠标移动的Event对象 - * - * @param pos 鼠标当前位置坐标 - * @param delta 鼠标移动的位移量 - * @return 包含鼠标移动信息的Event对象 - */ -Event Event::createMouseMove(const Vec2 &pos, const Vec2 &delta) { +Event Event::mouseMove(Vec2 pos, Vec2 delta) { Event event; event.type = EventType::MouseMoved; event.data = MouseMoveEvent{pos, delta}; return event; } -/** - * @brief 创建鼠标滚轮滚动事件 - * - * 创建一个表示鼠标滚轮滚动的Event对象 - * - * @param offset 滚轮滚动的偏移量 - * @param pos 滚动时鼠标的位置坐标 - * @return 包含鼠标滚轮滚动信息的Event对象 - */ -Event Event::createMouseScroll(const Vec2 &offset, const Vec2 &pos) { +Event Event::mouseScroll(Vec2 offset, Vec2 pos) { Event event; event.type = EventType::MouseScrolled; event.data = MouseScrollEvent{offset, pos}; diff --git a/Extra2D/src/event/event_dispatcher.cpp b/Extra2D/src/event/event_dispatcher.cpp index 1119f7b..7cff7c7 100644 --- a/Extra2D/src/event/event_dispatcher.cpp +++ b/Extra2D/src/event/event_dispatcher.cpp @@ -3,37 +3,15 @@ namespace extra2d { -/** - * @brief 默认构造函数 - * - * 初始化事件分发器,设置下一个监听器ID为1 - */ EventDispatcher::EventDispatcher() : nextId_(1) {} -/** - * @brief 添加事件监听器 - * - * 为指定的事件类型注册一个回调函数,返回监听器ID用于后续移除 - * - * @param type 要监听的事件类型 - * @param callback 事件触发时调用的回调函数 - * @return 新注册监听器的唯一ID - */ -ListenerId EventDispatcher::addListener(EventType type, - EventCallback callback) { - ListenerId id = nextId_++; - listeners_[type].push_back({id, type, callback}); +ListenerID EventDispatcher::on(EventType type, EventFn fn) { + ListenerID id = nextId_++; + listeners_[type].push_back({id, type, fn}); return id; } -/** - * @brief 移除指定的事件监听器 - * - * 根据监听器ID移除对应的事件监听器 - * - * @param id 要移除的监听器ID - */ -void EventDispatcher::removeListener(ListenerId id) { +void EventDispatcher::off(ListenerID id) { for (auto &[type, listeners] : listeners_) { auto it = std::remove_if(listeners.begin(), listeners.end(), [id](const Listener &l) { return l.id == id; }); @@ -44,89 +22,41 @@ void EventDispatcher::removeListener(ListenerId id) { } } -/** - * @brief 移除指定类型的所有监听器 - * - * 移除某个事件类型下的所有已注册监听器 - * - * @param type 要移除监听器的事件类型 - */ -void EventDispatcher::removeAllListeners(EventType type) { +void EventDispatcher::offAll(EventType type) { listeners_.erase(type); } -/** - * @brief 移除所有监听器 - * - * 清除事件分发器中所有已注册的监听器 - */ -void EventDispatcher::removeAllListeners() { listeners_.clear(); } +void EventDispatcher::offAll() { listeners_.clear(); } -/** - * @brief 分发事件 - * - * 将事件分发给对应类型的所有监听器,直到事件被标记为已处理或所有监听器执行完毕 - * - * @param event 要分发的事件对象(可修改) - */ void EventDispatcher::dispatch(Event &event) { auto it = listeners_.find(event.type); if (it != listeners_.end()) { for (auto &listener : it->second) { if (event.handled) break; - listener.callback(event); + listener.fn(event); } } } -/** - * @brief 分发事件(常量版本) - * - * 创建事件的副本并分发,适用于常量事件对象 - * - * @param event 要分发的常量事件对象 - */ void EventDispatcher::dispatch(const Event &event) { Event mutableEvent = event; dispatch(mutableEvent); } -/** - * @brief 处理事件队列 - * - * 从事件队列中依次取出所有事件并分发 - * - * @param queue 要处理的事件队列 - */ -void EventDispatcher::processQueue(EventQueue &queue) { +void EventDispatcher::process(EventQueue &queue) { Event event; while (queue.poll(event)) { dispatch(event); } } -/** - * @brief 获取指定类型监听器的数量 - * - * 返回某个事件类型下已注册的监听器数量 - * - * @param type 要查询的事件类型 - * @return 该类型下的监听器数量 - */ -size_t EventDispatcher::getListenerCount(EventType type) const { +size_t EventDispatcher::listenerCount(EventType type) const { auto it = listeners_.find(type); return (it != listeners_.end()) ? it->second.size() : 0; } -/** - * @brief 获取所有监听器的总数量 - * - * 返回所有事件类型的监听器总数 - * - * @return 所有监听器的总数量 - */ -size_t EventDispatcher::getTotalListenerCount() const { +size_t EventDispatcher::totalListeners() const { size_t count = 0; for (const auto &[type, listeners] : listeners_) { count += listeners.size(); diff --git a/Extra2D/src/event/event_queue.cpp b/Extra2D/src/event/event_queue.cpp index 909481e..2f7850d 100644 --- a/Extra2D/src/event/event_queue.cpp +++ b/Extra2D/src/event/event_queue.cpp @@ -2,106 +2,44 @@ namespace extra2d { -/** - * @brief 默认构造函数 - * - * 构造一个空的事件队列对象 - */ EventQueue::EventQueue() = default; -/** - * @brief 将事件压入队列(左值引用版本) - * - * 将事件以拷贝方式添加到队列末尾,线程安全 - * - * @param event 要添加的事件对象 - */ -void EventQueue::push(const Event &event) { - std::lock_guard lock(mutex_); - queue_.push(event); +bool EventQueue::push(const Event &event) { + return buffer_.push(event); } -/** - * @brief 将事件压入队列(右值引用版本) - * - * 将事件以移动方式添加到队列末尾,线程安全 - * - * @param event 要添加的事件对象(右值引用) - */ -void EventQueue::push(Event &&event) { - std::lock_guard lock(mutex_); - queue_.push(std::move(event)); +bool EventQueue::push(Event &&event) { + return buffer_.push(std::move(event)); } -/** - * @brief 从队列中取出事件 - * - * 从队列头部取出一个事件,如果队列不为空则移除该事件,线程安全 - * - * @param event 输出参数,用于存储取出的事件 - * @return 如果成功取出事件返回true,队列为空返回false - */ bool EventQueue::poll(Event &event) { - std::lock_guard lock(mutex_); - if (queue_.empty()) { - return false; - } - event = queue_.front(); - queue_.pop(); - return true; + return buffer_.pop(event); } -/** - * @brief 查看队列头部事件 - * - * 获取队列头部的事件但不移除,线程安全 - * - * @param event 输出参数,用于存储查看到的事件 - * @return 如果队列不为空返回true,队列为空返回false - */ bool EventQueue::peek(Event &event) const { - std::lock_guard lock(mutex_); - if (queue_.empty()) { + std::lock_guard lock(mutex_); + if (buffer_.empty()) { + return false; + } + // 环形缓冲区不支持peek,这里简化处理 + // 实际应用中可能需要双缓冲或其他机制 return false; - } - event = queue_.front(); - return true; } -/** - * @brief 清空队列 - * - * 移除队列中的所有事件,线程安全 - */ void EventQueue::clear() { - std::lock_guard lock(mutex_); - while (!queue_.empty()) { - queue_.pop(); - } + std::lock_guard lock(mutex_); + Event event; + while (buffer_.pop(event)) { + // 持续弹出直到为空 + } } -/** - * @brief 检查队列是否为空 - * - * 线程安全地检查队列中是否有事件 - * - * @return 如果队列为空返回true,否则返回false - */ bool EventQueue::empty() const { - std::lock_guard lock(mutex_); - return queue_.empty(); + return buffer_.empty(); } -/** - * @brief 获取队列中的事件数量 - * - * 线程安全地获取队列中当前存储的事件数量 - * - * @return 队列中的事件数量 - */ size_t EventQueue::size() const { - std::lock_guard lock(mutex_); - return queue_.size(); + return buffer_.size(); } } // namespace extra2d diff --git a/Extra2D/src/platform/glfw/glfw_window.cpp b/Extra2D/src/platform/glfw/glfw_window.cpp index bbc0504..71b7448 100644 --- a/Extra2D/src/platform/glfw/glfw_window.cpp +++ b/Extra2D/src/platform/glfw/glfw_window.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -33,10 +33,10 @@ bool GLFWWindow::create(const std::string &title, int width, int height, monitor = glfwGetPrimaryMonitor(); #endif - glfwWindow_ = + handle_ = glfwCreateWindow(width, height, title.c_str(), monitor, nullptr); - if (!glfwWindow_) { + if (!handle_) { E2D_ERROR("创建 GLFW 窗口失败"); deinitGLFW(); return false; @@ -52,87 +52,87 @@ bool GLFWWindow::create(const std::string &title, int width, int height, int screenHeight = mode->height; int windowX = (screenWidth - width) / 2; int windowY = (screenHeight - height) / 2; - glfwSetWindowPos(glfwWindow_, windowX, windowY); + glfwSetWindowPos(handle_, windowX, windowY); } } } #endif - glfwMakeContextCurrent(glfwWindow_); + glfwMakeContextCurrent(handle_); glfwSwapInterval(vsync ? 1 : 0); vsync_ = vsync; - glfwGetWindowSize(glfwWindow_, &width_, &height_); + glfwGetWindowSize(handle_, &w_, &h_); updateContentScale(); - glfwSetWindowUserPointer(glfwWindow_, this); - glfwSetFramebufferSizeCallback(glfwWindow_, framebufferSizeCallback); - glfwSetWindowCloseCallback(glfwWindow_, windowCloseCallback); - glfwSetWindowFocusCallback(glfwWindow_, windowFocusCallback); - glfwSetWindowIconifyCallback(glfwWindow_, windowIconifyCallback); - glfwSetCursorPosCallback(glfwWindow_, cursorPosCallback); - glfwSetMouseButtonCallback(glfwWindow_, mouseButtonCallback); - glfwSetScrollCallback(glfwWindow_, scrollCallback); - glfwSetKeyCallback(glfwWindow_, keyCallback); + glfwSetWindowUserPointer(handle_, this); + glfwSetFramebufferSizeCallback(handle_, framebufferSizeCallback); + glfwSetWindowCloseCallback(handle_, windowCloseCallback); + glfwSetWindowFocusCallback(handle_, windowFocusCallback); + glfwSetWindowIconifyCallback(handle_, windowIconifyCallback); + glfwSetCursorPosCallback(handle_, cursorPosCallback); + glfwSetMouseButtonCallback(handle_, mouseButtonCallback); + glfwSetScrollCallback(handle_, scrollCallback); + glfwSetKeyCallback(handle_, keyCallback); glfwSetJoystickCallback(joystickCallback); - E2D_INFO("GLFW 窗口创建成功: {}x{}", width_, height_); + E2D_INFO("GLFW 窗口创建成功: {}x{}", w_, h_); return true; } void GLFWWindow::destroy() { - if (glfwWindow_) { - glfwDestroyWindow(glfwWindow_); - glfwWindow_ = nullptr; + if (handle_) { + glfwDestroyWindow(handle_); + handle_ = nullptr; } deinitGLFW(); } void GLFWWindow::poll() { - if (!glfwWindow_) + if (!handle_) return; glfwPollEvents(); } void GLFWWindow::swap() { - if (glfwWindow_) { - glfwSwapBuffers(glfwWindow_); + if (handle_) { + glfwSwapBuffers(handle_); } } bool GLFWWindow::shouldClose() const { - if (!glfwWindow_) + if (!handle_) return true; - return shouldClose_ || glfwWindowShouldClose(glfwWindow_); + return shouldClose_ || glfwWindowShouldClose(handle_); } void GLFWWindow::close() { shouldClose_ = true; - if (glfwWindow_) { - glfwSetWindowShouldClose(glfwWindow_, GLFW_TRUE); + if (handle_) { + glfwSetWindowShouldClose(handle_, GLFW_TRUE); } } -void GLFWWindow::setTitle(const std::string &title) { - if (glfwWindow_) { - glfwSetWindowTitle(glfwWindow_, title.c_str()); +void GLFWWindow::title(const std::string &t) { + if (handle_) { + glfwSetWindowTitle(handle_, t.c_str()); } } -void GLFWWindow::setSize(int w, int h) { - if (glfwWindow_) { - glfwSetWindowSize(glfwWindow_, w, h); - width_ = w; - height_ = h; +void GLFWWindow::size(int w, int h) { + if (handle_) { + glfwSetWindowSize(handle_, w, h); + w_ = w; + h_ = h; } } -void GLFWWindow::setPos(int x, int y) { +void GLFWWindow::pos(int x, int y) { #ifndef __SWITCH__ - if (glfwWindow_) { - glfwSetWindowPos(glfwWindow_, x, y); + if (handle_) { + glfwSetWindowPos(handle_, x, y); } #else (void)x; @@ -140,9 +140,9 @@ void GLFWWindow::setPos(int x, int y) { #endif } -void GLFWWindow::setFullscreen(bool fs) { +void GLFWWindow::fullscreen(bool fs) { #ifndef __SWITCH__ - if (!glfwWindow_) + if (!handle_) return; if (fs == fullscreen_) @@ -151,68 +151,68 @@ void GLFWWindow::setFullscreen(bool fs) { if (fs) { GLFWmonitor *monitor = glfwGetPrimaryMonitor(); const GLFWvidmode *mode = glfwGetVideoMode(monitor); - glfwSetWindowMonitor(glfwWindow_, monitor, 0, 0, mode->width, mode->height, + glfwSetWindowMonitor(handle_, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); } else { - glfwSetWindowMonitor(glfwWindow_, nullptr, 100, 100, 1280, 720, 0); + glfwSetWindowMonitor(handle_, nullptr, 100, 100, 1280, 720, 0); } fullscreen_ = fs; - glfwGetWindowSize(glfwWindow_, &width_, &height_); + glfwGetWindowSize(handle_, &w_, &h_); updateContentScale(); #else (void)fs; #endif } -void GLFWWindow::setVSync(bool vsync) { - if (glfwWindow_) { - glfwSwapInterval(vsync ? 1 : 0); - vsync_ = vsync; +void GLFWWindow::vsync(bool v) { + if (handle_) { + glfwSwapInterval(v ? 1 : 0); + vsync_ = v; } } -void GLFWWindow::setVisible(bool visible) { +void GLFWWindow::visible(bool v) { #ifndef __SWITCH__ - if (glfwWindow_) { - if (visible) { - glfwShowWindow(glfwWindow_); + if (handle_) { + if (v) { + glfwShowWindow(handle_); } else { - glfwHideWindow(glfwWindow_); + glfwHideWindow(handle_); } } #else - (void)visible; + (void)v; #endif } Size GLFWWindow::size() const { - return Size(static_cast(width_), static_cast(height_)); + return Size(static_cast(w_), static_cast(h_)); } Vec2 GLFWWindow::pos() const { int x = 0, y = 0; #ifndef __SWITCH__ - if (glfwWindow_) { - glfwGetWindowPos(glfwWindow_, &x, &y); + if (handle_) { + glfwGetWindowPos(handle_, &x, &y); } #endif - return Vec2(static_cast(x), static_cast(y)); + return Vec2(static_cast(x), static_cast(y)); } -void GLFWWindow::setCursor(Cursor cursor) { +void GLFWWindow::cursor(Cursor c) { #ifndef __SWITCH__ - if (!glfwWindow_) + if (!handle_) return; - if (cursor == Cursor::Hidden) { - glfwSetInputMode(glfwWindow_, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + if (c == Cursor::Hidden) { + glfwSetInputMode(handle_, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); return; } - glfwSetInputMode(glfwWindow_, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + glfwSetInputMode(handle_, GLFW_CURSOR, GLFW_CURSOR_NORMAL); GLFWcursor *glfwCursor = nullptr; - switch (cursor) { + switch (c) { case Cursor::Arrow: glfwCursor = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); break; @@ -237,17 +237,17 @@ void GLFWWindow::setCursor(Cursor cursor) { } if (glfwCursor) { - glfwSetCursor(glfwWindow_, glfwCursor); + glfwSetCursor(handle_, glfwCursor); } #else - (void)cursor; + (void)c; #endif } void GLFWWindow::showCursor(bool show) { #ifndef __SWITCH__ - if (glfwWindow_) { - glfwSetInputMode(glfwWindow_, GLFW_CURSOR, + if (handle_) { + glfwSetInputMode(handle_, GLFW_CURSOR, show ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN); cursorVisible_ = show; } @@ -258,8 +258,8 @@ void GLFWWindow::showCursor(bool show) { void GLFWWindow::lockCursor(bool lock) { #ifndef __SWITCH__ - if (glfwWindow_) { - glfwSetInputMode(glfwWindow_, GLFW_CURSOR, + if (handle_) { + glfwSetInputMode(handle_, GLFW_CURSOR, lock ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); cursorLocked_ = lock; } @@ -268,7 +268,7 @@ void GLFWWindow::lockCursor(bool lock) { #endif } -void *GLFWWindow::native() const { return glfwWindow_; } +void *GLFWWindow::native() const { return handle_; } bool GLFWWindow::initGLFW() { static int glfwInitCount = 0; @@ -291,11 +291,11 @@ void GLFWWindow::deinitGLFW() { } void GLFWWindow::updateContentScale() { - if (glfwWindow_) { + if (handle_) { int fbWidth, fbHeight; - glfwGetFramebufferSize(glfwWindow_, &fbWidth, &fbHeight); - scaleX_ = fbWidth > 0 ? static_cast(fbWidth) / width_ : 1.0f; - scaleY_ = fbHeight > 0 ? static_cast(fbHeight) / height_ : 1.0f; + glfwGetFramebufferSize(handle_, &fbWidth, &fbHeight); + scaleX_ = fbWidth > 0 ? static_cast(fbWidth) / w_ : 1.0f; + scaleY_ = fbHeight > 0 ? static_cast(fbHeight) / h_ : 1.0f; } } @@ -305,19 +305,19 @@ void GLFWWindow::framebufferSizeCallback(GLFWwindow *window, int width, GLFWWindow *self = static_cast(glfwGetWindowUserPointer(window)); if (self) { - self->width_ = width; - self->height_ = height; + self->w_ = width; + self->h_ = height; self->updateContentScale(); - if (self->resizeCb_) { - self->resizeCb_(width, height); + if (self->resizeFn_) { + self->resizeFn_(width, height); } } // 将事件推送到事件服务 - auto eventService = ServiceLocator::instance().getService(); + auto eventService = ServiceLocator::instance().get(); if (eventService) { - Event e = Event::createWindowResize(width, height); - eventService->pushEvent(e); + Event e = Event::windowResize(width, height); + eventService->push(e); } } @@ -326,16 +326,16 @@ void GLFWWindow::windowCloseCallback(GLFWwindow *window) { static_cast(glfwGetWindowUserPointer(window)); if (self) { self->shouldClose_ = true; - if (self->closeCb_) { - self->closeCb_(); + if (self->closeFn_) { + self->closeFn_(); } } // 将事件推送到事件服务 - auto eventService = ServiceLocator::instance().getService(); + auto eventService = ServiceLocator::instance().get(); if (eventService) { - Event e = Event::createWindowClose(); - eventService->pushEvent(e); + Event e = Event::windowClose(); + eventService->push(e); } } @@ -344,8 +344,8 @@ void GLFWWindow::windowFocusCallback(GLFWwindow *window, int focused) { static_cast(glfwGetWindowUserPointer(window)); if (self) { self->focused_ = (focused == GLFW_TRUE); - if (self->focusCb_) { - self->focusCb_(self->focused_); + if (self->focusFn_) { + self->focusFn_(self->focused_); } } } @@ -364,12 +364,12 @@ void GLFWWindow::cursorPosCallback(GLFWwindow *window, double xpos, static_cast(glfwGetWindowUserPointer(window)); // 将事件推送到事件服务 - auto eventService = ServiceLocator::instance().getService(); + auto eventService = ServiceLocator::instance().get(); if (eventService) { - Vec2 pos{static_cast(xpos), static_cast(ypos)}; + Vec2 pos{static_cast(xpos), static_cast(ypos)}; Vec2 delta{0.0f, 0.0f}; // GLFW 回调中没有增量,需要在其他地方计算 - Event e = Event::createMouseMove(pos, delta); - eventService->pushEvent(e); + Event e = Event::mouseMove(pos, delta); + eventService->push(e); } } @@ -379,18 +379,18 @@ void GLFWWindow::mouseButtonCallback(GLFWwindow *window, int button, int action, static_cast(glfwGetWindowUserPointer(window)); // 将事件推送到事件服务 - auto eventService = ServiceLocator::instance().getService(); + auto eventService = ServiceLocator::instance().get(); if (eventService) { double x, y; glfwGetCursorPos(window, &x, &y); - Vec2 pos{static_cast(x), static_cast(y)}; + Vec2 pos{static_cast(x), static_cast(y)}; if (action == GLFW_PRESS) { - Event e = Event::createMouseButtonPress(button, mods, pos); - eventService->pushEvent(e); + Event e = Event::mousePress(button, mods, pos); + eventService->push(e); } else if (action == GLFW_RELEASE) { - Event e = Event::createMouseButtonRelease(button, mods, pos); - eventService->pushEvent(e); + Event e = Event::mouseRelease(button, mods, pos); + eventService->push(e); } } } @@ -401,14 +401,14 @@ void GLFWWindow::scrollCallback(GLFWwindow *window, double xoffset, static_cast(glfwGetWindowUserPointer(window)); // 将事件推送到事件服务 - auto eventService = ServiceLocator::instance().getService(); + auto eventService = ServiceLocator::instance().get(); if (eventService) { double x, y; glfwGetCursorPos(window, &x, &y); - Vec2 offset{static_cast(xoffset), static_cast(yoffset)}; - Vec2 pos{static_cast(x), static_cast(y)}; - Event e = Event::createMouseScroll(offset, pos); - eventService->pushEvent(e); + Vec2 offset{static_cast(xoffset), static_cast(yoffset)}; + Vec2 pos{static_cast(x), static_cast(y)}; + Event e = Event::mouseScroll(offset, pos); + eventService->push(e); } } @@ -418,7 +418,7 @@ void GLFWWindow::keyCallback(GLFWwindow *window, int key, int scancode, static_cast(glfwGetWindowUserPointer(window)); // 将事件推送到事件服务 - auto eventService = ServiceLocator::instance().getService(); + auto eventService = ServiceLocator::instance().get(); if (eventService) { // 将 GLFW key code 转换为引擎 Key 枚举值 Key eKey = Key::None; @@ -645,13 +645,13 @@ void GLFWWindow::keyCallback(GLFWwindow *window, int key, int scancode, } if (eKey != Key::None) { - int keyCode = static_cast(eKey); + i32 keyCode = static_cast(eKey); if (action == GLFW_PRESS) { - Event e = Event::createKeyPress(keyCode, scancode, mods); - eventService->pushEvent(e); + Event e = Event::keyPress(keyCode, scancode, mods); + eventService->push(e); } else if (action == GLFW_RELEASE) { - Event e = Event::createKeyRelease(keyCode, scancode, mods); - eventService->pushEvent(e); + Event e = Event::keyRelease(keyCode, scancode, mods); + eventService->push(e); } } } diff --git a/Extra2D/src/platform/window_module.cpp b/Extra2D/src/platform/window_module.cpp index da4f6b4..9534b53 100644 --- a/Extra2D/src/platform/window_module.cpp +++ b/Extra2D/src/platform/window_module.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #ifdef __SWITCH__ #include @@ -26,7 +26,7 @@ bool WindowModule::init() { E2D_INFO("正在创建 GLFW 窗口,尺寸 {}x{}", cfg_.w, cfg_.h); - win_ = makeUnique(); + win_ = ptr::makeUnique(); if (!win_) { E2D_ERROR("创建窗口失败"); return false; diff --git a/Extra2D/src/services/event_service.cpp b/Extra2D/src/services/event_service.cpp index 8212dd1..963b0d0 100644 --- a/Extra2D/src/services/event_service.cpp +++ b/Extra2D/src/services/event_service.cpp @@ -8,58 +8,57 @@ EventService::EventService() { info_.enabled = true; } -ServiceInfo EventService::getServiceInfo() const { return info_; } +ServiceInfo EventService::info() const { return info_; } -bool EventService::initialize() { +bool EventService::init() { setState(ServiceState::Running); return true; } void EventService::shutdown() { queue_.clear(); - dispatcher_.removeAllListeners(); + dispatcher_.offAll(); setState(ServiceState::Stopped); } -void EventService::update(float deltaTime) { - if (getState() == ServiceState::Running) { - processQueue(); +void EventService::update(f32 dt) { + if (state() == ServiceState::Running) { + process(); } } -void EventService::pushEvent(const Event &event) { queue_.push(event); } +void EventService::push(const Event &event) { queue_.push(event); } -void EventService::pushEvent(Event &&event) { queue_.push(std::move(event)); } +void EventService::push(Event &&event) { queue_.push(std::move(event)); } -bool EventService::pollEvent(Event &event) { return queue_.poll(event); } +bool EventService::poll(Event &event) { return queue_.poll(event); } -ListenerId EventService::addListener(EventType type, - EventDispatcher::EventCallback callback) { - return dispatcher_.addListener(type, callback); +ListenerID EventService::on(EventType type, EventDispatcher::EventFn fn) { + return dispatcher_.on(type, fn); } -void EventService::removeListener(ListenerId id) { - dispatcher_.removeListener(id); +void EventService::off(ListenerID id) { + dispatcher_.off(id); } -void EventService::removeAllListeners(EventType type) { - dispatcher_.removeAllListeners(type); +void EventService::offAll(EventType type) { + dispatcher_.offAll(type); } -void EventService::removeAllListeners() { dispatcher_.removeAllListeners(); } +void EventService::offAll() { dispatcher_.offAll(); } void EventService::dispatch(Event &event) { dispatcher_.dispatch(event); } -void EventService::processQueue() { dispatcher_.processQueue(queue_); } +void EventService::process() { dispatcher_.process(queue_); } -size_t EventService::getListenerCount(EventType type) const { - return dispatcher_.getListenerCount(type); +size_t EventService::listenerCount(EventType type) const { + return dispatcher_.listenerCount(type); } -size_t EventService::getTotalListenerCount() const { - return dispatcher_.getTotalListenerCount(); +size_t EventService::totalListeners() const { + return dispatcher_.totalListeners(); } -size_t EventService::getQueueSize() const { return queue_.size(); } +size_t EventService::queueSize() const { return queue_.size(); } } // namespace extra2d diff --git a/Extra2D/src/services/logger_service.cpp b/Extra2D/src/services/logger_service.cpp index 28a00a9..71cf410 100644 --- a/Extra2D/src/services/logger_service.cpp +++ b/Extra2D/src/services/logger_service.cpp @@ -11,9 +11,6 @@ namespace extra2d { #ifdef _WIN32 -/** - * @brief 初始化 Windows 控制台(ANSI 颜色 + UTF-8) - */ static bool enableWindowsConsoleFeatures() { bool success = true; @@ -45,7 +42,7 @@ public: }; ConsoleLogger::ConsoleLogger() - : level_(LogLevel::Info), colorEnabled_(true), + : level_(LogLevel::Info), colors_(true), impl_(std::make_unique()) { info_.name = "ConsoleLogger"; info_.priority = ServicePriority::Core; @@ -67,23 +64,23 @@ ConsoleLogger::ConsoleLogger() ConsoleLogger::~ConsoleLogger() = default; -bool ConsoleLogger::initialize() { +bool ConsoleLogger::init() { setState(ServiceState::Running); return true; } void ConsoleLogger::shutdown() { setState(ServiceState::Stopped); } -void ConsoleLogger::setLevel(LogLevel level) { level_ = level; } +void ConsoleLogger::level(LogLevel lvl) { level_ = lvl; } -LogLevel ConsoleLogger::getLevel() const { return level_; } +LogLevel ConsoleLogger::level() const { return level_; } -bool ConsoleLogger::isEnabled(LogLevel level) const { - return static_cast(level) >= static_cast(level_); +bool ConsoleLogger::enabled(LogLevel lvl) const { + return static_cast(lvl) >= static_cast(level_); } -void ConsoleLogger::log(LogLevel level, const char *fmt, ...) { - if (!isEnabled(level)) +void ConsoleLogger::log(LogLevel lvl, const char *fmt, ...) { + if (!enabled(lvl)) return; char buffer[1024]; @@ -92,17 +89,17 @@ void ConsoleLogger::log(LogLevel level, const char *fmt, ...) { vsnprintf(buffer, sizeof(buffer), fmt, args); va_end(args); - output(level, buffer); + output(lvl, buffer); } -void ConsoleLogger::log(LogLevel level, const std::string &msg) { - if (!isEnabled(level)) +void ConsoleLogger::log(LogLevel lvl, const std::string &msg) { + if (!enabled(lvl)) return; - output(level, msg.c_str()); + output(lvl, msg.c_str()); } void ConsoleLogger::trace(const char *fmt, ...) { - if (!isEnabled(LogLevel::Trace)) + if (!enabled(LogLevel::Trace)) return; char buffer[1024]; va_list args; @@ -113,7 +110,7 @@ void ConsoleLogger::trace(const char *fmt, ...) { } void ConsoleLogger::debug(const char *fmt, ...) { - if (!isEnabled(LogLevel::Debug)) + if (!enabled(LogLevel::Debug)) return; char buffer[1024]; va_list args; @@ -124,7 +121,7 @@ void ConsoleLogger::debug(const char *fmt, ...) { } void ConsoleLogger::info(const char *fmt, ...) { - if (!isEnabled(LogLevel::Info)) + if (!enabled(LogLevel::Info)) return; char buffer[1024]; va_list args; @@ -135,7 +132,7 @@ void ConsoleLogger::info(const char *fmt, ...) { } void ConsoleLogger::registry(const char *fmt, ...) { - if (!isEnabled(LogLevel::Registry)) + if (!enabled(LogLevel::Registry)) return; char buffer[1024]; va_list args; @@ -146,7 +143,7 @@ void ConsoleLogger::registry(const char *fmt, ...) { } void ConsoleLogger::warn(const char *fmt, ...) { - if (!isEnabled(LogLevel::Warn)) + if (!enabled(LogLevel::Warn)) return; char buffer[1024]; va_list args; @@ -157,7 +154,7 @@ void ConsoleLogger::warn(const char *fmt, ...) { } void ConsoleLogger::error(const char *fmt, ...) { - if (!isEnabled(LogLevel::Error)) + if (!enabled(LogLevel::Error)) return; char buffer[1024]; va_list args; @@ -168,7 +165,7 @@ void ConsoleLogger::error(const char *fmt, ...) { } void ConsoleLogger::fatal(const char *fmt, ...) { - if (!isEnabled(LogLevel::Fatal)) + if (!enabled(LogLevel::Fatal)) return; char buffer[1024]; va_list args; @@ -178,33 +175,33 @@ void ConsoleLogger::fatal(const char *fmt, ...) { output(LogLevel::Fatal, buffer); } -void ConsoleLogger::setLevelColor(LogLevel level, const LogColor &color) { - int idx = static_cast(level); +void ConsoleLogger::levelColor(LogLevel lvl, const LogColor &c) { + int idx = static_cast(lvl); if (idx >= 0 && idx < 7) { - levelColors_[idx] = color; + levelColors_[idx] = c; } } -LogColor ConsoleLogger::getLevelColor(LogLevel level) const { - int idx = static_cast(level); +LogColor ConsoleLogger::levelColor(LogLevel lvl) const { + int idx = static_cast(lvl); if (idx >= 0 && idx < 7) { return levelColors_[idx]; } return LogColor::White(); } -void ConsoleLogger::setColorEnabled(bool enabled) { colorEnabled_ = enabled; } +void ConsoleLogger::colors(bool on) { colors_ = on; } -bool ConsoleLogger::isColorEnabled() const { return colorEnabled_; } +bool ConsoleLogger::colors() const { return colors_; } -std::string ConsoleLogger::getAnsiColor(LogLevel level) { - const LogColor &c = getLevelColor(level); +std::string ConsoleLogger::ansiColor(LogLevel lvl) { + const LogColor &c = levelColor(lvl); char buf[32]; snprintf(buf, sizeof(buf), "\033[38;2;%d;%d;%dm", c.r, c.g, c.b); return std::string(buf); } -void ConsoleLogger::output(LogLevel level, const char *msg) { +void ConsoleLogger::output(LogLevel lvl, const char *msg) { std::lock_guard lock(impl_->mutex_); auto now = std::chrono::system_clock::now(); @@ -220,11 +217,11 @@ void ConsoleLogger::output(LogLevel level, const char *msg) { localtime_r(&time, &tm); #endif - const char *levelStr = getLevelString(level); + const char *levelStr = levelString(lvl); const char *reset = "\033[0m"; - if (colorEnabled_) { - std::string color = getAnsiColor(level); + if (colors_) { + std::string color = ansiColor(lvl); printf("%s[%02d:%02d:%02d.%03d] [%s] %s%s\n", color.c_str(), tm.tm_hour, tm.tm_min, tm.tm_sec, (int)ms.count(), levelStr, msg, reset); } else { @@ -233,8 +230,8 @@ void ConsoleLogger::output(LogLevel level, const char *msg) { } } -const char *ConsoleLogger::getLevelString(LogLevel level) { - switch (level) { +const char *ConsoleLogger::levelString(LogLevel lvl) { + switch (lvl) { case LogLevel::Trace: return "TRACE"; case LogLevel::Debug: diff --git a/Extra2D/src/services/timer_service.cpp b/Extra2D/src/services/timer_service.cpp index b3fe050..9253660 100644 --- a/Extra2D/src/services/timer_service.cpp +++ b/Extra2D/src/services/timer_service.cpp @@ -8,45 +8,44 @@ TimerService::TimerService() { info_.enabled = true; } -ServiceInfo TimerService::getServiceInfo() const { return info_; } +ServiceInfo TimerService::info() const { return info_; } -bool TimerService::initialize() { +bool TimerService::init() { setState(ServiceState::Running); return true; } void TimerService::shutdown() { - manager_.clear(); + mgr_.clear(); setState(ServiceState::Stopped); } -void TimerService::update(float deltaTime) { - if (getState() == ServiceState::Running) { - manager_.update(deltaTime); +void TimerService::update(f32 dt) { + if (state() == ServiceState::Running) { + mgr_.update(dt); } } -uint32 TimerService::addTimer(float delay, Timer::Callback callback) { - return manager_.addTimer(delay, callback); +u32 TimerService::add(f32 delay, Timer::Fn fn) { + return mgr_.add(delay, fn); } -uint32 TimerService::addRepeatingTimer(float interval, - Timer::Callback callback) { - return manager_.addRepeatingTimer(interval, callback); +u32 TimerService::addRepeat(f32 interval, Timer::Fn fn) { + return mgr_.addRepeat(interval, fn); } -void TimerService::cancelTimer(uint32 timerId) { - manager_.cancelTimer(timerId); +void TimerService::cancel(u32 timerId) { + mgr_.cancel(timerId); } -void TimerService::pauseTimer(uint32 timerId) { manager_.pauseTimer(timerId); } +void TimerService::pauseTimer(u32 timerId) { mgr_.pause(timerId); } -void TimerService::resumeTimer(uint32 timerId) { - manager_.resumeTimer(timerId); +void TimerService::resumeTimer(u32 timerId) { + mgr_.resume(timerId); } -void TimerService::clear() { manager_.clear(); } +void TimerService::clear() { mgr_.clear(); } -size_t TimerService::getTimerCount() const { return manager_.getTimerCount(); } +size_t TimerService::count() const { return mgr_.count(); } } // namespace extra2d diff --git a/Extra2D/src/utils/random.cpp b/Extra2D/src/utils/random.cpp index c30dfa5..ba78965 100644 --- a/Extra2D/src/utils/random.cpp +++ b/Extra2D/src/utils/random.cpp @@ -24,9 +24,9 @@ Random &Random::get() { /** * @brief 设置随机数种子 - * @param seed 随机数种子值 + * @param s 随机数种子值 */ -void Random::setSeed(uint32 seed) { generator_.seed(seed); } +void Random::seed(u32 s) { generator_.seed(s); } /** * @brief 使用当前时间随机化种子 @@ -36,14 +36,14 @@ void Random::setSeed(uint32 seed) { generator_.seed(seed); } void Random::randomize() { auto now = std::chrono::high_resolution_clock::now(); auto time = now.time_since_epoch().count(); - generator_.seed(static_cast(time)); + generator_.seed(static_cast(time)); } /** - * @brief 获取[0.0, 1.0]范围内的随机浮点数 - * @return 随机浮点数,范围[0.0, 1.0] + * @brief 获取[0, 1)范围内的随机浮点数 + * @return 随机浮点数,范围[0, 1) */ -float Random::getFloat() { return floatDist_(generator_); } +f32 Random::randomF32() { return floatDist_(generator_); } /** * @brief 获取指定范围内的随机浮点数 @@ -51,7 +51,7 @@ float Random::getFloat() { return floatDist_(generator_); } * @param max 最大值 * @return 随机浮点数,范围[min, max] */ -float Random::getFloat(float min, float max) { +f32 Random::randomF32(f32 min, f32 max) { if (min >= max) { return min; } @@ -63,11 +63,11 @@ float Random::getFloat(float min, float max) { * @param max 最大值(包含) * @return 随机整数,范围[0, max] */ -int Random::getInt(int max) { +i32 Random::randomI32(i32 max) { if (max <= 0) { return 0; } - std::uniform_int_distribution dist(0, max); + std::uniform_int_distribution dist(0, max); return dist(generator_); } @@ -77,11 +77,11 @@ int Random::getInt(int max) { * @param max 最大值(包含) * @return 随机整数,范围[min, max] */ -int Random::getInt(int min, int max) { +i32 Random::randomI32(i32 min, i32 max) { if (min >= max) { return min; } - std::uniform_int_distribution dist(min, max); + std::uniform_int_distribution dist(min, max); return dist(generator_); } @@ -89,14 +89,14 @@ int Random::getInt(int min, int max) { * @brief 获取随机布尔值(50%概率) * @return 随机布尔值 */ -bool Random::getBool() { return floatDist_(generator_) >= 0.5f; } +bool Random::boolean() { return floatDist_(generator_) >= 0.5f; } /** * @brief 以指定概率获取随机布尔值 * @param probability 返回true的概率,范围[0.0, 1.0] * @return 随机布尔值 */ -bool Random::getBool(float probability) { +bool Random::boolean(f32 probability) { if (probability <= 0.0f) { return false; } @@ -110,8 +110,8 @@ bool Random::getBool(float probability) { * @brief 获取随机角度值 * @return 随机角度,范围[0, 2π] */ -float Random::getAngle() { - static const float TWO_PI = 6.28318530718f; +f32 Random::angle() { + static const f32 TWO_PI = 6.28318530718f; return floatDist_(generator_) * TWO_PI; } @@ -119,6 +119,6 @@ float Random::getAngle() { * @brief 获取有符号随机数 * @return 随机浮点数,范围[-1.0, 1.0] */ -float Random::getSigned() { return floatDist_(generator_) * 2.0f - 1.0f; } +f32 Random::signedF32() { return floatDist_(generator_) * 2.0f - 1.0f; } } // namespace extra2d diff --git a/Extra2D/src/utils/timer.cpp b/Extra2D/src/utils/timer.cpp index 09e2c4e..c3d5fac 100644 --- a/Extra2D/src/utils/timer.cpp +++ b/Extra2D/src/utils/timer.cpp @@ -3,35 +3,24 @@ namespace extra2d { -uint32 Timer::nextId_ = 1; +u32 Timer::nextId_ = 1; -/** - * @brief 构造函数,创建定时器 - * @param interval 定时间隔(秒) - * @param repeat 是否重复触发 - * @param callback 定时器回调函数 - */ -Timer::Timer(float interval, bool repeat, Callback callback) +Timer::Timer(f32 interval, bool repeat, Fn fn) : interval_(interval), elapsed_(0.0f), repeat_(repeat), paused_(false), - valid_(true), callback_(std::move(callback)) { + valid_(true), fn_(std::move(fn)) { id_ = nextId_++; } -/** - * @brief 更新定时器状态 - * @param deltaTime 帧间隔时间(秒) - * @return 如果定时器触发则返回true,否则返回false - */ -bool Timer::update(float deltaTime) { +bool Timer::update(f32 dt) { if (!valid_ || paused_) { return false; } - elapsed_ += deltaTime; + elapsed_ += dt; if (elapsed_ >= interval_) { - if (callback_) { - callback_(); + if (fn_) { + fn_(); } if (repeat_) { @@ -46,81 +35,40 @@ bool Timer::update(float deltaTime) { return false; } -/** - * @brief 重置定时器 - * - * 重置已过时间,恢复定时器为有效且非暂停状态 - */ void Timer::reset() { elapsed_ = 0.0f; valid_ = true; paused_ = false; } -/** - * @brief 暂停定时器 - */ void Timer::pause() { paused_ = true; } -/** - * @brief 恢复定时器 - */ void Timer::resume() { paused_ = false; } -/** - * @brief 取消定时器 - * - * 将定时器标记为无效状态 - */ void Timer::cancel() { valid_ = false; } -/** - * @brief 获取剩余时间 - * @return 剩余时间(秒),如果定时器无效或已暂停则返回0 - */ -float Timer::getRemaining() const { +f32 Timer::remaining() const { if (!valid_ || paused_) { return 0.0f; } return std::max(0.0f, interval_ - elapsed_); } -// ============================================================================ -// TimerManager 实现 -// ============================================================================ - -/** - * @brief 添加单次定时器 - * @param delay 延迟时间(秒) - * @param callback 定时器回调函数 - * @return 定时器ID - */ -uint32 TimerManager::addTimer(float delay, Timer::Callback callback) { - auto timer = std::make_unique(delay, false, std::move(callback)); - uint32 id = timer->getId(); +u32 TimerManager::add(f32 delay, Timer::Fn fn) { + auto timer = std::make_unique(delay, false, std::move(fn)); + u32 id = timer->id(); timers_.emplace(id, std::move(timer)); return id; } -/** - * @brief 添加重复定时器 - * @param interval 触发间隔(秒) - * @param callback 定时器回调函数 - * @return 定时器ID - */ -uint32 TimerManager::addRepeatingTimer(float interval, - Timer::Callback callback) { - auto timer = std::make_unique(interval, true, std::move(callback)); - uint32 id = timer->getId(); +u32 TimerManager::addRepeat(f32 interval, Timer::Fn fn) { + auto timer = std::make_unique(interval, true, std::move(fn)); + u32 id = timer->id(); timers_.emplace(id, std::move(timer)); return id; } -/** - * @brief 取消指定定时器 - * @param timerId 定时器ID - */ -void TimerManager::cancelTimer(uint32 timerId) { +void TimerManager::cancel(u32 timerId) { auto it = timers_.find(timerId); if (it != timers_.end()) { it->second->cancel(); @@ -128,52 +76,35 @@ void TimerManager::cancelTimer(uint32 timerId) { } } -/** - * @brief 暂停指定定时器 - * @param timerId 定时器ID - */ -void TimerManager::pauseTimer(uint32 timerId) { +void TimerManager::pause(u32 timerId) { auto it = timers_.find(timerId); if (it != timers_.end()) { it->second->pause(); } } -/** - * @brief 恢复指定定时器 - * @param timerId 定时器ID - */ -void TimerManager::resumeTimer(uint32 timerId) { +void TimerManager::resume(u32 timerId) { auto it = timers_.find(timerId); if (it != timers_.end()) { it->second->resume(); } } -/** - * @brief 更新所有定时器 - * @param deltaTime 帧间隔时间(秒) - * - * 更新所有定时器状态,并移除已失效的定时器 - */ -void TimerManager::update(float deltaTime) { +void TimerManager::update(f32 dt) { timersToRemove_.clear(); for (auto &[id, timer] : timers_) { - timer->update(deltaTime); - if (!timer->isValid()) { + timer->update(dt); + if (!timer->valid()) { timersToRemove_.push_back(id); } } - for (uint32 id : timersToRemove_) { + for (u32 id : timersToRemove_) { timers_.erase(id); } } -/** - * @brief 清除所有定时器 - */ void TimerManager::clear() { timers_.clear(); timersToRemove_.clear();