Programming - Games - setup - using DX9 and visual basic

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

Basics of DirectDraw:

As This tutorial is dedicated for 2D games and graphics application. So we have started with DirectDraw, Which we have mentioned early that it is use for 2D programming.

In this Section you will learn some basic things about Direct Draw, How to initialize Direct Draw, How to display full screen.

Start New Windows Application >> In Solution Explorer >> Right Click the Reference and select Add Reference

A window will apear , Select Directx and Direct Draw from the list.

You will see two new references in Solution Explorer.

Now, Double click the Form1.vb and open its Code window nad write these lines.

Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectDraw

After importing the Directx assemblies, we are able to use directdraw in our program.

Now Declare the following class variables so that they can be use in whole class.

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

First we define an Device Object for DirectDraw. Then there are 2 lines regarding Surfaces. There are two types of surface Primary surface & Secondary Surface. Primary surface is the Surface which is visible and secondary is the back buffer to which we draw then that drawing will copy to primary buffer so that user can see. The coping of surfaces are too fast that no one can notice it.Clipper , Applications use the methods of the Clipper object to manage clip lists. Here we define an Object of Clipper Class.It adds Disposing event ( which is use to deallocate the resources from the device ( here it is dddevice) when it is not is use).

After declaring some variables define a main then Now go to your project properties and set the startup object as Sub Main.

Public Shared Sub Main()
Dim GameForm As New form1( ) ' Making Instance of Form1
Application.Exit( ) ' Exiting the Application
End Sub

First we declare the Object of Form1. As the Object created its constructor will call. Where our main game program starts. If user exits from there it will come back to Main and execute the" Application.Exit " which will end your application.

Now move to constructor of Form1 Class.

Public Sub New()
MyBase.New() ' Calling the Base Clas constructor
'This call is required by the Windows Form Designer.

dddevice = Nothing
dddevice = New Device()
dddevice.SetCooperativeLevel(Me, CooperativeLevelFlags.FullscreenExclusive) 'create a new instance of our Form1 class and pass the form into it

End Sub

First two lines are autogenerated, then we Show the form1. After that we Create an object of Class Device by first initializing the variable to Nothing then creating object. Next we set the cooperation level. First argument tells the device what to render into what something is being drawn, we will passed in form in this argument. Second parameter actually sets the cooperation level, we are using FullScreenExclusive becuase our application will be full screen and we want to have an exclusive access to the hardware. You can use different options according to your needs.

Next is a call to a sub ' Init( ) ' that initialize our surfaces and other things.Then we call a sub Gameloop( ) which is our main Game loop.

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)

End Sub

In this sub we initialize some objects like, Clip,Surface Decriptor,Primary Surface, Secondary Surface. These things must be initialize before using them, So this Sub will be call only once in the program.We have describe about Primary surface and Secondary sruface. Every Sruface needs Description , it contains information about the sruface that how the surface behaves and what it does. In Next line we create an Instance of Clipper Class ( Clipper class is defined above ).

Now we describe the description of the surface , first line ' sdesc.SurfaceCaps.PrimarySurface = True ' tells to enable Primary surfaces. Next we enable Flip for best performance (flipping is better than drawing). Next live tells the device that the surface is a complex one, more than one surface is created. Next one is backbuffercount, we set it to 1 becuase we only have one backbuffer surface ( secondary Sruface). Now we initialize the surface using the surface description. First we pass in the SurfaceDescription class and then our device. Next we set the window for our clipper. Then we clear our surface desciption variable so we can use it to describe another surface. Then we set the backbuffer property to true as the second surface will be our backbuffer. The next line creates the backbuffer by calling the GetAttachedSurface method of our primary surface, which will create a backbuffer for our primary surface.

The concept of primary surface and backbuffer (secondary surface ) is already defined above.

Private Sub Gameloop( )
Do While Me.Created
If Not Me.Focused Then
Application.DoEvents( )
ShowSurface( )
Application.DoEvents( )
End If
ending( )
End Sub

This Loop will run as long as the form is created becuase of the while loop ' Do While Me.Created ' . Now about focus, here we check if the form has the focus, if the form doesnt have the focus (the app is minized for example) we wont do any drawing, , but if it is focused ( Maximize ) we will simply continue with our drawing routine. Which is ShowSurface( ) .

Public Sub ShowSurface()
ssurface.ColorFill(Color.White) 'fill the backbuffer ( secondary Surface ) with a color
psurface.Flip(ssurface, FlipFlags.Wait) 'flip the backbuffer to the front
End Sub

Its simple routine, First we fill the color in secondary surface ( backbuffer) then we Flip is to Primary Surface.

Now in the last ' Ending ( ) ' routine ( we call it in our GameLoop( ) ).

Public Sub ending()
End Sub

We Dispose all the objects before exiting the program.

Now a easy subroutine for keypress.So that when User Press ' Esc ' , program will exit.

Private Sub form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Escape Then
End If
End Sub

Now when you run the Program you will see the full screen with White color.

next: restoring

metadata block
see also:
Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover Introduction to 3D Game Programming with DirectX 9.0 - This is quite a small book but it has good concise information with subjects like, maths introduction and picking.

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to this site, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

cover Visual Basic .NET for Microsoft Platforms only.

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2015 Martin John Baker - All rights reserved - privacy policy.