This toturial was written for me by Imran Khan (firstname.lastname@example.org,
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.
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.
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.
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.
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.
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.
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( ) .
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( ) ).
We Dispose all the objects before exiting the program.
Now a easy subroutine for keypress.So that when User Press ' Esc ' , program will exit.
Now when you run the Program you will see the full screen with White color.
|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.
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.
This site may have errors. Don't use for critical systems.