www.pudn.com > zfxcengine-0.1.0.zip > ceTextureObject_D3D9.cpp


#include "Core/ceExceptions.h" 
#include "Render/RenderD3D9/ceTextureObject_D3D9.h" 
#include "Core/ceMemManager.h" 
#include "SDL.h" 
 
#include  
 
namespace ZFXCE { 
	namespace Render { 
		////////////////////////////////////////////////////////////////////////////// 
		ceTextureObjectD3D9::ceTextureObjectD3D9(LPDIRECT3DDEVICE9 D3DDevice) 
		{ 
            m_pD3DDevice = D3DDevice; 
            m_pTexture   = NULL; 
		} 
		////////////////////////////////////////////////////////////////////////////// 
		ceTextureObjectD3D9::~ceTextureObjectD3D9(void) 
		{ 
			m_pD3DDevice = NULL; 
			m_pTexture->Release(); 
			m_pTexture = NULL; 
		} 
		////////////////////////////////////////////////////////////////////////////// 
		void ceTextureObjectD3D9::Load(void* pMem, UINT uiSize, BOOL Compressed, BOOL bMipMapping,  
				ceTexFilter TexFilter, ceTexType TexType) 
		{ 
			HRESULT hr = D3DXCreateTextureFromFileInMemory(m_pD3DDevice, pMem, uiSize, &m_pTexture); 
			if(hr == D3DERR_NOTAVAILABLE) 
				CE_EXCEPTION("This device does not support the queried technique.", CELS_ERROR); 
			if(hr == D3DERR_OUTOFVIDEOMEMORY) 
				CE_EXCEPTION("Microsoft Direct3D does not have enough display memory to perform the operation.", CELS_ERROR); 
			if(hr == D3DERR_INVALIDCALL) 
				CE_EXCEPTION("The method call is invalid. For example, a method's parameter may have an invalid value.", CELS_ERROR); 
			if(hr == D3DXERR_INVALIDDATA) 
				CE_EXCEPTION("The data is invalid.", CELS_ERROR); 
			if(hr == E_OUTOFMEMORY) 
				CE_EXCEPTION("Direct3D could not allocate sufficient memory to complete the call.", CELS_ERROR); 
			if(FAILED(hr)) 
				CE_EXCEPTION("Texture creation failed!", CELS_ERROR); 
/* 
LPDIRECT3DSURFACE9 pSurface=NULL; 
m_pTexture->GetSurfaceLevel(0, &pSurface); 
static int TexNumer = 0; 
std::stringstream strstream; 
strstream << "Text_No"<<++TexNumer<<".bmp"; 
D3DXSaveSurfaceToFile(strstream.str().c_str(), D3DXIFF_BMP, pSurface, NULL, NULL); 
*/ 
		//	m_pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); 
		//	m_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); 
		} 
		////////////////////////////////////////////////////////////////////////////// 
		void ceTextureObjectD3D9::LoadFromMemory(void* pbData, UINT uiSideLength, INT BPP, BOOL Compressed,  
										BOOL bMipMapping, UINT FilterType, UINT TexTyp) 
		{ 
            PUSH_FUNCTION; 
 
			// we ignore channels, because use only A8R8G8B8 
			HRESULT hr = m_pD3DDevice->CreateTexture(uiSideLength, uiSideLength, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &m_pTexture, NULL); 
			 
			if(hr != D3D_OK) 
			{ 
				if(hr == D3DERR_INVALIDCALL) 
				{ 
					CE_EXCEPTION(std::string(__FUNCTION__)+": The method call is invalid. For example, a method's parameter may have an invalid value.", CELS_ERROR); 
				} 
				else if(hr == D3DERR_OUTOFVIDEOMEMORY) 
				{ 
					CE_EXCEPTION(std::string(__FUNCTION__)+": Direct3D does not have enough display memory to perform the operation.", CELS_ERROR); 
				} 
				else if(hr == E_OUTOFMEMORY) 
				{ 
					CE_EXCEPTION(std::string(__FUNCTION__)+": Direct3D could not allocate sufficient memory to complete the call.", CELS_ERROR); 
				} 
				else if(m_pTexture == NULL) 
				{ 
					CE_EXCEPTION(std::string(__FUNCTION__)+": m_pTexture == NULL", CELS_ERROR); 
				} 
				else 
				{ 
					CE_EXCEPTION(std::string(__FUNCTION__)+": Failed to create texture!", CELS_ERROR); 
				} 
			} 
			 
			D3DLOCKED_RECT d3dRect; 
			hr = m_pTexture->LockRect(0, &d3dRect, NULL, 0); 
			INT LineWidth = d3dRect.Pitch >> 2; // 32 bit = 4 byte 
            INT iLength   = uiSideLength;//*BPP / 8; 
			void* pMemory = (DWORD*)d3dRect.pBits; 
			BYTE* pSrc = (BYTE*) pbData; 
 
                 
            // jeden pixel kopieren 
            for (int cy = 0; cy < uiSideLength; cy++)  
            { 
                for (int cx = 0; cx < uiSideLength; cx++)  
                { 
	                // 32-Bit 
	                DWORD Color = 0xff000000; 
	                int   i = (cy*uiSideLength + cx)*3; 
	                memcpy(&Color, &pSrc[i], sizeof(BYTE)*3); 
	                ((DWORD*)pMemory)[cx + (cy*LineWidth)] = Color; 
                } 
            } 
		/*	// jeden pixel kopieren 
			for (INT cy = 0; cy < uiSideLength; cy++)  
			{ 
                memcpy(&pMemory[cy*LineWidth], &pSrc[cy*iLength], iLength); 
		/*		for (INT cx = 0; cx < uiSideLength; cx++)  
				{ 
					if(BPP == 24) 
					{ 
						DWORD Color = 0xff000000; 
			  
						INT   i = (cy*(iLength) + cx)*3; 
			  
						Color += (pSrc[i]<<16) + (pSrc[i+1]<<8) + (pSrc[i+2]<<0); 
 
						pMemory[ cx*3 + (cy*LineWidth) ] = Color; 
					} 
					else if(BPP == 32) 
					{ 
						DWORD Color = 0x00000000; 
			  
						INT   i = (cy*(iLength) + cx)*4; 
			  
						// color = (a<<24) + (r<<16) + (g<<8) + (b<<0) 
						Color += (pSrc[i]<<24) + (pSrc[i+1]<<16) + (pSrc[i+2]<<8) + (pSrc[i+3]<<0); 
 
						pMemory[ (cx + (cy*LineWidth)) ] = Color; 
					} 
				} 
			}*/ 
 
			m_pTexture->UnlockRect(0); 
/* 
LPDIRECT3DSURFACE9 pSurface=NULL; 
m_pTexture->GetSurfaceLevel(0, &pSurface); 
static int TexNumer = 0; 
std::stringstream strstream; 
strstream << "MemText_No"<<++TexNumer<<".bmp"; 
D3DXSaveSurfaceToFile(strstream.str().c_str(), D3DXIFF_BMP, pSurface, NULL, NULL);*/ 
 
		} 
		////////////////////////////////////////////////////////////////////////////// 
		void ceTextureObjectD3D9::SetTextureFilter(UINT FilterType) 
		{ 
            assert(false && "unused"); 
		} 
	}	// namespace Render 
}	// namespace ZFXCE