refactor(core): 移除模块系统及相关功能

删除模块系统实现及相关代码,包括Module类、Application中的模块管理功能
移除AGENTS.md中关于模块模式的文档
This commit is contained in:
2026-03-17 05:02:54 +08:00
parent a09c344bb7
commit 0995fb05f7
4 changed files with 14 additions and 212 deletions

View File

@@ -181,13 +181,6 @@ Frostbite2D/
## Key Patterns ## Key Patterns
### Module Pattern
```cpp
void Application::use(Module& m) {
modules_.push_back(&m);
}
```
### Configuration Pattern ### Configuration Pattern
Use configuration structs with default values: Use configuration structs with default values:
```cpp ```cpp

View File

@@ -1,7 +1,6 @@
#pragma once #pragma once
#include <frostbite2D/core/window.h> #include <frostbite2D/core/window.h>
#include <frostbite2D/module/module.h>
#include <frostbite2D/types/type_alias.h> #include <frostbite2D/types/type_alias.h>
#include <string> #include <string>
@@ -64,18 +63,6 @@ public:
Application(const Application&) = delete; Application(const Application&) = delete;
Application& operator=(const Application&) = delete; Application& operator=(const Application&) = delete;
/**
* @brief 添加模块
* @param m 模块引用
*/
void use(Module& m);
/**
* @brief 批量添加模块
* @param modules 模块指针列表
*/
void use(std::initializer_list<Module*> modules);
/** /**
* @brief 使用默认配置初始化 * @brief 使用默认配置初始化
* @return 初始化成功返回 true * @return 初始化成功返回 true
@@ -167,21 +154,11 @@ private:
~Application(); ~Application();
/** /**
* @brief 初始化核心模块 * @brief 初始化核心模块
* @return 初始化成功返回 true * @return 初始化成功返回 true
*/ */
bool initCoreModules(); bool initCoreModules();
/**
* @brief 设置所有模块
*/
void setupAllModules();
/**
* @brief 销毁所有模块
*/
void destroyAllModules();
/** /**
* @brief 主循环 * @brief 主循环
*/ */
@@ -193,11 +170,10 @@ private:
void update(); void update();
/** /**
* @brief 渲染 * @brief 渲染
*/ */
void render(); void render();
std::vector<Module*> modules_;
Window* window_ = nullptr; Window* window_ = nullptr;
class Renderer* renderer_ = nullptr; class Renderer* renderer_ = nullptr;
AppConfig config_; AppConfig config_;

View File

@@ -1,93 +0,0 @@
#pragma once
namespace frostbite2D {
/**
* @brief 模块基类
* 所有模块只需继承此类,实现需要的生命周期方法
*/
class Module {
public:
/**
* @brief 虚析构函数
*/
virtual ~Module() = default;
/**
* @brief 设置模块(初始化)
* 在 Application::run() 开始前调用
*/
virtual void setupModule() {}
/**
* @brief 销毁模块
* 在 Application 关闭时调用
*/
virtual void destroyModule() {}
/**
* @brief 更新时
* 每帧调用
* @param ctx 更新上下文
*/
virtual void onUpdate() { }
/**
* @brief 渲染前
* 在渲染开始前调用
* @param ctx 渲染上下文
*/
virtual void beforeRender() { }
/**
* @brief 渲染时
* 在渲染阶段调用
* @param ctx 渲染上下文
*/
virtual void onRender() {}
/**
* @brief 渲染后
* 在渲染完成后调用
* @param ctx 渲染上下文
*/
virtual void afterRender() { }
/**
* @brief 事件处理
* 处理系统事件
* @param ctx 事件上下文
*/
virtual void handleEvent() { }
/**
* @brief 获取模块名称
* @return 模块名称字符串
*/
virtual const char *getName() const = 0;
/**
* @brief 获取模块优先级
* 数值越小越先执行
* @return 优先级值
*/
virtual int getPriority() const { return 0; }
/**
* @brief 检查模块是否已初始化
* @return 已初始化返回 true
*/
bool isInitialized() const { return initialized_; }
protected:
friend class Application;
/**
* @brief 设置初始化状态
* @param initialized 初始化状态
*/
void setInitialized(bool initialized) { initialized_ = initialized; }
bool initialized_ = false;
};
} // namespace frostbite2D

View File

@@ -12,29 +12,12 @@ Application &Application::get() {
return instance; return instance;
} }
void Application::use(Module &m) {
for (auto *existing : modules_) {
if (existing == &m) {
return;
}
}
modules_.push_back(&m);
}
void Application::use(std::initializer_list<Module *> modules) {
for (auto *m : modules) {
if (m) {
use(*m);
}
}
}
bool Application::init() { bool Application::init() {
AppConfig cfg; AppConfig cfg;
return init(cfg); return init(cfg);
} }
bool Application::init(const AppConfig &config) { bool Application::init(const AppConfig& config) {
if (initialized_) { if (initialized_) {
return true; return true;
} }
@@ -42,9 +25,9 @@ bool Application::init(const AppConfig &config) {
config_ = config; config_ = config;
// 平台相关初始化 // 平台相关初始化
#ifdef __SWITCH__ #ifdef __SWITCH__
switchInit(); switchInit();
#endif #endif
// 使用SDL2创建窗口 // 使用SDL2创建窗口
this->window_ = new Window(); this->window_ = new Window();
@@ -73,8 +56,6 @@ void Application::shutdown() {
running_ = false; running_ = false;
shouldQuit_ = true; shouldQuit_ = true;
destroyAllModules();
// 关闭渲染器 // 关闭渲染器
if (renderer_) { if (renderer_) {
renderer_->shutdown(); renderer_->shutdown();
@@ -87,9 +68,9 @@ void Application::shutdown() {
} }
// 平台相关清理 // 平台相关清理
#ifdef __SWITCH__ #ifdef __SWITCH__
switchShutdown(); switchShutdown();
#endif #endif
initialized_ = false; initialized_ = false;
running_ = false; running_ = false;
@@ -105,15 +86,15 @@ bool Application::initCoreModules() {
auto &asset = Asset::get(); auto &asset = Asset::get();
// 平台相关 switch平台不可以获取当前工作目录 // 平台相关 switch平台不可以获取当前工作目录
#ifndef __SWITCH__ #ifndef __SWITCH__
// 获取程序工作目录 // 获取程序工作目录
std::string workingDir = SDL_GetBasePath(); std::string workingDir = SDL_GetBasePath();
asset.setWorkingDirectory(workingDir); asset.setWorkingDirectory(workingDir);
SDL_Log("Asset working directory: %s", workingDir.c_str()); SDL_Log("Asset working directory: %s", workingDir.c_str());
#else #else
asset.setWorkingDirectory("/switch/Frostbite2D/" + config_.appName); asset.setWorkingDirectory("/switch/Frostbite2D/" + config_.appName);
SDL_Log("Asset working directory: %s", asset.getWorkingDirectory().c_str()); SDL_Log("Asset working directory: %s", asset.getWorkingDirectory().c_str());
#endif #endif
// 初始化渲染器 // 初始化渲染器
renderer_ = &Renderer::get(); renderer_ = &Renderer::get();
@@ -130,29 +111,6 @@ bool Application::initCoreModules() {
return true; return true;
} }
void Application::setupAllModules() {
SDL_Log("Setting up modules (%zu)...", modules_.size());
for (auto* module : modules_) {
if (module && !module->isInitialized()) {
module->setupModule();
module->setInitialized(true);
SDL_Log("Module '%s' initialized", module->getName());
}
}
}
void Application::destroyAllModules() {
SDL_Log("Destroying modules (%zu)...", modules_.size());
for (auto* module : modules_) {
if (module && module->isInitialized()) {
module->destroyModule();
module->setInitialized(false);
}
}
}
void Application::run() { void Application::run() {
if (!initialized_) { if (!initialized_) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Application not initialized!"); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Application not initialized!");
@@ -169,8 +127,6 @@ void Application::run() {
shouldQuit_ = false; shouldQuit_ = false;
paused_ = false; paused_ = false;
setupAllModules();
lastFrameTime_ = SDL_GetPerformanceCounter() / static_cast<double>(SDL_GetPerformanceFrequency()); lastFrameTime_ = SDL_GetPerformanceCounter() / static_cast<double>(SDL_GetPerformanceFrequency());
totalTime_ = 0.0f; totalTime_ = 0.0f;
deltaTime_ = 0.0f; deltaTime_ = 0.0f;
@@ -180,7 +136,6 @@ void Application::run() {
mainLoop(); mainLoop();
destroyAllModules();
running_ = false; running_ = false;
SDL_Log("Application stopped"); SDL_Log("Application stopped");
} }
@@ -213,11 +168,6 @@ void Application::mainLoop() {
shouldQuit_ = true; shouldQuit_ = true;
break; break;
} }
for (auto* module : modules_) {
if (module && module->isInitialized()) {
module->handleEvent();
}
}
} }
if (!paused_) { if (!paused_) {
@@ -234,12 +184,6 @@ void Application::update() {
lastFrameTime_ = currentTime; lastFrameTime_ = currentTime;
totalTime_ += deltaTime_; totalTime_ += deltaTime_;
for (auto* module : modules_) {
if (module && module->isInitialized()) {
module->onUpdate();
}
}
frameCount_++; frameCount_++;
fpsTimer_ += deltaTime_; fpsTimer_ += deltaTime_;
if (fpsTimer_ >= fpsUpdateInterval_) { if (fpsTimer_ >= fpsUpdateInterval_) {
@@ -250,24 +194,6 @@ void Application::update() {
} }
void Application::render() { void Application::render() {
for (auto* module : modules_) {
if (module && module->isInitialized()) {
module->beforeRender();
}
}
for (auto* module : modules_) {
if (module && module->isInitialized()) {
module->onRender();
}
}
for (auto* module : modules_) {
if (module && module->isInitialized()) {
module->afterRender();
}
}
if (window_) { if (window_) {
window_->swap(); window_->swap();
} }
@@ -276,4 +202,4 @@ void Application::render() {
const AppConfig& Application::getConfig() const { const AppConfig& Application::getConfig() const {
return config_; return config_;
} }
} // namespace frostbite2D } // namespace frostbite2D