Extra2D/Extra2D/include/extra2d/render/renderer.h

154 lines
4.0 KiB
C
Raw Normal View History

#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