Magic_Game/src/kiwano/render/DirectX/RendererImpl.h

145 lines
4.6 KiB
C
Raw Normal View History

// Copyright (c) 2016-2018 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
#include <kiwano/render/Renderer.h>
#include <kiwano/render/DirectX/FontCollectionLoader.h>
#include <kiwano/render/DirectX/RenderContextImpl.h>
#if defined(KGE_USE_DIRECTX10)
#include <kiwano/render/DirectX/D3D10DeviceResources.h>
#else
#include <kiwano/render/DirectX/D3D11DeviceResources.h>
#endif
namespace kiwano
{
#if defined(KGE_USE_DIRECTX10)
typedef ID3D10DeviceResources ID3DDeviceResources;
#else
typedef ID3D11DeviceResources ID3DDeviceResources;
#endif
class KGE_API RendererImpl
: public Renderer
{
public:
static RendererImpl& GetInstance();
2020-02-14 22:01:56 +08:00
void CreateTexture(Texture& texture, String const& file_path) override;
2020-02-14 22:01:56 +08:00
void CreateTexture(Texture& texture, Resource const& resource) override;
2020-02-14 22:01:56 +08:00
void CreateGifImage(GifImage& gif, String const& file_path) override;
2020-02-14 22:01:56 +08:00
void CreateGifImage(GifImage& gif, Resource const& resource) override;
2020-02-14 22:01:56 +08:00
void CreateGifImageFrame(GifImage::Frame& frame, GifImage const& gif, size_t frame_index) override;
2020-02-14 22:01:56 +08:00
void CreateFontCollection(Font& font, String const& file_path) override;
2020-02-14 22:01:56 +08:00
void CreateFontCollection(Font& font, Resource const& res) override;
2020-02-16 12:53:18 +08:00
void CreateTextLayout(TextLayout& layout, const String& content, const TextStyle& style) override;
2020-02-14 22:01:56 +08:00
void CreateLineShape(Shape& shape, Point const& begin_pos, Point const& end_pos) override;
2020-02-14 22:01:56 +08:00
void CreateRectShape(Shape& shape, Rect const& rect) override;
2020-02-14 22:01:56 +08:00
void CreateRoundedRectShape(Shape& shape, Rect const& rect, Vec2 const& radius) override;
2020-02-14 22:01:56 +08:00
void CreateEllipseShape(Shape& shape, Point const& center, Vec2 const& radius) override;
2020-02-14 22:01:56 +08:00
void CreateShapeSink(ShapeSink& sink) override;
2020-02-14 22:01:56 +08:00
void CreateBrush(Brush& brush, Color const& color) override;
2020-02-14 22:01:56 +08:00
void CreateBrush(Brush& brush, LinearGradientStyle const& style) override;
2020-02-14 22:01:56 +08:00
void CreateBrush(Brush& brush, RadialGradientStyle const& style) override;
void CreateStrokeStyle(StrokeStyle& stroke_style, CapStyle cap, LineJoinStyle line_join, const float* dash_array,
2020-02-14 22:01:56 +08:00
size_t dash_size, float dash_offset) override;
2020-02-14 22:01:56 +08:00
TextureRenderContextPtr CreateTextureRenderContext(const Size* desired_size = nullptr) override;
public:
2020-02-14 22:01:56 +08:00
void BeginDraw() override;
2020-02-14 22:01:56 +08:00
void EndDraw() override;
2020-02-14 22:01:56 +08:00
void Clear() override;
2020-02-14 22:01:56 +08:00
void Present() override;
2020-02-14 22:01:56 +08:00
RenderContext& GetContext() override;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取Direct2D设备资源
ID2DDeviceResources* GetD2DDeviceResources();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取Direct3D设备资源
ID3DDeviceResources* GetD3DDeviceResources();
2020-02-14 22:01:56 +08:00
/// \~chinese
/// @brief 重设渲染输出大小
void Resize(uint32_t width, uint32_t height) override;
2020-02-14 22:01:56 +08:00
protected:
RendererImpl();
2020-02-14 22:01:56 +08:00
void MakeContextForWindow(WindowPtr window) override;
2020-02-14 22:01:56 +08:00
void Destroy() override;
HRESULT HandleDeviceLost();
private:
RenderContextImplPtr render_ctx_;
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 RenderContext& RendererImpl::GetContext()
{
return *render_ctx_;
}
inline ID2DDeviceResources* RendererImpl::GetD2DDeviceResources()
{
KGE_ASSERT(d2d_res_);
2020-02-15 17:32:32 +08:00
return d2d_res_.Get();
}
inline ID3DDeviceResources* RendererImpl::GetD3DDeviceResources()
{
KGE_ASSERT(d3d_res_);
2020-02-15 17:32:32 +08:00
return d3d_res_.Get();
}
} // namespace kiwano