| 
									
										
										
										
											2017-10-10 01:14:03 +08:00
										 |  |  | #include "winbase.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  | static HWND s_HWnd = nullptr; | 
					
						
							|  |  |  | static ID2D1Factory * s_pDirect2dFactory = nullptr; | 
					
						
							|  |  |  | static ID2D1HwndRenderTarget * s_pRenderTarget = nullptr; | 
					
						
							|  |  |  | static ID2D1SolidColorBrush * s_pSolidBrush = nullptr; | 
					
						
							|  |  |  | static IWICImagingFactory * s_pIWICFactory = nullptr; | 
					
						
							|  |  |  | static IDWriteFactory * s_pDWriteFactory = nullptr; | 
					
						
							|  |  |  | static steady_clock::time_point s_tNow; | 
					
						
							| 
									
										
										
										
											2017-10-10 01:14:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 02:44:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | HWND &GetHWnd() | 
					
						
							| 
									
										
										
										
											2017-10-10 01:14:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 	return s_HWnd; | 
					
						
							| 
									
										
										
										
											2017-10-10 01:14:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 02:44:44 +08:00
										 |  |  | ID2D1Factory * &GetFactory() | 
					
						
							| 
									
										
										
										
											2017-10-10 01:14:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 	return s_pDirect2dFactory; | 
					
						
							| 
									
										
										
										
											2017-10-11 11:15:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 23:58:39 +08:00
										 |  |  | IWICImagingFactory * &GetImagingFactory() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 	if (!s_pIWICFactory) | 
					
						
							| 
									
										
										
										
											2017-10-15 23:58:39 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		CoCreateInstance( | 
					
						
							|  |  |  | 			CLSID_WICImagingFactory, | 
					
						
							|  |  |  | 			NULL, | 
					
						
							|  |  |  | 			CLSCTX_INPROC_SERVER, | 
					
						
							|  |  |  | 			IID_IWICImagingFactory, | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 			reinterpret_cast<void **>(&s_pIWICFactory) | 
					
						
							| 
									
										
										
										
											2017-10-15 23:58:39 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 	return s_pIWICFactory; | 
					
						
							| 
									
										
										
										
											2017-10-15 23:58:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 02:44:44 +08:00
										 |  |  | ID2D1HwndRenderTarget * &GetRenderTarget() | 
					
						
							| 
									
										
										
										
											2017-10-11 11:15:17 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 	if (!s_pRenderTarget) | 
					
						
							| 
									
										
										
										
											2017-10-15 23:58:39 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		RECT rc; | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 		GetClientRect(s_HWnd, &rc); | 
					
						
							| 
									
										
										
										
											2017-10-15 23:58:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		D2D1_SIZE_U size = D2D1::SizeU( | 
					
						
							|  |  |  | 			rc.right - rc.left, | 
					
						
							|  |  |  | 			rc.bottom - rc.top | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Create a Direct2D render target.
 | 
					
						
							|  |  |  | 		HRESULT hr; | 
					
						
							| 
									
										
										
										
											2017-10-20 00:59:26 +08:00
										 |  |  | 		hr = GetFactory()->CreateHwndRenderTarget( | 
					
						
							| 
									
										
										
										
											2017-10-15 23:58:39 +08:00
										 |  |  | 			D2D1::RenderTargetProperties(), | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 			D2D1::HwndRenderTargetProperties(s_HWnd, size), | 
					
						
							|  |  |  | 			&s_pRenderTarget | 
					
						
							| 
									
										
										
										
											2017-10-15 23:58:39 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		ASSERT(SUCCEEDED(hr), "Create Render Target Failed!"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 	return s_pRenderTarget; | 
					
						
							| 
									
										
										
										
											2017-10-11 11:15:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  | IDWriteFactory * &GetDirectWriteFactory() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!s_pDWriteFactory) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		HRESULT hr; | 
					
						
							|  |  |  | 		hr = DWriteCreateFactory( | 
					
						
							|  |  |  | 			DWRITE_FACTORY_TYPE_SHARED, | 
					
						
							|  |  |  | 			__uuidof(IDWriteFactory), | 
					
						
							|  |  |  | 			reinterpret_cast<IUnknown**>(&s_pDWriteFactory) | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		ASSERT(SUCCEEDED(hr), "Create DirectWrite Factory Failed!"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return s_pDWriteFactory; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 23:58:39 +08:00
										 |  |  | ID2D1SolidColorBrush * &GetSolidColorBrush() | 
					
						
							| 
									
										
										
										
											2017-10-14 18:43:32 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 	if (!s_pSolidBrush) | 
					
						
							| 
									
										
										
										
											2017-10-15 02:46:24 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-10-20 00:59:26 +08:00
										 |  |  | 		HRESULT hr; | 
					
						
							|  |  |  | 		hr = GetRenderTarget()->CreateSolidColorBrush( | 
					
						
							|  |  |  | 			D2D1::ColorF(D2D1::ColorF::White),  | 
					
						
							|  |  |  | 			&s_pSolidBrush | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		ASSERT(SUCCEEDED(hr), "Create Solid Color Brush Failed!"); | 
					
						
							| 
									
										
										
										
											2017-10-15 02:46:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | 	return s_pSolidBrush; | 
					
						
							| 
									
										
										
										
											2017-10-14 18:43:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:22:25 +08:00
										 |  |  | steady_clock::time_point &GetNow() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return s_tNow; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | long long GetInterval(steady_clock::time_point tLast) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return duration_cast<milliseconds>(s_tNow - tLast).count(); | 
					
						
							|  |  |  | } |