The basic things for your first Direct Draw application

In this tutorial you're going to make your own Direct Draw application. I won't explain everything in detail, to make it understandable and, to be honest, because I don't know everything. I think you need to know the basics of programming in Visual Basic to understand this tutorial.

You can download the massive Direct X 7 SDK (Software Development Kit) of 128 mb. I've downloaded it (I've got a cable modem) and it's useful but also very complex. 

1: Create a new application. Select in the Project- References screen the DirectX 7 for Visual Basic Type Library. If you don't see this library, you need to download DirectX 7 from Microsofts DirectX page. 

2: Declare the Direct X object:

Dim DX As New DirectX7

3: Declare the DirectDraw object. I've named it DD.

Dim DD As DirectDraw7

4: Declare the Surface Descriptions. The surface description is the description of a surface (Duh!). For this tutorial we only need 2 Direct Draw surface descriptions. I've named them DDSD1 and DDSD2.

Dim DDSD1 As DDSURFACEDESC2
Dim DDSD2 As DDSURFACEDESC2

5: Now declare the surfaces. We need 3 surfaces for this tutorial: a backbuffer, primarysf and a surface that contains the background image. I explain the purpose of the backbuffer and primarysf later.

Dim BackBufferSf As DirectDrawSurface7
Dim PrimarySf As DirectDrawSurface7
Dim BackGroundSf As DirectDrawSurface7

6: The last thing to declare is the rectangle of the backgroundsf that we use to blit. The RECT is a type with four integers: The top, left, bottom and right coordinate of the area that we blit of the backgroundsf.

Dim BackGroundR As RECT

7: Now comes the initializing of the Direct Draw objects. In the Form_Load event type the following code that will set the DD object:

Set DD = DX.DirectDrawCreate("")

8: The next thing to do is to set the Cooperative level of the DD object. We want to run the application in fullscreen and we want to allow the user to reboot the sytem with Ctrl Alt Delete in case the application crashes. If you want to run the program in fullscreen, you'll also need to set the exclusive flag.

DD.SetCooperativeLevel Me.hWnd, DDSCL_EXCLUSIVE Or DDSCL_FULLSCREEN Or DDSCL_ALLOWREBOOT

9: Now we have to set the display mode. We want the application to run in a resolution of 640 by 480 in 16 bit color, so we set the displaymode to: (just set the last two parameters to 0 and DDSDM_DEFAULT and don't ask me why, it always works)

DD.SetDisplayMode 640, 480, 16, 0, DDSDM_DEFAULT

10: The DD object is initialized, now comes the initialisation of the primarysf. We put the description of the primarysf into DDSD1 (DirectDrawSurfaceDescription). You don't have to understand exactly how this works, but just remind that the primary surface has one backbuffer and can be flipped.

DDSD1.lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
DDSD1.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX
DDSD1.lBackBufferCount = 1
Set PrimarySf = DD.CreateSurface(DDSD1)

11: The next thing to do is to initialize the backbuffer. Just like the primarysf, you don't have to know exactly how it works and why it is done this way.

Dim Caps As DDSCAPS2
Caps.lCaps = DDSCAPS_BACKBUFFER
Set BackBufferSf = PrimarySf.GetAttachedSurface(Caps)

12: The last thing to initialize is the backgroundsf. This is the only surface that has a image. First we load the background image into the backgroundsf. Then we set the backgroundrectangle that describes which part of the backgroundimage will be blitted. Because we want to blit the entire backgroundsf, the top coordinate is 0, the left coordinate also 0, the bottom coordinate the height of the background, in this case also the screenheight, 480 and the right is 640. We don't have to set the top and left coordinate, because they are 0.

Set BackGroundSf = DD.CreateSurfaceFromFile(App.Path & "\ddtutbackground.bmp", DDSD2)

BackGroundR.Right = 640
BackGroundR.Bottom = 480

13: Now comes the blitting!! We make a sub named BlitIt. The blitting works like this: First we blit all the surfaces that we want to show to the backbuffer. We only have one surface to show, the backgroundsf. The user doesn't see this. Then we flip the primary surface so the user sees the backbuffer. This doesn't look very logical, but it is very efficient and it gives the possibility for nice animations.

Sub BlitIt()

Call BackBufferSf.BltFast(0, 0, BackGroundSf, BackGroundR, DDBLTFAST_WAIT)

PrimarySf.Flip Nothing, DDFLIP_WAIT

End Sub

14: In case you haven't noticed, the sub Blitit isn't called anywhere so the blitting doesn't happen. So paste the following code into the Form_Load event, below the initialisations:

Call BlitIt

15: Almost done! Last but surely not least make sure the user can end the application because there is no close button in a fullscreen Direct Draw application. With this code, if the user presses a button, the application will unload.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Unload Me

End Sub

16: Paste this code into the Form_Unload event. Never forget to restore the displaymode and make sure you set the cooperativelevel to normal!!

Call DD.RestoreDisplayMode
Call DD.SetCooperativeLevel(Me.hWnd, DDSCL_NORMAL)
End

That was it! The tutorial isn't as short as I said it would be after all, but I hope you understand how you can use Direct Draw. You can download this tutorial HERE.

If you have any comments or whatever, feel free to e-mail me by clicking HERE.

<-- Back to tutorials