Click here to Skip to main content
11,929,166 members (65,667 online)
Click here to Skip to main content
Add your own
alternative version


49 bookmarked

DX Game Utility Class Library

, 9 Nov 2003
Rate this:
Please Sign up or sign in to vote.
A managed DirectX class library that can be used as a foundation for basic 2D games. Demo included.

Latest news

For latest news on this project, please visit Feel free to e-mail me at with any questions or comments that you may have.


I made this project with a single goal in mind; to create a reusable Managed DirectX class library that can be used as a foundation for basic 2D games. I hope that I have succeeded. As a fellow game hobbyist, I present the GameUtil class library to you, so you too can benefit from its code. You can use it in any way you so desire; start your own 2D game, extend and/or alter the code for your own needs, or just browse the source code for learning purposes.

For your convenience and as a mini-tutorial, I've included a demo project that shows how easy it is to get a basic 2D game engine built, using the GameUtil class library.


Aside from .NET 1.0/1.1 (which you should have already), you'll need to have the latest version of Managed DirectX SDK (DirectX 9.0 SDK Update Summer 2003 as of this writing). Yes, even the demo project requires it.



This class can be used as your game surface, which can be attached to any .NET control. It's been made flexible to allow both window (whether it be a Form or Control) and pseudo full screen modes. It handles everything for you, including the backbuffer work and restoring lost surfaces.

Creating and displaying a DDGameSurface couldn't be any simpler;

DDGameSurface gameSurface = new DDGameSurface(control);

gameSurface.DrawText(0, 0, "Hello!", false);



The DDGraphic class is derived from the Surface class and provides a much more game friendly functionality, which includes setting frames, frame sizes, and retrieving frame areas to draw. This is perfect for animation. But don't worry, you aren't required to actually set any of these details if your graphic doesn't have any frames.

Here's an extension of the above example, which shows how to create and display a DDGameGraphic;

DDGraphic playerGraphic = new DDGraphic("player.bmp", 
    new SurfaceDescription(), gameSurface);
playerGraphic.FrameSize = new Size(32, 32);

if (gameSurface.CanDraw) {
      gameSurface.DrawTransparent(0, 0, playerGraphic, 




Normally you wouldn't create a DDGraphic directly, instead you'll probably want this class handle all that dirty work. The DDGameGraphics class acts as a lookup table for your game graphics, which you can change on the fly with some simple XML editing skills. This way, you can load all of your game graphics (or graphics for a particular map) with a simple object instantiation. Another thing that this class provides is flexibility. No longer will you need to hardcore all of those nasty constants to where your graphics are located, instead you can use the DDGameGraphics class which loads them for you, using information provided by an XML document. And yes, there is support for embedded graphics.

Here's a taste, further expanding the code above;

// graphics.xml (note that the only required attributes are key and path)

      <graphic key="player" path="Namespace.player.bmp" 
            frameWidth="32" frameHeight="32" transColor="00111111" 

      <graphic key="enemy" path="Namespace.enemy.bmp" 
            frameWidth="32" frameHeight="32" transColor="00111111" 



The DIMouse class may seem a bit odd to work with at first. This is because a DIMouse object will only update its internal state when the Update() method is called. Everything else however, is a breeze to understand and use. The DIMouse class encapsulates pretty much everything you'd expect to get from a mouse, including cursor coordinates, button clicks (even wheel clicks), and wheel movement. Unlike a DDGameSurface however, a DIMouse must be created for a Form.

Here's a quick example;

DIMouse mouse = new DIMouse(form);

// Inside game loop.
if (mouse.LeftClick) {
      Console.Writeline("Left click at coordinates: " + mouse.Location);
// --



DIKeyboard works identically to the way DIMouse does, except that it has a different interface for accessing keyboard information.

DIKeyboard in action:

DIKeyboard keyboard = new DIKeyboard(form);

// Inside game loop.
if (keyboard[Key.A]) {
      Console.Writeline("The A key is pressed!");
// --



What game would be complete without some sort of high performance timer? The QPTimer class encapsulates the Query Performance Counter and Frequency for you, so all you have to do is work with its simplistic interface. And don't worry, the ticks per second is a floating point number. As a side note, there isn't a property that returns the ticks per millisecond, but that's easy enough to solve on your own - just multiply the TicksPerSecond property by 1000.

A game loop example showing off the QPTimer :

QPTimer timer = new QPTimer();

while (gameRunning) {

      // Process mouse input.
      // Process keyboard input.
      // Update game logic with timer.TicksPerSecond.
      // Draw graphics.

      Console.Writeline("FPS: " + 1.0F/timer.TicksPerSecond);


GameUtil Demo

Just giving you a class library without actually showing how it's used to create a game engine would be quite pointless. So I've gone ahead and created a demo which shows how to put most of the GameUtil class library's features to work. Heck, if you really wanted to, you could use the demo as a template for creating your own 2D game. At the very least, it'd make a good tutorial on how to get started.

I would like to point out a note or two; The FPS shown in the demo are slower then what you'd expect in an actual game, simply because the demo engine is doing some reflection work and also updating .NET controls at the same time (I was more interested in showing features than making it efficient). Despite this, the demo can draw about 2,500+ graphics and still get a steady ~30 FPS (my computer is an 800mhz Athlon, with 250mb of PC133 RAM and a Geforce 2 MX, so your mileage may vary).

Closing Comments

Thanks for reading, and I hope you find this class library useful.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Dan Peverill
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralWonderful!! Pin
invisible8615-Jul-10 8:02
memberinvisible8615-Jul-10 8:02 
GeneralDirectX and VS2005 Pin
Tareq_Gamal13-Aug-06 2:28
memberTareq_Gamal13-Aug-06 2:28 
GeneralDirectDraw Transparency problem Pin
Mishan Pog3-May-05 6:53
sussMishan Pog3-May-05 6:53 
GeneralRe: DirectDraw Transparency problem Pin
gbravoo28-Apr-06 1:50
membergbravoo28-Apr-06 1:50 
QuestionNew Version ? Pin
Boo!18-Oct-04 12:24
memberBoo!18-Oct-04 12:24 
GeneralAVI Pin
waelahmed24-Jan-04 9:53
memberwaelahmed24-Jan-04 9:53 
GeneralRe: AVI Pin
Andy Acid26-Feb-04 3:22
sussAndy Acid26-Feb-04 3:22 
GeneralManaged Direct X Pin
stano10-Nov-03 22:51
memberstano10-Nov-03 22:51 
GeneralRe: Managed Direct X Pin
Dan Peverill10-Nov-03 23:18
memberDan Peverill10-Nov-03 23:18 
GeneralVery nice Pin
Broken God10-Nov-03 21:05
memberBroken God10-Nov-03 21:05 
GeneralRe: Very nice Pin
Dan Peverill10-Nov-03 23:19
memberDan Peverill10-Nov-03 23:19 
QuestionNice, but....VC6 version? Pin
Roger Allen10-Nov-03 7:43
memberRoger Allen10-Nov-03 7:43 
AnswerRe: Nice, but....VC6 version? Pin
boyone10-Nov-03 8:20
memberboyone10-Nov-03 8:20 
GeneralRe: Nice, but....VC6 version? Pin
Dan Peverill10-Nov-03 12:42
memberDan Peverill10-Nov-03 12:42 
GeneralRe: Nice, but....VC6 version? Pin
WorldMaker11-Nov-03 10:11
memberWorldMaker11-Nov-03 10:11 
AnswerRe: Nice, but....VC6 version? Pin
Dan Peverill10-Nov-03 12:38
memberDan Peverill10-Nov-03 12:38 
AnswerRe: Nice, but....VC6 version? Pin
WillemM9-Jan-04 8:07
memberWillemM9-Jan-04 8:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 10 Nov 2003
Article Copyright 2003 by Dan Peverill
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid