So here's part 1 of my tutorial series on Direct3D

This part won't contain any Direct3D code yet, instead I'll tell you something about the different stages of games first.
Before we enter the game loop:
Stage 1: Initializing
This is where it all begins, in this stage you create a window, set up DirectX and finally load your models and such.
Stage 2: Game start
In this stage you set up the game itself, doing stuff like loading maps, show a menu and pretty much everything else that doesn't need to run every frame.
During the game loop:
Stage 3: Input
We use this stage to read information from the keyboard & mouse
Stage 4: Game logic
This is where you update everything to do with game logic, like ammo left, did we move? can an enemy see us? are we still inside the world?
Stage 5: Rendering the graphics
This is where DirectX comes in, in this stage everything should be done calculating and we can move on to rendering it.
Stage 6: Restarting the loop
We're done with the current frame, so we go back to stage 3 and do it all again!
After the game loop:
Stage 7: The Cleanup
No, cleaning up doesn't mean the thing your girlfriend is supposed to do. This is the stage where you delete allocated memory, unload objects etc.
Now that you're familiar with the basic flow of a game, let's do some actual programming!
In order to use DirectX we'll have to create a window in which it'll run, so make a new
EMPTY win32 project and add a new .cpp file(I named mine MainWndProc.cpp) so we can get started
Here's the code for our MainWndProc.cpp
Code:
#define VC_EXTRALEAN // because we don't need all that extra crap ;)
#include <windows.h>
const static int GAME_WIDTH = 800,
GAME_HEIGHT = 600;
// WndProc prototype, this will handle most of the messages.
LRESULT CALLBACK myWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
// WinMain is the entry point of our win32 application.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
/*
Initializing -> Stage 1(we don't have a Stage 2 yet)
*/
HWND hWnd; // a handle to the window we create
WNDCLASSEX wc; // the window class
ZeroMemory(&wc, sizeof(WNDCLASSEX)); // clear out the windows class(saves us a few initialization lines ;))
// fill the struct with the info we want.
wc.cbSize = sizeof(WNDCLASSEX); // the size of the structure
wc.style = CS_HREDRAW | CS_VREDRAW; // window styles, we specify horizontal and vertical redraw
wc.lpfnWndProc = myWndProc; // pointer to our message handler
wc.hInstance = hInstance; // the global instance of this application taken from WinMain
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load up the default arrow cursor
wc.hbrBackground = (HBRUSH)COLOR_WINDOW; // set the background color to the default window color
wc.lpszClassName = L"MPGHGame1"; // the classname
RegisterClassEx(&wc); // register the window class
// create the window and use the result as the handle
hWnd = CreateWindowEx(NULL, // extra styles, no need for it
L"MPGHGame1", // classname again
L"MPGH D3D Tutorial Series part 1", // window title
WS_OVERLAPPEDWINDOW, // window style
300, // x-pos
300, // y-pos
GAME_WIDTH, // width
GAME_HEIGHT, // height
NULL, // no parent window
NULL, // no menu
hInstance, // the application handle
NULL); // used with multiple windows, which we don't.
ShowWindow(hWnd, nCmdShow); // Make the window visible
/*
The loop -> (we don't have Stage 3, 4 and 5 yet)
*/
MSG msg; // this will hold the windows messages
// wait for the next message to appear in the queue
while(GetMessage(&msg, NULL, 0, 0))
{
// translate keystrokes
TranslateMessage(&msg);
// dispatch the message to the WndProc function
DispatchMessage(&msg);
}
/*
Stage 7 would go here.
*/
// return WM_QUIT message to Windows
return msg.wParam;
}
// Our main message handler
LRESULT CALLBACK myWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// look up what to do for a given message
switch(message)
{
// WM_DESTROY is passed when the window closes.
case WM_DESTROY:
{
PostQuitMessage(0); // exit the application
return 0; // return 0 so we break out of the loop.
}
break;
}
// This will handle messages that we didn't handle.
return DefWindowProc (hWnd, message, wParam, lParam);
}
The only thing it does is create a new window, compile it, have a look and study the code! We'll be building off of this in the next part so make sure you understand everything here!
I'll post part 2 next week =)