Extra2D/Extra2D/include/extra2d/app/application.h

114 lines
2.0 KiB
C++

#pragma once
#include <extra2d/core/module.h>
#include <extra2d/core/registry.h>
#include <extra2d/core/service_locator.h>
#include <extra2d/core/types.h>
#include <string>
namespace extra2d {
class GLFWWindow;
class WindowModule;
/**
* @brief 应用程序类
*/
class Application {
public:
static Application &get();
Application(const Application &) = delete;
Application &operator=(const Application &) = delete;
/**
* @brief 应用信息
*/
std::string appName = "Extra2D App";
std::string appVersion = "1.0.0";
std::string organization = "";
/**
* @brief 注册模块
* @tparam T 模块类型
* @tparam Args 构造函数参数
* @return 模块指针
*/
template <typename T, typename... Args> T *use(Args &&...args) {
return Registry::instance().use<T>(std::forward<Args>(args)...);
}
/**
* @brief 获取模块
* @tparam T 模块类型
* @return 模块指针
*/
template <typename T> T *get() const { return Registry::instance().get<T>(); }
/**
* @brief 初始化
* @return 初始化成功返回 true
*/
bool init();
/**
* @brief 关闭
*/
void shutdown();
/**
* @brief 运行主循环
*/
void run();
/**
* @brief 请求退出
*/
void quit();
/**
* @brief 暂停
*/
void pause();
/**
* @brief 恢复
*/
void resume();
bool paused() const { return paused_; }
bool running() const { return running_; }
/**
* @brief 获取窗口
* @return 窗口指针
*/
GLFWWindow *window();
f32 dt() const { return dt_; }
f32 totalTime() const { return totalTime_; }
int fps() const { return fps_; }
private:
Application();
~Application();
void mainLoop();
void update();
void render();
bool initialized_ = false;
bool running_ = false;
bool paused_ = false;
bool shouldQuit_ = false;
f32 dt_ = 0.0f;
f32 totalTime_ = 0.0f;
f64 lastFrameTime_ = 0.0;
int frameCount_ = 0;
f32 fpsTimer_ = 0.0f;
int fps_ = 0;
};
} // namespace extra2d