Jump to content

  • Log In with Google      Sign In   
  • Create Account

cool dude

Member Since 27 May 2012
Offline Last Active Nov 06 2014 03:26 PM

Topics I've Started

problem initializing Direct 3d Device

17 November 2012 - 11:29 AM

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>#endifCamera.h#ifndef __CAMERA_H_INCLUDED__#define __CAMERA_H_INCLUDED__#include "defines.h"class Camera{public:Camera();//~Camera();void SetUpCamera(LPDIRECT3DDEVICE9 );};#endifGraphics.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 hWndLPDIRECT3DDEVICE9 p_dx_Device;OURCUSTOMVERTEX cv_Vertices[3];LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer;/*IDirect3D9* pDirect3D;IDirect3DDevice9* pDevice;IDirect3DSurface9* pBackBuffer;*/};#endifRenderer.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;};#endifWindows.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]

problem initializing Direct 3d Device

15 November 2012 - 10:37 PM

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

Making a game from scratch

21 July 2012 - 01:01 PM

hello
i am planning to make a game from scratch using c++ with directx9 and i have experience making games using other's engine in directx. Can anyone suggest me a book or a way to start. I am looking for a book similar to Programming a Multiplayer FPS in DirectX but the problem with this book is the final code is directly given and executed. I am looking for a book which shows step by step building of an engine and then a game over it.

Help Regarding hlsl shaders

02 June 2012 - 01:23 PM

float4x4 World;
float4x4 View;float4x4 Projection;

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;

struct VertexShaderInput
{
float4 Position : POSITION0;
};

struct VertexShaderOutput
{
float4 Position : POSITION0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;

float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);

return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
return AmbientColor * AmbientIntensity;
}

technique Ambient
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}



i am trying to learn how to use shaders with directx and i found this code over d net

icould not understand the following thingsin above code

float4 Position : (POSITION0)?;

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 ?

Also how these function works
technique Ambient
{
pass Pass1(wt is this)
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}

Also, let me know if there is any video tutorial on shaders with directx.

Help Regarding hlsl shaders

02 June 2012 - 01:19 PM

n;float4x4 World;
float4x4 View;float4x4 World;
float4x4 View;
float4x4 Projectiofloat4x4 World;

float4x4 View;float4x4 World;

float4x4 View;
float4x4 Projection;

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;

struct VertexShaderInput
{
float4 Position : POSITION0;
};

struct VertexShaderOutput
{
float4 Position : POSITION0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;

float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);

return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
return AmbientColor * AmbientIntensity;
}

technique Ambient
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}float4x4 Projection;

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;

struct VertexShaderInput
{
float4 Position : POSITION0;
};

struct VertexShaderOutput
{
float4 Position : POSITION0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;

float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);

return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
return AmbientColor * AmbientIntensity;
}

technique Ambient
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}



i am trying to learn how to use shaders with directx and i found this code over d net

icould not understand the following thingsin above code

float4 Position : (POSITION0)?;

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 ?

Also how these function works
technique Ambient
{
pass Pass1(wt is this)
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}

Also, let me know if there is any video tutorial on shaders with directx.

PARTNERS