Wednesday, April 30, 2008

Tutorial 2 : Initializing DirectX

After creating the window, we need to give it DirectX capabilities. To get access to DirectX graphics, we need to include the header file d3d9.h. We also need to add the header file d3dx9.h, as it contains a lot of useful functions and macros.

Following the OOP methodology, I created a class which deals with the creation and release of the devices, handling lost devices and the rendering.

//  DxBase.cpp

void cDXBase::Init( const HWND hWnd )
{
m_Hwnd = hWnd;

DirectxInit() ;
#ifdef WINDOWED
SetParameters(false) ;
#else
SetParameters(true) ;
#endif
CreateDirectxDevice() ;
}

void cDXBase::DirectxInit()
{
//create the Direct3d Object
m_pD3D = Direct3DCreate9(D3D_SDK_VERSION) ;

if(m_pD3D == NULL)
{
MessageBox(NULL, _T("Direct3d object creation failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK) ;
}

// get the display mode
m_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &m_displayMode );

// get the device caps
m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_Caps) ;
}

To initialize DirectX graphics we first create the DirectX object with a call to Direct3DCreate9. The object lets configure the application based on the video card capabilities. This is achieved with a call to GetDeviceCaps. We also get the current display mode with a call to GetAdapterDisplay mode.


//  DxBase.cpp

void cDXBase::SetParameters(const BOOL bFullScreen)
{
ZeroMemory(&m_d3dpp, sizeof(m_d3dpp)) ;

m_d3dpp.BackBufferCount = 1 ;
m_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE ;
m_d3dpp.MultiSampleQuality = 0 ;
m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD ;
m_d3dpp.hDeviceWindow = m_Hwnd ;
m_d3dpp.Flags = 0 ;
m_d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT ;
m_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE ;
m_d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8 ; //pixel format
m_d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8 ; // depth format
m_d3dpp.EnableAutoDepthStencil = true ;

if(bFullScreen)
{
// if its a full screen app
m_d3dpp.BackBufferWidth = m_displayMode.Width ;
m_d3dpp.BackBufferHeight = m_displayMode.Height ;
m_d3dpp.Windowed = false; // fullscreen
m_d3dpp.FullScreen_RefreshRateInHz = m_displayMode.RefreshRate;
}
else
{
// if its a windowed app
m_d3dpp.Windowed = true ;
m_d3dpp.EnableAutoDepthStencil = TRUE ;
m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16 ;
}
}

Next, we need to fill up a D3DPRESENT_PARAMETERS structure. This structure is used to specify how DirectX is going to behave. If the application is full screen, then the BackBufferWidth, BackBufferHeight and FullScreen_RefreshRateInHZ members need to be set.

//DxBase.cpp

void cDXBase::CreateDirectxDevice()
{
int vp = 0 ; // the typeof vertex processing

if(m_Caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
{
// hardware vertex processing is supported.
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING ;
}
else
{
// use software vertex processing.
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING ;
}

// Create the D3DDevice
if(FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
m_Hwnd,
vp,
&m_d3dpp,
&m_pd3dDevice)))
{
MessageBox(NULL, _T("Direct3d m_pd3dDevice creation failed!"), _T("Error!"),MB_ICONEXCLAMATION | MB_OK) ;
PostQuitMessage(0) ;
DestroyWindow(m_Hwnd) ;
}
}

Now, we create the device. First, we check if Hardware processing is supported or not. Then, we create the device with a call to CreateDevice. If device creation fails, we post an error and quit.

// Dxbase.cpp

HRESULT cDXBase::BeginRender()
{
HRESULT hr;

// check if the device is available
hr = IsAvailable() ;

if(hr == D3DERR_DEVICELOST || hr == D3DERR_DEVICENOTRESET)
{
HandleLostDevice(hr) ;
}
else
{
if(FAILED(hr))
{
PostQuitMessage(0) ;
}
}


if(SUCCEEDED(hr))
{
// clear the frame
m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,m_BkColor, 1.0f, 0) ;

hr = m_pd3dDevice->BeginScene() ;
}

return hr;
}


//DxBase.inl

inline HRESULT cDXBase::IsAvailable()
{
return(m_pd3dDevice->TestCooperativeLevel()) ;
}

inline void cDXBase::EndRender( const HRESULT hr )
{
if(SUCCEEDED(hr))
{
m_pd3dDevice->EndScene() ;
}
m_pd3dDevice->Present(NULL, NULL, NULL, NULL) ;
}

With the device initialized, we need to update the message loop to render geometry. First, we check if the device is available. This is done with a call to TestCooperativeLevel. Before we can render any geometry we need to call BeginScene. Then, we need to clear the surface we are drawing on with a call to Clear. This clears the back buffer with the specified color. When we are done rendering, we need to call EndScene. At this point, we still can't see the geometry as we cleared the back buffer. To switch between the front and back buffer we need to call Present, which displays the what we just rendered to the screen.

// DxBase.cpp

void cDXBase::Release()
{
// release the Direct3d device
SAFE_RELEASE(m_pd3dDevice) ;

// release the Direct3d object
SAFE_RELEASE(m_pD3D) ;
}

When we quit the message loop, we need to give resources back to Windows by releasing the COM interfaces. The Direct3D object and the Direct3D Device are both COM objects. So, to destroy the COM instances, we’ll release them in the reverse order that they are created by calling Release.

//  DxBase.cpp

void cDXBase::HandleLostDevice(HRESULT hr)
{
if(hr == D3DERR_DEVICELOST)
{
Sleep(500) ;
}
else
{
if(hr == D3DERR_DEVICENOTRESET)
{
//The m_pd3dDevice is ready to be Reset
hr = ResetDevice() ;
}
}
}

HRESULT cDXBase::ResetDevice()
{
if (m_pd3dDevice)
{
HRESULT hr ;

hr = m_pd3dDevice->Reset(&m_d3dpp) ;

return hr ;
}

return 0;
}

One more thing we need to handle is lost DirectX devices. The device can be lost when the window is minimized or when we switch among windows etc. This is done by a simple call to Reset.

Now we just need to make a few changes to our MainWindow to integrate DirectX.


//  MainWindow.cpp
GRAPHIC_API HWND cMainWindow::Init( const HINSTANCE &hInstance, const int &nCmdShow, LPCTSTR lpWindowTitle,const int iFullScreenWidth, const int iFullScreenHeight, cBaseApp* const pGameApp )
{
// earlier stuff
m_iFullScreenWidth = iFullScreenWidth ;
m_iFullScreenHeight = iFullScreenHeight ;

// earlier stuff

// initialize DirectX
cDXBase::GetInstance().Init(hWnd);

return hWnd;
}

HWND cMainWindow::CreateMyWindow( const int &nCmdShow, LPCTSTR lpWindowTitle )
{
// earlier stuff
#else
// create the window in full screen mode
m_Hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
_T("Window"),
lpWindowTitle,
WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
0, 0,
m_iFullScreenWidth,m_iFullScreenHeight,
NULL,
NULL,
m_hInstance,
this) ;
#endif

// earlier stuff
}

LRESULT CALLBACK cMainWindow::WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
// earlier stuff

case WM_SIZE:
case WM_MOVE:
OnReset();
return 0 ;

case WM_KEYDOWN:

switch(wParam)
{
case VK_ESCAPE:
DestroyWindow(hwnd) ;
break ;

}
return 0 ;

// earlier stuff

case WM_DESTROY:
OnDestroy();
return 0 ;

// earlier stuff
}

}

void cMainWindow::Run()
{
// earlier stuff

//No message to process?
// Then do your game stuff here

OnRender();
}
}
}

void cMainWindow::OnRender()
{
HRESULT hr;

hr = cDXBase::GetInstance().BeginRender();
if (SUCCEEDED(hr))
{
cDXBase::GetInstance().EndRender(hr);
}
}

void cMainWindow::OnDestroy()
{
// release the graphic object
cDXBase::GetInstance().Release();

ReleaseCapture() ;
PostQuitMessage(0) ;
}

void cMainWindow::OnReset()
{
GetWinRect() ;
cDXBase::GetInstance().ResetDevice();
}

Now that we have added DirectX, on running the code we should see a blue screen

Code
Binaries

Tuesday, April 29, 2008

Tutorial 1 : Creating a Window

Before we create our game ,we first need to know how to create a window. This involves the following basic steps

  1. Define and register a window class that describes the window that we need to make
  2. Create the window
  3. Create the message loop to update the window based on input or game logic
  4. Create an event handler to responds to the events sent by window

Luckily, the code just needs to be written just once. I have also taken a slightly Object Oriented approach.

//  Main.cpp

int WINAPI WinMain(const HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd ;

CheckForMemoryLeaks() ;

//Initialize the window class
hwnd = cMainWindow::GetInstance().Init( hInstance, nCmdShow, _T("Test Game"));

if(hwnd == NULL)
{
PostQuitMessage(0) ;
}

cMainWindow::GetInstance().Run();

Cleanup() ;

return 0;
}


//  MainWindow.cpp

GRAPHIC_API HWND cMainWindow::Init( const HINSTANCE &hInstance, const int &nCmdShow, LPCTSTR lpWindowTitle )
{
m_hInstance = hInstance;
//m_WndProc = WndProc;

//Register the Window Class
RegisterWin();

//Create the Window
return(CreateMyWindow(nCmdShow, lpWindowTitle)) ;
}

void cMainWindow::RegisterWin()
{
WNDCLASSEX wc ;

wc.cbSize = sizeof(WNDCLASSEX) ;
wc.style = 0 ;
wc.lpfnWndProc = (WNDPROC)cMainWindow::StaticWndProc ;
wc.cbClsExtra = 0 ;
wc.cbWndExtra = 0 ;
wc.hInstance = m_hInstance ;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION) ;
wc.hCursor = LoadCursor(NULL, IDC_ARROW) ;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
wc.lpszMenuName = NULL ;
wc.lpszClassName = _T("Window") ;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION) ;
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"),MB_ICONEXCLAMATION | MB_OK) ;
exit(0) ;
}
}

HWND cMainWindow::CreateMyWindow( const int &nCmdShow, LPCTSTR lpWindowTitle )
{

#ifdef WINDOWED
// create the window in windowed mode
m_Hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
_T("Window"),
lpWindowTitle,
WS_OVERLAPPEDWINDOW ,
0, 0,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
m_hInstance,
this) ;
#else
// create the window in full screen mode
m_Hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
_T("Window"),
lpWindowTitle,
WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
0, 0,
1280,764,
NULL,
NULL,
m_hInstance,
this) ;
#endif

if(m_Hwnd == NULL)
{
MessageBox(NULL, _T("Window Creation Failed!"), _T("Error!"),MB_ICONEXCLAMATION | MB_OK) ;
return NULL ;
}
GetWinRect() ;
ShowWindow(m_Hwnd, nCmdShow) ;
UpdateWindow(m_Hwnd) ;

return m_Hwnd ;
}

void cMainWindow::MoveWin()
{
MoveWindow(m_Hwnd,m_iLeftPos,m_iTopPos,m_iClientWidth,m_iClientHeight,true) ;
}

void cMainWindow::GetWinRect()
{
RECT clientRect, windowRect ;

GetClientRect(m_Hwnd,&clientRect) ;
GetWindowRect(m_Hwnd,&windowRect) ;
m_iClientWidth = (clientRect.right - clientRect.left) ;
m_iClientHeight = (clientRect.bottom - clientRect.top) ;
m_iTopPos = (windowRect.top - clientRect.top) ;
m_iLeftPos = (windowRect.left - clientRect.left) ;
}

The WinMain function is the entry point for all Windows programs. We initialize the window by calling cMainWindow::Init.
First, we define the window class by filling out a WNDCLASSEX structure. This structure contains all the properties of the window that we want to create. The window is registered by calling the function RegisterClassEx.

With the window class registered, we then create the window with a call to the function CreateWindowEx. This is where we specify the size and position of the window along with the Window Styles. Note that we pass this as the last parameter to CreateWindowEx. The reason for this will be explained in just some time. The method to create Windowed and FullScreen application differs. The #ifdef WINDOWED above is used to do just that.

With the window registered and created we can display the window with a call to ShowWindow and UpdateWindow.

//  MainWindow.cpp

void cMainWindow::Run()
{
MSG Msg ;

PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE) ;
// run till completed
while (Msg.message!=WM_QUIT)
{

// is there a message to process?
if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
// dispatch the message
TranslateMessage(&Msg) ;
DispatchMessage(&Msg) ;
}
else
{
//No message to process?
// Then do your game stuff here
}
}
}

The message loop or cMainWindow::Run is what continuously updates the application until the user wants to quit. To be more efficient, we will only update and render a frame when there are no messages in the message queue. First we need to check if there are any messages that the window needs to take care of such as resizing, closing, etc. We check if there are any messages on the queue with PeekMessage. If there is a message, we test if the message is a quit message. If it’s not a quit message, we send the message off to our event handler by calling TranslateMessage and DispatchMessage.

// MainWindow.cpp

LRESULT CALLBACK cMainWindow::WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
static BOOL bLtButtonPressed = false ;
PAINTSTRUCT ps ;
HDC hdc ;

switch(msg)
{

case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
EndPaint (hwnd, &ps) ;
return 0 ;

case WM_SIZE:
case WM_MOVE:
cMainWindow::GetInstance().GetWinRect() ;

return 0 ;

case WM_KEYDOWN:

switch(wParam)
{
case VK_ESCAPE:
DestroyWindow(hwnd) ;
break ;

}
return 0 ;

case WM_CLOSE:
DestroyWindow(hwnd) ;
return 0 ;

case WM_DESTROY:
ReleaseCapture() ;
PostQuitMessage(0) ;
return 0 ;

default:
return DefWindowProc(hwnd, msg, wParam, lParam) ;
}
}

LRESULT CALLBACK cMainWindow::StaticWndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
if ( msg == WM_CREATE )
{
SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG)((CREATESTRUCT *)lParam)->lpCreateParams );
}

cMainWindow *targetApp = (cMainWindow*)GetWindowLongPtr( hwnd, GWLP_USERDATA );

if ( targetApp )
{
return targetApp->WndProc( hwnd, msg, wParam, lParam );
}

return DefWindowProc( hwnd, msg, wParam, lParam );
}

The window procedure, WndProc, is where we process all our messages. You can name this function whatever you want as long as you pass the same name to the WNDCLASSEX structure above. To process messages, just do a switch on the message to handle each case. There are a lot of possible messages, but usually you’ll just need a few.

One feature to notice is that there are two window procedures: WndProc and StaticWndProc. When we fill out the WNDCLASSEX.lpfnWndProc member of the window class, we need to specify a pointer to a function that has a specific function declaration:

LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )

When the program is compiled, another parameter is added to all non-static member functions, a this pointer, which changes the function declaration so it is incompatible with what is required for the lpfnWndProc member. Static functions on the other hand, do not receive this extra parameter, which is why we set the lpfnWndProc member to StaticWndProc. However, static functions can only access static member variables. Since all the other variables are non-static, we need a way to access them.

If you look at the CreateWindowEx function, the last parameter, lpParam, is defined as a “Pointer to a value to be passed to the window through the CREATESTRUCT structure passed in the lpParam parameter of the WM_CREATE message.” So we can store any type of pointer we want here, such as a this pointer. This pointer, which will be accessible during a WM_CREATE message, could be used to send messages meant for our application to a non-static window procedure, which would allow us to access the non-static data of our class. But if this pointer is only accessible during a WM_CREATE message, we have to store it with the window when the WM_CREATE message arrives so that all future messages will find their way to our non-static window procedure. We can store our this pointer in the user-defined attribute using the SetWindowLongPtr function with the GWLP_USERDATA offset flag. With the this pointer now stored with our window, we can access it in all subsequent messages with the GetWindowLongPtr function. Once the pointer is retreived, we can cast the pointer to a cMainWindow pointer and access all the non-static functions of the class, such as the non-static window procedure. Using this, we route all messages to their corresponding non-static window procedure.

That's all there is to creating a window.

Code
Binary