Setting up your gameloop for Direct X

This topic is concerned with the first baby steps of learning to program using the DirectX library in C++. We’ll have a look at how you access the video-display-surface(access the gfx hardware), create a buffer to avoid flickering and run the example.
First install a direct x sdk if you’re not using Windows8. I installed a version released in october 2006 to make sure it was compatible with the examples in my literature(DirectX does not support backwards compatability(Microsoft are notoriously bad at this (*waving fist at VisualC++))).

Also this tip will assume that you have a basic understanding of gameloops in win32 C++ programming. If you don’t, get it before, it’s probably required for whatever you intend to do anyway. The logic of a generic gameloop using peekmessage on the windowsqueue usually deploys all gamelogic in three methods, Game_Init(), Game_Run() and Game_End(). This seperates the gameloop and windows stuff from our game. The init part is used to setup the datatypes used by the program and then we utilize them in Game_Run(). When the program exits we deallocate all memory that our program used in Game_End().

If you have used windows GDI before, you know that to paint on the screen one has to create a devicecontext(a surface to paint on) and refresh this when events occur. The difference here is that we are using the direct x library that enables us to communicate with computer hardware directly(direct x where x is some type of hardware device), in this case the default graphics card.
So lets setup our gameloop for direct x. The following code will only show direct x aspects, to create the whole program you can use a generic gameloop or your own custom bells and whistles loop.

#include <d3d9.h> (duh, remember to point your compiler to your SDK folder)
#include <time.h>

//forward declare Direct 3d objects:
LPDIRECT3D9 d3d = NULL;//long pointer to the direct3d9 interface
LPDIRECT3DDEVICE9 d3ddev = NULL;//long pointer

int Game_Init(HWND hwnd){

//initialize Direct3D
d3d = Direct3DCreate9(D3D_SDK_VERSION);//Init d3d object
if (d3d == NULL)//If something goes wrong

MessageBox(hwnd, ”Error initializing Direct3D”, ”Error”, MB_OK);
return 0;


set Direct3D presentation parameters
these must be set before device is created
with createdevice. The struct values set
in this example is not all available but
those neccesary for this example.
D3DPRESENT_PARAMETERS d3dpp;//define variable for the setup struct
ZeroMemory(&d3dpp, sizeof(d3dpp));//make sure the struct values are 0
d3dpp.Windowed = TRUE;//guess 😉
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;//Swapping effect for backbuffer(to avoid flickering)
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;//Format of the back buffer,
//unknown means desktop format in a window

//create Direct3D device
D3DADAPTER_DEFAULT,//use default video card
D3DDEVTYPE_HAL,//use hardware renderer
hwnd,//window handle
D3DCREATE_SOFTWARE_VERTEXPROCESSING,//do not use transform & lighting
&d3dpp,//presentation parameters we defined above (row 38-42)
&d3ddev//pointer to device object

if (d3ddev == NULL)
MessageBox(hwnd, ”Error creating Direct3D device”, ”Error”, MB_OK);
return 0;

//set random number seed

//return okay
return 1;


Thats it for the direct x part. However you may have other stuff like levelstructs, soundstuff and variables for hte game that also needs to be defined and setup. I usually seperate these in own methods aswell, remember basic oop, a method should only do one job (in a ideal setting).

Let’s have a look on the method that runs the game.
void Game_Run(HWND hwnd)

//make sure the Direct3D device is valid
if (d3ddev == NULL)

//clear the backbuffer to green
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,0), 1.0f, 0);

//start rendering
if (d3ddev->BeginScene())

      //stop rendering

//display the back buffer on the screen(which we set to green remember..)
d3ddev->Present(NULL, NULL, NULL, NULL);


Finally we run game end and release the stuff allocated for the graphics part of our program
before quitting. Dont forget this as it leaves the process in the background on your computer. That’s av mistake considered ”very bad” by the most modest of teachers in programming.
void Game_End(HWND hwnd)

//display close message
MessageBox(hwnd, ”Program is about to end”, ”Game_End”, MB_OK);

//release the Direct3D device
if (d3ddev != NULL)

//release the Direct3D object
if (d3d != NULL)


Finally I want to put out a disclaimer that this is a learning process for two me so there might be some errors in the examples. I only post stuff I fairly confident on but you never know.

Note: The indentation doesn’t work on wordpress that well so if some codes looks bad it’s the damn indentation. I have to manually adjust it at the moment. In the future I might use jpg’s instead.

  1. Miedziaq

    Hello. I have a problem. I compile this code on my computer with Release and Debug parameters, and on second computer i see error about ”Error creating direct device”. Have you any tips?

    • ratache

      ”Error creating device” means what it says, and I can’t get a proper picture of your issue from the sparse info in your question. Make sure all dependencies are specified correct. If you are using two computers(?) for some reason and have copied the project to the second computer, makes sure to update the folder paths to the direct x sdk folders. Or to avoid this in the future copy all dlls you need to a resource folder in your project and link that. That’s all I can think of, creating a device should be pretty straightforward.

      Fore more info about setting up the environment; try this Setting up the DirectX SDK with Visual Studio 2010, How do I add DirectX SDK into Visual Studio 2012. Also browser msdn and if all else fails check google/stackoverflow/daniweb/books.


Fyll i dina uppgifter nedan eller klicka på en ikon för att logga in:

Du kommenterar med ditt Logga ut /  Ändra )


Du kommenterar med ditt Google-konto. Logga ut /  Ändra )


Du kommenterar med ditt Twitter-konto. Logga ut /  Ändra )


Du kommenterar med ditt Facebook-konto. Logga ut /  Ändra )

Ansluter till %s