Sign in to follow this  
Lakshmi Narayan

problem initializing Direct 3d Device

Recommended Posts

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.

I am pasting the code below.

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);
}

Share this post


Link to post
Share on other sites
Khatharr    8812
Please use source tags. It's the little red [ ... ] that's at the end of the second row in the post editor.

Also, this goes in the DirectX forum. Edited by Khatharr

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