Jump to content
  • Advertisement
Sign in to follow this  
TheSilverHammer

DirectX 2D questions in C#

This topic is 3920 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have decided to start tinkering with DirectX and decided to use C#. I am a very good C++ programmer, but I really like the C# language better, and the directX API seems to be so much better. Anyway... I have been trying to mess with sprites for the past 4 or 5 days. I just want to create my own robust set of sprite handling classes. After working through using my own polygons and whatnot, I finally moved to using the Sprite class. AT first I thought this was a directX 10 thing, but now that I know its DX 9 (or earlier) I plan on using it. Anyhow, I have been trying to learn the basics of moving sprites, rotating, scaling, and using alpha maps. For basic positioning and drawing, I was using the Sprite.Draw() function. However that didn't let me rotate the images, so I moved to Draw2D. Then after searching for some other information, I discovered that the Sprite has a .Transform property, so I can use matrixes. Question 1: So, my first question to the experts here, is should I stick with Draw2D or should I drop back down to Draw and use Matrixes? Which is faster? In general, what is the best way of handling scaled, rotated, and translated sprites? Question 2: When I was using just vertexes and making my own polygons, I could use a coordinate system ranging from (-1,-1) to (1,1). This made things nice and easy since I never had to worry about the actual screen size. Now going to the Sprite class, I am back to absolute screen positions. Is there a 'mode' I can switch to go back to using the -1.0 to 1.0 coordinate system? Question 3a: (textures and powers of 2) I have been looking for some textures I can use in a 2D space game. I couldn't find much, but this one site that had a whole bunch of animated abstract sprites. They are big and very pretty, but they are 96 x 96, which isn't a power of 2. I have noticed if I ask DirectX what my texture size is, it always gives me a power of 2 boundry. However, if I actually use what it is telling me (ie: Texture Width is 128 instead of 96) all the texture coordinates are wrong. If I just hard-code stuff as 96 x 96 it all works (with some exceptions). Why doesn't using the 128 size work? Id assume that DX would stretch the texture, but it doesn't seem to. Question 3b: Unless, I apply a filter. Then everything gets messed up. If I use mip-mapping, the minute the mip-map kicks in, my texture gets out of alignment. If I apply any filters to the image, it is also hosed. Yet using the basic 1:1 scale of 96x96 works fine, only as soon as I change my sprite size, it gets screwy. Question 3c: If a texture has many other textures in it, does the master texture have to be a power of 2 or just do the smaller textures inside need to be a power of 2? IE: 64 x (64 x 100 frames) = 64 x 6400 Question 3d: Not really a code question, but is there anyone who can point me to a public domain source of some 2d textures I could use in a 2d space game? Question 4: A lot of documentation says that when a device resets, you lose everything but vertex buffers. Resets happen when you re-size a DirectX window. Yet I can move and resize my directX window and my sprites do not lose their textures. What is going on?

Share this post


Link to post
Share on other sites
Advertisement
If you're looking for a fast, easy to use API for rendering, etc...and you're interested in both C# and DirectX, I highly recommend using XNA. Especially if you're new to DirectX.

In case you're not familiar with XNA, it's Microsoft's Managed API for DirectX, written as a C# wrapper around DirectX 9, which allows easy development for both the PC and XBox 360. MDX, Microsoft's old Managed API has been discontinued and is no longer be developed for.

In response to your original question, XNA has a nifty little class called SpriteBatch, which is designed to be used for drawing sprites as 2D Textures.

It has a Draw function with several overloads, several of which support scaling, rotation, color tinting, etc...

For more info check out SpriteBatch.Draw() on MSDN.

Cheers!

Share this post


Link to post
Share on other sites
Check out Chad's great tutorials

http://www.chadvernon.com/blog/tutorials/managed-directx-2/

He does have a section about using sprites, but can I recommend you checking out his quad class. You can use quads instead of sprites and you get much more control over them. This will also mean you can choose to use absolute coord system or not.

You will need to change his custom vertex used in his quad class because it's using the absolute coord just like a sprite does. Here is what you need to change. This will also add Z to your sprite.

    public struct PositionNormalColored2Textured
{
public Vector3 Position;
public Vector3 Normal;
public int Color;
public float Tu1;
public float Tv1;
public float Tu2;
public float Tv2;
public static readonly VertexFormats Format = VertexFormats.Position | VertexFormats.Normal | VertexFormats.Diffuse | VertexFormats.Texture2;
public static readonly int StrideSize = DXHelp.GetTypeSize(typeof(PositionNormalColored2Textured));

Share this post


Link to post
Share on other sites
From what I understand about XNA, and I may be wrong, is that XNA requires people to pay to play or something like that. I can't just make a game and hand it out to people can I?

Also I have Dev studio 2005, doesn't XNA require some express version of dev studio?

Share this post


Link to post
Share on other sites
Quote:
Original post by TheSilverHammer
From what I understand about XNA, and I may be wrong, is that XNA requires people to pay to play or something like that. I can't just make a game and hand it out to people can I?

Also I have Dev studio 2005, doesn't XNA require some express version of dev studio?


To play games created with XNA on the XBox 360 console, you need to pay a yearly fee. To play games created with XNA on a PC, you only need the freely available XNA redistributable installed. No fees are needed to play on the PC.

As for Visual Studio, XNA requires Visual C# 2005 Express Edition to function correctly. It is possible to use other compilers and languages, but you lose some functionality. Visual C# 2005 Express Edition is a free download from the microsoft website, is about 35mb, and installs nicely side-to-side with other versions of VS, so you shouldn't have a problem if you wish to use it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!