Magic_Game/src/kiwano/renderer/RenderTarget.h

247 lines
5.2 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-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-08-21 16:33:41 +08:00
// <20><><EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD>ģʽ
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-16 00:50:54 +08:00
// <20><>ȾĿ<C8BE><C4BF>
class KGE_API RenderTarget
2019-11-13 14:33:15 +08:00
: public common::noncopyable
2019-08-16 00:50:54 +08:00
{
public:
2019-08-20 19:32:36 +08:00
bool IsValid() const;
2019-08-16 00:50:54 +08:00
void BeginDraw();
void EndDraw();
void CreateLayer(
2019-08-16 10:12:34 +08:00
LayerArea& layer
2019-08-16 00:50:54 +08:00
) const;
void DrawGeometry(
Geometry const& geometry,
2019-09-29 22:23:13 +08:00
float stroke_width,
2019-08-16 00:50:54 +08:00
StrokeStyle stroke = StrokeStyle::Miter
) const;
void FillGeometry(
2019-09-09 22:02:53 +08:00
Geometry const& geometry
2019-08-16 00:50:54 +08:00
) const;
void DrawLine(
Point const& point1,
Point const& point2,
2019-09-29 22:23:13 +08:00
float stroke_width,
2019-08-16 00:50:54 +08:00
StrokeStyle stroke = StrokeStyle::Miter
) const;
void DrawRectangle(
Rect const& rect,
2019-09-29 22:23:13 +08:00
float stroke_width,
2019-08-16 00:50:54 +08:00
StrokeStyle stroke = StrokeStyle::Miter
) const;
void FillRectangle(
2019-09-09 22:02:53 +08:00
Rect const& rect
2019-08-16 00:50:54 +08:00
) const;
void DrawRoundedRectangle(
Rect const& rect,
Vec2 const& radius,
2019-09-29 22:23:13 +08:00
float stroke_width,
2019-08-16 00:50:54 +08:00
StrokeStyle stroke = StrokeStyle::Miter
) const;
void FillRoundedRectangle(
Rect const& rect,
2019-09-09 22:02:53 +08:00
Vec2 const& radius
2019-08-16 00:50:54 +08:00
) const;
void DrawEllipse(
Point const& center,
Vec2 const& radius,
2019-09-29 22:23:13 +08:00
float stroke_width,
2019-08-16 00:50:54 +08:00
StrokeStyle stroke = StrokeStyle::Miter
) const;
void FillEllipse(
Point const& center,
2019-09-09 22:02:53 +08:00
Vec2 const& radius
2019-08-16 00:50:54 +08:00
) const;
2019-08-21 16:33:41 +08:00
void DrawTexture(
Texture const& texture,
2019-08-16 00:50:54 +08:00
Rect const& src_rect,
Rect const& dest_rect
) const;
2019-08-21 16:33:41 +08:00
void DrawTexture(
Texture const& texture,
2019-08-16 00:50:54 +08:00
const Rect* src_rect = nullptr,
const Rect* dest_rect = nullptr
) const;
void DrawTextLayout(
TextLayout const& layout,
Point const& offset = Point{}
) const;
void PushClipRect(
Rect const& clip_rect
);
void PopClipRect();
void PushLayer(
2019-08-20 19:32:36 +08:00
LayerArea& layer
2019-08-16 00:50:54 +08:00
);
void PopLayer();
void Clear();
void Clear(
Color const& clear_color
);
2019-09-29 22:23:13 +08:00
float GetOpacity() const;
2019-08-16 00:50:54 +08:00
2019-09-09 22:02:53 +08:00
Brush GetCurrentBrush() const;
2019-08-20 23:51:12 +08:00
Matrix3x2 GetGlobalTransform() const;
2019-08-16 00:50:54 +08:00
void SetOpacity(
2019-09-29 22:23:13 +08:00
float opacity
2019-08-16 00:50:54 +08:00
);
2019-09-09 22:02:53 +08:00
void SetCurrentBrush(
Brush const& brush
);
void SetDefaultBrushColor(
Color const& color
);
2019-08-16 00:50:54 +08:00
void SetTransform(
const Matrix3x2& matrix
);
2019-08-20 23:51:12 +08:00
void SetGlobalTransform(
const Matrix3x2& matrix
);
2019-08-21 12:47:19 +08:00
void SetGlobalTransform(
const Matrix3x2* matrix
);
2019-08-16 00:50:54 +08:00
// <20><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ģʽ
void SetAntialiasMode(
bool enabled
);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD>ģʽ
void SetTextAntialiasMode(
2019-08-21 16:33:41 +08:00
TextAntialiasMode mode
2019-08-16 00:50:54 +08:00
);
2019-08-21 12:47:19 +08:00
// <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
);
2019-08-16 00:50:54 +08:00
public:
struct Status
{
2019-09-29 22:23:13 +08:00
int primitives;
2019-08-16 00:50:54 +08:00
Time start;
Duration duration;
2019-08-27 15:29:32 +08:00
Status() : primitives(0) {}
2019-08-16 00:50:54 +08:00
};
void SetCollectingStatus(bool collecting);
void IncreasePrimitivesCount() const;
2019-09-29 22:23:13 +08:00
inline Status const& GetStatus() const { return status_; }
2019-08-21 16:33:41 +08:00
2019-09-29 22:23:13 +08:00
inline ComPtr<ID2D1RenderTarget> GetRenderTarget() const { KGE_ASSERT(render_target_); return render_target_; }
2019-08-16 00:50:54 +08:00
2019-09-29 22:23:13 +08:00
inline ComPtr<ITextRenderer> GetTextRenderer() const { KGE_ASSERT(text_renderer_); return text_renderer_; }
2019-08-20 19:32:36 +08:00
2019-08-21 16:33:41 +08:00
ComPtr<ID2D1StrokeStyle> GetStrokeStyle(StrokeStyle style) const;
2019-08-16 00:50:54 +08:00
public:
RenderTarget();
2019-08-20 19:32:36 +08:00
HRESULT CreateDeviceResources(
2019-08-16 00:50:54 +08:00
ComPtr<ID2D1RenderTarget> rt,
ComPtr<ID2DDeviceResources> dev_res
);
2019-08-20 19:32:36 +08:00
void DiscardDeviceResources();
2019-08-16 00:50:54 +08:00
protected:
2019-09-29 22:23:13 +08:00
float opacity_;
2019-08-16 00:50:54 +08:00
bool antialias_;
2019-08-21 12:47:19 +08:00
bool fast_global_transform_;
2019-08-16 00:50:54 +08:00
mutable bool collecting_status_;
mutable Status status_;
2019-08-21 16:33:41 +08:00
TextAntialiasMode text_antialias_;
2019-08-16 00:50:54 +08:00
ComPtr<ITextRenderer> text_renderer_;
2019-08-20 19:32:36 +08:00
ComPtr<ID2D1RenderTarget> render_target_;
ComPtr<ID2D1SolidColorBrush> default_brush_;
ComPtr<ID2D1Brush> current_brush_;
ComPtr<ID2DDeviceResources> device_resources_;
2019-08-21 12:47:19 +08:00
Matrix3x2 global_transform_;
2019-08-16 00:50:54 +08:00
};
// λͼ<CEBB><CDBC>ȾĿ<C8BE><C4BF>
2019-08-21 16:33:41 +08:00
class KGE_API TextureRenderTarget
2019-08-16 00:50:54 +08:00
: public RenderTarget
{
public:
2019-08-21 16:33:41 +08:00
TextureRenderTarget();
2019-08-16 00:50:54 +08:00
2019-08-21 16:33:41 +08:00
Texture GetOutput() const;
2019-08-16 00:50:54 +08:00
};
}