Delta Engine Blog

AI, Robotics, multiplatform game development and Strict programming language

Moving to Managed DirectX for .NET 2.0

The rocket commander website does support few new features now (hour, day, week lists and top 100 for all lists). Btw: Thanks to all beta testers who reported bugs.

Monday of this week the DirectX December 2005 SDK did came out (with updated Managed DirectX for .NET 2.0 stuff and even a technical preview of DirectX 10, but this does require Vista to run).

Today I tried to port my NormalMapCompressor tool to Managed DirectX for .NET 2.0 (December 2005 Edition). If you want to port your DirectX code to .NET 2.0, this post could be helpful :) If you ask why even bother porting, here is the answer: For easier debugging (using Visual Studio 2005 with .NET 1.1 DirectX causes a lot of annoying Loader Lock errors when trying to debug, etc.)! Also I like to refactor my code from time to time and make it more clean if I continue working on it, this is what happens if you port to Managed DirectX for .NET 2.0.
Please also see this post from ZMan about DirectX for .NET 2.0 changes (he already wrote about it for the October 2005 edition).

I will only mention things that happend in this little project (NormalMapCompressor), I'm sure porting Rocket Commander over will take a couple of more refactorings.

  • The DXHelp class doesn't exist anymore. For using DXHelp.GetTypeSize(typeof(YourVertexStruct)) you have to use the VertexInformation class now:
    VertexInformation.GetDeclarationVertexSize(YourVertexStruct.VertexDeclaration, 0); and obviously you have to define the VertexDeclaration in your vertex class now (but I had that done anyways).
  • TextureLoader doesn't exist anymore either, I've used it to get image information.
    For that you can now use Texture.GetImageInformationFromFile
    (was TextureLoader.ImageInformationFromFile(filename) before).
  • Many methods now expect the device as the first parameter instead of the last (more consistent method parameters now). Some examples: Mesh.FromFile, Mesh.Clone or the Mesh constructors.
  • LockVertexBuffer and many other Mesh methods are simplified now and use generics and the GraphicBuffer class now. For example the following code:
    TangentVertex[] verts =
    (TangentVertex[])someMesh.LockVertexBuffer(
    typeof(TangentVertex),
    LockFlags.None,
    new int[1] { someMesh.NumberVertices });

    gets converted to:
    TangentVertex[] verts = someMesh.LockVertexBuffer(LockFlags.None).
    ReadArray(someMesh.NumberVertices);

    If you want to write to the GraphicBuffer just use Write(.), thats also much easier.
  • PresentParameters.Windowed is now called PresentParameters.IsWindowed
  • For some strange reason the device has to be created with an IntPtr instead of a form or control, ZMan also pointed that out here. Just write form.Handle or control.Handle now.
  • Device GetDeviceCaps becomes GetDeviceCapabilities, also all other Caps became Capabilities.
  • Matrix methods got a lot longer names, say hello to Matrix.PerspectiveFieldOfViewLeftHanded and Matrix.LookAtLeftHanded (instead of Matrix.PerspectiveFovLH and Matrix.LookAtLH). There are a lot more.
  • Material does not have the .Ambient property anymore, just .AmbientColor now, which is the ColorValue. This pretty much sucks because I use Color's all over the place and have to convert them to ColorValue now, wtf? Same for Diffuse, Specular and even SpecularSharpness got renamed to Power (I still think SpecularSharpness is a way better name). I don't agree with ZMan who says "nice to see this confusing mess tidied up".
    Material mat = new Material();
    mat.Ambient = darkCol;
    mat.Diffuse = lightCol;
    mat.Specular = lightCol;
    mat.SpecularSharpness = 16.0f;

    becomes
    Material mat = new Material();
    mat.AmbientColor = ColorValue.FromColor(darkCol);
    mat.DiffuseColor = ColorValue.FromColor(lightCol);
    mat.SpecularColor = ColorValue.FromColor(lightCol);
    mat.Power = 16.0f;
  • Light.Type becomes Light.LightType, I also don't like that change (Light.Type says it all, doesn't it?), but who cares.
  • TextureLoader.FromFile(device, filename) and TextureLoader.FromCubeFile(device, filename) are now new Texture(device, filename) and new CubeTexture(device, filename), much cleaner. Why not remove Effect.FromFile and similar methods too?
  • Effect.FromFile does require an EffectPool now (I just created an empty one, I only use 1 effect in NormalMapCompressor). Also the paramerters have changed a bit.

And now the only thing I couldn't resolve yet. If anyone has ANY tips or tricks to do this, please tell me!
I could not find out how to do TextureLoader.Save or any texture save functionality at all. I need this to save the generated textures as dds files (compressed of course), saving bmp files is no problem (just locking the texture data and building a bitmap with it, then saving that bitmap). I already tried to contact specialists like Tom Miller or ZMan, but no answer yet ^^

When that problem is resolved I will release the new version of the NormalMapCompressor.