• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Lakshmi Narayan

problem initializing Direct 3d Device

2 posts in this topic

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]
0

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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.

[code]



// 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);
}
[/code]

Directx stuff is hard! So make sure you understand classes and other C++ concepts well!
1

Share this post


Link to post
Share on other sites
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.
0

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  
Followers 0