Programming - Games - setup - using DX9 and C#

This toturial was written for me by Imran Khan (imranahmedkhan82@hotmail.com, iak1982@yahoo.com).
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.cs and open its Code window nad write these lines.

using Microsoft.DirectX;
using 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 Device dddevice=null;
public Surface psurface;
public Surface ssurface;
public Clipper clip=null;

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( ) .

static void Main()
{
Form1 Gameform=new Form1();
Application.Exit();
}

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.which is also calling the constructor of base class.

public Form1(): base()
{
InitializeComponent();
this.Show(); // Showing the Form
dddevice=new Device(); // Creating Device Object
dddevice.SetCooperativeLevel(this,CooperativeLevelFlags.FullscreenExclusive);
Init();
Gameloop();
}

First two lines are autogenerated, then we Show the form1. After that we Create an object of Class Device. 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 method ' Init( ) ' that initialize our surfaces and other things.Then we call Gameloop( ) which is our main Game loop.

public void Init( )
{

psurface = null;
ssurface = null;
SurfaceDescription sdesc=new SurfaceDescription();
Clipper clip=new Clipper(dddevice);
clip.Window=this;
sdesc.SurfaceCaps.PrimarySurface =true;
sdesc.SurfaceCaps.Flip = true;
sdesc.SurfaceCaps.Complex = true;
sdesc.BackBufferCount = 1;
psurface = new Surface(sdesc, dddevice);
psurface.Clipper = clip;
sdesc.Clear();
sdesc.SurfaceCaps.BackBuffer = true;
ssurface = psurface.GetAttachedSurface(sdesc.SurfaceCaps);

}

In this method we initialize some objects like, Clip,Surface Decriptor,Primary Surface, Secondary Surface. These things must be initialize before using them, So this method 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 void Gameloop()
{
do
{
if(!this.Focused)
Application.DoEvents();
else
ShowSurface();
Application.DoEvents();
}while(this.Created);

ending();
}

This Loop will run as long as the form is created becuase of the while loop ' Do while( this.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 void ShowSurface()
{
ssurface.ColorFill(Color.White); //fill the backbuffer ( secondary Surface ) with a color
psurface.Flip(ssurface, FlipFlags.Wait); //flip the backbuffer to the front
}

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

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

public void ending()
{
dddevice.Dispose();
psurface.Dispose();
ssurface.Dispose();
}

We Dispose all the objects before exiting the program.

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

private void f_keydown(Object sender,System.Windows.Forms.KeyEventArgs e)
{

if(e.KeyCode==Keys.Escape)
{
ending();
Application.Exit();
}

}

To work this event properly u have to define event in "InitializeComponent()" method.write this line at the end of method.

this.KeyDown+=new System.Windows.Forms.KeyEventHandler(this.f_keydown);

So when ever a User Press key, " f_keydown" will be called."f_keydown" check whether the user press Escape key or not. If User press Escape, program will exit.

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

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

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