#include "StdAfx.h"
#include "Texture.h"
#include "Utility.h"
using namespace System::Runtime::InteropServices;
namespace Sunlight
{
namespace DirectX
{
namespace Graphics
{
Texture::Texture() :
m_pDeviceObject(NULL),
m_pTexture(NULL),
Filename(NULL),
m_bCreated(false)
{
}
Texture::~Texture()
{
}
void Texture::Create()
{
if (m_bCreated)
return;
if (m_pDeviceObject == NULL)
throw new ArgumentNullException(S"DeviceObject");
if (!m_pDeviceObject->IsCreated)
return;
IDirect3DTexture8 __nogc *pTexture;
#ifdef _UNICODE
LPWSTR tszFilename = (LPWSTR)(void *)Marshal::StringToCoTaskMemUni(Filename);
#else
LPSTR tszFilename = (LPSTR)(void *)Marshal::StringToCoTaskMemAnsi(Filename);
#endif
HRESULT h = ::D3DXCreateTextureFromFileEx((LPDIRECT3DDEVICE8)m_pDeviceObject->Direct3DDevice,
tszFilename, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT,
0, NULL, NULL, &pTexture);
CoTaskMemFree(tszFilename);
if (FAILED(h))
{
if (h == ERROR_FILE_NOT_FOUND)
throw new IO::FileNotFoundException(String::Format(S"Texture file {0} not found.", Filename), Filename);
if (h == D3DXERR_INVALIDDATA)
throw new IO::FileLoadException(String::Format(S"Texture file {0} could not be loaded.", Filename), Filename);
throw new Sunlight::DirectX::DirectXException(S"IDirect3DDevice8::CreateVertexBuffer", h);
}
m_pTexture = pTexture;
m_bCreated = true;
}
void Texture::Destroy()
{
if (m_pTexture != NULL)
{
m_pTexture->Release();
m_pTexture = NULL;
}
m_bCreated = false;
}
void Texture::OnDeviceCreated(Object * , EventArgs * )
{
Create();
}
void Texture::OnDeviceDestroyed(Object * , EventArgs * )
{
Destroy();
}
Device *Texture::get_DeviceObject()
{
return m_pDeviceObject;
}
void Texture::set_DeviceObject(Device *pDevice)
{
m_pDeviceObject = pDevice;
__hook(&Device::Created, m_pDeviceObject, &Texture::OnDeviceCreated, this);
__hook(&Device::Releasing, m_pDeviceObject, &Texture::OnDeviceDestroyed, this);
}
IDirect3DTexture8 __nogc *Texture::get_Direct3DTexture()
{
return m_pTexture;
}
int Texture::get_Width()
{
if (!m_bCreated)
return 0;
D3DSURFACE_DESC desc;
m_pTexture->GetLevelDesc(0, &desc);
return desc.Width;
}
int Texture::get_Height()
{
if (!m_bCreated)
return 0;
D3DSURFACE_DESC desc;
m_pTexture->GetLevelDesc(0, &desc);
return desc.Height;
}
}
}
}