Topic : Setting up DirectDraw 7
Author : Matthew Allen
Page : 1

Setting up DirectDraw 7
by Matthew Allen
MXF Entertainment (http://www.cfxweb.net/mxf/)
updated: 2/12/00


Notes:

In any program that you use the below code (or any DirectDraw code), be sure to use the header ddraw.h and the libraries dxguid.lib and ddraw.lib (in that order). To get these, get the DirectX SDK.
You must do the Windows setup before the DirectDraw setup because setting the display mode requires passing a handle to your window (a HWND) to SetCooperativeLevel( ). Here is a tutorial on how to do that. I would suggest that you copy and paste the below code into the Windows tutorial code right after the part that you create, show, and update the window.
FAILED( ) is a function defined in ddraw.h that checks the return values of the DirectX funtions to see if they failed. It returns zero for a SUCCESS and non-zero for a FAILURE. For example, in:
FAILED( DirectDrawCreate( NULL, &lpDDTemp, NULL ) )
if DirectDrawCreate( ) returns DD_OK (a value that indicates a success), then FAILED( ) will return non-zero.

// VARIABLES

// The DirectDraw object, used to create the Primary surface and
// access the DirectDraw functions.
LPDIRECTDRAW7 lpDD = NULL;

// The primary DDraw surface, this represents the screen.
LPDIRECTDRAWSURFACE7 lpDDSPrimary = NULL;

// The back buffer, things are blitted onto this then flipped to
// the Primary surface.
LPDIRECTDRAWSURFACE7 lpDDSBack = NULL;

// A surface description structure, used to describe the
// primary and back surfaces.
DDSURFACEDESC2 ddsd;

// A surface capabilities structure, used to tell
// CreateSurface() what kind of surface to create.
DDSCAPS2 ddscaps;

// GET DIRECT DRAW OBJECT

// Create the DDraw object. This is used to access anything
// DirectDraw does.
if ( FAILED( DirectDrawCreateEx( NULL, ( LPVOID* )&lpDD,
                                 IID_IDirectDraw7, NULL ) ) )
   return FALSE;

// SET DISPLAY MODE

// Get exclusive mode. Be sure to allow people to press
// Alt-Ctrl-Del ( DDSCL_ALLOWREBOOT ).
if ( FAILED( lpDD->SetCooperativeLevel( hwnd, DDSCL_EXCLUSIVE |
                           DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT ) ) )
   return FALSE;

// Set the display mode to 800x600 with 16 bits per pixel.
if ( FAILED( lpDD->SetDisplayMode( 800, 600, 16, 0, 0 ) ) )
   return FALSE;

// CREATE PRIMARY SURFACE

// Create the primary surface with 1 back buffer. Make it
// the height and width of the screen.

// First, zero the memory used by the ddsd structure.
// If you don't do this, it may have some random data that
// might mess up CreateSurface().
ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );

// DDSD_CAPS makes CreateSurface() check the
// ddsd.ddsCaps.dwCaps value. DDS_BACKBUFFERCOUNT makes it
// check the ddsd.dwBackBufferCount value.
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;

// Make it the primary surface that can be flipped.
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
                      DDSCAPS_FLIP | DDSCAPS_COMPLEX;

// Give it one back buffer.
ddsd.dwBackBufferCount = 1;

// Create the primary surface using the description in ddsd.
if ( FAILED( lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL ) ) )
   return FALSE;

// CREATE BACK BUFFER
if ( FAILED( lpDDSPrimary->GetAttachedSurface( &ddscaps, &lpDDSBack ) ) )
   return FALSE;


Page : 1