2026-02-11 19:40:26 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
#include <extra2d/core/types.h>
|
2026-02-15 20:13:18 +08:00
|
|
|
|
#include <extra2d/core/module.h>
|
2026-02-15 11:40:57 +08:00
|
|
|
|
#include <extra2d/core/service_locator.h>
|
2026-02-15 08:51:31 +08:00
|
|
|
|
#include <extra2d/config/app_config.h>
|
|
|
|
|
|
#include <extra2d/config/config_manager.h>
|
2026-02-15 00:22:24 +08:00
|
|
|
|
#include <extra2d/platform/iwindow.h>
|
|
|
|
|
|
#include <string>
|
2026-02-15 20:13:18 +08:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
#include <initializer_list>
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
2026-02-15 00:22:24 +08:00
|
|
|
|
class IInput;
|
2026-02-15 10:08:44 +08:00
|
|
|
|
class RenderBackend;
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 应用程序类
|
|
|
|
|
|
* 使用服务定位器模式管理模块依赖,支持依赖注入和测试Mock
|
|
|
|
|
|
*/
|
2026-02-11 19:40:26 +08:00
|
|
|
|
class Application {
|
|
|
|
|
|
public:
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取单例实例
|
|
|
|
|
|
* @return 应用程序实例引用
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
static Application& get();
|
|
|
|
|
|
|
|
|
|
|
|
Application(const Application&) = delete;
|
|
|
|
|
|
Application& operator=(const Application&) = delete;
|
|
|
|
|
|
|
2026-02-15 20:13:18 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 添加模块
|
|
|
|
|
|
* @param m 模块引用
|
|
|
|
|
|
*/
|
|
|
|
|
|
void use(Module& m);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 批量添加模块
|
|
|
|
|
|
* @param modules 模块指针列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
void use(std::initializer_list<Module*> modules);
|
|
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 使用默认配置初始化
|
|
|
|
|
|
* @return 初始化成功返回 true
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
bool init();
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 使用指定配置初始化
|
|
|
|
|
|
* @param config 应用配置
|
|
|
|
|
|
* @return 初始化成功返回 true
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
bool init(const AppConfig& config);
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 使用配置文件初始化
|
|
|
|
|
|
* @param configPath 配置文件路径
|
|
|
|
|
|
* @return 初始化成功返回 true
|
|
|
|
|
|
*/
|
2026-02-15 08:51:31 +08:00
|
|
|
|
bool init(const std::string& configPath);
|
2026-02-15 00:22:24 +08:00
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 关闭应用程序
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
void shutdown();
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 运行主循环
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
void run();
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 请求退出
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
void quit();
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 暂停应用程序
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
void pause();
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 恢复应用程序
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
void resume();
|
2026-02-15 08:51:31 +08:00
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 检查是否暂停
|
|
|
|
|
|
* @return 暂停状态返回 true
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
bool isPaused() const { return paused_; }
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 检查是否运行中
|
|
|
|
|
|
* @return 运行中返回 true
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
bool isRunning() const { return running_; }
|
|
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取窗口
|
|
|
|
|
|
* @return 窗口引用
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
IWindow& window() { return *window_; }
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取渲染器
|
|
|
|
|
|
* @return 渲染器引用
|
|
|
|
|
|
*/
|
2026-02-15 10:08:44 +08:00
|
|
|
|
RenderBackend& renderer();
|
2026-02-15 08:51:31 +08:00
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取输入接口
|
|
|
|
|
|
* @return 输入接口引用
|
|
|
|
|
|
*/
|
|
|
|
|
|
IInput& input();
|
2026-02-15 00:22:24 +08:00
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取场景服务
|
|
|
|
|
|
* @return 场景服务共享指针
|
|
|
|
|
|
*/
|
|
|
|
|
|
SharedPtr<class ISceneService> scenes();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取计时器服务
|
|
|
|
|
|
* @return 计时器服务共享指针
|
|
|
|
|
|
*/
|
|
|
|
|
|
SharedPtr<class ITimerService> timers();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取事件服务
|
|
|
|
|
|
* @return 事件服务共享指针
|
|
|
|
|
|
*/
|
|
|
|
|
|
SharedPtr<class IEventService> events();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取相机服务
|
|
|
|
|
|
* @return 相机服务共享指针
|
|
|
|
|
|
*/
|
|
|
|
|
|
SharedPtr<class ICameraService> camera();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 进入场景
|
|
|
|
|
|
* @param scene 场景指针
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
void enterScene(Ptr<class Scene> scene);
|
|
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取帧间隔时间
|
|
|
|
|
|
* @return 帧间隔时间(秒)
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
float deltaTime() const { return deltaTime_; }
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取总运行时间
|
|
|
|
|
|
* @return 总运行时间(秒)
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
float totalTime() const { return totalTime_; }
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取当前帧率
|
|
|
|
|
|
* @return 帧率
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
int fps() const { return currentFps_; }
|
|
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取配置管理器
|
|
|
|
|
|
* @return 配置管理器引用
|
|
|
|
|
|
*/
|
2026-02-15 08:51:31 +08:00
|
|
|
|
ConfigManager& config();
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取应用配置
|
|
|
|
|
|
* @return 应用配置常量引用
|
|
|
|
|
|
*/
|
2026-02-15 08:51:31 +08:00
|
|
|
|
const AppConfig& getConfig() const;
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 注册自定义服务
|
|
|
|
|
|
* @tparam T 服务接口类型
|
|
|
|
|
|
* @param service 服务实例
|
|
|
|
|
|
*/
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
|
void registerService(SharedPtr<T> service) {
|
|
|
|
|
|
ServiceLocator::instance().registerService(service);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取服务
|
|
|
|
|
|
* @tparam T 服务接口类型
|
|
|
|
|
|
* @return 服务共享指针
|
|
|
|
|
|
*/
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
|
SharedPtr<T> getService() {
|
|
|
|
|
|
return ServiceLocator::instance().getService<T>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-11 19:40:26 +08:00
|
|
|
|
private:
|
2026-02-15 00:22:24 +08:00
|
|
|
|
Application() = default;
|
|
|
|
|
|
~Application();
|
|
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
/**
|
2026-02-15 20:13:18 +08:00
|
|
|
|
* @brief 初始化核心模块
|
2026-02-15 11:40:57 +08:00
|
|
|
|
* @return 初始化成功返回 true
|
|
|
|
|
|
*/
|
2026-02-15 20:13:18 +08:00
|
|
|
|
bool initCoreModules();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 设置所有模块
|
|
|
|
|
|
*/
|
|
|
|
|
|
void setupAllModules();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 销毁所有模块
|
|
|
|
|
|
*/
|
|
|
|
|
|
void destroyAllModules();
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 注册核心服务
|
|
|
|
|
|
*/
|
|
|
|
|
|
void registerCoreServices();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 主循环
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
void mainLoop();
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 更新
|
|
|
|
|
|
*/
|
2026-02-15 00:22:24 +08:00
|
|
|
|
void update();
|
2026-02-15 11:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 渲染
|
|
|
|
|
|
*/
|
2026-02-15 08:51:31 +08:00
|
|
|
|
void render();
|
2026-02-15 00:22:24 +08:00
|
|
|
|
|
2026-02-15 20:13:18 +08:00
|
|
|
|
std::vector<Module*> modules_;
|
2026-02-15 10:08:44 +08:00
|
|
|
|
IWindow* window_ = nullptr;
|
2026-02-15 00:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
bool initialized_ = false;
|
|
|
|
|
|
bool running_ = false;
|
|
|
|
|
|
bool paused_ = false;
|
|
|
|
|
|
bool shouldQuit_ = false;
|
|
|
|
|
|
|
|
|
|
|
|
float deltaTime_ = 0.0f;
|
|
|
|
|
|
float totalTime_ = 0.0f;
|
|
|
|
|
|
double lastFrameTime_ = 0.0;
|
|
|
|
|
|
int frameCount_ = 0;
|
|
|
|
|
|
float fpsTimer_ = 0.0f;
|
|
|
|
|
|
int currentFps_ = 0;
|
2026-02-11 19:40:26 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-15 11:40:57 +08:00
|
|
|
|
}
|