Initializing the lpd3dDevice

Started by
4 comments, last by NeverKnovvsbestt 18 years ago
Im having trouble bliting an image to the screen because my lpd3dDevice is not initializing correctly this is my code: bool DirectXManager::init(HWND hwnd, int bufferWidth, int bufferHeight) { lpd3d = Direct3DCreate9(D3D_SDK_VERSION); if(NULL == (lpd3d = Direct3DCreate9(D3D_SDK_VERSION))) { lastResult = E_FAIL; MessageBox(hwnd, "Unable to init DirectXManager", "ERROR", 0); return false; } D3DPRESENT_PARAMETERS d3dpp; d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.BackBufferCount = 1; d3dpp.BackBufferHeight = bufferWidth; d3dpp.BackBufferWidth = bufferHeight; d3dpp.hDeviceWindow = hwnd; if(FAILED(lpd3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &lpd3dDevice))) { lastResult = E_FAIL; MessageBox(hwnd, "lpd3dDevice init failure", "ERROR", 0); return false; } return true; } any suggestions?
Advertisement
What happens?

Are you sure you want to use the reference device?

Also, you're calling Direct3DCreate9 twice.
When ever i try to use the lpd3dDevice my Exception handling says that it is null. i fixed the double initialization of the lpd3d but that wasn't the problem.
here is the full class:
--------------------------------------------------------------------------------

header
///////////////////////////////////////////////////////////////////////////
#pragma once

#include<windows.h>
#include<d3d9.h>
#include<d3dx9.h>

class DirectXManager
{
public:
DirectXManager(HWND, int, int);
~DirectXManager(void);

void clear(void);
void present(void);
IDirect3DSurface9* createSurface(int, int);
IDirect3DSurface9* loadSurfaceFromFile(char*);
IDirect3DSurface9* getBackBuffer(void);
bool drawImage(IDirect3DSurface9*, RECT*, RECT*);
void release(void);

private:
bool init(HWND, int, int);

HRESULT lastResult;
LPDIRECT3D9 lpd3d;
LPDIRECT3DDEVICE9 lpd3dDevice;
};

cpp
////////////////////////////////////////////////////////////////////
#include"DirectXManager.h"

DirectXManager::DirectXManager(HWND hWindow, int bufferWidth, int bufferHeight)
{
init(hWindow, bufferWidth, bufferHeight);
}

bool DirectXManager::init(HWND hwnd, int bufferWidth, int bufferHeight)
{
if(NULL == (lpd3d = Direct3DCreate9(D3D_SDK_VERSION)))
{
lastResult = E_FAIL;
MessageBox(hwnd, "Unable to init DirectXManager", "ERROR", 0);
return false;
}

D3DPRESENT_PARAMETERS d3dpp;
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = bufferWidth;
d3dpp.BackBufferWidth = bufferHeight;
d3dpp.hDeviceWindow = hwnd;

if(FAILED(lpd3d->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&lpd3dDevice)))
{
lastResult = E_FAIL;
MessageBox(hwnd, "lpd3dDevice init failure", "ERROR", 0);
return false;
}

return true;
}

void DirectXManager::clear(void)
{
if(NULL == lpd3dDevice)
{
MessageBox(NULL, "Cannot clear: lpd3dDevice is NULL", "ERROR", 0);
return;
}

lpd3dDevice->Clear(
0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
}

void DirectXManager::present(void)
{
if(NULL == lpd3dDevice)
{
MessageBox(NULL, "Cannot present: lpd3dDevice is NULL", "ERROR", 0);
return;
}

lpd3dDevice->Present(NULL, NULL, NULL, NULL);
}

IDirect3DSurface9* DirectXManager::createSurface(int width, int height)
{
if(NULL == lpd3dDevice)
{
MessageBox(NULL, "Cannot create surface: lpd3dDevice is NULL", "ERROR", 0);
return NULL;
}

HRESULT hResult;
IDirect3DSurface9 *surface;

hResult = lpd3dDevice->CreateOffscreenPlainSurface(
width, height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);

if(FAILED(hResult))
return NULL;

return surface;
}

IDirect3DSurface9* DirectXManager::loadSurfaceFromFile(char* filePath)
{
HRESULT hResult;
IDirect3DSurface9 *surface;
D3DXIMAGE_INFO d3dxImageInfo;

hResult = D3DXGetImageInfoFromFile(filePath, &d3dxImageInfo);

if(FAILED(hResult))
return NULL;

surface = createSurface(d3dxImageInfo.Width, d3dxImageInfo.Height);

hResult = D3DXLoadSurfaceFromFile(
surface, NULL, NULL, filePath, NULL, D3DX_DEFAULT, 0, NULL);

if(FAILED(hResult))
return NULL;

return surface;
}

IDirect3DSurface9* DirectXManager::getBackBuffer()
{
if(NULL == lpd3dDevice)
{
MessageBox(NULL, "Cannot get back buffer: lpd3dDevice is NULL", "ERROR", 0);
return NULL;
}

IDirect3DSurface9 *surface;
HRESULT hResult;

hResult = lpd3dDevice->GetBackBuffer(
0, 0, D3DBACKBUFFER_TYPE_MONO, &surface);

if(FAILED(hResult))
return NULL;

return surface;
}

bool DirectXManager::drawImage(
IDirect3DSurface9 *surface, RECT *srcRect, RECT *destRect)
{
if(NULL == lpd3dDevice)
{
MessageBox(NULL, "Cannot draw image: lpd3dDevice is NULL", "ERROR", 0);
return false;
}

HRESULT hResult = lpd3dDevice->StretchRect(
surface, srcRect, getBackBuffer(), destRect, D3DTEXF_NONE);

return !FAILED(hResult);
}

void DirectXManager::release(void)
{
if(lpd3dDevice != NULL)
lpd3dDevice->Release();

if(lpd3d != NULL)
lpd3d->Release();
}

DirectXManager::~DirectXManager(void)
{
}
I have put some helpful comments in for you and changed a bit of your code so that it may have a better chance of working

I think your main problem was with the Present Parameters structure which was passed to Direct3D with some of its members with strange numbers which would have caused Direct3D to fail

Here it is :)
bool DirectXManager::init(HWND hwnd, int bufferWidth, int bufferHeight){  // This should work with only one call  lpd3d = Direct3DCreate9(D3D_SDK_VERSION);  if(NULL == lpd3d)  {    lastResult = E_FAIL;    MessageBox(hwnd, "Unable to init DirectXManager", "ERROR", 0);    return false;  }  D3DPRESENT_PARAMETERS d3dpp;    // The members of this structure are likely to have strange values now  // so we clear the whole structure to 0's  memset(&d3dpp, 0, sizeof(d3dpp));  // Now we can setup the structure without setting every member because now  // the members we don't need to set are 0  d3dpp.Windowed = TRUE;  d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;  d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;  d3dpp.BackBufferCount = 1;  d3dpp.BackBufferHeight = 0; // For a windowed app these can be 0 in which case  d3dpp.BackBufferWidth = 0;  // it will use the client area of the window  d3dpp.hDeviceWindow = hwnd;  if(FAILED(lpd3d->CreateDevice(      D3DADAPTER_DEFAULT,      D3DDEVTYPE_HAL, // Use HAL... a lot faster than D3DDEVTYPE_REF,      hwnd,      D3DCREATE_SOFTWARE_VERTEXPROCESSING,      &d3dpp,      &lpd3dDevice)))   {     lastResult = E_FAIL;     MessageBox(hwnd, "lpd3dDevice init failure", "ERROR", 0);     return false;   }   return true;}
That worked!
Thanks!

This topic is closed to new replies.

Advertisement