What You Need
Strong knowledge of C++
Direct-X SDK Current version that supports Direct-X 8.1
Microsoft Visual Studios (6.0 w/ C++ Support, 2003 (Preferable), 2005)
Information
C++ with Direct-X is extremely complicated. Some one with no prior C++ knowledge shouldn't attempt this tutorial. If you are using this as a learning experience, feel free to read.
By the way, we are using Direct-X 8.1 because Microsoft decided managed is the best way to program in 9.0… So we aren't going to program in it.
What we will try to do now is make a basic window that you can use in later tutorials to begin writing your first game in Direct-X!
Lets Start
If you don't have any of the noted software above installed, you probably should install it. You also need to install the SDK, you should do that after installing your compiler otherwise you will have to add the libraries/headers manually the compiler's options.
Creating a Project
If you have some C++ knowledge you should know what to do. Make a new EMPTY PROJECT called "MPGH.net Tutorial 1".
Now, you must access your "Project's Settings". Open your Project's Settings, head to the Link tab (Project Settings and Link may not exist, it depends on what compiler your using. I am using VS 2003). In that tab there are about seven settings, on the first input box enter "d3d8.lib". This library holds all you need for basic Direct-X functionality (functions). Well you are basically ready.
Coding Begins
Right click and add "New" > "CPP File", if you know where else goto "File" > "New" > "CPP File". Call this CPP file "Main.cpp", I like keeping my base file where all main function is placed. However I use my game name as the CPP name when its a big project I am working on. Anyways open up your new Main CPP file, whatever you named it.
First we need the Direct-X base header file that will use the libary file you added to your project above. This header file is called "d3d8.h". Include this header file:
Code:
#include <d3d8.h>
//Application Title, using this variable the window class is easy to unregister
static char appTitle[ ]= "Starting in C++/Direct-X 8.1 - Tutorial 1";
We don't want your code to be sketchy so we need to make a header file. Make a new header file as you named your Main CPP file, for example if you named your main cpp "Main.cpp" then call your header file "Main.h".
Include this header file to your main cpp so code looks something like this:
Code:
#include <d3d8.h>
#include "Main.h"
Since we are using the Windows API to display your window and Direct-X surface, we need to create a main method initialize the project (Where your game starts). Add the following function to your main cpp.
Code:
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIsntance, LPSTR cmd, INT cmdsh)
{
Now we must configure the window class settings. Add the following code:
Code:
//Declares the windows class
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX); //Sets the window’s size
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; //Sets the window’s style
wc.lpfnWndProc = MsgProc; //Passes the message handler
wc.hInstance = hInstance; //Passes the instance handler
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); //Colors the window background black
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); //Uses the default application icon - large
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); //Uses the default application icon - small
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //Uses the default application pointer
wc.lpszClassName = appTitle; //Window’s Name
//Or Use:
//WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW | CS_OWNDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, appTitle, NULL};
//Register the window class
RegisterClassEx(&wc);
The settings of the window are all done. Now we have to pass the settings to the window we are going to create.
Code:
//Create the window
HWND hWnd = CreateWindow(appTitle, appTitle, WS_OVERLAPPEDWINDOW, 100, 100, 640, 480, NULL, NULL, wc.hInstance, NULL );
//Show the window
ShowWindow(hWnd, nCmdShow);
Great! Now your window shows – Code isn’t fully functional so expect errors if you try to run it. It doesn’t stay up though. We need to initialize Direct-X and create a while loop to keep the game running while it should be.
Code:
//If DirectX Initializes properly, simply begin updating window
if (InitializeDirectX())
{
while (gameIsRunning)
{
//Update the window
UpdateWindow(hWnd);
}
}
UnregisterClass(appTitle, wc.hInstance);
Now… We need a message handler to finish this tutorial up.
Code:
//Message handler
LRESULT WINAPI MsgProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
switch(wMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}
return DefWindowProc(hWnd, WMsg, wParam, lParam);
}
Now we need to add these two function declarations to your main header file.
Code:
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIsntance, LPSTR cmd, INT cmdsh);
LRESULT WINAPI MsgProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);
Good job guy! You did it, you setup the basic Win32 window where your game will play.
Project File & Source Code
*This tutorial isn’t complete, may be buggy, I wrote this tutorial straight from my head*