Files
Frostbite2D/Frostbite2D/include/frostbite2D/core/application.h
Lenheart 62b0f6dafd feat(渲染): 添加虚拟分辨率支持并重构相机系统
实现虚拟分辨率渲染系统,支持不同缩放模式
重构相机控制器以使用虚拟分辨率计算可见区域
移除硬编码的屏幕尺寸,改为动态获取
添加分辨率状态管理及相关工具函数
更新窗口和渲染器以处理分辨率变化
2026-04-06 23:17:26 +08:00

228 lines
4.5 KiB
C++

#pragma once
#include <frostbite2D/core/window.h>
#include <frostbite2D/graphics/camera.h>
#include <frostbite2D/graphics/render_resolution.h>
#include <frostbite2D/types/type_alias.h>
#include <frostbite2D/event/event.h>
#include <string>
#include <memory>
namespace frostbite2D {
/**
* @brief 应用配置结构体
* 仅包含应用级别的配置项,模块配置由各模块自行管理
*/
struct AppConfig {
/**
* @brief 应用名称
*/
std::string appName = "frostbite2D App";
/**
* @brief 应用版本号
*/
std::string appVersion = "1.0.0";
/**
* @brief 组织名称
*/
std::string organization = "frostbite";
/**
* @brief 窗口配置
*/
WindowConfig windowConfig;
/**
* @brief 目标平台
*/
PlatformType targetPlatform = PlatformType::Auto;
/**
* @brief 是否启用虚拟分辨率
*/
bool useVirtualResolution = false;
/**
* @brief 虚拟分辨率宽度
*/
int virtualWidth = 0;
/**
* @brief 虚拟分辨率高度
*/
int virtualHeight = 0;
/**
* @brief 虚拟分辨率缩放模式
*/
ResolutionScaleMode resolutionMode = ResolutionScaleMode::Fit;
/**
* @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:
using StartCallback = Function<void()>;
/**
* @brief 获取单例实例
* @return 应用程序实例引用
*/
static Application& get();
Application(const Application&) = delete;
Application& operator=(const Application&) = delete;
/**
* @brief 使用默认配置初始化
* @return 初始化成功返回 true
*/
bool init();
/**
* @brief 使用指定配置初始化
* @param config 应用配置
* @return 初始化成功返回 true
*/
bool init(const AppConfig& config);
/**
* @brief 关闭应用程序
*/
void shutdown();
/**
* @brief 运行主循环
* @param callback 主循环开始前执行一次的启动回调
*/
void run(StartCallback callback);
/**
* @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 总运行时间(秒)
*/
float totalTime() const { return totalTime_; }
/**
* @brief 获取当前帧率
* @return 帧率
*/
int fps() const { return currentFps_; }
/**
* @brief 获取应用配置
* @return 应用配置常量引用
*/
const AppConfig& getConfig() const;
/**
* @brief 获取窗口
* @return 窗口指针
*/
Window* getWindow() const { return window_; }
/**
* @brief 获取渲染器
* @return 渲染器指针
*/
class Renderer* getRenderer() const { return renderer_; }
/**
* @brief 获取 delta time
* @return 帧间隔时间(秒)
*/
float deltaTime() const { return deltaTime_; }
private:
Application() = default;
~Application();
/**
* @brief 初始化核心模块
* @return 初始化成功返回 true
*/
bool initCoreModules();
/**
* @brief 主循环
*/
void mainLoop();
/**
* @brief 更新
*/
void update();
/**
* @brief 渲染
*/
void render();
std::unique_ptr<Event> convertSDLEvent(const SDL_Event& sdlEvent);
void dispatchEvent(const Event& event);
Window* window_ = nullptr;
class Renderer* renderer_ = nullptr;
Camera* camera_ = nullptr;
AppConfig config_;
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;
const float fpsUpdateInterval_ = 1.0f;
};
}