Sign in to follow this  

DX9 and PNG images/sprites?

This topic is 3588 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

Firstly, I'm fairly new to the world of DirectX.. I'm working on a Breakout game right now, and it's working surprisingly well. I have it limited at about 60fps, and it appears to be very smooth. I'm also working with C# 2008, but targetted for the .NET 2.0 framework. Now for the actual questions, of which I have two: 1) GIF Transparency. Can it be done? I'm using the Sprite class to do my drawing, and it doesn't seem to draw transparencies very well... and by not very well, I mean not at all. Is there a property that needs to be set for the Sprite object for it to understand transparency? 2) GIF Animations. Can they be done with a standard animated GIF? Or am I stuck to using the actual Sprite mentality of said class? If so, how? Thanks in advance, and slick forum you guys got here! [Edited by - timeshifter on February 15, 2008 4:27:42 PM]

Share this post


Link to post
Share on other sites
a) GIF Transparency is not supported in the sense that it will not automatically work. It will merely show the color you've chosen to be transparent (as you've seen). The obvious solution to this is when loading your texture to specify the COLORKEY parameter to be the same color as your GIF image. This will cause the color to become transparent when you render it with Alpha Blending enabled.

b) GIF animations are not supported in any way. Attempting to load an animated GIF would be the same as loading it into most image editors -- you'll only see the first frame. Your best bet is to roll your own animation class.

You're better off just using the PNG or TGA file format anyway, as they support 32 bit color, with various levels of alpha blending.

Share this post


Link to post
Share on other sites
You'll have to handle animation yourself, I'm afraid. The typical way of doing this is to use the ID3DXSprite interface, and load a texture that has all of your animation frames in one row. Then you loop through your frames by specifying a source rectangle when rendering. I could show you code in C++, but I don't know C# I'm afraid.

Share this post


Link to post
Share on other sites
It can be done, but you have to load them with GDI+ and then create a texture and draw all the frames to it(or just create one texture per frame). This tends to use up quite a lot of memory, thus defeating the whole point of the GIF format to begin with, so I would suggest using something like PNG or DDS.

If you're interested I wrote a journal post along with the C#/MDX code to do it here. If the link doesn't work, it's from August 7th, 2006.

Share this post


Link to post
Share on other sites
I recieved this when clicking your link:

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'CLng'

/community/forums/mod/journal/journal.asp, line 224




Besides that, the animations have kinda fallen aside... not a big priority anymore. The real thing I want to get working is the transparencies... having fragments of bricks fall over other bricks with black backgrounds in the fragment image isn't very appealing...

Another thing, if anybody knows a decently easy way, would be sprite rotation... in following Intellisense, it does SOMETHING, but it's not what it seems like it should... I'll post code later if anyone wants to see what I've tried that doesn't work.

Share this post


Link to post
Share on other sites
Quote:
Original post by timeshifter
I recieved this when clicking your link
Corrected link

As for the rest of your question, how are you using sprites at the moment? Are you rendering quads in screen space, or are you using ID3DXSprite (Or whatever it's called in C#)?

Share this post


Link to post
Share on other sites
@FlimFlam:
I did change to PNG's, and they do have alpha transparency. A bit of poking around with a program I found online and I found two lines that enable the transparency:
device.RenderState.AlphaBlendEnable = true;
device.RenderState.DestinationBlend = Microsoft.DirectX.Direct3D.Blend.BothInvSourceAlpha;

Looks great, too.

@Evil Steve:
I'm using the Microsoft.DirectX.Direct3D.Sprite class, and as far as I can tell, it's drawing the sprite straight to the device using Textures that I load in at the start of the app.

Share this post


Link to post
Share on other sites
Quote:
Original post by timeshifter
device.RenderState.AlphaBlendEnable = true;
device.RenderState.DestinationBlend = Microsoft.DirectX.Direct3D.Blend.BothInvSourceAlpha;


If you just want transparency and not blending, your performance might be better using alpha testing.

Device.RenderState.AlphaTestEnable = true;
Device.RenderState.AlphaFunction = Direct3D.Compare.Greater;

Share this post


Link to post
Share on other sites
Quote:
Original post by timeshifter
Doing that actually made every sprite drawn completely transparent so nothing showed...


You can try adding this to his code:

Device.RenderState.ReferenceAlpha = 0x7F;

I've always had weird issues using alpha testing when reference alpha was 0.

Keep in mind that your textures do need an alpha channel for alpha testing to work, so we're assuming you're using PNGs now and not trying it with the GIFs.

Share this post


Link to post
Share on other sites
Still not working... before I go too much further with this stuff, what exactly is this test doing? From what it seems to me, I'm telling it to test the alpha channel for a value greater than 0x7f, and render it transparent if it passes... is that correct?

Share this post


Link to post
Share on other sites
I've modified the title, since it's no longer GIF's I'm dealing with.. .and I have a sneaking suspicion I may not have the transparency I think I do. I'll confirm that when I can... until then, I guess the rotation is the next big thing. How can I draw my Sprite with point rotation? It seems to be eluding me...

Share this post


Link to post
Share on other sites
Quote:
Original post by timeshifter
I'm telling it to test the alpha channel for a value greater than 0x7f, and render it transparent if it passes... is that correct?


"render it transparent" doesn't really make sense, the whole point of something being transparent is to not render it.

What the test does is test the alpha channel and if it's greater than 0x7F renders its, if the test fails the pixel/texel is ignored. You can of course change the reference value and the operation to something other than greater.

Note that since this is a boolean test, it's either on or off, you can't use it for smooth transparency/blending. However it's a lot faster then blending since blending adds/subtracts/multiplies colors together.

Share this post


Link to post
Share on other sites
Yeah, i suppose that doesn't make much sense... tells you how new I am to game dev, I guess.

But your description certainly cleared things up. Thanks for that. Now if I could just get my rotations right...

Share this post


Link to post
Share on other sites

This topic is 3588 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