This is a very simple DirectDraw sample using Microsoft DirectX under Visual C++.
DirectDraw Sample Program (18.1 KiB, 9,787 hits)
Setting up DirectX under Visual C++
I most likely won’t be doing DirectX development under Watcom or Borland C/C++ or Delphi or VisualBasic etc; so if you want such info included here, you’ll have to send it to me.
Firstly, the directories must be set up so that Visual C/C++ can find the DirectX include files and libraries:
- Access the Tools/Options/Directories tabbed dialog.
- Select “library directories” from the drop-down list, and add the directory of the DX SDK libraries, e.g. “d:dxsdksdklib”
- Select “include directories” from the drop-down list, and add the directory of the DX SDK header files, e.g. “d:dxsdksdkinc”.
- If you are going to be using some of the DX utility headers used in the samples, then also add the samplesmisc directory, e.g. “d:dxsdksdksamplesmisc” to your includes path.
Note that the version of DirectX that normally ships with Visual C++ isn’t usually the latest, so to make sure that the compiler doesn’t find the older version located in its own directories, add the include and library paths for the SDK in front of the default include and library paths.
You must also for each application that uses DirectX explicitly add the required libraries to the project. Do this in Project/Settings (Alt+F7), under the “Link” tab for each configuration of your project. For DirectDraw, add ddraw.lib in the Object/Library modules box. You also need to add dxguid.lib here if your application uses any of the DirectX COM interface ID’s, eg IID_IDirectDraw7
The DirectDraw sample
The general outline of our sample DirectDraw application is as follows:
- Create a normal Windows window
- Set up our DirectX variables
- Initialize a DirectDraw object
- Set the “cooperative level” and display modes as necessary (explained later)
- Create front and back surfaces
- If in windowed mode, create and attach a clipper
- Render to the back buffer
- Perform the flipping. If in full-screen mode, just flip. If in windowed mode, you need to blit from the back surface to the primary surface each frame.
- Repeat from step 7 until we exit
- Clean up
We are going to need a number of variables for our DirectDraw application. These can be global variables or class members, that’s up to you. The same goes for functions. Here are the variables we’re going to use:
LPDIRECTDRAW g_pDD; // DirectDraw object
LPDIRECTDRAWSURFACE g_pDDSPrimary; // DirectDraw primary surface
LPDIRECTDRAWSURFACE g_pDDSBack; // DirectDraw back surface
LPDIRECTDRAWCLIPPER g_pClipper; // Clipper for windowed mode
HWND g_hWnd; // Handle of window
bool g_bFullScreen; // are we in fullscreen mode?
All of these variables and functions I place in a seperate file, which can be called anything you want, although you should not use file names that already exist, such as “ddraw.h”. This compiler is likely to get confused about which one you want. I’ve used dd.h and dd.cpp in the sample.
Remember to ensure that these variables are initialized to NULL before we begin. If you were creating classes, you could do this in the constructor of the class.
Here is the general layout of my dd.h and dd.cpp files:
The dd.h header file uses #ifndef and #define directives to include ddraw.h header file and other declarations.
extern LPDIRECTDRAW g_pDD;
extern void DirectXFunction();
g_pDD = NULL;