diff --git a/Extra2D/include/extra2d/core/service_locator.h b/Extra2D/include/extra2d/core/service_locator.h index 31f6492..798b2c6 100644 --- a/Extra2D/include/extra2d/core/service_locator.h +++ b/Extra2D/include/extra2d/core/service_locator.h @@ -48,7 +48,7 @@ public: static_assert(std::is_base_of_v, "T must derive from IService"); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); auto typeId = std::type_index(typeid(T)); services_[typeId] = std::static_pointer_cast(service); orderedServices_.push_back(service); @@ -64,7 +64,7 @@ public: static_assert(std::is_base_of_v, "T must derive from IService"); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); auto typeId = std::type_index(typeid(T)); factories_[typeId] = [factory]() -> SharedPtr { return std::static_pointer_cast(factory()); @@ -86,7 +86,7 @@ public: static_assert(std::is_base_of_v, "T must derive from IService"); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); auto typeId = std::type_index(typeid(T)); auto it = services_.find(typeId); @@ -113,7 +113,7 @@ public: static_assert(std::is_base_of_v, "T must derive from IService"); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); auto typeId = std::type_index(typeid(T)); auto it = services_.find(typeId); if (it != services_.end()) { @@ -128,7 +128,7 @@ public: * @return 已注册返回 true */ template bool hasService() const { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); auto typeId = std::type_index(typeid(T)); return services_.find(typeId) != services_.end() || factories_.find(typeId) != factories_.end(); @@ -139,7 +139,7 @@ public: * @tparam T 服务接口类型 */ template void unregisterService() { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); auto typeId = std::type_index(typeid(T)); auto it = services_.find(typeId); @@ -214,7 +214,7 @@ private: std::unordered_map()>> factories_; std::vector> orderedServices_; - mutable std::mutex mutex_; + mutable std::recursive_mutex mutex_; }; /** diff --git a/Extra2D/src/core/service_locator.cpp b/Extra2D/src/core/service_locator.cpp index de00dfa..24d1644 100644 --- a/Extra2D/src/core/service_locator.cpp +++ b/Extra2D/src/core/service_locator.cpp @@ -9,7 +9,7 @@ ServiceLocator& ServiceLocator::instance() { } bool ServiceLocator::initializeAll() { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); for (auto& service : orderedServices_) { if (!service) continue; @@ -31,7 +31,7 @@ bool ServiceLocator::initializeAll() { } void ServiceLocator::shutdownAll() { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); for (auto it = orderedServices_.rbegin(); it != orderedServices_.rend(); ++it) { @@ -44,7 +44,7 @@ void ServiceLocator::shutdownAll() { } void ServiceLocator::updateAll(float deltaTime) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); for (auto& service : orderedServices_) { if (service && service->isInitialized()) { @@ -57,7 +57,7 @@ void ServiceLocator::updateAll(float deltaTime) { } void ServiceLocator::pauseAll() { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); for (auto& service : orderedServices_) { if (service && service->isInitialized()) { @@ -67,7 +67,7 @@ void ServiceLocator::pauseAll() { } void ServiceLocator::resumeAll() { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); for (auto& service : orderedServices_) { if (service && service->isInitialized()) { @@ -77,12 +77,12 @@ void ServiceLocator::resumeAll() { } std::vector> ServiceLocator::getAllServices() const { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); return orderedServices_; } void ServiceLocator::clear() { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); for (auto it = orderedServices_.rbegin(); it != orderedServices_.rend(); ++it) { diff --git a/examples/hello_module/config.json b/examples/hello_module/config.json deleted file mode 100644 index f6661c2..0000000 --- a/examples/hello_module/config.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "app": { - "name": "HelloModule Example", - "version": "1.0.0" - }, - "window": { - "title": "Hello Module Example", - "width": 800, - "height": 600, - "mode": "windowed", - "vsync": true - }, - "hello": { - "greeting": "Hello from custom module!", - "repeatCount": 3, - "enableLogging": true - } -} diff --git a/examples/hello_module/hello_module.cpp b/examples/hello_module/hello_module.cpp index 3ddb514..42eac83 100644 --- a/examples/hello_module/hello_module.cpp +++ b/examples/hello_module/hello_module.cpp @@ -3,40 +3,42 @@ namespace extra2d { -HelloModule::HelloModule(const HelloCfg& cfg) : cfg_(cfg) {} +HelloModule::HelloModule(const HelloCfg &cfg) : cfg_(cfg) {} -HelloModule::HelloModule(std::function configFn) { - configFn(cfg_); +HelloModule::HelloModule(std::function configFn) { + configFn(cfg_); } HelloModule::~HelloModule() { - if (initialized_) { - shutdown(); - } + if (initialized_) { + shutdown(); + } } bool HelloModule::init() { - if (initialized_) return true; - - std::cout << "HelloModule initialized" << std::endl; - std::cout << " Greeting: " << cfg_.greeting << std::endl; - std::cout << " Repeat count: " << cfg_.repeatCount << std::endl; - - initialized_ = true; + if (initialized_) return true; + + E2D_INFO("HelloModule initialized"); + E2D_INFO(" Greeting: {}", cfg_.greeting); + E2D_INFO(" Repeat count: {}", cfg_.repeatCount); + + initialized_ = true; + return true; } void HelloModule::shutdown() { - if (!initialized_) return; - - std::cout << "HelloModule shutdown" << std::endl; - initialized_ = false; + if (!initialized_) + return; + + E2D_INFO("HelloModule shutdown"); + initialized_ = false; } void HelloModule::sayHello() const { - for (int i = 0; i < cfg_.repeatCount; ++i) { - std::cout << cfg_.greeting << std::endl; - } + for (int i = 0; i < cfg_.repeatCount; ++i) { + std::cout << cfg_.greeting << std::endl; + } } } // namespace extra2d diff --git a/examples/hello_module/hello_module.h b/examples/hello_module/hello_module.h index abe1353..1280eb8 100644 --- a/examples/hello_module/hello_module.h +++ b/examples/hello_module/hello_module.h @@ -1,8 +1,9 @@ #pragma once -#include -#include +#include #include +#include + namespace extra2d { @@ -10,14 +11,11 @@ namespace extra2d { * @brief Hello模块配置结构 */ struct HelloCfg { - std::string greeting; - int repeatCount; - int priority; + std::string greeting; + int repeatCount; + int priority; - HelloCfg() - : greeting("Hello, Extra2D!") - , repeatCount(1) - , priority(100) {} + HelloCfg() : greeting("Hello, Extra2D!"), repeatCount(1), priority(100) {} }; /** @@ -26,42 +24,42 @@ struct HelloCfg { */ class HelloModule : public Module { public: - /** - * @brief 配置类型别名(向后兼容) - */ - using Cfg = HelloCfg; + /** + * @brief 配置类型别名(向后兼容) + */ + using Cfg = HelloCfg; - /** - * @brief 构造函数 - * @param cfg 配置 - */ - explicit HelloModule(const HelloCfg& cfg = HelloCfg{}); + /** + * @brief 构造函数 + * @param cfg 配置 + */ + explicit HelloModule(const HelloCfg &cfg = HelloCfg{}); - /** - * @brief 构造函数(Lambda 配置) - * @param configFn 配置函数 - */ - explicit HelloModule(std::function configFn); - - /** - * @brief 析构函数 - */ - ~HelloModule() override; - - bool init() override; - void shutdown() override; - bool ok() const override { return initialized_; } - const char* name() const override { return "hello"; } - int priority() const override { return cfg_.priority; } - - /** - * @brief 执行问候操作 - */ - void sayHello() const; + /** + * @brief 构造函数(Lambda 配置) + * @param configFn 配置函数 + */ + explicit HelloModule(std::function configFn); + + /** + * @brief 析构函数 + */ + ~HelloModule() override; + + bool init() override; + void shutdown() override; + bool ok() const override { return initialized_; } + const char *name() const override { return "hello"; } + int priority() const override { return cfg_.priority; } + + /** + * @brief 执行问候操作 + */ + void sayHello() const; private: - HelloCfg cfg_; - bool initialized_ = false; + HelloCfg cfg_; + bool initialized_ = false; }; } // namespace extra2d diff --git a/examples/hello_module/main.cpp b/examples/hello_module/main.cpp index 1496202..2302bde 100644 --- a/examples/hello_module/main.cpp +++ b/examples/hello_module/main.cpp @@ -1,21 +1,18 @@ #include "hello_module.h" #include -#include using namespace extra2d; class HelloScene : public Scene { public: - static Ptr create() { return makeShared(); } - void onEnter() override { Scene::onEnter(); - std::cout << "HelloScene entered" << std::endl; + E2D_INFO("HelloScene entered"); setBackgroundColor(Color(0.1f, 0.1f, 0.2f, 1.0f)); auto hello = Application::get().get(); if (hello) { - std::cout << "Scene calling HelloModule from onEnter..." << std::endl; + E2D_INFO("Scene calling HelloModule from onEnter..."); hello->sayHello(); } } @@ -23,16 +20,16 @@ public: void onUpdate(float dt) override { Scene::onUpdate(dt); time_ += dt; - - if (time_ >= 5.0f) { + if (time_ >= 1.0f) { auto *hello = Application::get().get(); if (hello) { - std::cout << "Scene calling HelloModule from onUpdate..." << std::endl; + E2D_INFO("Scene calling HelloModule from onUpdate..."); hello->sayHello(); } time_ = 0.0f; } } + void onRender(RenderBackend &renderer) override { Scene::onRender(renderer); } private: float time_ = 0.0f; @@ -48,18 +45,21 @@ int main(int argc, char *argv[]) { cfg.h = 600; cfg.backend = "glfw"; }); - app.use([](auto &cfg) { cfg.priority = 10; }); + app.use([](auto &cfg) { + cfg.backend = "opengl"; + cfg.priority = 10; + }); app.use([](auto &cfg) { cfg.greeting = "Hello from custom module!"; cfg.repeatCount = 3; }); if (!app.init()) { - std::cerr << "Failed to initialize application" << std::endl; + E2D_INFO("Failed to initialize application"); return 1; } - auto scene = HelloScene::create(); + auto scene = makeShared(); app.enterScene(scene); app.run(); app.shutdown();