Magic_Game/src/kiwano/renderer/RenderContext.h

424 lines
8.6 KiB
C
Raw Normal View History

2019-08-16 00:50:54 +08:00
// Copyright (c) 2016-2019 Kiwano - Nomango
//
// 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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-11-13 14:33:15 +08:00
#include <kiwano/core/time.h>
2019-12-27 10:51:34 +08:00
#include <kiwano/core/ObjectBase.h>
2019-10-11 21:55:29 +08:00
#include <kiwano/renderer/Brush.h>
#include <kiwano/renderer/Texture.h>
#include <kiwano/renderer/Geometry.h>
#include <kiwano/renderer/TextLayout.h>
#include <kiwano/renderer/LayerArea.h>
#include <kiwano/renderer/win32/TextRenderer.h>
2019-08-16 00:50:54 +08:00
namespace kiwano
{
2019-12-27 10:51:34 +08:00
class Renderer;
2020-01-10 15:22:12 +08:00
KGE_DECLARE_SMART_PTR(RenderContext);
KGE_DECLARE_SMART_PTR(TextureRenderContext);
2019-12-27 10:51:34 +08:00
2019-12-31 16:01:41 +08:00
/**
* \addtogroup Render
* @{
*/
/// \~chinese
/// @brief <20><><EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD>ģʽ
2019-08-21 16:33:41 +08:00
enum class TextAntialiasMode
{
Default, ///< ϵͳĬ<CDB3><C4AC>
ClearType, ///< ClearType <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GrayScale, ///< <20>Ҷȿ<D2B6><C8BF><EFBFBD><EFBFBD><EFBFBD>
None ///< <20><><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>
2019-08-21 16:33:41 +08:00
};
/// \~chinese
2020-01-10 15:22:12 +08:00
/// @brief <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @details <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD>ɻ<EFBFBD><C9BB><EFBFBD>ͼԪ<CDBC>Ļ<EFBFBD><C4BB>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><D8B6><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class KGE_API RenderContext
2020-01-09 08:45:00 +08:00
: public virtual ObjectBase
2019-08-16 00:50:54 +08:00
{
2019-12-28 22:04:08 +08:00
friend class Renderer;
2019-08-16 00:50:54 +08:00
public:
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-20 19:32:36 +08:00
bool IsValid() const;
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void BeginDraw();
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void EndDraw();
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void DrawGeometry(
Geometry const& geometry,
2019-09-29 22:23:13 +08:00
float stroke_width,
2020-01-10 11:43:07 +08:00
const StrokeStyle& stroke = StrokeStyle()
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void FillGeometry(
2019-09-09 22:02:53 +08:00
Geometry const& geometry
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void DrawLine(
Point const& point1,
Point const& point2,
2019-09-29 22:23:13 +08:00
float stroke_width,
2020-01-10 11:43:07 +08:00
const StrokeStyle& stroke = StrokeStyle()
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void DrawRectangle(
Rect const& rect,
2019-09-29 22:23:13 +08:00
float stroke_width,
2020-01-10 11:43:07 +08:00
const StrokeStyle& stroke = StrokeStyle()
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void FillRectangle(
2019-09-09 22:02:53 +08:00
Rect const& rect
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void DrawRoundedRectangle(
Rect const& rect,
Vec2 const& radius,
2019-09-29 22:23:13 +08:00
float stroke_width,
2020-01-10 11:43:07 +08:00
const StrokeStyle& stroke = StrokeStyle()
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void FillRoundedRectangle(
Rect const& rect,
2019-09-09 22:02:53 +08:00
Vec2 const& radius
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void DrawEllipse(
Point const& center,
Vec2 const& radius,
2019-09-29 22:23:13 +08:00
float stroke_width,
2020-01-10 11:43:07 +08:00
const StrokeStyle& stroke = StrokeStyle()
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void FillEllipse(
Point const& center,
2019-09-09 22:02:53 +08:00
Vec2 const& radius
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-21 16:33:41 +08:00
void DrawTexture(
2019-12-26 19:25:43 +08:00
Texture const& texture,
2019-08-16 00:50:54 +08:00
Rect const& src_rect,
Rect const& dest_rect
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-21 16:33:41 +08:00
void DrawTexture(
2019-12-26 19:25:43 +08:00
Texture const& texture,
2019-08-16 00:50:54 +08:00
const Rect* src_rect = nullptr,
const Rect* dest_rect = nullptr
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void DrawTextLayout(
TextLayout const& layout,
Point const& offset = Point{}
2019-12-11 13:44:40 +08:00
);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-12-27 10:51:34 +08:00
void CreateTexture(
Texture& texture,
math::Vec2T<uint32_t> size,
D2D1_PIXEL_FORMAT format
);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-12-28 22:04:08 +08:00
void PushClipRect(Rect const& clip_rect);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void PopClipRect();
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-12-28 22:04:08 +08:00
void PushLayer(LayerArea& layer);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void PopLayer();
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void Clear();
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-12-28 22:04:08 +08:00
void Clear(Color const& clear_color);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
float GetBrushOpacity() const;
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-12-27 23:42:51 +08:00
BrushPtr GetCurrentBrush() const;
2019-09-09 22:02:53 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-20 23:51:12 +08:00
Matrix3x2 GetGlobalTransform() const;
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void SetBrushOpacity(
float opacity
);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-09-09 22:02:53 +08:00
void SetCurrentBrush(
2019-12-27 23:42:51 +08:00
BrushPtr brush
2019-09-09 22:02:53 +08:00
);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-16 00:50:54 +08:00
void SetTransform(
const Matrix3x2& matrix
);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-20 23:51:12 +08:00
void SetGlobalTransform(
const Matrix3x2& matrix
);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-08-21 12:47:19 +08:00
void SetGlobalTransform(
const Matrix3x2* matrix
);
/// \~chinese
/// @brief <20><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ģʽ
2019-08-16 00:50:54 +08:00
void SetAntialiasMode(
bool enabled
);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD>ģʽ
2019-08-16 00:50:54 +08:00
void SetTextAntialiasMode(
2019-08-21 16:33:41 +08:00
TextAntialiasMode mode
2019-08-16 00:50:54 +08:00
);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><DFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-08-20 19:32:36 +08:00
bool CheckVisibility(
2019-08-20 21:15:15 +08:00
Rect const& bounds,
2019-08-20 19:32:36 +08:00
Matrix3x2 const& transform
);
/// \~chinese
2020-01-10 15:22:12 +08:00
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD>С
void Resize(Size const& size);
2019-12-28 22:04:08 +08:00
2019-08-16 00:50:54 +08:00
public:
/// \~chinese
2020-01-10 15:22:12 +08:00
/// @brief <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
2019-08-16 00:50:54 +08:00
struct Status
{
uint32_t primitives; ///< <20><>ȾͼԪ<CDBC><D4AA><EFBFBD><EFBFBD>
Time start; ///< <20><>Ⱦ<EFBFBD><C8BE>ʼʱ<CABC><CAB1>
Duration duration; ///< <20><>Ⱦʱ<C8BE><CAB1>
2019-08-27 15:29:32 +08:00
Status();
2019-08-16 00:50:54 +08:00
};
/// \~chinese
/// @brief <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
2019-12-30 10:00:24 +08:00
void SetCollectingStatus(bool enable);
2019-08-16 00:50:54 +08:00
/// \~chinese
2020-01-10 15:22:12 +08:00
/// @brief <20><>ȡ<EFBFBD><C8A1>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
Status const& GetStatus() const;
2019-08-21 16:33:41 +08:00
2019-12-27 10:51:34 +08:00
protected:
2020-01-10 15:22:12 +08:00
RenderContext();
2019-12-28 22:04:08 +08:00
ComPtr<ID2D1RenderTarget> GetRenderTarget() const;
2019-08-16 00:50:54 +08:00
ComPtr<ITextRenderer> GetTextRenderer() const;
2019-08-20 19:32:36 +08:00
2019-12-28 22:04:08 +08:00
private:
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
2020-01-10 15:22:12 +08:00
HRESULT CreateDeviceResources(ComPtr<ID2D1Factory> factory, ComPtr<ID2D1RenderTarget> ctx);
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
2019-08-20 19:32:36 +08:00
void DiscardDeviceResources();
2019-08-16 00:50:54 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȾͼԪ<CDBC><D4AA><EFBFBD><EFBFBD>
void IncreasePrimitivesCount(uint32_t increase = 1) const;
2019-12-23 18:05:08 +08:00
private:
2019-08-16 00:50:54 +08:00
bool antialias_;
2019-08-21 12:47:19 +08:00
bool fast_global_transform_;
float brush_opacity_;
2019-08-21 16:33:41 +08:00
TextAntialiasMode text_antialias_;
2019-08-16 00:50:54 +08:00
ComPtr<ITextRenderer> text_renderer_;
2020-01-10 15:22:12 +08:00
ComPtr<ID2D1RenderTarget> render_ctx_;
2019-12-27 23:42:51 +08:00
BrushPtr current_brush_;
2019-12-28 22:04:08 +08:00
Rect visible_size_;
2019-08-21 12:47:19 +08:00
Matrix3x2 global_transform_;
2019-12-28 22:04:08 +08:00
mutable bool collecting_status_;
mutable Status status_;
2019-08-16 00:50:54 +08:00
};
2019-12-27 23:42:51 +08:00
/// \~chinese
2020-01-10 15:22:12 +08:00
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @details <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class KGE_API TextureRenderContext
: public RenderContext
2019-08-16 00:50:54 +08:00
{
2019-12-27 10:51:34 +08:00
friend class Renderer;
2019-08-16 00:50:54 +08:00
public:
2019-12-27 23:42:51 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2019-12-27 10:51:34 +08:00
bool IsValid() const;
2019-12-27 23:42:51 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
/// @param[out] texture <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @return <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
2019-12-27 23:42:51 +08:00
bool GetOutput(Texture& texture);
2019-12-27 10:51:34 +08:00
private:
2020-01-10 15:22:12 +08:00
TextureRenderContext();
2019-08-16 00:50:54 +08:00
2019-12-27 10:51:34 +08:00
ComPtr<ID2D1BitmapRenderTarget> GetBitmapRenderTarget() const;
2020-01-10 15:22:12 +08:00
void SetBitmapRenderTarget(ComPtr<ID2D1BitmapRenderTarget> ctx);
2019-12-27 10:51:34 +08:00
private:
ComPtr<ID2D1BitmapRenderTarget> bitmap_rt_;
2019-08-16 00:50:54 +08:00
};
2019-12-27 10:51:34 +08:00
2019-12-31 16:01:41 +08:00
/** @} */
2019-12-27 23:42:51 +08:00
2020-01-10 15:22:12 +08:00
inline RenderContext::Status::Status()
: primitives(0)
{
}
2020-01-10 15:22:12 +08:00
inline RenderContext::Status const& RenderContext::GetStatus() const
{
return status_;
}
2020-01-10 15:22:12 +08:00
inline ComPtr<ID2D1RenderTarget> RenderContext::GetRenderTarget() const
{
2020-01-10 15:22:12 +08:00
KGE_ASSERT(render_ctx_);
return render_ctx_;
}
2020-01-10 15:22:12 +08:00
inline ComPtr<ITextRenderer> RenderContext::GetTextRenderer() const
{
KGE_ASSERT(text_renderer_);
return text_renderer_;
}
2020-01-10 15:22:12 +08:00
inline float RenderContext::GetBrushOpacity() const
{
return brush_opacity_;
}
2020-01-10 15:22:12 +08:00
inline BrushPtr RenderContext::GetCurrentBrush() const
2019-12-27 23:42:51 +08:00
{
return current_brush_;
}
2020-01-10 15:22:12 +08:00
inline Matrix3x2 RenderContext::GetGlobalTransform() const
2019-12-27 23:42:51 +08:00
{
return global_transform_;
}
2020-01-10 15:22:12 +08:00
inline void RenderContext::SetBrushOpacity(float opacity)
{
brush_opacity_ = opacity;
}
2020-01-10 15:22:12 +08:00
inline void RenderContext::SetGlobalTransform(const Matrix3x2& matrix)
2019-12-27 23:42:51 +08:00
{
SetGlobalTransform(&matrix);
}
2020-01-10 15:22:12 +08:00
inline void RenderContext::SetCurrentBrush(BrushPtr brush)
2019-12-27 23:42:51 +08:00
{
current_brush_ = brush;
if (current_brush_)
{
current_brush_->SetOpacity(brush_opacity_);
}
2019-12-27 23:42:51 +08:00
}
2020-01-10 15:22:12 +08:00
inline bool TextureRenderContext::IsValid() const
2019-12-27 10:51:34 +08:00
{
2019-12-27 23:42:51 +08:00
return bitmap_rt_ != nullptr;
2019-12-27 10:51:34 +08:00
}
2020-01-10 15:22:12 +08:00
inline ComPtr<ID2D1BitmapRenderTarget> TextureRenderContext::GetBitmapRenderTarget() const
2019-12-27 10:51:34 +08:00
{
return bitmap_rt_;
}
2020-01-10 15:22:12 +08:00
inline void TextureRenderContext::SetBitmapRenderTarget(ComPtr<ID2D1BitmapRenderTarget> ctx)
2019-12-27 10:51:34 +08:00
{
2020-01-10 15:22:12 +08:00
bitmap_rt_ = ctx;
2019-12-27 10:51:34 +08:00
}
2019-08-16 00:50:54 +08:00
}