Skip to main content
GameDev.net gamedev.net
🔒 Locked

The Appropriate libraries to use for a 2D game

Started by Meldinoor Feb 21, 2007 at 4:07 AM 7 replies 1.5k views
Original Post
Meldinoor
Meldinoor
Just want to start out saying that I love this forum, and GameDev.net as a whole. No other site has been as inspiring and helpful, and I thank all of you who are willing to give up programming time, to share your knowledge and experience of the noble art of game development. That said, I shall proceed to introduce my project that began as an idea about one year ago. About a year ago I began working on an idea for a 2D role-playing game. Being a beginner I have written and re-written the game engine a number of times as I have learned new things Here's some info: For graphics I'm using bitmap images, and Direct3D's sprite renderer, and the code is written in C#. Now, I'm not going to bore you with too many details on the project, but I need some advice: The game's perspective is a 2D isometric view from above, where I simulate depth by rendering the more distant sprites first. The difficulty comes when I want to add simple primitives like rectangles for controls and textboxes for example. I use the Direct3D.Device object's DrawUserPrimitives method to render these, but the problem is that they end up behind my sprites. Of course, the primitives drawn with this method are really 3D-objects so they are rendered differently than the sprites. Any advice on how to solve this? And should I really be using the DirectX.Direct3D library for a 2D game? Or should I try another library? Thanks
TheAdmiral
TheAdmiral
Regarding which library to choose, Direct3D is just fine for 2D work. Along with OpenGL (and derivatives), it forms the only way to take full advantage of hardware acceleration. If you can write a complete 2D game in Direct3D, then the learning curve for a 3D game will be all the much shorter. Besides, if you want to stick with C# then you have little (though still some) choice in the matter.

Have you looked into Z-buffering/Z-testing/Depth-testing (they're all synonymous)? It's not clear from your post if you're using it, but if not, I suggest you do: it's an extremely handy means of having game objects appear in a perspective-correct order without having to render them in order.

With a Z-buffer installed you are free to render your '3D' objects in any order you like, usually grouping them by texture/shader/other render-state, to maximise performance. Once all the depth-tested objects are rendered, you can switch the Z-buffer off and render the UI, safe in the knowledge that it will appear 'on top' of everything else.

I've never used MDX, but I'd guess that the crucial call is
Direct3D.Device.SetRenderState(D3DRS_ZENABLE, <True/False>);
Of course, you need to set up your device to 'enable depth stencil', but this is thoroughly documented all over the web.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Meldinoor
Meldinoor
Thanks for the quick reply Admiral :)
I'll take a closer look at Z-buffering, although I've already been working along those lines. I have tried rendering using the Z-buffer for my "3D" objects, but I don't think the Direct3D.Sprite object renders using any z-buffer values at all :( So I'm not sure what to do with the 2D-objects that I want to draw behind the 3D objects...
Meldinoor
Meldinoor
To be a bit more specific:
My main difficulty is drawing simple polygons on-screen along with the images drawn by Direct3D.Sprite.Draw2D(), without the polygons ending up behind the sprites...
wafflezone
wafflezone
Sounds like your problem was solved but if you're still looking for a game library to use, the sprite functions in XNA worked great for me.
TheAdmiral
TheAdmiral
Well, regardless of what's in the buffer (i.e. the sprites that have already been rendered), any calls to Draw[Indexed]Primitive[UP] will draw over it provided Z-buffering is turned off. So it doesn't matter that Draw2D() doesn't use its z-value. Simply make a call to
Direct3D.Device.SetRenderState(D3DRS_ZENABLE, false);
before rendering the geometry that comprises your HUD, then
Direct3D.Device.SetRenderState(D3DRS_ZENABLE, true);
afterwards, and the HUD should come out on top. If it doesn't, then the device was probably created without depth bits.

By the by, I recommend you get into the hang of using Sprite.Draw() rather than Sprite.Draw2D(), as it allows you to make full use of the depth-buffer. This is what I gleaned from taking a peek at some samples, so I may be wrong. Perhaps somebody more qualified in MDX than me can clarify things.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Meldinoor
Meldinoor
Thanks friends... I've looked into XNA and tried Depth Buffering, and I think it'll solve the problem :)
DvDmanDT
DvDmanDT
Can't you use transformed vertices to output your menues? Or do they still end up behind the sprites?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.