A great Code Editor for Windows
If you haven't used EditPadPro yet, I strongly suggest checking it out. I've been using this editor for years and it is truely the best piece of software I've ever used. It even allows you to link your project to your FTP server and upload files as you are editing them automatically. Just save the file in the editor and it will be uploaded to your server -- no waiting for the file to be uploaded.

I usually never pay for software I find online, but I actually went ahead and upgraded to the full version that unlocks a bunch of features, well worth the $49.95
You can courteously use the Free Evaluation Demo for as long as you want.

Download Now for FREE and
Make your coding life easier


A fully functional demo (only some features are missing)
DirectDraw - 2 - Creating a DirectDraw app Source code
JUMP TO - dd1 - dd2 - dd3 - dd4 - dd5 - dd6 - dd7

Direct Draw - Creating a Direct Draw Application

I'll take the window base code from my second win tutorial and will keep adding the Direct Draw functionality to it. In this tutorial I will go over the initialization of DirectDraw interface 7 and changing the screen resolution. Let's include the DirectDraw(ddraw.h) header and some variables into our program first.


#include <ddraw.h>

LPDIRECTDRAW	lpdd = NULL;
LPDIRECTDRAW7	lpdd7 = NULL;

int swidth = 640;
int sheight = 480;
int sbpp = 16;



lpdd and lpdd7 are two DirectDraw objects. these are the objects responsible for managing direct draw accelerator capabilities. it's interesting to note that in reality there can only be one DirectDraw object. lpdd is only used once in the initialization code to request the DirectDraw7 interface and after that it's released. the main DirectDraw object however is only released in the end of the program inside the DirectDrawRelease function which can be looked up in the source code. swidth and sheight are the display width and height, and sbpp is the color depth(bits per pixel). The DirectDraw initialization takes place after the window is created in WinMain( ). I talked about this in a previous win tutorial, when creating a window with CreateWindowEx you will need to set the window style flag to WS_POPUP for full-screen applications. Here is the DirectDraw init code.

// Initialize DirectDraw

long rval;

rval = DirectDrawCreate(NULL, &lpdd, NULL);
if(rval != DD_OK)
{
	MessageBox(hwnd, "Failed to create the DirectDraw object", "Error", MB_OK);
	quit = true;
}

rval = lpdd->QueryInterface(IID_IDirectDraw7, (LPVOID*)&lpdd7);
if(rval != DD_OK)
{
	MessageBox(hwnd, "Failed to query interface for DirectDraw7", "Error", MB_OK);
	quit = true;
}

if(lpdd)
{
	lpdd->Release();
	lpdd = NULL;
}
	
rval = lpdd7->SetCooperativeLevel(hwnd,
                                  DDSCL_FULLSCREEN|
                                  DDSCL_EXCLUSIVE|
                                  DDSCL_ALLOWREBOOT);
if(rval != DD_OK)
{
	MessageBox(hwnd, "Failed to set DirectDraw cooperative level", "Error", MB_OK);
	quit = true;
}

rval = lpdd7->SetDisplayMode(swidth, sheight, sbpp, 0, 0);
if(rval != DD_OK)
{
	MessageBox(hwnd, "Failed to set display mode", "Error", MB_OK);
	quit = true;
}

I'll go over the init functions. First you need to create the DirectDraw7 object. One of the ways this could be done is by creating a DirectDraw object(lpdd) and then requesting the DirectDraw7 interface by querying it from lpdd. In the code these are the two functions that do just that.

  • DirectDrawCreate(NULL, &lpdd, NULL);
  • lpdd->QueryInterface(IID_IDirectDraw7, (LPVOID*)&lpdd7);

    You have to pass the DirectDraw object to DirectDrawCreate. Then pass IID_IDirectDraw7(the reference id of the version you're requesting) and a pointer to a DirectDraw object that will hold the main DirectDraw7 interface to lpdd->QueryInterface. lpdd is only used for querying and after querying is done you have to release it.

    The next step is setting the cooperative level. You call this method to determine the behavior of the application by passing flags to it. Usually for a full-screen DirectDraw application you will need to OR together 3 flags.

  • DDSCL_FULLSCREEN - Indicates that the exclusive-mode owner will be responsible for the entire primary surface(more about surfaces in the following tutorial). This flag must be used with DDSCL_EXCLUSIVE (driver independent)
  • DDSCL_EXCLUSIVE - Requests the exclusive level(full-screen, exclusive mode). This flag must be used with the DDSCL_FULLSCREEN flag.
  • DDSCL_ALLOWREBOOT - Allows ctrl+alt+del to function while in exclusive (full-screen) mode.

    And finally you change the display resolution by calling

  • lpdd7->SetDisplayMode(swidth, sheight, sbpp, 0, 0);

    Pass the width, height and the bit depth information to it. The last two parameters are not used and should be set to 0. Last thing to note is that DirectDraw automatically resets the resolution back to what it was, after you quit the program. In the source code I also added a quick Escape key test in the message loop so you don't have to alt-F4 out of it and a function called DirectDrawRelease that releases the DirectDraw object we've created. By the way, this has nothing to do with direct draw drivers.


  • DirectDraw - 2 - Creating a DirectDraw app Source code
    JUMP TO - dd1 - dd2 - dd3 - dd4 - dd5 - dd6 - dd7



    Fallout Software Home Page