Skip to main content
gamedev.net
🔒 Locked

click all texture except alpha

Started by lexor May 22, 2002 at 7:04 AM 2 replies 550+ views
Original Post
lexor
lexor
Hi! I have a question for my 2d interface I'm currently developing. What I've done is to load a texture with CreateTextureFromFileEx onto a D3DXSprite. I use a color key to make RGB(0,255,0) transparent and that works just fine. BUT. Now I want to make my texture clickible, and for that I preveusly used a transparent rect on top of my 'button'. This works but is not very satisfying couse of the rough edges the box gives me. What I want to do (if it's possible) is to make all EXCEPT the transparent parts of my button-texture clickible. Is there a way to say: 'if buttonsprite -transparent parts is clicked then' Is this possible? Oh, I'm useing VB and DX8 for a 2d game. Thanks in advance [edited by - lexor on May 22, 2002 5:43:47 PM]
_______ www.leXor.net
cybrgeek
cybrgeek
When you recive the mouse down event figure out what button is getting clicked, then adjust the position based on the button, example, if the button is located at (150, 200) and the click is at (163, 220) then subtract the button position from the X and Y positions to get the spot on the button that got clicked (13, 20). Then you need to check if that pixel is transparent. You can either lock the texture and then check the alpha value of that pixel to see if it is zero, or generate an alpha map, just an array of boolean values the size of the number of pixels of that texture that is either 1 for a solid pixel, and 0 for a transparent pixel. The alpha map would be generated when the texture was loaded. The first option would be best for button clicks, but the locking and unlocking texture memory is slow, so if you wanted to do this for a mouse over you would probably want to go with the second option.
lexor
lexor
OK, Thanks. But do you know how I generate a boolean-style mask for all transparent pixels.

My little game ala Monkey Island needs to have lots of this stuff, both for collision detection, and selection (mouse over and mouse click), so I dont want to hand code the masks for all pixels in every D3DXSprite. The 'lock texture and check if the a visible or transparent pixel is clicked'-method sounds nice but may be alitle hevey if I got 10 sprites with different (non boxy) shapes.

Is there not a way to just load the textue and say that the transparent pixels could not be clicked, just like they could not be seen.

Please give me any hints on how to do this, tho is a keypart of my game. and again, I'm useing vb/dx8/d3dxsprites to draw all textures.

Thanks

[edited by - lexor on May 22, 2002 7:00:46 PM]
_______ www.leXor.net
cybrgeek
cybrgeek
No, you can''t specify an unclickable area. Solid pixel maps work fast though, I use this for my collision detection in the game I''m working on now. It''s written in C++, but I''ll give you the basic idea. Every texture I have has a structure associated with it. That structure stores information about the texture pointer, the source rect, the rotation, etc. It also contains a pointer to an solid pixel mask. I don''t know if you are forcing one texture format to be used, I''m not, but it is a lot easier if you don''t have to code for every possible texture format. For this example I''ll use 32 bit textures in the A8R8G8B8 format. First you load the texture, then you lock the whole texture to get access to the bits. As long as all you are trying to find is the alpha value being zero you can just grab four bytes at a time from the locked bits, then use something like this to see if the pixel is transparent.

''Lock the texture, ReDim boolean array to lHeight by lWidth
''Copy one pixel at a time to a long variable
''Check the alpha value of the pixel
If (lPixel And &HFF000000) = 0 Then
''...The pixel is tranparent
Surface.pSolidPixelMap(lCountHeight, lCountWidth) = false
Else
''Solid pixel
Surface.pSolidPixelMap(lCountHeight, lCountWidth) = true
End If
''Unlock the texture


When mouse click over button


If Button.Surface.pSolidPixelMap(X, Y) = true Then
''Click the button.
...
End If


Hope this helps.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.