Wednesday, October 29, 2003

I was doing a little book research on Sprites in DirectX when I came across Kevin Harris' CodeSampler.com website.  He has a TON of samples in C++ there, the DirectX 9 samples being located here.  On that page, you will find a sample titled "Creating 2D Sprites with D3DXSprite"  It's one of those simple DX samples that gets right to the point, but still gives you something "meaty" to tinker with (as are most of his samples).  Anyway, the challenge is that none of his samples use Managed DirectX.  I took a few hours last night (plus 30 minutes today to have Tom Miller correct my errors :-) ) to rewrite the app in Managed DirectX and C#.  You can get the code here (csd3dxsprite.zip (44.07 KB) all set up for VS 2003).  There's a few points to keep in mind:

  • The code was rewritten to heavily leverage the event model in .NET windows forms.  Overall, it should be very understandable, but you can clearly see the difference between the C++ and C# version in this area.
  • The code is about 1/3 smaller than the C++ version.
  • The code is not an example of stellar "defensive programming".  I just wanted to do a fast conversion.  Feel free to criticize, fix it, make it prettier, etc. 
  • Finally, those of you familiar with DX will notice two things:
    • The "main loop" lingers inside the OnPaint event, triggering a "this.Invalidate()" at the end, effectively forcing OnPaint to be raised again.  Tom Miller claims that this is a more effective mechanism to work inside the eventing model but still follow the basics of a "game loop".  He wrote the Managed DirectX code, so who am I to argue? :-)
    • The code uses the new DX9 Sprite class, which is unrelated to DirectDraw sprites (they are basically textured quads).

Anyway...enjoy!

10/29/2003 8:14:43 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [3]  |  Trackback
11/6/2003 9:22:20 AM (Pacific Standard Time, UTC-08:00)
Cool!
11/6/2003 10:16:25 AM (Pacific Standard Time, UTC-08:00)
Still cool...but the code doesn't work for me. I found that the version of the SDK I'm using has some different signatures for both Sprite.Begin and for Sprite.Draw. Are you using the Summer 2003 Update? I don't actually have that one installed on this machine.

But even when I changed it, I'm still getting an exception. No time to look into it right now, but it's probably also a version issue. Appears to be a clipping error - I get something like "Source rect is outside the bounds of the destination rectangle". No time to debug it here, though. Maybe I can look at it tonight, as I've been meaning to build something much like this.
11/7/2003 6:27:17 AM (Pacific Standard Time, UTC-08:00)
Yes, you need the latest SDK update. Regarding the Source rect problem... I ran the program on my laptop yesterday and noticed some strange behaviour. I will look into it this weekend.
David Weller
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Theme design by Jelle Druyts

Pick a theme: