• Understanding the Windows Aspect of DirectX

General and Gameplay Programming

Understanding the Windows Aspect of 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 
You will also need a BOOL global that will be true when our window has the focus.

BOOL bActive;
We are also going to have to defines for the Window Class Name and the window Title.

#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 */ }
That's the initialization function all wrapped up in a nice neat little ball. I put a comment in to tell you where your game and DirectX initialization code will go. You should be able to see what it's doing by reading the comments but let's recap.
1. Setup and register the windows class.
2. Create the window with a call to CreateWindowEx(...);
3. Test to make sure the hwnd was created and if not return FALSE
4. Show the window and update the window
That's all there is to initialization. Now on to the Windowproc function.

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 */
Now we move on to WinMain. WinMain is the Main() of a Win32 program. This will contain our main loop in which we will run the main game logic. Lets take a look at it.

/* * 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 */
That is all you need to know to create a window to get started with DirectX.

Report Article

User Feedback

You need to be a member in order to leave a review