Transparent PNG on texture

Started by
8 comments, last by Evil Steve 15 years, 2 months ago
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]
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.
If you are drawing the cursor with a textured quad, then you need to configure the texture stage for the cursor texture properly and you need to configure the frame buffer for alpha blending.

Chapter 11. Basic Texturing
Chapter 14. The Frame Buffer

My free book on Direct3D: "The Direct3D Graphics Pipeline"
My blog on programming, vintage computing, music, politics, etc.: Legalize Adulthood!

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.
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 );

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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.
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?
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.
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.
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.

This topic is closed to new replies.

Advertisement