In the past few days, I modified the previously written GDI-based game. Since the machine is slow, the game is running very slow, so I decided to study the DirectX8.1 DirectDraw to modify the game. It used to read the DirectDraw information written by online friends, mostly written IdirectDraw interfaces, so I decided to study the use of IDirectDraw7.
DirectX8.1 There are five types of IDirectDraw, IDirectDraw2, IDirectDraw3, iDirectdraw4, and iDirectDraw7.
Today, I mainly organize the simple use of iDirectdraw7.
It is probably like this when using IDirectDraw.
First, initialize the Window type (using POPUP only when full screen
Second, starting DirectDraw after initializing Window
Third, create a DirectDraw instance pointer LPDD
Fourth, LPDD-> SetCoopeRATIVELEVEL settings display full screen exclusive or form
5. LPDD-> SetDisplayMode (640, 480, 8) Set display mode
Sixth, DDSurfaceDesc DDSD information configuration
Seven, LPDD-> CreateSurface creates the main page
Eight, LPDDSPRIMARY-> GetAttachedSurface (& DDSCAPS, & LPDSBACK) Extract the background buffer page
Nine, use the DirectDraw's function to handle some BMP, or get HDC to operate LPDDSBACK-> GetDC (& HDC) ... (slightly, slow slowly later).
Ten, lpddsprimary-> flip (null, 0) Once FLIP, the two surface pointers are exchanged! LPDDSPRIMARY points to the background surface, while LPDDSBack points to the original front desk main surface.
11. Release the resources used.
Void FreedDraw (Void)
{
IF (LPDD! = null) {
IF (LPDDSPRIMARY! = NULL) {
LPDDSPRIMARY-> Release ();
LPDDSPRIMARY = NULL;
}
LPDD-> Release ();
LPDD = NULL;
}
}
So how is iDirectDraw7 use?
Only the above applications are only different
Such as declared variables
It turned out to be:
LPDIRECTDRAW LPDD; / / DIRECTDRAW object
LPDIRECTDRAWSURFACE LPDDSPRIMARY; / / DIRECTDRAW main surface
LPDIRECTDRAWSURFACE LPDDSBACK; // Background buffer surface
DDSurfaceDesc DDSD; / / Surface Description
DDSCAPS DDSCAPS;
it is ... now:
LPDIRECTDRAW7 LPDD; // DirectDraw object
LPDIRECTDRAWSURFACE7 LPDDSPRIMARY; // DirectDraw main surface
LPDIRECTDRAWSURFACE7 LPDDSBACK; // Background buffer surface
DDSurfaceDesc2 DDSD; // Surface Description
DDSCAPS2 DDSCAPS;
And it should be used immediately after the surface description is declared.
ZeromeMory (& DDSD, SIZEOF (DDSD));
ZeromeMory (& DDSCAPS, SIZEOF (DDSCAPS));
These two statements make the variables in DDSD and DDSCAPs are zero, and if not doing this, the following CreateSurface will be unsuccessful. Use the pointer to the LPDD
IF (DirectDrawCreateex (NULL, (VOID **) & LPDD, IID_IDIRECTDRAW7, NULL)! = DD_OK
Return False;
IID_IDIRECTDRAW7 is the GUID interface name of iDirectDraw7, so you must have a DxGuid.lib library in Link.
And as long as there is ddraw.lib.
There is no difference in the setDisplaymode function, there are three parameters, now there are five, after the two are doing
DWREFRESHRATE
Refresh Rate of the New Mode. Set this value to 0 to request the default refresh rate for the driver.
Big intending is: Used to set the refresh frequency when display, (65MHz, 75MHz, 85MHz), if set to 0 Use the current default setting.
DWFlags
Flags describing additional options. Currently, the only valid flag is DDSDM_STANDARDVGAMODE, which causes the method to set Mode 13 instead of Mode X 320x200x8 mode. If you are setting another resolution, bit depth, or a Mode X mode, do not use this flag And set the parameter to 0.
IF (LPDD-> SetDisplayMode (640, 480, 8, 0, 0)! = DD_OK)
Return False;
Then other other uses, some people must think that IdirectDraw is enough, but I think Microsoft's new idirectdraw must have its truth, at least, the more gain it.
There is a process below, the framework is the framework of a small example of my friend N5.
#include
#include
#include
// Global variable
LPDIRECTDRAW7 LPDD; // DirectDraw object
LPDIRECTDRAWSURFACE7 LPDDSPRIMARY; // DirectDraw main surface
LPDIRECTDRAWSURFACE7 LPDDSBACK; // Background buffer surface
HDC HDC;
Char szmsg1 [] = "My First DirectDraw program";
CHAR SZMSG2 [] = "Press ESC Exit";
Bool Bactive = True;
Hwnd hwnd;
// Function declaration
LResult Callback WinProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM);
Bool Initwindow (Hinstance Hinstance, INT NCMDSHOW);
Bool initddraw (void); // Initialization DirectX
Void FreedDraw (void); // Release DirectX Object
Void mainloop (void); // Game main loop
/ / -------------------------------------------------------------------------------------------- -------
// Function: WinMain ()
// Function: WIN32 application portfolio function. Perform initialization work, processing message loop // --------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------
Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance,
LPSTR LPCMDLINE, INT NCMDSHOW)
{
MSG msg;
// Initialization main window
IF (!in, ncmdshow)
Return False;
// Initialize the DirectDraw environment and implement the DirectDraw function
IF (! initddraw ()) {
MessageBox (getActiveWindow (), "Initialization DirectDraw process error!", "Error", MB_OK;
FREEDDRAW ();
DESTROYWINDOW ());
Return False;
}
While (1) {
IF (PEEKMESSAGE (& MSG, NULL, 0, 0, PM_NOREMOVE)) {// If there is a message to handle the message
IF (! GetMessage (& MSG, NULL, 0, 0))
Return msg.wparam;
TranslateMessage (& MSG);
DispatchMessage (& MSG);
}
Else if (bactage) {// If the program is active, enter the game main loop
Mainloop ();
}
// Waiting for the message
Else WaitMessage ();
}
Return msg.wparam;
}
// --------------------------------------
// Function: initwindow ()
// Function: Create a main window
// --------------------------------------
Static Bool Initwindow (Hinstance Hinstance, Int Ncmdshow)
{
// hwnd hwnd; // window handle
WNDCLASS WC; // window structure
// Fill the window structure
wc.style = 0;
Wc.lpfnWndproc = WinProc;
wc.cbclsextra = 0;
wc.cbWndextra = 0;
wc.hinstance = hinstance;
Wc.hicon = loadicon (Hinstance, IDi_Application);
Wc.hcursor = loadingCursor (NULL, IDC_ARROW);
Wc.hbrbackground = (hbrush) getStockObject (Black_brush);
Wc.lpsz GeneNuname = NULL;
wc.lpszclassname = "Dxhello";
// Register window class
RegisterClass (& WC);
// Create a main window
HWND = CREATEWINDOWEX (0, "Dxhello", "", WS_POPUP,
0, 0, GetSystemmetrics (SM_CXSCREEN), GetSystemMetrics (SM_CYSCREEN),
NULL, NULL, HINSTANCE, NULL;
IF (! hwnd)
Return False;
ShowWindow (HWND, NCMDSHOW);
UpdateWindow (HWND); Return True;
}
/ / -------------------------------------------------------------------------------------------- -
// Function: WinProc ()
// Function: Handle the main window message
/ / -------------------------------------------------------------------------------------------- -
Lresult Callback WinProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM)
{
Switch (Message) {
Case WM_ActivateApp:
BACTIVE = WPARAM;
Break;
Case WM_KeyDown: // Key Message
Switch (wparam) {
Case vk_escape:
Postmessage (hwnd, wm_close, 0, 0);
Break;
}
Break;
Case WM_SETCURSOR:
SetCursor (NULL);
Return True;
Case WM_DESTROY: // Exit Message
FREEDDRAW ();
PostquitMessage (0);
Break;
}
// Call the default message processing process
Return DefWindowProc (Hwnd, Message, WPARAM, LPARAM);
}
/ / -------------------------------------------------------------------------------------------- ----------------
// Function: initddraw ()
// Features: Initialize the DirectDraw environment and implement it. Include: Create a DirectDRAW object,
// Set the display mode, create the main surface
/ / -------------------------------------------------------------------------------------------- ----------------
Bool initddraw (void)
{
DDSurfaceDesc2 DDSD; // Surface Description
DDSCAPS2 DDSCAPS;
// HDC HDC; // Equipment Environment Handle
// Create a DirectDraw object
IF (DirectDrawCreateex (NULL, (VOID **) & LPDD, IID_IDIRECTDRAW7, NULL)! = DD_OK
Return False;
// Get exclusive and full screen mode
IF (LPDD-> SetCoopeRarativeAVelevel (getActiveWindow (), DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)! = DD_OK)
Return False;
/ / Set display mode
IF (LPDD-> SetDisplayMode (640, 480, 8, 0, 0)! = DD_OK)
Return False;
// Fill the main surface information
ZeromeMory (& DDSD, SIZEOF (DDSD));
ZeromeMory (& DDSCAPS, SIZEOF (DDSCAPS));
Ddsd.dwsize = sizeof (ddsd);
DDSD.DWFLAGS = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
DDSD.DDSCAPS.DWCAPS = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
DDSD.DWBACKBUFFERCOUNT = 1;
// Create a main surface object
IF (LPDD-> CreateSurface (& DDSD, & LPDDSPRIMARY, NULL)! = DD_OK)
Return False;
/ / Extract the background cache surface pointer
DDSCAPS.DWCAPS = DDSCAPS_BACKBUFFER; if (LPDDSPRIMARY-> GetAttachedSurface (& DDSCAPS, & LPDDSBACK)! = DD_OK)
Return False;
Return True;
}
/ / -------------------------------------------------------------------------------------------- -------
// Function: freeddraw ()
// Features: Release all DirectDraw objects
/ / -------------------------------------------------------------------------------------------- -------
Void FreedDraw (Void)
{
IF (LPDD! = null) {
IF (LPDDSPRIMARY! = NULL) {
LPDDSPRIMARY-> Release ();
LPDDSPRIMARY = NULL;
}
LPDD-> Release ();
LPDD = NULL;
}
}
/ / -------------------------------------------------------------------------------------------- -------
// Function: mainloop ()
// Function: Game main loop
/ / -------------------------------------------------------------------------------------------- -------
Void Mainloop (Void)
{
Static I = 0;
// Operation on the background buffer surface
IF (LPDDSBACK-> Getdc (& HDC) == DD_OK) {
SetBkcolor (HDC, RGB (0 i, 255-i, 0 I));
SetTextColor (HDC, RGB (0 i, 0 i, 255-I));
Textout (HDC, 220, 200, SZMSG1, LSTRLEN (SZMSG1));
Textout (HDC, 280, 220, Szmsg2, Lstrlen (Szmsg2));
LPDDSBACK-> ReleaseDC (HDC);
i = 10;
IF (i> 255) i = 0;
}
IF (lpddsprimary-> flip (null, 0)! = DD_OK) {// Once flip, two knots of the two surfaces are exchanged! LPDDSPRIMARY points to the background surface, so
FREEDDRAW (); // You will see the words written on the back surface of the background, and LPDDSBACK points to the original front desk main surface.
PostquitMessage (0); // drop it to the background
}
}