170 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C++
		
	
	
	
		
		
			
		
	
	
			170 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C++
		
	
	
	
|  | #include "..\ebase.h"
 | |||
|  | #include "..\emanagers.h"
 | |||
|  | 
 | |||
|  | 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 D2D1_COLOR_F s_nClearColor = D2D1::ColorF(D2D1::ColorF::Black); | |||
|  | 
 | |||
|  | 
 | |||
|  | bool e2d::ERenderer::__createDeviceIndependentResources() | |||
|  | { | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><EFBFBD><DEB9><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ںͳ<DABA><CDB3><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ͬ
 | |||
|  | 	HRESULT hr = D2D1CreateFactory( | |||
|  | 		D2D1_FACTORY_TYPE_SINGLE_THREADED,  | |||
|  | 		&s_pDirect2dFactory | |||
|  | 	); | |||
|  | 
 | |||
|  | 	ASSERT(SUCCEEDED(hr), "Create Device Independent Resources Failed!"); | |||
|  | 
 | |||
|  | 	if (SUCCEEDED(hr)) | |||
|  | 	{ | |||
|  | 		// <20><><EFBFBD><EFBFBD> WIC <20><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳһ<CDB3><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>ʽ<EFBFBD><CABD>ͼƬ
 | |||
|  | 		hr = CoCreateInstance( | |||
|  | 			CLSID_WICImagingFactory, | |||
|  | 			NULL, | |||
|  | 			CLSCTX_INPROC_SERVER, | |||
|  | 			IID_IWICImagingFactory, | |||
|  | 			reinterpret_cast<void**>(&s_pIWICFactory) | |||
|  | 		); | |||
|  | 		ASSERT(SUCCEEDED(hr), "Create WICImagingFactory Failed!"); | |||
|  | 	} | |||
|  | 
 | |||
|  | 	if (SUCCEEDED(hr)) | |||
|  | 	{ | |||
|  | 		// <20><><EFBFBD><EFBFBD> DirectWrite <20><><EFBFBD><EFBFBD>
 | |||
|  | 		hr = DWriteCreateFactory( | |||
|  | 			DWRITE_FACTORY_TYPE_SHARED, | |||
|  | 			__uuidof(IDWriteFactory), | |||
|  | 			reinterpret_cast<IUnknown**>(&s_pDWriteFactory) | |||
|  | 		); | |||
|  | 		ASSERT(SUCCEEDED(hr), "Create DirectWrite Factory Failed!"); | |||
|  | 	} | |||
|  | 
 | |||
|  | 	return SUCCEEDED(hr); | |||
|  | } | |||
|  | 
 | |||
|  | bool e2d::ERenderer::__createDeviceResources() | |||
|  | { | |||
|  | 	HRESULT hr = S_OK; | |||
|  | 	 | |||
|  | 	if (!s_pRenderTarget) | |||
|  | 	{ | |||
|  | 		HWND hWnd = EWindow::getHWnd(); | |||
|  | 
 | |||
|  | 		// <20><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>Щ<EFBFBD><D0A9>ԴӦ<D4B4><D3A6> Direct3D <20>豸<EFBFBD><E8B1B8>ʧʱ<CAA7>ؽ<EFBFBD><D8BD><EFBFBD>
 | |||
|  | 		// <20><><EFBFBD>統 isVisiable <20><><EFBFBD>ģ<DEB8><C4A3>ȵ<EFBFBD>
 | |||
|  | 		RECT rc; | |||
|  | 		GetClientRect(hWnd, &rc); | |||
|  | 
 | |||
|  | 		D2D1_SIZE_U size = D2D1::SizeU( | |||
|  | 			rc.right - rc.left, | |||
|  | 			rc.bottom - rc.top | |||
|  | 		); | |||
|  | 
 | |||
|  | 		// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> Direct2D <20><>ȾĿ<C8BE><C4BF>
 | |||
|  | 		hr = s_pDirect2dFactory->CreateHwndRenderTarget( | |||
|  | 			D2D1::RenderTargetProperties(), | |||
|  | 			D2D1::HwndRenderTargetProperties( | |||
|  | 				hWnd, | |||
|  | 				size), | |||
|  | 			&s_pRenderTarget | |||
|  | 		); | |||
|  | 
 | |||
|  | 		ASSERT(SUCCEEDED(hr), "Create Render Target Failed!"); | |||
|  | 	} | |||
|  | 
 | |||
|  | 	if (SUCCEEDED(hr)) | |||
|  | 	{ | |||
|  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˢ
 | |||
|  | 		hr = s_pRenderTarget->CreateSolidColorBrush( | |||
|  | 			D2D1::ColorF(D2D1::ColorF::White), | |||
|  | 			&s_pSolidBrush | |||
|  | 		); | |||
|  | 		ASSERT(SUCCEEDED(hr), "Create Solid Color Brush Failed!"); | |||
|  | 	} | |||
|  | 	return SUCCEEDED(hr); | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::ERenderer::__discardDeviceResources() | |||
|  | { | |||
|  | 	SafeReleaseInterface(&s_pRenderTarget); | |||
|  | 	SafeReleaseInterface(&s_pSolidBrush); | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::ERenderer::__discardResources() | |||
|  | { | |||
|  | 	SafeReleaseInterface(&s_pDirect2dFactory); | |||
|  | 	SafeReleaseInterface(&s_pRenderTarget); | |||
|  | 	SafeReleaseInterface(&s_pSolidBrush); | |||
|  | 	SafeReleaseInterface(&s_pIWICFactory); | |||
|  | 	SafeReleaseInterface(&s_pDWriteFactory); | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::ERenderer::__render() | |||
|  | { | |||
|  | 	HRESULT hr = S_OK; | |||
|  | 
 | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
 | |||
|  | 	ERenderer::__createDeviceResources(); | |||
|  | 
 | |||
|  | 	// <20><>ʼ<EFBFBD><CABC>Ⱦ
 | |||
|  | 	s_pRenderTarget->BeginDraw(); | |||
|  | 	// ʹ<>ñ<EFBFBD><C3B1><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ
 | |||
|  | 	s_pRenderTarget->Clear(s_nClearColor); | |||
|  | 
 | |||
|  | 	// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
 | |||
|  | 	ESceneManager::__render(); | |||
|  | 
 | |||
|  | 	// <20><>ֹ<EFBFBD><D6B9>Ⱦ
 | |||
|  | 	hr = s_pRenderTarget->EndDraw(); | |||
|  | 
 | |||
|  | 	if (hr == D2DERR_RECREATE_TARGET) | |||
|  | 	{ | |||
|  | 		// <20><><EFBFBD><EFBFBD> Direct3D <20>豸<EFBFBD><E8B1B8>ִ<EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
 | |||
|  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD>ʱ<EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD>Դ
 | |||
|  | 		hr = S_OK; | |||
|  | 		ERenderer::__discardDeviceResources(); | |||
|  | 	} | |||
|  | 
 | |||
|  | 	if (FAILED(hr)) | |||
|  | 	{ | |||
|  | 		// <20><>Ⱦʱ<C8BE><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ֪<CEB4>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>Ϸ
 | |||
|  | 		ASSERT(false, L"Renderer error!"); | |||
|  | 		EGame::quit(); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | 
 | |||
|  | void e2d::ERenderer::setBackgroundColor(UINT32 color) | |||
|  | { | |||
|  | 	s_nClearColor = D2D1::ColorF(color); | |||
|  | } | |||
|  | 
 | |||
|  | ID2D1Factory * e2d::ERenderer::getID2D1Factory() | |||
|  | { | |||
|  | 	return s_pDirect2dFactory; | |||
|  | } | |||
|  | 
 | |||
|  | ID2D1HwndRenderTarget * e2d::ERenderer::getRenderTarget() | |||
|  | { | |||
|  | 	return s_pRenderTarget; | |||
|  | } | |||
|  | 
 | |||
|  | ID2D1SolidColorBrush * e2d::ERenderer::getSolidColorBrush() | |||
|  | { | |||
|  | 	return s_pSolidBrush; | |||
|  | } | |||
|  | 
 | |||
|  | IWICImagingFactory * e2d::ERenderer::getIWICImagingFactory() | |||
|  | { | |||
|  | 	return s_pIWICFactory; | |||
|  | } | |||
|  | 
 | |||
|  | IDWriteFactory * e2d::ERenderer::getIDWriteFactory() | |||
|  | { | |||
|  | 	return s_pDWriteFactory; | |||
|  | } |