|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionDirectX 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 http://msdn.microsoft.com/directx/ Ok, enough of an introduction, let's get to the code... The GraphicsOne 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 try
{
back.Draw(new Rectangle(150, 275+(40 * iOption),32,20),
Selected, rcRect, DrawFlags.DoNotWait | DrawFlags.KeySource);
}
catch(SurfaceLostException)
{
RestoreSurfaces();
}
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 Microsoft.DirectX.DirectDraw.Device displayDevice;
Microsoft.DirectX.DirectSound.Device soundDevice;
To create the try
{
SkidBuffer = new SecondaryBuffer(
loadedAssembly.GetManifestResourceStream("Invasion.skid.wav"),
soundDevice);
}
catch(SoundException)
{
Console.WriteLine("load sounds error 1");
}
Rest of the ConversionOverall, the remaining parts that needed to be converted went smoothly. I
removed the linked lists in favor of the 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 DebuggingDebugging 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.
HistoryThe 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.
|
||||||||||||||||||||||