Understanding the Windows Aspect of DirectX
Covers most of the Win32 you need to know to make games in DirectX.
Even though DirectX mostly shields you from Win32 programming, you still have to create the window. Don't freak out yet. It's really easy and can be reused in any future Windows program that you write. What we're going to do with this tutorial is create a window with a black background. We will not be using any DirectX in this tutorial. We simply want to create a window that can be reused later. I will display the functions in this tutorial and try to give a brief understanding of everything. But I will only focus on the parts that are important to the DirectX programming tutorials.
The first thing you will need will be too globals that will handle the window.
HWND hWnd; // this is the main window handle
HINSTANCE hInstance // this is the instance of the BOOL bActive;#define NAME "Win Tutorial"
#define TITLE "Win Tutorial"The first function we are going to do is the DoInit(). Here is the function.../*
* doInit - do work required for every instance of the application:
* create the window, initialize data
*/
static BOOL doInit( HINSTANCE hInstance, int nCmdShow )
{
HWND hwnd;
WNDCLASS wc; // this is used to access your windows class components
/*
* set up and register window class
*/
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NAME;
wc.lpszClassName = NAME;
RegisterClass( &wc );
/*
* create a window
*/
hwnd = CreateWindowEx(
0,
NAME,
TITLE,
WS_POPUP,
0,
0,
1,
1,
NULL,
NULL,
hInstance,
NULL );
/*
* if something went wrong with the CreateWindowEx function then return FALSE
*/
if( !hwnd )
{
return FALSE;
}
/*
* Show the window and make sure that it is updated
*/
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );
/* Below here you will initialize your DirectX components and your game */
}- Setup and register the windows class.
- Create the window with a call to CreateWindowEx(...);
- Test to make sure the hwnd was created and if not return FALSE
- Show the window and update the window
All that the WindowProc function does is test for Windows messages to find out what's going on in the application. You will test for keystates(until you write DirectInput functions) in the WM_KEYDOWN case and you will shutdown DirectX components and Release COM objects in the WM_DESTROY case. Here is the function.
long FAR PASCAL WindowProc( HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam )
{
hWnd = hwnd;
switch( message )
{
/*
* this case says that our application either has the focus or it doesn't. wParam is the
* message that is being sent by the application if we have the focus, bActive is TRUE,
* otherwise it's FALSE
*/
case WM_ACTIVATEAPP:
bActive = wParam;
break;
/*
* turn the cursor off by setting it's value to NULL
*/
case WM_SETCURSOR:
SetCursor(NULL);
return TRUE;
case WM_CREATE:
break;
/*
* test your key states in this case
*/
case WM_KEYDOWN:
switch( wParam )
{
case VK_ESCAPE:
case VK_F12:
PostMessage(hwnd,WM_CLOSE,0,0);
break;
}
break;
/*
* this case is touched when the application is shutting down
*/
case WM_DESTROY:
/* you will shut down your game here. */
PostQuitMessage( 0 );
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
} /* WindowProc *//*
* WinMain - initialization, message loop
*/
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if( !doInit( hInstance, nCmdShow ) )
{
return FALSE;
}
/* this is the main windows loop for our application */
while( 1 )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
{
if( !GetMessage( &msg, NULL, 0, 0 ) )
return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
/* if the application has the focus */
else if( bActive )
{
/*
* right here is where we process our game logic. We process it here so that we don't
* unnecessarily update the game loop. We don't want to update if another application
* has the focus because we don't need to
*/
}
else
{
WaitMessage();
}
}
} /* WinMain */Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Unknown
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion