Issue dated - 04th October 2004

-


Previous Issues

CURRENT ISSUE
INDIA NEWS
REVIEW / GLOBAL
COLUMNS
TECH FORUM

THE C# COLUMN

BETWEEN THE BYTES
TECHNOLOGY
SPECIALS <NEW>
Symantec Report
Security Headquarters
JobsDB
MINDPRINTS
HMA BANKBIZ
EC SERVICES
ARCHIVES/SEARCH
IT APPOINTMENTS
Openings At Jobstreet.com
WRITE TO US
SUBSCRIBE/RENEW
CUSTOMER SERVICE
ADVERTISE
ABOUT US

 Network Sites
  IT People
  Network Magazine
  Business Traveller
  Exp. Hotelier & Caterer
  Exp. Travel & Tourism
  Exp. Pharma Pulse
  Exp. Healthcare Mgmt.
  Express Textile
 Group Sites
  ExpressIndia
  Indian Express
  Financial Express

 
Front Page > TechSpace > Story Print this Page|  Email this page

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

 

<Back to top>


© Copyright 2003: Indian Express Group (Mumbai, India). All rights reserved throughout the world. This entire site is compiled in
Mumbai by The Business Publications Division of the Indian Express Group of Newspapers.
Please contact our Webmaster for any queries on this site.