Code:
bool CD3DApp::InitD3D(HINSTANCE hInstance,
int width,
int height,
bool windowed,
D3DDEVTYPE DeviceType)
{
m_iWidth=width;
m_iHeight=height;
m_bWindowed=windowed;
m_hAppInstance=hInstance;
m_pDeviceType=DeviceType;
//
// Create the main application window.
//
WNDCLASS wc;
wc.style=0;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)g_WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = "CD3DApp";
if( !RegisterClass(&wc) )
{
::MessageBox(0, "RegisterClass() - FAILED", 0, 0);
return false;
}
HWND hwnd=0;
DWORD style=0;
if (m_bWindowed)
{
style=WS_OVERLAPPEDWINDOW;
}
else
{
style=WS_OVERLAPPEDWINDOW | WS_VISIBLE;
}
m_hwndAppWindowHandle = ::CreateWindow("CD3DApp",
NULL,
style,
0,
0,
m_iWidth,
m_iHeight,
0,
0,
hInstance,
0);
if( !m_hwndAppWindowHandle )
{
::MessageBox(0,0,"CreateWindow() - FAILED",0);
return false;
}
//
// Init D3D:
//
HRESULT hr = 0;
// Step 1: Create the IDirect3D9 object.
IDirect3D9* d3d9 = 0;
d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
if( !d3d9 )
{
::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);
return false;
}
// Step 2: Check for hardware vp.
d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, DeviceType, &m_pDeviceCaps);
int vp = 0;
if( m_pDeviceCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
// Step 3: Fill out the D3DPRESENT_PARAMETERS structure.
if (m_bWindowed)
{
m_pPresentParams.BackBufferWidth = 0;
m_pPresentParams.BackBufferHeight = 0;
m_pPresentParams.BackBufferFormat = D3DFMT_UNKNOWN;
}
else
{
m_pPresentParams.BackBufferWidth = m_iWidth;
m_pPresentParams.BackBufferHeight = m_iHeight;
m_pPresentParams.BackBufferFormat = D3DFMT_A8R8G8B8;
}
m_pPresentParams.BackBufferCount = 1;
m_pPresentParams.MultiSampleType = D3DMULTISAMPLE_NONE;
m_pPresentParams.MultiSampleQuality = 0;
m_pPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_pPresentParams.hDeviceWindow = m_hwndAppWindowHandle;
m_pPresentParams.Windowed = m_bWindowed;
m_pPresentParams.EnableAutoDepthStencil = true;
m_pPresentParams.AutoDepthStencilFormat = D3DFMT_D24S8;
m_pPresentParams.Flags = 0;
m_pPresentParams.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
m_pPresentParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
// Step 4: Create the device.
hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT,
DeviceType,
m_hwndAppWindowHandle,
vp,
&m_pPresentParams,
&m_pDevice);
if( FAILED(hr) )
{
// try again using a 16-bit depth buffer
m_pPresentParams.AutoDepthStencilFormat = D3DFMT_D16;
hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT,
DeviceType,
m_hwndAppWindowHandle,
vp,
&m_pPresentParams,
&m_pDevice);
if( FAILED(hr) )
{
d3d9->Release(); // done with d3d9 object
::MessageBox(0, "CreateDevice() - FAILED", 0, 0);
return false;
}
}
if (m_bWindowed)
{
ShowWindow(m_hwndAppWindowHandle,SW_SHOW);
}
d3d9->Release(); // done with d3d9 object
return true;
}
Most of this can be found in books. This comes from one of my sources - Introduction to 3D Game Programming with DirectX 9.0 by Frank Luna. www.moon-labs.com is it's website and a 2nd edition is due out soon which I'll grab in a heartbeat. You can get it at www.amazon.com.
I used a lot of ideas from this book, however, have significantly altered the entire setup to better suit my needs. His approach was too "C-like" for me so I encapsulated, re-arranged, sorted, off-loaded some code to other classes, and the like.
The class looks like this:
Code:
#ifndef CD3DApplication
#define CD3DApplication
#include <d3dx9.h>
#include <string>
#include <limits>
#include "CDXInput.h"
#include "CMouse.h"
#include "CKeyboard.h"
#include "CDXAudio.h"
#include "CDXShowWnd.h"
const float INFINITY = FLT_MAX;
const float EPSILON = 0.001f;
LRESULT CALLBACK g_WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK g_VideoWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
#define CDX_AUDIO_SOUNDFX 0x01
#define CDX_AUDIO_MUSIC 0x03
#define MODE_START 0x01
#define MODE_PAUSE 0x02
#define MODE_GAME 0x03
class CD3DApp
{
protected:
int m_iWidth;
int m_iHeight;
bool m_bWindowed;
D3DDEVTYPE m_pDeviceType;
D3DPRESENT_PARAMETERS m_pPresentParams;
HWND m_hwndAppWindowHandle;
HINSTANCE m_hAppInstance;
D3DCAPS9 m_pDeviceCaps;
DWORD m_dwStyle;
//Add ins
CDXInput Input;
CMouse *Mouse;
CKeyboard *Keyboard;
CDXAudio *Audio;
CDXSoundEmitter *SoundFX;
CDXSoundEmitter *Music;
CDXShowWnd *m_pDirectShow;
IDirect3DDevice9 *m_pDevice;
int m_iGameMode;
public:
int EnterMsgLoop(void);
CD3DApp(void):m_iWidth(0),m_iHeight(0),m_bWindowed(false),
m_hwndAppWindowHandle(0),m_hAppInstance(0),
m_pDevice(NULL),m_pDirectShow(NULL) { }
virtual ~CD3DApp(void)
{
delete Keyboard;
delete Mouse;
}
int GetMode(void) {return m_iGameMode;}
void SetMode(int iGameMode) {m_iGameMode=iGameMode;}
CDXSoundEmitter *GetSoundSystem(WORD wSoundObject)
{
switch (wSoundObject)
{
case CDX_AUDIO_SOUNDFX: return SoundFX;break;
case CDX_AUDIO_MUSIC: return Music;break;
}
}
CKeyboard *GetKeyboard(void)
{
if (Keyboard)
{
return Keyboard;
} else return NULL;
}
CMouse *GetMouse(void)
{
if (Mouse)
{
return Mouse;
} else return NULL;
}
void SetWindowStyle(DWORD dwStyle) {m_dwStyle=dwStyle;}
//DWORD GetWindowStyle(void) { return m_dwStyle;}
//void SetPresentParams(D3DPRESENT_PARAMETERS d3dpp) {m_PresentParams=d3dpp;};
//void GetPresentParams(D3DPRESENT_PARAMETERS *d3dpp) {d3dpp=&m_PresentParams;};
bool GetCaps(D3DCAPS9 *devCaps)
{
devCaps=&m_pDeviceCaps;
return true;
}
//
// Init
//
bool InitD3D(HINSTANCE hInstance, // [in] Application instance.
int width, // [in] Backbuffer dimensions.
int height,
bool windowed, // [in] Windowed (true)or full screen (false).
D3DDEVTYPE deviceType);
bool InitInput(void);
bool InitAudio(DWORD dwNumPChannels,DWORD dwMSLatency);
// Lights
//
D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color);
D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color);
D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color);
//
// Materials
//
D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p);
//
// Bounding Objects
//
struct BoundingBox
{
BoundingBox();
bool isPointInside(D3DXVECTOR3& p);
D3DXVECTOR3 _min;
D3DXVECTOR3 _max;
};
struct BoundingSphere
{
BoundingSphere();
D3DXVECTOR3 _center;
float _radius;
};
float GetRandomFloat(float lowBound, float highBound);
void GetRandomVector(
D3DXVECTOR3* out,
D3DXVECTOR3* min,
D3DXVECTOR3* max);
//
// Conversion
//
DWORD FtoDw(float f);
// Interpolation
float LI(float a, float b, float t);
virtual bool Setup(void)=0;
virtual bool Render(float fTimeDelta)=0;
virtual bool Cleanup(void)=0;
};
#endif
It's a mess and needs sep classes for lights and materials. I show you this to prove you need not just one book, but many. So far I have over 30 some books about all of this stuff. The more the better.