Click here to Skip to main content
Click here to Skip to main content

Invasion - C# Style

, 28 Jan 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
This "Space Invaders" - like game was ported to C# using the new DirectX9 to demonstrate the ease of managed DirectX.
<!-- Download Links --> <!-- Article image -->

Title Screen Middle of the game

<!-- Add the rest of your HTML here -->


DirectX has changed quite a bit over the years and the new DirectX 9 offers some new things that previous versions never did.  DX9 provides new functionalities for enhanced 3D manipulations.  But one big change is the addition of a managed DirectX so that you can create applications using a .Net language and still tie into DirectX.  This is without having to work around a COM Interoperability issue.  DirectX9 directly supports managed code and makes life for the DirectX/C# developer much simpler in the long run.  The Managed DirectX classes include a DirectDraw set of classes to use for 2D manipulation.  This 2D interface is not a new version of DirectDraw - it is a wrapper to get to the DirectDraw7 interface.  This is good for a lot of developers that still want to do 2D games and also want to use a .NET language.

The DirectX SDK can be obtained from Microsoft at

Ok, enough of an introduction, let's get to the code...

The Graphics

One of the first things that I did was to convert the BMP files into PNG.  I did this for a couple of reasons.  The first is that PNG files are smaller, so that having them embedded into the executable directly will not increase the file size as much as bitmaps would.  The second reason is to demonstrate that other file formats can be used to create surfaces from.  This is not directly in DX9, but there are C# classes to read the data in correctly.  I had converted to JPG at first, but the quality setting was too low and the images did not look as good. 

Handling error codes from the calls to methods like Draw are different from DX8 too.  There is not a returned error code, just a try/catch to get the errors.

    back.Draw(new Rectangle(150, 275+(40 * iOption),32,20), 
              Selected, rcRect, DrawFlags.DoNotWait | DrawFlags.KeySource);

The Sounds

Sounds ended up being a very simple thing overall, but I did have a few problems with it.  At first, the samples for using DirectSound seemed simple enough until you notice that the Device class that DirectSound uses is the same class name that DirectDraw uses.  This meant that I had to fully qualify the class names when I created those variables.

Microsoft.DirectX.DirectDraw.Device displayDevice;
Microsoft.DirectX.DirectSound.Device soundDevice;

To create the SecondaryBuffer objects to play the sounds, I decided to use a stream from the executable to get WAV files into the buffers.  To do this a GetManifestResourceStream was used.  The problems that I had here were that the resource had to be fully qualified with the assembly name too.

    SkidBuffer = new SecondaryBuffer(
    Console.WriteLine("load sounds error 1");

Rest of the Conversion

Overall, the remaining parts that needed to be converted went smoothly.  I removed the linked lists in favor of the ArrayList class.  This really cleaned up the code when it came time to add/remove UFOs, bullets, and extras.  You could argue that using a vector from STL would clean up the handling of the linked list too or the use of some other type of container class.  I agree that is the reason for the conversion.

One of the tricky parts of converting an application from C++ to C# is remembering all of the little differences between the languages.  For example, in C++ you can use rand() to generate a random number.  For C#, random numbers are generated with an instance of the Random class.  This works just great, except for one thing.  I originally setup each UFO with its own random variable.  This worked just fine until I realized that each UFO was getting the same random numbers.  This is because the instance of Random, seeds the variable with the current time.  This meant that the separate UFO variables would get a random number generator all seeded with the same time.  Each of the random generators created the exact same numbers.  Once I moved the random number generator to the Invasion class, then each UFO had its own values and things worked as expected. 


Debugging a fullscreen DirectX application is always a tricky thing to do.  For this conversion, Console.Writeline was used to print out data to the debug window. Unhandled exceptions were the root of most of the crashes that were experienced; this includes InvalidRectangleException as the biggest problem.  This seems very important with Managed DirectX to handle all exceptions with a try/catch.  A second display could have been used to make things simpler, but having the output in the debug window after the crash was good enough to fix the issues. 


The original code for this game came from Invasion - A computer game using DirectDraw by Mauricio Ritter.  It is a great little game that had all of the right graphics and unlimited game play.  Mauricio did a great job, so I made sure to leave his name in the credits.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Steve Maier
Software Developer (Senior) Microsoft
United States United States
No Biography provided
Follow on   Twitter

Comments and Discussions

QuestionThe Port to Windows Phone in C# and XNA is DONE! PinmemberDan Colasanti30-Jun-11 21:41 
QuestionI add a windows7 multi-touch feature into your game ,do you have interest to add the feature in your program? Pinmemberjuliuske20-Jan-10 21:21 
AnswerRe: I add a windows7 multi-touch feature into your game ,do you have interest to add the feature in your program? PinmemberSteve Maier28-Aug-10 8:40 
GeneralNew version soon PinmemberSteve Maier31-Aug-06 9:08 
GeneralShooting UFO bullets PinmemberJohnDonaldson22-Feb-06 7:38 
GeneralRe: Shooting UFO bullets PinmemberSteve Maier22-Feb-06 18:22 
GeneralRe: Shooting UFO bullets PinmemberJohnDonaldson24-Feb-06 8:25 
Generalslow! PinmemberKeimax21-Jan-06 2:11 
GeneralRe: slow! PinmemberSteve Maier22-Jan-06 11:13 
GeneralRe: slow! Pinmembersontek30-Mar-06 12:14 
GeneralRe: slow! PinmemberSteve Maier30-Mar-06 16:21 
GeneralRe: slow! Pinmembermitsubishiuk6-May-06 12:28 
GeneralCan't Play and DirectX reference not found Pinmemberkenexcelon1-Mar-05 17:01 
GeneralRe: Can't Play and DirectX reference not found PinmemberSteve Maier1-Mar-05 19:13 
GeneralTransparency Pinmemberarkam7-Dec-04 22:59 
GeneralRe: Transparency PinmemberSteve Maier8-Dec-04 3:26 
GeneralRe: Transparency Pinmemberrajshukla12317-Sep-05 1:32 
Generalfront.Draw V's front.Flip Pinmemberexhaulted3-Nov-04 23:12 
GeneralNext Version PinmemberBassam Abdul-Baki21-Oct-04 16:05 
GeneralRe: Next Version PinmemberSteve Maier21-Oct-04 17:36 
GeneralRe: Next Version PinmemberBassam Abdul-Baki22-Oct-04 4:43 
GeneralRe: Next Version PinmemberSteve Maier22-Oct-04 8:01 
GeneralRe: Next Version PinmemberBassam Abdul-Baki22-Oct-04 8:41 
GeneralDirectInput vs NET Form event handers PinmemberMark Austin17-Sep-04 9:37 
GeneralRe: DirectInput vs NET Form event handers PinmemberSteve Maier17-Sep-04 10:00 
GeneralLooking for samples using D3DX.Mesh Pinmemberjlabbe13-Sep-04 7:19 
GeneralExcellent Game! PinmemberBassam Abdul-Baki28-Jul-04 14:24 
GeneralRe: Excellent Game! PinmemberSteve Maier28-Jul-04 16:25 
Generalregds to usage of images Pinmemberpushkar_007-May-04 21:15 
GeneralRe: regds to usage of images PinmemberSteve Maier7-May-04 22:00 
GeneralTransparent images Pinmembermichal81p24-Feb-04 12:21 
GeneralRe: Transparent images PinmemberSteve Maier24-Feb-04 12:39 
GeneralRe: Transparent images Pinmemberkusokurae22-Nov-04 2:01 
GeneralCan't run the examples Pinmembershobley19-Dec-03 9:20 
GeneralRe: Can't run the examples PinmemberSteve Maier19-Dec-03 9:23 
GeneralRe: Can't run the examples Pinmembershobley29-Dec-03 5:57 
GeneralRe: Can't run the examples PinmemberDarth_Sulfur20-Jul-05 10:25 
QuestionHow to create big surface? PinmemberLiu Junfeng20-Aug-03 17:06 
GeneralReferences not found Pinmember_cancer_23-Jul-03 8:03 
GeneralRe: References not found PinmemberSteve Maier23-Jul-03 8:38 
GeneralRe: References not found Pinsuss[si]cris4-Aug-03 11:12 
GeneralRe: References not found PinmemberSteve Maier4-Aug-03 11:36 
GeneralRe: References not found PinmemberNajak31-Dec-03 11:05 
Generalproblem with class Audio Pinmemberusma2-Jul-03 6:53 
GeneralGreat Job PinmemberJohn Lyon-Smith1-May-03 9:38 
GeneralU Da Man! PinmemberRon Icard18-Feb-03 4:46 
GeneralGreat PinsussAnthony Urwin23-Jan-03 5:17 
GeneralGreat Job ! PinmemberMauricio Ritter18-Jan-03 13:06 
GeneralRe: Great Job ! PineditorNishant S18-Jan-03 13:17 
GeneralRe: Great Job ! PinsussAnonymous12-Feb-03 2:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web01 | 2.8.150327.1 | Last Updated 29 Jan 2003
Article Copyright 2003 by Steve Maier
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid