Advertisement Jump to content
Sign in to follow this  
mikazo

Transparent PNG on texture

This topic is 3623 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'm developing a game in C++ and DirectX and I'm having problems making my own mouse cursor. I'm using DirectInput for the mouse, and I have a PNG of a cursor, with the background of the cursor image transparent. The background for my game is a 2D PNG texture that is repeated, and underneath that, the background colour is black. My problem is, when I draw the cursor on top of the background PNG, the transparent parts of the cursor PNG eat through my texture background, so it ends up being black. Thanks [Edited by - mikazo on February 16, 2009 7:41:59 PM]

Share this post


Link to post
Share on other sites
Advertisement
Is it possible that, rather than the cursor image 'eating' through the background image, the transparency for your cursor image is not set up properly? (Perhaps you accidentally saved your cursor image without transparency, making the area around the cursor black, rather than transparent, so it looks as though the image is 'eating' through the background)? Or, it's possible that your DirectX device is not set up for transparency -- if you have any other semi-transparent sprites that render correctly, then you can rule out that possibility.

Share this post


Link to post
Share on other sites
I changed one line of code to this:

d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);

I checked those articles and did some more Googling on textures and alpha values and frame buffers, but I still can't seem to get it to work. I know that my PNG is transparent in the right spots, it's just that DirectX isn't rendering it properly I guess.

Does anyone have some more specific instructions I can try? If you need to see code, I can paste some.

Share this post


Link to post
Share on other sites
Did you also set the other states accordingly? Simply enabling alpha blending alone isn't enough.

Here's a snippet of pseudo code:


SetState( RS_ALPHATEST, RSV_ENABLE );
SetState( RS_ALPHABLENDING, RSV_ENABLE );

SetState( RS_SRC_BLEND, RSV_SRC_ALPHA );
SetState( RS_DEST_BLEND, RSV_INV_SRC_ALPHA );

SetState( RS_ALPHAFUNC, RSV_COMPARE_GREATEREQUAL );
SetState( RS_ALPHAREF, 8 );

SetState( RS_COLOR_OP, RSV_MODULATE );
SetState( RS_COLOR_ARG_1, RSV_TEXTURE );
SetState( RS_COLOR_ARG_2, RSV_DIFFUSE );

SetState( RS_ALPHA_OP, RSV_MODULATE );
SetState( RS_ALPHA_ARG_1, RSV_TEXTURE );
SetState( RS_ALPHA_ARG_2, RSV_DIFFUSE );

SetState( RS_COLOR_OP, RSV_DISABLE, 1 );
SetState( RS_ALPHA_OP, RSV_DISABLE, 1 );

Share this post


Link to post
Share on other sites
I managed to find the first six lines you posted at this page

http://msdn.microsoft.com/en-us/library/bb172599(VS.85).aspx

But I can't seem to find values for the rest of your pseudo-code.

These lines do make alpha/transparency work somewhat, but not completely properly. Has anyone seen how to set the rest of the values? Thanks.

Share this post


Link to post
Share on other sites
First, you shouldn't be using DirectInput for mouse input.
Second, you really shouldn't use DirectInput for mouse input at any time a mouse pointer is involved. All you're going to do is make your app feel sluggish, annoying to use, and nothing like any of the rest of the OS.
Unreal Tournament 2004 (I think, certainly some version of Unreal Tournament) is an excellent example of why you shouldn't do that.
Mandatory link on the subject.

As for the actual problem, can we see the code you use to load the texture, and the code you have for rendering? Usually this is enough:

pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);


Also, are you using the Debug runtimes? Any relevant output from them?
Can we see a screenshot of what you have currently? Are you remembering to draw the sprites in back-to-front Z order?

Share this post


Link to post
Share on other sites
Thank you Evil Steve, that code snippet made my alpha/transparency work properly. I have been using the debug runtimes. My game is 2D so I was hoping DirectInput wouldn't make too much of an impact on performance. Thanks for helping me solve the problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by mikazo
My game is 2D so I was hoping DirectInput wouldn't make too much of an impact on performance.


But this is not about performance. DirectInput for mouse input doesn't apply the pointer ballistics settings from your windows control panel. It thus forces the user to get used to a completely different mouse behaviour when switching from/to the game. This will be quite ok for a FPS game, but can be really annoying for a game which has a visible mouse pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tom KQT
Quote:
Original post by mikazo
My game is 2D so I was hoping DirectInput wouldn't make too much of an impact on performance.


But this is not about performance. DirectInput for mouse input doesn't apply the pointer ballistics settings from your windows control panel. It thus forces the user to get used to a completely different mouse behaviour when switching from/to the game. This will be quite ok for a FPS game, but can be really annoying for a game which has a visible mouse pointer.
Yup, exactly. Performance isn't an issue with DirectInput, but it is lower performance than using Window messages - the point in the journal entry about it was that you shouldn't use DirectInput because you think it'll be faster.

For mouse input, there's 3 main issues:
1. No pointer ballistics - so the mouse feels sluggish or jittery.
2. No way to get pixel coordinates from a DirectInput mouse device.
3. Considerably more code to get it working.

I'd really recommend using window messages or GetCursorPos() instead; if you're writing a 2D game where you're using the mouse a lot, this will drive the user absolutely insane. If I downloaded a 2D game demo and it was using DirectInput for mouse movement for the whole game, I'd almost certainly uninstall / delete it rather than get annoyed at the sluggish / twitchy controls.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!