Hi,
What is the best method to handle a "lost device" in DirectX 9.0c?
Please look at the source code below and tell me if I am doing it correctly or not.
I "Reset" the DirectX device and reload the DirectX Sprites...
Thanks!
JeZ+Lee
Sprite Loading Function:
bool Visuals::LoadSpritesIntoMemoryAndInitialize(bool DeviceReset)
{
HRESULT result;
char filePath[256];
strcpy_s(filePath, "~\0");
for (int index = 0; index < NumberOfSprites; index++)
{
switch(index)
{
case 0:
strcpy_s(filePath, "Data/Visuals/Screen-Fade-Black-Box.png");
break;
case 5:
strcpy_s(filePath, "Data/Visuals/16BitSoft-Logo.png");
break;
case 6:
strcpy_s(filePath, "Data/Visuals/Title-BG.png");
break;
case 7:
strcpy_s(filePath, "Data/Visuals/TC5-Logo.png");
break;
default:
strcpy_s(filePath, "~");
break;
}
strcat_s(filePath, "\0");
if (filePath[0] != '~')
{
D3DXIMAGE_INFO imageInfo;
result = D3DXGetImageInfoFromFileA(filePath, &imageInfo);
// if FAILED (hResult){
// return false;
// }
D3DXCreateTextureFromFileExA(DXDevice, filePath, imageInfo.Width, imageInfo.Height, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &Sprites[index].Texture);
result = D3DXCreateSprite(DXDevice, &Sprites[index].DXSprite);
if (DeviceReset == false)
{
Sprites[index].ScreenX = 400.0f;
Sprites[index].ScreenY = 240.0f;
Sprites[index].ScaleX = 1.0f;
Sprites[index].ScaleY = 1.0f;
Sprites[index].RotationDegree = 0.0f;
Sprites[index].RedHue = 255;
Sprites[index].GreenHue = 255;
Sprites[index].BlueHue = 255;
Sprites[index].Transparency = 255;
Sprites[index].Smooth = true;
Sprites[index].FlipX = false;
Sprites[index].FlipY = false;
Sprites[index].OriginalWidth = imageInfo.Width;
Sprites[index].OriginalHeight = imageInfo.Height;
D3DSURFACE_DESC textureInfo;
Sprites[index].Texture->GetLevelDesc(0, &textureInfo);
Sprites[index].TextureWidth = textureInfo.Width;
Sprites[index].TextureHeight = textureInfo.Height;
Sprites[index].AnimationTimer = -1.0f;
}
}
}
return(true);
}
Screen Render Function:
void Visuals::DisplayScreenBufferOntoDisplay(void)
{
HRESULT result;
result = DXDevice->Present( NULL, NULL, NULL, NULL );
if (result != D3D_OK)
{
D3DDISPLAYMODE d3ddm;
DXD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
DXDevice->Reset(&d3dpp);
for (int index = 0; index < NumberOfSprites; index++)
{
if (Sprites[index].DXSprite != NULL) Sprites[index].DXSprite->Release();
if (Sprites[index].Texture != NULL) Sprites[index].Texture->Release();
}
LoadSpritesIntoMemoryAndInitialize(true);
}
}