Programming - Games - 2D - using DX9 and C#

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 ColorKey ck;

 

public void Init( )
{

psurface = null;
ssurface = null;
SurfaceDescription sdesc=new SurfaceDescription();
Clipper clip=new Clipper(dddevice);
clip.Window=this;
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 = 10066329;//RGB(153, 153, 153);
ck.ColorSpaceLowValue = 10066329; //RGB(153, 153, 153);
charsurface = new Surface("character.bmp", sdesc, dddevice);
charsurface.SetColorKey(ColorKeyFlags.SourceDraw, ck); //set the color key for the surface
}

All the things are same except these

ck.ColorSpaceHighValue = 10066329; //RGB(153, 153, 153)
ck.ColorSpaceLowValue = 10066329; //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.

To Convert RGB(153,153,153) into 1 integer , we have to follow the following mathematical method:

Red = 1, Green = 256 and Blue = 65536. So to translate an RGB value into integer you multiply each color value by the nueric value that corresponds to it.As we have taken here Gray then

153 * 1 (red) = 153 , 153 * 256(green) = 39168 , 153 * 65536 (blue) = 10027008,

Now add thes values:

153 + 39168 + 10027008 = 10066329

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 void ShowSurface()
{

try
{
ssurface.ColorFill(Color.Red);
ssurface.DrawFast(x4act, 300, charsurface, frames[frm4act], DrawFastFlags.DoNotWait);
psurface.Flip(ssurface, FlipFlags.Wait);
}
catch
{
psurface.Dispose();
Init();
}

}

New Version :

public void ShowSurface()
{

try
{
ssurface.ColorFill(Color.Red);
ssurface.DrawFast(x4act, 300, charsurface, frames[frm4act], DrawFastFlags.SourceColorKey);
psurface.Flip(ssurface, FlipFlags.Wait);
}
catch
{
psurface.Dispose();
Init();
}

}

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 Gray 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-2022 Martin John Baker - All rights reserved - privacy policy.