Sign in to follow this  
NeverKnovvsbestt

Initializing the lpd3dDevice

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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)
{
}

Share this post


Link to post
Share on other sites
Adam Hamilton    271
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;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this