Issue dated - 06th September 2004

-


Previous Issues

CURRENT ISSUE
INDIA NEWS
OPINION
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

The C# Column

Of Gold Coins

In this article we will create an application that displays three coins at different positions on the screen in such a manner that we get an effect of rotation. To create the application, select ‘DirectX 9 Visual C# Wizard’ from the Templates list. Then select ‘DirectDraw’ application type.

Look at the following bitmap. It contains 60 images each denoting the different position of the coin in rotation. The three images that we are going to display are marked with colored rectangles.

We start with initialising the window options and setting the display mode. For this, add the following code in the constructor of the GraphicsClass class before the call to CreateSurfaces( ) method.

localDevice.SetCooperativeLevel ( owner,
CooperativeLevelFlags.FullscreenExclusive |
CooperativeLevelFlags.AllowReboot |
CooperativeLevelFlags.NoWindowChanges ) ;
localDevice.SetDisplayMode ( 800, 600, 24, 0, false ) ;
InitializeDirectDraw( ) ;

Using the SetCooperativeLevel( ) method we have acquired the full screen exclusive mode of the top level window. We have then set the mode of the display device hardware by calling the SetDisplayMode( ) method. We have next called the InitializeDirectDraw( ) method to create the primary and secondary surfaces. The method is given below.

private void InitializeDirectDraw( )
{
SurfaceDescription desc =
new SurfaceDescription( ) ;
desc.SurfaceCaps.PrimarySurface = true ;
desc.SurfaceCaps.Complex = true ;
desc.SurfaceCaps.Flip = true ;
desc.BackBufferCount = 1 ;
surfacePrimary = new Surface (desc, localDevice) ;
SurfaceCaps caps = new SurfaceCaps( ) ;
caps.BackBuffer = true ;
surfaceSecondary =
surfacePrimary.GetAttachedSurface ( caps ) ;
}

The wizard adds the references surfacePrimary and surfaceSecondary to the GraphicsClass class. Here we have specified the description of the primary surface and created the same. Once the primary surface is created, the secondary surface is obtained from it.

In the CreateSurfaces( ) method we have created the offscreen surface as shown below.

private void CreateSurfaces( )
{
SurfaceDescription desc =
new SurfaceDescription( ) ;
desc.SurfaceCaps.OffScreenPlain = true ;
coinSurface = new Surface (
“goldcoin.bmp”, desc, localDevice ) ;
PixelFormat pixformat =
surfacePrimary.PixelFormat ;
ColorKey ck = new ColorKey( ) ;
ck.ColorSpaceHighValue = pixformat.RBitMask ;
ck.ColorSpaceLowValue = pixformat.RBitMask ;
coinSurface.SetColorKey (
ColorKeyFlags.SourceDraw, ck ) ;
}

Here, we have loaded the ‘goldcoin.bmp’ file in the offscreen surface referenced by coinSurface.

Add the following variables as data members of the GraphicsClass class.

private int[ ] frames = { 0, 20, 40 } ;
private Surface coinSurface = null ;

The RenderGraphics( ) method displays the bitmap file on primary surface. Add the following code in the try block given in RenderGraphics( ) method.

surfaceSecondary.ColorFill (
Color.Black ) ;
DrawCoin (
frames [ 0 ], 378, 106 ) ;
DrawCoin (
frames [ 1 ], 68, 406 ) ;
DrawCoin (
frames [ 2 ], 568, 406 ) ;
for ( int i = 0 ; i < 3 ; i++ )
{
frames [ i ] += 1 ;
if ( frames [ i ] == 60 )
frames [ i ] = 0 ;
}
surfacePrimary.Flip (
null, FlipFlags.Wait ) ;

To draw the images on surface, we have called the DrawCoin( ) method. The image is drawn from three different offsets at three different positions on the secondary buffer. For this, we have created the array frames containing indices of images. The values 0, 20 and 40 represent the offset of the bitmap. The images at these positions get drawn on the surface. Every time the RenderGraphics( ) method is called the offset is incremented by 1.

To the DrawCoin( ) method we have passed the offset of image to be drawn and position where it should get displayed.

The DrawCoin( ) method is given below.

public void DrawCoin (
int n , int x, int y )
{
Rectangle r =
new Rectangle( ) ;
r.X = ( n % 10 ) * 150 ;
r.Y = ( int ) Math.Floor (
n / 10.0 ) * 150 ;
r.Width = 150 ;
r.Height = 150 ;
Rectangle pos = new
Rectangle (0, 0, 150, 150) ;
pos.Offset ( new Point (
x, y ) ) ;
surfaceSecondary.Draw (
pos, coinSurface, r, DrawFlags.KeySource |
DrawFlags.Wait ) ;
}

In r we have calculated the position of next source image to be drawn, while pos contains the destination position. Ultimately, the Draw( ) method draws the images on screen.

When you run the application you will see golden coins rotating on the screen as given below.

 

 

 

 

 

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.