Magic_Game/src/kiwano/render/Renderer.h

304 lines
8.1 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +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
//
// 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
//
// 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/Component.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/platform/Window.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/Font.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/render/GifImage.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/TextStyle.hpp>
2020-02-08 00:17:31 +08:00
#include <kiwano/render/RenderContext.h>
#include <kiwano/render/TextureRenderContext.h>
2020-01-21 11:05:28 +08:00
#include <kiwano/render/DirectX/FontCollectionLoader.h>
2019-07-30 00:41:06 +08:00
#if defined(KGE_USE_DIRECTX10)
2020-01-21 11:05:28 +08:00
#include <kiwano/render/DirectX/D3D10DeviceResources.h>
2019-07-30 00:41:06 +08:00
#else
2020-01-21 11:05:28 +08:00
#include <kiwano/render/DirectX/D3D11DeviceResources.h>
2019-07-30 00:41:06 +08:00
#endif
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2019-07-30 00:41:06 +08:00
#if defined(KGE_USE_DIRECTX10)
2020-01-21 10:09:55 +08:00
typedef ID3D10DeviceResources ID3DDeviceResources;
2019-07-30 00:41:06 +08:00
#else
2020-01-21 10:09:55 +08:00
typedef ID3D11DeviceResources ID3DDeviceResources;
2019-07-30 00:41:06 +08:00
#endif
2020-01-21 10:09:55 +08:00
/**
* \~chinese
* \defgroup Render <EFBFBD><EFBFBD>Ⱦ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
/**
* \addtogroup Render
* @{
*/
/**
* \~chinese
* @brief <EFBFBD><EFBFBD>Ⱦ<EFBFBD><EFBFBD>
*/
class KGE_API Renderer
: public Singleton<Renderer>
, public EventComponent
{
friend Singleton<Renderer>;
public:
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
Color const& GetClearColor() const;
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
void SetClearColor(Color const& clear_color);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>رմ<D8B1>ֱͬ<D6B1><CDAC>
void SetVSyncEnabled(bool enabled);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>DPI
void SetDpi(float dpi);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param[out] texture <20><><EFBFBD><EFBFBD>
/// @param[in] file_path ͼƬ·<C6AC><C2B7>
void CreateTexture(Texture& texture, String const& file_path);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param[out] texture <20><><EFBFBD><EFBFBD>
/// @param[in] resource ͼƬ<CDBC><C6AC>Դ
void CreateTexture(Texture& texture, Resource const& resource);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>GIFͼ<46><CDBC>
/// @param[out] gif GIFͼ<46><CDBC>
/// @param[in] file_path ͼƬ·<C6AC><C2B7>
void CreateGifImage(GifImage& gif, String const& file_path);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>GIFͼ<46><CDBC>
/// @param[out] gif GIFͼ<46><CDBC>
/// @param[in] resource ͼƬ<CDBC><C6AC>Դ
void CreateGifImage(GifImage& gif, Resource const& resource);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>GIFͼ<46><CDBC>֡
/// @param[out] frame GIFͼ<46><CDBC>֡
/// @param[in] gif GIFͼ<46><CDBC>
/// @param[in] frame_index ֡<>±<EFBFBD>
void CreateGifImageFrame(GifImage::Frame& frame, GifImage const& gif, size_t frame_index);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param[out] font <20><><EFBFBD><EFBFBD>
/// @param[in] file_paths <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7>
2020-02-06 16:54:47 +08:00
void CreateFontCollection(Font& font, String const& file_path);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param[out] font <20><><EFBFBD><EFBFBD>
/// @param[in] res_arr <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
2020-02-06 16:54:47 +08:00
void CreateFontCollection(Font& font, Resource const& res);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>ʽ
/// @param[out] layout <20><><EFBFBD><EFBFBD><E5B2BC>
void CreateTextFormat(TextLayout& layout);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD>
/// @param[out] layout <20><><EFBFBD><EFBFBD><E5B2BC>
void CreateTextLayout(TextLayout& layout);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><DFB6><EFBFBD>״
/// @param[out] geo <20><>״
/// @param[in] begin_pos <20>߶<EFBFBD><DFB6><EFBFBD><EFBFBD><EFBFBD>
/// @param[in] end_pos <20>߶<EFBFBD><DFB6>յ<EFBFBD>
2020-02-05 19:56:22 +08:00
void CreateLineShape(Shape& shape, Point const& begin_pos, Point const& end_pos);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״
/// @param[out] geo <20><>״
/// @param[in] rect <20><><EFBFBD>δ<EFBFBD>С
2020-02-05 19:56:22 +08:00
void CreateRectShape(Shape& shape, Rect const& rect);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>Բ<EFBFBD>Ǿ<EFBFBD><C7BE><EFBFBD><EFBFBD><EFBFBD>״
/// @param[out] geo <20><>״
/// @param[in] rect <20><><EFBFBD>δ<EFBFBD>С
/// @param[in] radius Բ<>ǰ뾶
2020-02-05 19:56:22 +08:00
void CreateRoundedRectShape(Shape& shape, Rect const& rect, Vec2 const& radius);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Բ<EFBFBD><D4B2>״
/// @param[out] geo <20><>״
/// @param[in] center <20><>ԲԲ<D4B2><D4B2>
/// @param[in] radius <20><>Բ<EFBFBD>
2020-02-05 19:56:22 +08:00
void CreateEllipseShape(Shape& shape, Point const& center, Vec2 const& radius);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param[out] sink <20><>״<EFBFBD><D7B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2020-02-05 19:56:22 +08:00
void CreateShapeSink(ShapeSink& sink);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param[out] render_context <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2020-02-07 20:50:27 +08:00
/// @param[in] desired_size <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
2020-02-08 00:17:31 +08:00
void CreateTextureRenderContext(TextureRenderContext& render_context, const Size* desired_size = nullptr);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB>ˢ
/// @param[out] brush <20><>ˢ
/// @param[in] color <20><>ɫ
2020-02-06 16:54:47 +08:00
void CreateBrush(Brush& brush, Color const& color);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>仭ˢ
/// @param[out] brush <20><>ˢ
/// @param[in] style <20><><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD>ʽ
2020-02-06 16:54:47 +08:00
void CreateBrush(Brush& brush, LinearGradientStyle const& style);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򽥱仭ˢ
/// @param[out] brush <20><>ˢ
/// @param[in] style <20><><EFBFBD>򽥱<EFBFBD><F2BDA5B1><EFBFBD>ʽ
2020-02-06 16:54:47 +08:00
void CreateBrush(Brush& brush, RadialGradientStyle const& style);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
/// @param[out] stroke_style <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
/// @param[in] cap <20>߶ζ˵<CEB6><CBB5><EFBFBD>ʽ
/// @param[in] line_join <20>߶<EFBFBD><DFB6><EFBFBD><E0BDBB>ʽ
/// @param[in] dash_array <20><><EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>
/// @param[in] dash_size <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
/// @param[in] dash_offset <20><><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>
void CreateStrokeStyle(StrokeStyle& stroke_style, CapStyle cap, LineJoinStyle line_join, const float* dash_array,
size_t dash_size, float dash_offset);
public:
/// \~chinese
/// @brief <20><>ʼ<EFBFBD><CABC>Ⱦ
void BeginDraw();
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ
void EndDraw();
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void Clear();
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void Present();
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
RenderContext& GetContext();
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡĿ<C8A1><EFBFBD><EAB4B0>
WindowHandle GetTargetWindow() const;
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
Size const& GetOutputSize() const;
/// \~chinese
/// @brief <20><>ȡDirect2D<32><EFBFBD><E8B1B8>Դ
ID2DDeviceResources* GetD2DDeviceResources();
/// \~chinese
/// @brief <20><>ȡDirect3D<33><EFBFBD><E8B1B8>Դ
ID3DDeviceResources* GetD3DDeviceResources();
public:
void SetupComponent() override;
void DestroyComponent() override;
void HandleEvent(Event* evt) override;
private:
Renderer();
~Renderer();
HRESULT HandleDeviceLost();
void ResizeTarget(uint32_t width, uint32_t height);
private:
bool vsync_;
WindowHandle target_window_;
Color clear_color_;
Size output_size_;
RenderContext render_ctx_;
2020-01-21 10:09:55 +08:00
ComPtr<ID2DDeviceResources> d2d_res_;
ComPtr<ID3DDeviceResources> d3d_res_;
ComPtr<IFontCollectionLoader> font_collection_loader_;
ComPtr<IResourceFontFileLoader> res_font_file_loader_;
ComPtr<IResourceFontCollectionLoader> res_font_collection_loader_;
};
/** @} */
inline WindowHandle Renderer::GetTargetWindow() const
{
return target_window_;
}
inline Size const& Renderer::GetOutputSize() const
{
return output_size_;
}
inline Color const& Renderer::GetClearColor() const
{
return clear_color_;
}
inline RenderContext& Renderer::GetContext()
{
return render_ctx_;
}
2020-01-21 10:09:55 +08:00
inline ID2DDeviceResources* Renderer::GetD2DDeviceResources()
{
KGE_ASSERT(d2d_res_);
return d2d_res_.get();
}
inline ID3DDeviceResources* Renderer::GetD3DDeviceResources()
{
KGE_ASSERT(d3d_res_);
return d3d_res_.get();
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano