feat(渲染): 添加虚拟分辨率支持并重构相机系统

实现虚拟分辨率渲染系统,支持不同缩放模式
重构相机控制器以使用虚拟分辨率计算可见区域
移除硬编码的屏幕尺寸,改为动态获取
添加分辨率状态管理及相关工具函数
更新窗口和渲染器以处理分辨率变化
This commit is contained in:
2026-04-06 23:17:26 +08:00
parent 35c80247b3
commit 62b0f6dafd
16 changed files with 631 additions and 321 deletions

View File

@@ -1,11 +1,12 @@
#pragma once
#include <frostbite2D/graphics/types.h>
#include <frostbite2D/graphics/texture.h>
#include <frostbite2D/graphics/shader.h>
#include <frostbite2D/graphics/shader_manager.h>
#include <frostbite2D/graphics/batch.h>
#include <frostbite2D/graphics/camera.h>
#include <frostbite2D/graphics/render_resolution.h>
#include <frostbite2D/graphics/shader.h>
#include <frostbite2D/graphics/shader_manager.h>
#include <frostbite2D/graphics/texture.h>
#include <frostbite2D/graphics/types.h>
#include <string>
namespace frostbite2D {
@@ -13,55 +14,71 @@ namespace frostbite2D {
class Renderer {
public:
static Renderer& get();
bool init();
void shutdown();
void beginFrame();
void endFrame();
void flush();
void setViewport(int x, int y, int width, int height);
void setWindowSize(int width, int height, float contentScaleX = 1.0f,
float contentScaleY = 1.0f);
void setVirtualResolutionEnabled(bool enabled);
void setVirtualResolution(int width, int height);
void setResolutionScaleMode(ResolutionScaleMode mode);
void setClearColor(float r, float g, float b, float a = 1.0f);
void setClearColor(const Color& color);
void clear(uint32_t flags);
void setCamera(Camera* camera);
Camera* getCamera() { return camera_; }
void drawQuad(const Vec2& pos, const Size& size, float cr = 1.0f, float cg = 1.0f,
float cb = 1.0f, float ca = 1.0f);
int getVirtualWidth() const;
int getVirtualHeight() const;
Rect getViewportRect() const;
const RenderResolutionState& getResolutionState() const {
return resolutionState_;
}
Vec2 screenToVirtual(const Vec2& screenPos) const;
Vec2 virtualToScreen(const Vec2& virtualPos) const;
void drawQuad(const Vec2& pos, const Size& size, float cr = 1.0f,
float cg = 1.0f, float cb = 1.0f, float ca = 1.0f);
void drawQuad(const Vec2& pos, const Size& size, Ptr<Texture> texture);
void drawQuad(const Rect& rect, const Color& color);
void drawSprite(const Vec2& pos, const Size& size, Ptr<Texture> texture);
void drawSprite(const Vec2& pos, const Rect& srcRect, const Vec2& texSize,
Ptr<Texture> texture, const Color& color = Color(1, 1, 1, 1));
void drawSprite(const Vec2& pos, const Rect& srcRect, const Vec2& texSize,
Ptr<Texture> texture,
const Color& color = Color(1, 1, 1, 1));
void setupBlendMode(BlendMode mode);
ShaderManager& getShaderManager() { return shaderManager_; }
Batch& getBatch() { return batch_; }
private:
Renderer();
// ~Renderer() 在 shutdown() 中手动调用销毁
void updateUniforms();
void recalculateResolutionState();
void syncCameraViewport();
ShaderManager shaderManager_;
Batch batch_;
Camera* camera_ = nullptr;
uint32_t clearColor_[4] = {0, 0, 0, 255};
int viewportX_ = 0;
int viewportY_ = 0;
int viewportWidth_ = 1280;
int viewportHeight_ = 720;
bool useVirtualResolution_ = false;
int virtualWidth_ = 1280;
int virtualHeight_ = 720;
ResolutionScaleMode resolutionMode_ = ResolutionScaleMode::Fit;
RenderResolutionState resolutionState_;
bool initialized_ = false;
Renderer(const Renderer&) = delete;
Renderer& operator=(const Renderer&) = delete;
};
}
} // namespace frostbite2D