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

This toturial was written for me by Imran Khan (,
Copyright (c) 2004 is owned by Martin Baker

Surface With Images:

We have learnt how to make a surface and how to display it. In this Section we will learn how to make surface with images.There can be more then 1 surfaces, Like 1 surface show the background, 1 show the character, 1 show the enemy and so on.Every Surface contains one image ( depends upon your requirements ).

All the codes are continuation of the previous code. Now we add something to 2 methods, ' Init( ) ' and 'Show Surface( ) '.In Init we will initiate new surface and in showSurface we will display that Surface.

First of all we will declare a instance for new surface

public Device dddevice=null;
public Surface psurface; //Primary Surface ( user visible)
public Surface ssurface; // Secondary surface ( Back buffer)
public Clipper clip=null;
public Surface charsurface; // Image surface

We just added one line ' public Surface charsurface ' which is the surface on which we draw our picture.

Now on Init( ) method.

public void Init( )

psurface = null;
ssurface = null;
SurfaceDescription sdesc=new SurfaceDescription();
Clipper clip=new Clipper(dddevice);
sdesc.SurfaceCaps.PrimarySurface =true;
sdesc.SurfaceCaps.Flip = true;
sdesc.SurfaceCaps.Complex = true;
sdesc.BackBufferCount = 1;
psurface = new Surface(sdesc, dddevice);
psurface.Clipper = clip;
sdesc.SurfaceCaps.BackBuffer = true;
ssurface = psurface.GetAttachedSurface(sdesc.SurfaceCaps);
sdesc.SurfaceCaps.OffScreenPlain = true;
charsurface = new Surface("gametitle.bmp", sdesc, dddevice);

All the method is same except the last 3lines. First we clear our surface desciption variable by calling ' sdesc.clear( ) ' , so we can use it to describe another surface then we set the surface as simple surface , not a backbuffer. In the next line we create new instance of the Surface class, First Argument take the path of the Bitmap(you can change the path, depends where you bitmap is placed ),2nd argument is the Surface Description and the last argument is the Device object which we are using.

Add more 3 lines if you want more surfaces with pictures.

Now we add some lines to ShowSurface( ) method to show our new surface.

public void ShowSurface()

ssurface.DrawFast(150, 150, charsurface, DrawFastFlags.DoNotWait);
ssurface.ForeColor = Color.Black;
ssurface.DrawText(540, 565, "Made By Imran Ahmed Khan", false);
psurface.Flip(ssurface, FlipFlags.Wait);


You know the First 2 lines, 3rd line shows how to draw Surface on Backbuffer (Secondary Buffer), First 2 arugments are x & y cordinated (where you want to show your picture on screen), 3rd argument is the surface object,4th argument is the Flag. If you specify DoNotWait then the surface will we drawn faster, the device will not wait until the surface is not being used by anything else. Another flag is ' Wait ' , which is opposite to 'DoNotWait' flag. You can also use method 'Draw' to draw Bitmap Surface but ' DrawFast ' draw faster then the ' Draw ' method because 'Draw' performs BitBlockTransfer for drawing.

You can use other methods to draw circle ,rectangle etc by using there methods.( which is easy )

The next 2 lines are for drawing Text on the Screen. First we set the color of the surface so that the text will be in Black. Next line shows the method to DrawText on screen. First 2 arguments of this method are x and y cordinated ( where you want to show your text on screen ) and the last argument indicates whether to draw the text at the last position that wasnt used yet, if you set this to True there is absolutely no point in setting the x and y coordinates.

Rest of the program is same as previous.

Now when you run this program you will the image in the middle and a text written on right lower corner.


next: animation

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.