| 
									
										
										
										
											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-08-18 10:23:54 +08:00
										 |  |  |  | #include "../base/time.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | #include "Image.h"
 | 
					
						
							|  |  |  |  | #include "Geometry.h"
 | 
					
						
							|  |  |  |  | #include "TextLayout.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-16 10:12:34 +08:00
										 |  |  |  | #include "LayerArea.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-18 10:23:54 +08:00
										 |  |  |  | #include "win32/TextRenderer.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace kiwano | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	// <20><>ȾĿ<C8BE><C4BF>
 | 
					
						
							|  |  |  |  | 	class KGE_API RenderTarget | 
					
						
							|  |  |  |  | 		: public noncopyable | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 	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, | 
					
						
							|  |  |  |  | 			Color const& stroke_color, | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 			Float32 stroke_width, | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			StrokeStyle stroke = StrokeStyle::Miter | 
					
						
							|  |  |  |  | 		) const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void FillGeometry( | 
					
						
							|  |  |  |  | 			Geometry const& geometry, | 
					
						
							|  |  |  |  | 			Color const& fill_color | 
					
						
							|  |  |  |  | 		) const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void DrawLine( | 
					
						
							|  |  |  |  | 			Point const& point1, | 
					
						
							|  |  |  |  | 			Point const& point2, | 
					
						
							|  |  |  |  | 			Color const& stroke_color, | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 			Float32 stroke_width, | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			StrokeStyle stroke = StrokeStyle::Miter | 
					
						
							|  |  |  |  | 		) const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void DrawRectangle( | 
					
						
							|  |  |  |  | 			Rect const& rect, | 
					
						
							|  |  |  |  | 			Color const& stroke_color, | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 			Float32 stroke_width, | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			StrokeStyle stroke = StrokeStyle::Miter | 
					
						
							|  |  |  |  | 		) const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void FillRectangle( | 
					
						
							|  |  |  |  | 			Rect const& rect, | 
					
						
							|  |  |  |  | 			Color const& fill_color | 
					
						
							|  |  |  |  | 		) const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void DrawRoundedRectangle( | 
					
						
							|  |  |  |  | 			Rect const& rect, | 
					
						
							|  |  |  |  | 			Vec2 const& radius, | 
					
						
							|  |  |  |  | 			Color const& stroke_color, | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 			Float32 stroke_width, | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			StrokeStyle stroke = StrokeStyle::Miter | 
					
						
							|  |  |  |  | 		) const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void FillRoundedRectangle( | 
					
						
							|  |  |  |  | 			Rect const& rect, | 
					
						
							|  |  |  |  | 			Vec2 const& radius, | 
					
						
							|  |  |  |  | 			Color const& fill_color | 
					
						
							|  |  |  |  | 		) const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void DrawEllipse( | 
					
						
							|  |  |  |  | 			Point const& center, | 
					
						
							|  |  |  |  | 			Vec2 const& radius, | 
					
						
							|  |  |  |  | 			Color const& stroke_color, | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 			Float32 stroke_width, | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			StrokeStyle stroke = StrokeStyle::Miter | 
					
						
							|  |  |  |  | 		) const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void FillEllipse( | 
					
						
							|  |  |  |  | 			Point const& center, | 
					
						
							|  |  |  |  | 			Vec2 const& radius, | 
					
						
							|  |  |  |  | 			Color const& fill_color | 
					
						
							|  |  |  |  | 		) const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void DrawImage( | 
					
						
							|  |  |  |  | 			Image const& image, | 
					
						
							|  |  |  |  | 			Rect const& src_rect, | 
					
						
							|  |  |  |  | 			Rect const& dest_rect | 
					
						
							|  |  |  |  | 		) const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void DrawImage( | 
					
						
							|  |  |  |  | 			Image const& image, | 
					
						
							|  |  |  |  | 			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-08-18 22:49:44 +08:00
										 |  |  |  | 		Float32 GetOpacity() const; | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void SetOpacity( | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 			Float32 opacity | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 		); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void SetTransform( | 
					
						
							|  |  |  |  | 			const Matrix3x2& matrix | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ģʽ
 | 
					
						
							|  |  |  |  | 		void SetAntialiasMode( | 
					
						
							|  |  |  |  | 			bool enabled | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD>ģʽ
 | 
					
						
							|  |  |  |  | 		void SetTextAntialiasMode( | 
					
						
							|  |  |  |  | 			TextAntialias mode | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-20 19:32:36 +08:00
										 |  |  |  | 		bool CheckVisibility( | 
					
						
							|  |  |  |  | 			Size const& content_size, | 
					
						
							|  |  |  |  | 			Matrix3x2 const& transform | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 	public: | 
					
						
							|  |  |  |  | 		struct Status | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 			Int32 primitives; | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			Time start; | 
					
						
							|  |  |  |  | 			Duration duration; | 
					
						
							|  |  |  |  | 		}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void SetCollectingStatus(bool collecting); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void IncreasePrimitivesCount() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 10:23:54 +08:00
										 |  |  |  | 		inline Status const& GetStatus() const						{ return status_; } | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-20 19:32:36 +08:00
										 |  |  |  | 		inline ComPtr<ID2D1RenderTarget> GetRenderTarget() const	{ KGE_ASSERT(render_target_); return render_target_; } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		inline ComPtr<ITextRenderer> GetTextRenderer() const		{ KGE_ASSERT(text_renderer_); return text_renderer_; } | 
					
						
							| 
									
										
										
										
											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-08-18 22:49:44 +08:00
										 |  |  |  | 		Float32							opacity_; | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 		bool							antialias_; | 
					
						
							|  |  |  |  | 		mutable bool					collecting_status_; | 
					
						
							|  |  |  |  | 		mutable Status					status_; | 
					
						
							|  |  |  |  | 		TextAntialias					text_antialias_; | 
					
						
							|  |  |  |  | 		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-16 00:50:54 +08:00
										 |  |  |  | 	}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// λͼ<CEBB><CDBC>ȾĿ<C8BE><C4BF>
 | 
					
						
							|  |  |  |  | 	class KGE_API ImageRenderTarget | 
					
						
							|  |  |  |  | 		: public RenderTarget | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 	public: | 
					
						
							|  |  |  |  | 		ImageRenderTarget(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-20 19:32:36 +08:00
										 |  |  |  | 		Image GetOutput() const; | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 	}; | 
					
						
							|  |  |  |  | } |