154 lines
4.0 KiB
C
154 lines
4.0 KiB
C
|
|
#pragma once
|
|||
|
|
|
|||
|
|
#include <extra2d/core/color.h>
|
|||
|
|
#include <extra2d/core/math_types.h>
|
|||
|
|
#include <extra2d/render/camera.h>
|
|||
|
|
#include <extra2d/render/shape_renderer.h>
|
|||
|
|
#include <extra2d/render/sprite_renderer.h>
|
|||
|
|
#include <extra2d/render/texture.h>
|
|||
|
|
#include <extra2d/render/render_context.h>
|
|||
|
|
#include <glm/mat4x4.hpp>
|
|||
|
|
#include <memory>
|
|||
|
|
|
|||
|
|
namespace extra2d {
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 高层渲染器
|
|||
|
|
*
|
|||
|
|
* 封装 ShapeRenderer 和 SpriteRenderer,提供简单易用的 2D 渲染 API
|
|||
|
|
*/
|
|||
|
|
class Renderer {
|
|||
|
|
public:
|
|||
|
|
Renderer();
|
|||
|
|
~Renderer();
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 初始化渲染器
|
|||
|
|
*/
|
|||
|
|
bool init();
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 关闭渲染器
|
|||
|
|
*/
|
|||
|
|
void shutdown();
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 开始帧渲染
|
|||
|
|
* @param clearColor 清屏颜色
|
|||
|
|
*/
|
|||
|
|
void beginFrame(const Color& clearColor = Colors::Black);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 结束帧渲染
|
|||
|
|
*/
|
|||
|
|
void endFrame();
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 设置视图投影矩阵
|
|||
|
|
*/
|
|||
|
|
void setViewProjection(const glm::mat4& viewProjection);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取视图投影矩阵
|
|||
|
|
*/
|
|||
|
|
const glm::mat4& viewProjection() const { return viewProjection_; }
|
|||
|
|
|
|||
|
|
// =========================================================================
|
|||
|
|
// 图形绘制 API
|
|||
|
|
// =========================================================================
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制线条
|
|||
|
|
*/
|
|||
|
|
void drawLine(const Vec2& start, const Vec2& end, const Color& color, float width = 1.0f);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制矩形(描边)
|
|||
|
|
*/
|
|||
|
|
void drawRect(const Rect& rect, const Color& color, float width = 1.0f);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制矩形(填充)
|
|||
|
|
*/
|
|||
|
|
void fillRect(const Rect& rect, const Color& color);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制圆形(描边)
|
|||
|
|
*/
|
|||
|
|
void drawCircle(const Vec2& center, float radius, const Color& color,
|
|||
|
|
int segments = 32, float width = 1.0f);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制圆形(填充)
|
|||
|
|
*/
|
|||
|
|
void fillCircle(const Vec2& center, float radius, const Color& color, int segments = 32);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制三角形(描边)
|
|||
|
|
*/
|
|||
|
|
void drawTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3,
|
|||
|
|
const Color& color, float width = 1.0f);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制三角形(填充)
|
|||
|
|
*/
|
|||
|
|
void fillTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3, const Color& color);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制多边形(描边)
|
|||
|
|
*/
|
|||
|
|
void drawPolygon(const std::vector<Vec2>& points, const Color& color, float width = 1.0f);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制多边形(填充)
|
|||
|
|
*/
|
|||
|
|
void fillPolygon(const std::vector<Vec2>& points, const Color& color);
|
|||
|
|
|
|||
|
|
// =========================================================================
|
|||
|
|
// 精灵绘制 API
|
|||
|
|
// =========================================================================
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制精灵
|
|||
|
|
*/
|
|||
|
|
void drawSprite(const Texture& texture, const Rect& destRect, const Rect& srcRect,
|
|||
|
|
const Color& color = Colors::White, float rotation = 0.0f,
|
|||
|
|
const Vec2& anchor = Vec2(0.5f, 0.5f));
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制精灵(简化版)
|
|||
|
|
*/
|
|||
|
|
void drawSprite(const Texture& texture, const Vec2& position,
|
|||
|
|
const Color& color = Colors::White);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 绘制精灵(带缩放)
|
|||
|
|
*/
|
|||
|
|
void drawSprite(const Texture& texture, const Vec2& position, const Vec2& scale,
|
|||
|
|
const Color& color = Colors::White);
|
|||
|
|
|
|||
|
|
// =========================================================================
|
|||
|
|
// 统计信息
|
|||
|
|
// =========================================================================
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取绘制调用次数
|
|||
|
|
*/
|
|||
|
|
uint32 drawCalls() const;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取精灵数量
|
|||
|
|
*/
|
|||
|
|
uint32 spriteCount() const;
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
glm::vec4 toVec4(const Color& color) const;
|
|||
|
|
|
|||
|
|
std::unique_ptr<ShapeRenderer> shapeRenderer_;
|
|||
|
|
std::unique_ptr<SpriteRenderer> spriteRenderer_;
|
|||
|
|
glm::mat4 viewProjection_{1.0f};
|
|||
|
|
bool inFrame_ = false;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
} // namespace extra2d
|