Programming - Games - 2D - using DX9 and visual basic

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

Private dddevice As Device = Nothing
Private psurface As Surface ' Primary Surface ( user visible)
Private ssurface As Surface ' Secondary surface ( Back buffer)
Dim charsurface As Surface ' Image surface
Private clip As Clipper = Nothing

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

Now on Init( ) method.

Public Sub Init( )

psurface = Nothing
ssurface = Nothing
Dim sdesc As New SurfaceDescription()
clip = New Clipper(dddevice)
clip.Window = Me
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)

End Sub

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 Sub 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)
Catch ex As SurfaceLostException
End Try
End Sub

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 coz ' 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.