Programming - Games - 2D - using DX9 and visual basic

This toturial was written for me by Imran Khan (imranahmedkhan82@hotmail.com, iak1982@yahoo.com).
Copyright (c) 2004 is owned by Martin Baker

Applying Transparency:

When you run the previous version of the program, all things are fine except the Grey color around the character. This Grey Color is the image background color.if the picture got pink color as background it will show the pink color. We want to elimate this color.So that it will look more better.

First Declare an instance of ColorKey class.

Private ck As ColorKey

 

Public Sub Init( )

psurface = Nothing
ssurface = Nothing
Dim sdesc As New SurfaceDescription()
clip = New Clipper(dddevice)
clip.Window = targetform
sdesc.SurfaceCaps.PrimarySurface = True
sdesc.SurfaceCaps.Flip = True
sdesc.SurfaceCaps.Complex = True
sdesc.BackBufferCount = 1
psurface = New Surface(sdesc, dddevice)
psurface.Clipper = clip
sdesc.Clear()
sdesc.SurfaceCaps.BackBuffer = True
ssurface = psurface.GetAttachedSurface(sdesc.SurfaceCaps)
sdesc.Clear()
sdesc.SurfaceCaps.OffScreenPlain = True
ck.ColorSpaceHighValue = RGB(153, 153, 153)
ck.ColorSpaceLowValue = RGB(153, 153, 153)
charsurface = New Surface("character.bmp", sdesc, dddevice) 'initialize new surface
charsurface.SetColorKey(ColorKeyFlags.SourceDraw, ck) 'set the color key for the surface

End Sub

All the things are same except these

ck.ColorSpaceHighValue = RGB(153, 153, 153)
ck.ColorSpaceLowValue = RGB(153, 153, 153)

We specify the Range of the color to make transparent, Here we want to make Grey to be transparent so we give the same values in both arguments. (153, 153, 153) is the RGB combination of Grey.One can make more then 1 color Transparent.

Then we delcare a character surface object ( we done this before )

The last line sets the ColorKey that we created eariler. The first argument specifies that we will use the source which is our surface for transparency and then the second argument is the ColorKey structure we made eariler.

Now replace a single line in ShowSurface( ) method

Older Version:

Public Sub ShowSurface()
Try
ssurface.ColorFill(Color.Red)

ssurface.DrawFast(x4act, 100, charsurface, frames(frm4act), DrawFastFlags.DoNotWait)

Catch ex As SurfaceLostException
Init()
End Try
End Sub

New Version :

Public Sub ShowSurface()
Try
ssurface.ColorFill(Color.Red)

ssurface.DrawFast(x4act, 100, charsurface, frames(frm4act), DrawFastFlags.SourceColorKey)

Catch ex As SurfaceLostException
Init()
End Try

psurface.Flip(ssurface, FlipFlags.Wait)
End Sub

We must specify that we will use a color key in the DrawFastFlags, so we change the flags to SourceColorKey which will cause the color we set in the ColorKey to be drawn transparent.

Now when you run the Program, You will see no White color Around character.

next: background


metadata block
see also:
Correspondence about this page

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.