Magic_Game/src/kiwano/render/RenderContext.h

278 lines
7.9 KiB
C
Raw Normal View History

2019-08-16 00:50:54 +08:00
// Copyright (c) 2016-2019 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
2019-08-16 00:50:54 +08:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
2019-08-16 00:50:54 +08:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
2019-08-16 00:50:54 +08:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2019-12-27 10:51:34 +08:00
#include <kiwano/core/ObjectBase.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/core/Time.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/Brush.h>
2020-02-05 19:56:22 +08:00
#include <kiwano/render/Shape.h>
#include <kiwano/render/Layer.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/render/TextLayout.h>
#include <kiwano/render/Texture.h>
2019-08-16 00:50:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
class Renderer;
KGE_DECLARE_SMART_PTR(RenderContext);
/**
* \addtogroup Render
* @{
*/
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 文字抗锯齿模式
2020-01-21 10:09:55 +08:00
enum class TextAntialiasMode
{
2020-02-10 17:32:04 +08:00
Default, ///< 系统默认
ClearType, ///< ClearType 抗锯齿
GrayScale, ///< 灰度抗锯齿
None ///< 不启用抗锯齿
2020-01-21 10:09:55 +08:00
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 渲染上下文
2020-01-21 10:09:55 +08:00
/// @details
2020-02-10 17:32:04 +08:00
/// 渲染上下文将完成基础图元的绘制,并将绘制结果输出到特定的目标中(如窗口或纹理)
2020-01-21 10:09:55 +08:00
class KGE_API RenderContext : public virtual ObjectBase
{
friend class Renderer;
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 是否有效
virtual bool IsValid() const = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 开始渲染
virtual void BeginDraw();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 结束渲染
virtual void EndDraw();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制纹理
/// @param texture 纹理
/// @param src_rect 源纹理裁剪矩形
/// @param dest_rect 绘制的目标区域
virtual void DrawTexture(Texture const& texture, const Rect* src_rect = nullptr,
const Rect* dest_rect = nullptr) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制文本布局
/// @param layout 文本布局
/// @param offset 偏移量
virtual void DrawTextLayout(TextLayout const& layout, Point const& offset = Point()) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制形状轮廓
/// @param shape 形状
2020-02-16 20:14:01 +08:00
virtual void DrawShape(Shape const& shape) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制线段
/// @param point1 线段起点
/// @param point2 线段终点
2020-02-16 20:14:01 +08:00
virtual void DrawLine(Point const& point1, Point const& point2) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制矩形边框
/// @param rect 矩形
2020-02-16 20:14:01 +08:00
virtual void DrawRectangle(Rect const& rect) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制圆角矩形边框
/// @param rect 矩形
/// @param radius 圆角半径
2020-02-16 20:14:01 +08:00
virtual void DrawRoundedRectangle(Rect const& rect, Vec2 const& radius) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制椭圆边框
/// @param center 圆心
/// @param radius 椭圆半径
2020-02-16 20:14:01 +08:00
virtual void DrawEllipse(Point const& center, Vec2 const& radius) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充形状
/// @param shape 形状
virtual void FillShape(Shape const& shape) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充矩形
/// @param rect 矩形
virtual void FillRectangle(Rect const& rect) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充圆角矩形
/// @param rect 矩形
/// @param radius 圆角半径
virtual void FillRoundedRectangle(Rect const& rect, Vec2 const& radius) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充椭圆
/// @param center 圆心
/// @param radius 椭圆半径
virtual void FillEllipse(Point const& center, Vec2 const& radius) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建纹理
/// @param texture 纹理
/// @param size 纹理像素大小
virtual void CreateTexture(Texture& texture, math::Vec2T<uint32_t> size) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置绘制的裁剪区域
/// @param clip_rect 裁剪矩形
virtual void PushClipRect(Rect const& clip_rect) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 取消上一次设置的绘制裁剪区域
virtual void PopClipRect() = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置图层区域
/// @param layer 图层区域
virtual void PushLayer(Layer& layer) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 取消上一次设置的图层区域
virtual void PopLayer() = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 清空渲染内容
virtual void Clear() = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 使用纯色清空渲染内容
/// @param clear_color 清屏颜色
virtual void Clear(Color const& clear_color) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取渲染区域大小
virtual Size GetSize() const = 0;
2020-02-08 00:17:31 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取画刷透明度
virtual float GetBrushOpacity() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取当前画刷
virtual BrushPtr GetCurrentBrush() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取全局二维变换
virtual const Matrix3x2& GetGlobalTransform() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置画刷透明度
virtual void SetBrushOpacity(float opacity);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-16 20:14:01 +08:00
/// @brief 设置当前使用的画刷
virtual void SetCurrentBrush(BrushPtr brush);
2020-01-21 10:09:55 +08:00
2020-02-16 20:14:01 +08:00
/// \~chinese
/// @brief 设置当前使用的线条样式
virtual void SetCurrentStrokeStyle(StrokeStylePtr stroke);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置抗锯齿模式
virtual void SetAntialiasMode(bool enabled) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置文字抗锯齿模式
virtual void SetTextAntialiasMode(TextAntialiasMode mode) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 检查边界是否在视区内
virtual bool CheckVisibility(Rect const& bounds, Matrix3x2 const& transform) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 重设渲染上下文大小
virtual void Resize(Size const& size) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置上下文的二维变换
virtual void SetTransform(const Matrix3x2& matrix) = 0;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置全局二维变换
virtual void SetGlobalTransform(const Matrix3x2& matrix);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置全局二维变换
virtual void SetGlobalTransform(const Matrix3x2* matrix);
2020-01-21 10:09:55 +08:00
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 渲染上下文状态
2020-01-21 10:09:55 +08:00
struct Status
{
2020-02-10 17:32:04 +08:00
uint32_t primitives; ///< 渲染图元数量
Time start; ///< 渲染起始时间
Duration duration; ///< 渲染时长
2020-01-21 10:09:55 +08:00
Status();
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 启用或禁用状态收集功能
2020-01-21 10:09:55 +08:00
void SetCollectingStatus(bool enable);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取渲染上下文状态
2020-01-21 10:09:55 +08:00
Status const& GetStatus() const;
protected:
RenderContext();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 增加渲染图元数量
2020-01-21 10:09:55 +08:00
void IncreasePrimitivesCount(uint32_t increase = 1) const;
protected:
bool antialias_;
bool fast_global_transform_;
mutable bool collecting_status_;
float brush_opacity_;
TextAntialiasMode text_antialias_;
BrushPtr current_brush_;
2020-02-16 20:14:01 +08:00
StrokeStylePtr current_stroke_;
Rect visible_size_;
Matrix3x2 global_transform_;
mutable Status status_;
2020-01-21 10:09:55 +08:00
};
/** @} */
inline RenderContext::Status::Status()
: primitives(0)
{
}
inline RenderContext::Status const& RenderContext::GetStatus() const
{
return status_;
}
} // namespace kiwano