Sign in to follow this  

DirectX draw simple 2D shapes inside a Sprite (C#)

This topic is 3862 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 guys! Maybe someone think this is a stupid question, but i'm a novice, and i really need help ;). I can't figure out how to draw basic 2D shapes like rectangle, circle, line,... inside a sprite. Does anyone know how to help me? PS: C# code would be preferred, but C++ it's also Ok. Thanks to eveyone! Have a nice day.

Share this post


Link to post
Share on other sites
The easiest way to do this is to use an image editor and save a texture to disk of whatever shape you want, with an alpha channel. Then just draw a quad (sprite) with your texture on it. You don't draw 'inside' the sprite, you draw the sprite itself!

Share this post


Link to post
Share on other sites
For Materwork:
That's a good idea, but it's not really "friendly using".
I mean, what if i have to draw a line indide a rectangle near a circle?
I would have to load 3 Texture, set it to the needed position and scale 'em adeguatelly.
Uhm...what do u think about that?

For Dave:
Yes i do. I want do draw inside my sprite some basic shape like cicle, lines, etc..

Thanks 4 reply!
Hope u can help me.
Have a nice day.

Share this post


Link to post
Share on other sites
Ok, if you want to do it on the fly you'll need a different strategy. Your use of the word 'sprite' threw me off, because its often easier to use a sprite in a game then to program/model geometry for small objects.

Are you trying to use Direct3D for this? If so you'll probably need to create a texture, then lock it and update it with your shape procedurally (using whatever algorithms you want to come up with the final pixel/texel colors). Then you would unlock the texture, set it as the device's current texture, and render your quad with it.

You could still do this using a series of images from an image editor and transform them and render them to a render target -- kind of like a stamp, but the image could be a line, circle, or whatever which you then rotate and scale into position.

Share this post


Link to post
Share on other sites
I know i'm trying to do a "strange" thing using sprites, but i explain you better what's my idea:

I'm developing my own 3D engine and i want make possible to draw simple shapes on the screen like every 3D engines does.
In my case, i also want makes possible to show or hide the bounding box of a sprite, drawing the corrisponding rectangle on it just for debug issues.

That's why i need to draw shapes on sprites.
Any idea for this.

Thank you for reply MasterWorks.
Have a nice day

Share this post


Link to post
Share on other sites
Why would you want sprites for basic 2D objects? You just have to set the vertex format to XYZ/rhw (and vertex structure too), and supply x/y coordinates, leaving z blank. You also have to disable z-buffer, lighting and such. Lines and polygons are drawn with Draw..Primitive methods, but circles aren't supported directly, so you'll probably just have to use 16-32 lines to form it ({cos(A);sin(A)} for 32 As in the range of 0..2*PI).

If you want it to be stored in a texture after that, you can create a D3DXRenderToTexture object which is described in SDK and is relatively simple. I can drop the code if you have problems, but it is in C++.

Share this post


Link to post
Share on other sites
I don't know about circles, but for lines, since there is a line primitive that is natively supported, you could probably do a few tricks. Like set up an index buffer that points to the corner vertices of your sprites and render a lineList (which would draw lines around the outside boundaries of your sprites).

Share this post


Link to post
Share on other sites

This topic is 3862 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this