Frostbite2D/Fostbite2D/include/fostbite2D/core/application.h

243 lines
4.9 KiB
C++

#pragma once
#include <fostbite2D/core/window.h>
#include <fostbite2D/module/module.h>
#include <fostbite2D/types/type_alias.h>
#include <string>
namespace frostbite2D {
/**
* @brief 应用配置结构体
* 仅包含应用级别的配置项,模块配置由各模块自行管理
*/
struct AppConfig {
std::string appName = "frostbite2D App";
std::string appVersion = "1.0.0";
std::string organization = "frostbite";
WindowConfig windowConfig;
PlatformType targetPlatform = PlatformType::Auto;
/**
* @brief 创建默认配置
* @return 默认的应用配置实例
*/
static AppConfig createDefault() {
AppConfig config;
config.appName = "Frostbite2D App";
config.appVersion = "1.0.0";
config.organization = "frostbite";
config.targetPlatform = PlatformType::Auto;
config.windowConfig = WindowConfig();
return config;
}
};
/**
* @brief 应用程序类
*/
class Application {
public:
/**
* @brief 获取单例实例
* @return 应用程序实例引用
*/
static Application& get();
Application(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 使用默认配置初始化
* @return 初始化成功返回 true
*/
bool init();
/**
* @brief 使用指定配置初始化
* @param config 应用配置
* @return 初始化成功返回 true
*/
bool init(const AppConfig& config);
/**
* @brief 使用配置文件初始化
* @param configPath 配置文件路径
* @return 初始化成功返回 true
*/
bool init(const std::string& configPath);
/**
* @brief 关闭应用程序
*/
void shutdown();
/**
* @brief 运行主循环
*/
void run();
/**
* @brief 请求退出
*/
void quit();
/**
* @brief 暂停应用程序
*/
void pause();
/**
* @brief 恢复应用程序
*/
void resume();
/**
* @brief 检查是否暂停
* @return 暂停状态返回 true
*/
bool isPaused() const { return paused_; }
/**
* @brief 检查是否运行中
* @return 运行中返回 true
*/
bool isRunning() const { return running_; }
// /**
// * @brief 获取窗口
// * @return 窗口引用
// */
// IWindow& window() { return *window_; }
// /**
// * @brief 获取渲染器
// * @return 渲染器引用
// */
// RenderBackend& renderer();
// /**
// * @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 场景指针
// */
// void enterScene(Ptr<class Scene> scene);
/**
* @brief 获取帧间隔时间
* @return 帧间隔时间(秒)
*/
float deltaTime() const { return deltaTime_; }
/**
* @brief 获取总运行时间
* @return 总运行时间(秒)
*/
float totalTime() const { return totalTime_; }
/**
* @brief 获取当前帧率
* @return 帧率
*/
int fps() const { return currentFps_; }
/**
* @brief 获取应用配置
* @return 应用配置常量引用
*/
const AppConfig& getConfig() const;
private:
Application() = default;
~Application();
/**
* @brief 初始化核心模块
* @return 初始化成功返回 true
*/
bool initCoreModules();
/**
* @brief 设置所有模块
*/
void setupAllModules();
/**
* @brief 销毁所有模块
*/
void destroyAllModules();
/**
* @brief 注册核心服务
*/
void registerCoreServices();
/**
* @brief 主循环
*/
void mainLoop();
/**
* @brief 更新
*/
void update();
/**
* @brief 渲染
*/
void render();
std::vector<Module*> modules_;
Window* window_ = nullptr;
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;
};
}