|
DirectInput - The Walking Bear II
(Continued from last week
)
Last
week we had created the surfaces and initialised the structure elements.
Let's move ahead.
Given below is the CreateSurfaces( ) method. The two files containing clouds
bitmaps have names 'clouds1.bmp' and 'clouds2.bmp'. The files containing foreground
trees have names 'treefront1.bmp' and 'treefront2.bmp. The files containing
background trees have names 'treeback1.bmp' and 'treeback2.bmp. The twelve files
containing Bear bitmaps have names 'BW1L.bmp', 'BW2L.bmp' and so on. We have
passed the name of the file to the constructor of the Surface class and created
surfaces for each of these bitmaps. As the background colour of all the bitmaps
is red, we have masked the red colour by specifying RBitMask value.
private void CreateSurfaces( )
{
desc.Clear( ) ;
desc.SurfaceCaps.OffScreenPlain = true ;
PixelFormat pixformat =
surfacePrimary.PixelFormat ;
ColorKey ck = new ColorKey( ) ;
ck.ColorSpaceHighValue = pixformat.RBitMask ;
ck.ColorSpaceLowValue = pixformat.RBitMask ;
String fileName ;
for ( int i = 0 ; i < 2; i++ )
{
fileName=String.Format(
"images\\treeback{0}.bmp", + 1 ) ;
treeback[ i ] = new Surface ( fileName, desc,
localDevice ) ;
treeback[ i ].SetColorKey (
ColorKeyFlags.SourceDraw, ck ) ;
fileName = String.Format (
"images\\treefront{0}.bmp", i + 1);
treefront[ i ] = new Surface ( fileName, desc,
localDevice ) ;
treefront[ i ].SetColorKey (
ColorKeyFlags.SourceDraw, ck ) ;
fileName = String.Format (
"images\\clouds{0}.bmp", i + 1 ) ;
cloud[ i ] = new Surface ( fileName, desc,
localDevice ) ;
cloud[ i ].SetColorKey (
ColorKeyFlags.SourceDraw, ck ) ;
}
for ( int i = 0 ; i < 12 ; i++ )
{
fileName = String.Format (
"images\\BW{0}L.bmp", i + 1 ) ;
bear[ i ] = new Surface ( fileName,
desc, localDevice ) ;
bear[ i ].SetColorKey (
ColorKeyFlags.SourceDraw, ck ) ;
}
bearRect = new Rectangle ( 0, 0, 135, 85 ) ;
}
In the RenderGraphics( ) method actual drawing is done. In the try block firstly,
we have filled the surface with a shade of blue colour. Next, we have created
a bounding rectangle and drawn the clouds, treeback, bear and treefront images.
Then flipped the surfaces to show the images on screen.
In the catch block will be executed when surfaces are lost due to switch over
to other window, etc. In this case, we have called the CreateSurfaces( ) method
again to create the surfaces with our images. The RenderGraphics( ) method is
given below.
public void RenderGraphics ( Point destination )
{
if ( !owner.Created )return ;
if (surfacePrimary == null ||
surfaceSecondary == null )
return ;
try
{
surfaceSecondary.ColorFill (
Color.FromArgb (0, 0, 152, 245 ) ) ;
Rectangle rr = Rectangle.FromLTRB (
imgPos.c + imgPos.x, 0, I
mgPos.c + 800 + imgPos.x, 600 ) ;
surfaceSecondary.Draw ( rr, cloud[ 0 ],
DrawFlags.KeySource |
DrawFlags.DoNotWait ) ;
rr=Rectangle.FromLTRB (imgPos.c1 +
imgPos.x,0, imgPos.c1 +
800 + imgPos.x, 600 ) ;
surfaceSecondary.Draw ( rr, cloud[ 1 ],
DrawFlags.KeySource |
DrawFlags.DoNotWait ) ;
surfaceSecondary.Draw
(
Rectangle.FromLTRB( imgPos.b +
imgPos.y, 0, imgPos.b + 1200 + imgPos.y,
600 ), treeback[0], DrawFlags.KeySource
| DrawFlags.DoNotWait ) ;
surfaceSecondary.Draw( Rectangle.FromLTRB(
imgPos.b1+ imgPos.y, 0, imgPos.b1 +1200
+ imgPos.y, 600 ), treeback[1],
DrawFlags.KeySource |
DrawFlags.DoNotWait) ;
Rectangle pos = Rectangle.FromLTRB (
400, 521 - bearRect.Height, 400 + bearRect.Width, 521) ;
surfaceSecondary.Draw ( pos, bear [
imgPos.bearImgNo ], DrawFlags.KeySource
| DrawFlags.DoNotWait ) ;
surfaceSecondary.Draw ( Rectangle.FromLTRB
( imgPos.f + imgPos.z, 0, imgPos.f + 1200 +
imgPos.z, 600 ), treefront[0],
DrawFlags.KeySource |
DrawFlags.DoNotWait ) ;
surfaceSecondary.Draw(
Rectangle.FromLTRB(
imgPos.f1 + imgPos.z, 0, imgPos.f1 + 1200
+ imgPos.z,600), treefront[1],
DrawFlags.KeySource |DrawFlags.DoNotWait ) ;
surfacePrimary.Flip ( null, FlipFlags.Wait ) ;
}catch ( SurfaceLostException )
{
if ( localDevice.TestCooperativeLevel( )
== true )
{
localDevice.RestoreAllSurfaces( ) ;
CreateSurfaces( ) ;
}
}
}
Once the screen is displayed, user would press the left arrow key to move the
bear to the left and tree and cloud images to the right. For this, we must capture
the keyboard events. This is done in the GetInputState( ) method of the InputClass
class.
To be continued
 |
Yashavant Kanetkar, one of
the first Express Computer columnists, is an established software
expert, speaker and author with several best-sellers to his
credit, including titles like “Let Us C” and the “Fundas” series.
Contact him at kanetkar@dcubesoft.com |
|