the black hole 
it all ends here. 
bhC login
name:
pass:
 
home
 
 the black hole's front page. all the latest vb rpg development and vbgaming news, as well as this week's feature rpg project!
articles
 
rpg theory (7)
directx in vb (3)
vb rpg programming (4)
miscellaneous (4)
files
 
rpg / tile engine source (23)
full vb games w/ source (5)
'helper' libraries and utilities (13)
effect samples (5)
miscellaneous (10)
rpg projects
 
 vb rpg projects under development by members of the bhC. show them your support, and check out their games!
message board
 
 a forum to interact with your fellow vb rpg programmers. you do *not* have to be a bhC member to post!
links
 
vb rpg programming (5)
vb gaming (14)
general game programming (6)
vb sites (6)
miscellaneous (2)
general rpg programming (2)
contact
 
 you want to contact the guy behind the black hole? here's where to do it!


 
 


<< Back to Category List

Colorkeys: Setting to the color of a specific pixel - page 1


pages: prev | 1 | next
Colorkeys: Setting to a Specific Pixel
By: Brian Clark


This tutorial is to help with setting the color key of your directdraw7 surface to the color of a specific pixel. For this tutorial we will use the pixel at 0,0. The purpose of a color key is to making a certain color invisible when the surface is blited(shown).
We will imagine a picture of a tree for learning, now in a game we wouldn't want a big red box around our tree would we?

I am asuming that you already know how to load your picture into a surface, but just incase I will show a simple loading rountine.
Function LoadSpriteIntoDXS(DXObject As DirectDraw7, ByVal BMPFile As String) As DirectDrawSurface7
Dim TempDXS As DirectDrawSurface7 'Temporary surface, to render the bitmap.
Dim TempDXD As DDSURFACEDESC2
Dim TempRect as RECT 'Temorary RECT to lock the surface with.

' Fill in DX surface description
With TempDXD
	.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
	.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
End With

' Loads the bitmap onto the DX surface
Set TempDXS = DXObject.CreateSurfaceFromFile(BMPFile, TempDXD)     'FromFile(BMPFile, TempDXD)

'Colorkey Code Goes Here

' Returns created DX surface
Set LoadSpriteIntoDXS = TempDXS

' Cleanup
Set TempDXS = Nothing
End Function
From here on we will just add to this function the ability to setup colorkeys. There are only a few steps needed to setup your colorkeys.

Load the Bitmap onto the surface
Lock the Surface
Retrieve the Color Value
Unlock the Surface
Set the Colorkey

We have already done the first step, so we will now work on the rest. To lock the surface we just need to add the folowing code.
TempDXS.Lock EmptyRect, TempDXD, DDLOCK_WAIT, 0
Ok, just in case your unfamilar with this function here is some explaintion of what were doing.

TempRECT is just an empty Rect, if you pass an empty RECT then DX locks the whole surface.
TempDXD is a the surface description we used to create the surface with.
The DDLOCK_WAIT flag specifies to wait for the last surface opperation to finnish.

Next we retrive the actual color value of the pixel at 0,0. Once the surface is locked this is very simple to do. Once again just add these lines.
Dim TempColor as Long 'Temporary storage for the color value.
TempColor = TempDXS.GetLockedPixel(0, 0)
This is pretty self explainatory, the GetLockedPixel function just returns the long value of the color at pixel 0,0. Remember that DirectX numbers it pixel from 0 to Width - 1, so if your image is 300 pixels wide then the first one is at 0, and the last is at 299. It also does this for height. Next we will Unlock our surface, this is very easy so I will not bother to explain it, but here is the code.
TempDXS.Unlock TempRECT

This is the part we have been waiting for!!! Time to actually setup the colorkey, this is not very hard, but can be confusing for a beginner.
Dim TempDDCK As DDCOLORKEY
TempDDCK.low = TempColor
TempDDCK.high = TempColor

TempDXS.SetColorKey DDCKEY_SRCBLT, TempDDCK

As you can see we set both the low & high values to the same color, if you want it is posible to make a whole range of colors transparent. Then all you have to do is assign the color key to our surface. The most important thing is using DDCKEY_SRCBLT this specifies that we want to use this as the source colorkey for blitting. You can also setup destination colorkeys, but I will not get into that. Now I will show you the new function with colorkey support added.
Function LoadSpriteIntoDXS(DXObject As DirectDraw7, ByVal BMPFile As String) As DirectDrawSurface7
Dim TempDXS As DirectDrawSurface7 'Temporary surface, to render the bitmap.
Dim TempDXD As DDSURFACEDESC2
Dim TempRect as RECT 'Temorary RECT to lock the surface with.

' Fill in DX surface description
With TempDXD
	.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
	.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
End With

' Loads the bitmap onto the DX surface
Set TempDXS = DXObject.CreateSurfaceFromFile(BMPFile, TempDXD)     'FromFile(BMPFile, TempDXD)

Dim TempDDCK As DDCOLORKEY
Dim TempColor as Long 'Temporary storage for the color value.

TempDXS.Lock EmptyRect, TempDXD, DDLOCK_WAIT, 0
TempColor = TempDXS.GetLockedPixel(0, 0)
TempDXS.Unlock TempRECT

TempDDCK.low = TempColor
TempDDCK.high = TempColor
TempDXS.SetColorKey DDCKEY_SRCBLT, TempDDCK

' Returns created DX surface
Set LoadSpriteIntoDXS = TempDXS

' Cleanup
Set TempDXS = Nothing
End Function
Ok, now we will put this new function to use. Here is just a small snipet of code to show you how to use it.
Set ddsTree = LoadSpriteIntoDXS(DD, App.Path & "\Tree.bmp")
ddsBack.BltFast 0, 0, ddsTree, rectTree, DDBLTFAST_SRCCOLORKEY Or DDBLTFAST_WAIT
Remember to use the DDBLTFAST_SRCCOLORKEY flag when blitting or else it will not work. Ok that is all for this tutorial, I hope that you learned how to use ColorKeys in your own projects.

If you have any questions or comment PLEASE fell free to email me at Troll1184@cs.com


pages: prev | 1 | next