problem initializing Direct 3d Device

Started by
1 comment, last by Lakshmi Narayan 11 years, 5 months ago
Hello
I was trying to set up a basic game frame work using directx with c++ and i get a run time error of not able to initialize direct 3d device.
It would be great if some one tells me where i am going wrong.The code actually works properly before splitting it up into classes
[source lang="cpp"]Defines.h

#ifndef __DEFINES_H_INCLUDED__
#define __DEFINES_H_INCLUDED__

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


Camera.h

#ifndef __CAMERA_H_INCLUDED__
#define __CAMERA_H_INCLUDED__
#include "defines.h"
class Camera
{
public:
Camera();
//~Camera();
void SetUpCamera(LPDIRECT3DDEVICE9 );
};
#endif

Graphics.h


#ifndef __GRAPHICS_H_INCLUDED__
#define __GRAPHICS_H_INCLUDED__
#include "defines.h"
class Graphics
{
public:
Graphics();
//~Graphics();
LPDIRECT3DDEVICE9 InitializeDevice(HWND );
LPDIRECT3DVERTEXBUFFER9 FillVertices(HWND , LPDIRECT3DDEVICE9 );
struct OURCUSTOMVERTEX
{
float x,y,z;
DWORD color;
};
private:
LPDIRECT3D9 p_dx_Object;//HWND hWnd
LPDIRECT3DDEVICE9 p_dx_Device;
OURCUSTOMVERTEX cv_Vertices[3];
LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer;
/*IDirect3D9* pDirect3D;
IDirect3DDevice9* pDevice;
IDirect3DSurface9* pBackBuffer;*/
};
#endif

Renderer.h

#ifndef __RENDERER_H_INCLUDED__
#define __RENDERER_H_INCLUDED__
#include "defines.h"
#include "Graphics.h"
class Renderer:Graphics
{
public:
Renderer();
//~Renderer();
void DrawScene(LPDIRECT3DDEVICE9 , LPDIRECT3DVERTEXBUFFER9 );
private:
//Graphics g;
};
#endif


Windows.cpp

#include<Windows.h>
#include"Game.h"
int run =5;
LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM parameter1,LPARAM parameter2)
{
switch(uint_Message)
{
case WM_KEYDOWN:
{
run=0;
break;
}
break;
}

return DefWindowProc(han_Wind,uint_Message,parameter1,parameter2);
}

HWND NewWindow(LPCTSTR str_Title,int int_XPos, int int_YPos, int int_Width, int int_Height)
{
WNDCLASSEX wnd_Structure;

wnd_Structure.cbSize = sizeof(WNDCLASSEX);
wnd_Structure.style = CS_HREDRAW | CS_VREDRAW;
wnd_Structure.lpfnWndProc = OurWindowProcedure;
wnd_Structure.cbClsExtra = 0;
wnd_Structure.cbWndExtra = 0;
wnd_Structure.hInstance = GetModuleHandle(NULL);
wnd_Structure.hIcon = NULL;
wnd_Structure.hCursor = NULL;
wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
wnd_Structure.lpszMenuName = NULL;
wnd_Structure.lpszClassName = L"WindowClassName";
wnd_Structure.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

RegisterClassEx(&wnd_Structure);

return CreateWindowEx(WS_EX_CONTROLPARENT, L"WindowClassName", str_Title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, int_XPos, int_YPos, int_Width, int_Height, NULL, NULL, GetModuleHandle(NULL), NULL);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow)
{
MSG msg_Message;

HWND han_Window = NewWindow(L"DirectX C++ Tutorial",100,100,500,500);
//LPDIRECT3DDEVICE9 p_Device = g.InitializeDevice(han_Window);
//LPDIRECT3DVERTEXBUFFER9 p_dx_VB = g.FillVertices(han_Window, p_Device);
//c.SetUpCamera(p_Device);
Game gm;
while(run)
{
if(PeekMessage(&msg_Message,han_Window,0,0,PM_REMOVE))
{
DispatchMessage(&msg_Message);
}
else
{
gm.Play(han_Window);
}
}

// p_Device->Release();
gm.Destroy();
DestroyWindow(han_Window);

return 0;
}

Renderer.cpp

#include"Renderer.h"
Renderer::Renderer()
{
}
void Renderer:: DrawScene(LPDIRECT3DDEVICE9 p_dx_Device, LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer)
{
p_dx_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(72,61,139), 1.0f, 0);
p_dx_Device->BeginScene();

p_dx_Device->SetStreamSource(0, p_dx_VertexBuffer, 0, sizeof(OURCUSTOMVERTEX));
p_dx_Device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
p_dx_Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

p_dx_Device->EndScene();
p_dx_Device->Present(NULL, NULL, NULL, NULL);
}

Graphics.cpp

#include"Graphics.h"
#include<Windows.h>

Graphics :: Graphics()
{
}
LPDIRECT3DDEVICE9 Graphics :: InitializeDevice(HWND han_WindowToBindTo)
{
//LPDIRECT3D9 p_dx_Object;
// LPDIRECT3DDEVICE9 p_dx_Device;

p_dx_Object = Direct3DCreate9(D3D_SDK_VERSION);
if (p_dx_Object == NULL)
{
MessageBox(han_WindowToBindTo,L"DirectX Runtime library not installed!",L"InitializeDevice()",MB_OK);
}

D3DPRESENT_PARAMETERS dx_PresParams;
ZeroMemory( &dx_PresParams, sizeof(dx_PresParams) );
dx_PresParams.Windowed = TRUE;
dx_PresParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
dx_PresParams.BackBufferFormat = D3DFMT_UNKNOWN;

if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, han_WindowToBindTo, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))
{
if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, han_WindowToBindTo, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))
{
MessageBox(han_WindowToBindTo,L"Failed to create even the reference device!",L"InitializeDevice()",MB_OK);
}
}

p_dx_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
p_dx_Device->SetRenderState(D3DRS_LIGHTING,false);

return p_dx_Device;
}
LPDIRECT3DVERTEXBUFFER9 Graphics:: FillVertices(HWND han_Window, LPDIRECT3DDEVICE9 p_dx_Device)
{
// OURCUSTOMVERTEX cv_Vertices[3];
// LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer;

cv_Vertices[0].x = 5.0f;
cv_Vertices[0].y = 10.0f;
cv_Vertices[0].z = 0.0f;
cv_Vertices[0].color = 0xffff0000;

cv_Vertices[1].x = 10.0f;
cv_Vertices[1].y = 0.0f;
cv_Vertices[1].z = 0.0f;
cv_Vertices[1].color = 0xff00ff00;

cv_Vertices[2].x = 0.0f;
cv_Vertices[2].y = 0.0f;
cv_Vertices[2].z = 0.0f;
cv_Vertices[2].color = 0xff00ffff;

if (FAILED(p_dx_Device->CreateVertexBuffer(3*sizeof(OURCUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &p_dx_VertexBuffer, NULL)))
{
MessageBox(han_Window,L"Error while creating VertexBuffer",L"FillVertices()",MB_OK);
}

VOID* p_Vertices;
if (FAILED(p_dx_VertexBuffer->Lock(0, 3*sizeof(OURCUSTOMVERTEX), (void**)&p_Vertices, 0)))
{
MessageBox(han_Window,L"Error trying to lock",L"FillVertices()",MB_OK);
}else{
memcpy(p_Vertices, cv_Vertices, 3*sizeof(OURCUSTOMVERTEX));
p_dx_VertexBuffer->Unlock();
}

return p_dx_VertexBuffer;
}

Game.cpp

#include"Game.h"
Game::Game()
{
}
void Game :: Play(HWND hWnd1)
{
// MSG msg_Message;

//HWND han_Window = w.NewWindow(L"DirectX C++ Tutorial",100,100,500,500);
p_Device = g.InitializeDevice(hWnd1);
p_dx_VB = g.FillVertices(hWnd1, p_Device);
c.SetUpCamera(p_Device);
p_Device->Release();
}
void Game :: Destroy()
{
p_Device->Release();
}

Camera.cpp

#include"Camera.h"
Camera :: Camera ()
{
}
void Camera :: SetUpCamera(LPDIRECT3DDEVICE9 p_dx_Device)
{
D3DXVECTOR3 m_EyePos(0, 0, -30);
D3DXVECTOR3 m_TargetPos(0, 0, 0);
D3DXVECTOR3 m_UpVector(0, 1, 0);
D3DXMATRIXA16 m_View;
D3DXMatrixLookAtLH(&m_View, &m_EyePos, &m_TargetPos, &m_UpVector);
p_dx_Device->SetTransform(D3DTS_VIEW, &m_View);
D3DXMATRIX m_Projection;
D3DXMatrixPerspectiveFovLH(&m_Projection, D3DX_PI/4, 500/500, 1, 50);
p_dx_Device->SetTransform(D3DTS_PROJECTION, &m_Projection);
} [/source]
Advertisement
Did you copy the original code from Riemer's website? That would be why it worked before, but unfortunately you've mangled it quite badly in the class-ifying process smile.png I was feeling generous this morning so I fixed it for you

Here are the changes:

- Changed the window procedure and got rid of the global "int run" variable. (In general people stay away from global variables in C++; if you want to know why, there's a discussion somewhere in the General Programming forum I think).
- Only need 4 files
- You didn't provide Game.h? So I basically rewrote the Game class.
- Game class does not own the Direct3D interfaces - they are owned by Graphics
- Initialisation of Direct3D now happens in Graphics::Graphics() rather than Game::Play() (this is probably what was causing the problem)
- Got rid of the Renderer class (not needed)
- Changed the PeekMessage() call, passing in NULL for the HWND parameter. This makes PeekMessage read all messages destined for the program (thread) not just the window
- Renamed the OURCUSTOMVERTEX struct to Vertex and moved it out of the Graphics class
- Added some stuff to Graphics class
- Lots of smaller changes.





// Defines.h

#ifndef __DEFINES_H_INCLUDED__
#define __DEFINES_H_INCLUDED__

#include<DX/d3d9.h>
#include<DX/d3dx9.h>
#endif

// Camera.h

#ifndef __CAMERA_H_INCLUDED__
#define __CAMERA_H_INCLUDED__
#include "defines.h"
class Camera
{
public:
Camera();
//~Camera();
void SetUpCamera(LPDIRECT3DDEVICE9 );
};
#endif

// Game.h

#ifndef _GAME_H_INCLUDED_
#define _GAME_H_INCLUDED_
#include "Graphics.h"
class Game
{
Graphics* pGraphics;
public:
Game(HWND hWnd);
~Game();
void Play();
};
#endif

//Graphics.h


#ifndef __GRAPHICS_H_INCLUDED__
#define __GRAPHICS_H_INCLUDED__
#include "defines.h"
struct Vertex
{
float x,y,z;
DWORD color;
};
class Graphics
{
public:
Graphics(HWND han_WindowToBindTo);
~Graphics();
void RenderScene();
private:
LPDIRECT3D9 p_dx_Object;
LPDIRECT3DDEVICE9 p_dx_Device;
Vertex cv_Vertices[3];
LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer;
};
#endif

// Windows.cpp

#include<Windows.h>
#include"Game.h"
LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM parameter1,LPARAM parameter2)
{
switch(uint_Message)
{
case WM_KEYDOWN: // delete this line to disable any-key-exit
case WM_DESTROY: // when the user presses the red X button
PostQuitMessage(0);
return 0;
}
return DefWindowProc(han_Wind,uint_Message,parameter1,parameter2);
}

HWND NewWindow(LPCTSTR str_Title,int int_XPos, int int_YPos, int int_Width, int int_Height)
{
WNDCLASSEX wnd_Structure;

wnd_Structure.cbSize = sizeof(WNDCLASSEX);
wnd_Structure.style = CS_HREDRAW | CS_VREDRAW;
wnd_Structure.lpfnWndProc = OurWindowProcedure;
wnd_Structure.cbClsExtra = 0;
wnd_Structure.cbWndExtra = 0;
wnd_Structure.hInstance = GetModuleHandle(NULL);
wnd_Structure.hIcon = NULL;
wnd_Structure.hCursor = NULL;
wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
wnd_Structure.lpszMenuName = NULL;
wnd_Structure.lpszClassName = "WindowClassName";
wnd_Structure.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

RegisterClassEx(&wnd_Structure);

return CreateWindowEx(WS_EX_CONTROLPARENT, "WindowClassName", str_Title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, int_XPos, int_YPos, int_Width, int_Height, NULL, NULL, GetModuleHandle(NULL), NULL);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow)
{
HWND han_Window = NewWindow("DirectX C++ Tutorial",100,100,500,500);
Game gm(han_Window);
MSG msg_Message;
while(true)
{
if(PeekMessage(&msg_Message,NULL,0,0,PM_REMOVE))
{
DispatchMessage(&msg_Message);
}
else
{
gm.Play();
}
if (msg_Message.message == WM_QUIT)
break;
}
// p_Device->Release();
DestroyWindow(han_Window);

return 0;
}

// Graphics.cpp

#include"Graphics.h"
#include<Windows.h>

Graphics :: Graphics(HWND han_WindowToBindTo)
{
// INITIALISE DIRECT3D

p_dx_Object = Direct3DCreate9(D3D_SDK_VERSION);
if (p_dx_Object == NULL)
{
MessageBox(NULL, "DirectX Runtime library not installed!", "InitializeDevice()",MB_OK);
}

D3DPRESENT_PARAMETERS dx_PresParams;
ZeroMemory( &dx_PresParams, sizeof(dx_PresParams) );
dx_PresParams.Windowed = TRUE;
dx_PresParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
dx_PresParams.BackBufferFormat = D3DFMT_UNKNOWN;

if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, han_WindowToBindTo, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))
{
if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, han_WindowToBindTo, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))
{
MessageBox(NULL, "Failed to create even the reference device!", "InitializeDevice()",MB_OK);
}
}
p_dx_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
p_dx_Device->SetRenderState(D3DRS_LIGHTING,false);
// FILL VERTICES
cv_Vertices[0].x = 5.0f;
cv_Vertices[0].y = 10.0f;
cv_Vertices[0].z = 0.0f;
cv_Vertices[0].color = 0xffff0000;

cv_Vertices[1].x = 10.0f;
cv_Vertices[1].y = 0.0f;
cv_Vertices[1].z = 0.0f;
cv_Vertices[1].color = 0xff00ff00;

cv_Vertices[2].x = 0.0f;
cv_Vertices[2].y = 0.0f;
cv_Vertices[2].z = 0.0f;
cv_Vertices[2].color = 0xff00ffff;

if (FAILED(p_dx_Device->CreateVertexBuffer(3*sizeof(Vertex), 0, D3DFVF_XYZ|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &p_dx_VertexBuffer, NULL)))
{
MessageBox(NULL, "Error while creating VertexBuffer", "FillVertices()",MB_OK);
}

VOID* p_Vertices;
if (FAILED(p_dx_VertexBuffer->Lock(0, 3*sizeof(Vertex), (void**)&p_Vertices, 0)))
{
MessageBox(NULL, "Error trying to lock", "FillVertices()",MB_OK);
}
else
{
memcpy(p_Vertices, cv_Vertices, 3*sizeof(Vertex));
p_dx_VertexBuffer->Unlock();
}
}
Graphics::~Graphics()
{
p_dx_VertexBuffer->Release();
p_dx_Device->Release();
p_dx_Object->Release();
}
void Graphics::RenderScene()
{
p_dx_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(72,61,139), 1.0f, 0);
p_dx_Device->BeginScene();

p_dx_Device->SetStreamSource(0, p_dx_VertexBuffer, 0, sizeof(Vertex));
p_dx_Device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
p_dx_Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

p_dx_Device->EndScene();
p_dx_Device->Present(NULL, NULL, NULL, NULL);
}
// Game.cpp

#include"Game.h"
Game::Game(HWND hWnd)
{
pGraphics = new Graphics(hWnd);
}
Game::~Game()
{
delete pGraphics;
}
void Game :: Play()
{
pGraphics->RenderScene();
}

// Camera.cpp

#include"Camera.h"
Camera :: Camera ()
{
}
void Camera :: SetUpCamera(LPDIRECT3DDEVICE9 p_dx_Device)
{
D3DXVECTOR3 m_EyePos(0, 0, -30);
D3DXVECTOR3 m_TargetPos(0, 0, 0);
D3DXVECTOR3 m_UpVector(0, 1, 0);
D3DXMATRIXA16 m_View;
D3DXMatrixLookAtLH(&m_View, &m_EyePos, &m_TargetPos, &m_UpVector);
p_dx_Device->SetTransform(D3DTS_VIEW, &m_View);
D3DXMATRIX m_Projection;
D3DXMatrixPerspectiveFovLH(&m_Projection, D3DX_PI/4, 500/500, 1, 50);
p_dx_Device->SetTransform(D3DTS_PROJECTION, &m_Projection);
}


Directx stuff is hard! So make sure you understand classes and other C++ concepts well!
Thanks a lot for sparing your time on this and for editing my code. Now the code works after making some changes. Also, one of the problems with the earlier one was i missed a call statement to drawscene() but not able to understand why "the device not initialized" error occurred because even in the earlier code ,the device initialization was done in graphics class and i just called a function from game class passing HWND parameter.

Thanks again i really appreciate it, spent almost two weeks trying to figure out.

This topic is closed to new replies.

Advertisement