DirectX8.1 DirectDraw7 Research Book (2)

zhaozj2021-02-16  54

Let's take a look at the program, the framework of the program is rewritten with the Hello World of the Windows Application generated by VC6. Here did not explain the program in ALT TAB, and did not say the problem. Didn't use G_LPDD-> CreatePalette (, ,,,), when the function is used, with 256 colors of #define bpp 8, there may be color distortion, while 16-bit color / when you don't have to use this function! There will be no color distortion. But use this way, I will explain it later.

// Directx.cpp: Defines the entry point for the application.

//

#include "stdafx.h"

#include "resource.h"

#include

#define max_loadstring 100

// Global Variables:

Hinstance hinst; // current instance

Tchar sztitle [max_loadstring]; // the title bar text

Tchar szwindowclass [max_loadstring]; // the title bar text

// Foward Declarations of Functions Included in this Code Module:

Atom MyRegisterClass (Hinstance Hinstance);

Bool InitInstance (Hinstance, Int);

Lresult Callback WndProc (HWND, UINT, WPARAM, LPARAM);

// --------------------- global variables and some predefined macros ------------------ -------------------

// These two predefined macros are useful for convenient release pointers in the FREEDRAW () function below.

#define Safe_Delete (p) {if (p) {delete (p); (p) = null;}}

#define safe_release (p) {if (p) {(p) -> Release (); (p) = null;}}

// Display mode setting 640 × 480 × 8

#define width 640

#define height 480

#define bpp 8

// Try #define BPP 16 What is the effect? ?

// Don't use G_LPDD-> CreatePalette (, ,,,); function, with 256 colors of #define BPP 8, there may be color distortion, while 16-bit color // do not have to use this function! There will be no color distortion.

HRESULT INITDIRECTDRAW (Hinstance Hinst); // Initializing DirectDraw

HRESULT MainLoop (); // is known as Gameloop

Void freedraw (); // Releases all DirectDraw pointers

LPDIRECTDRAW7 g_lpdd; // Pointer to the entity

LPDIRECTDRAWSURFACE7 G_LPFRONTSURFACE; // a PagelpDirectDrawsurface7 g_lpbacksurface; // b Page

LPDIRECTDRAWSURFACE7 g_lpoffscreen; // Used to the off-screen page in the background

Bool g_bactive = true; // Decide whether mainloop is running

/ / -------------------------------------------------------------------------------------------- -----------------------------

// function: inIDirectdraw ()

// Used to initialize

/ / Complete the task: Create a g_lpdd pointer entity, get the display mode, set the display mode, create page information

// Create a main surface object, extract the background cache surface pointer, create an off-screen page with a picture

HRESULT INITDIRECTDRAW (Hinstance Hinst)

{

HRESULT HR;

DDSURFACEDESC2 DDSD;

DDSCAPS2 DDSCAPS;

ZeromeMory (& DDSD, SIZEOF (DDSD));

ZeromeMory (& DDSCAPS, SIZEOF (DDSCAPS));

FREEDRAW ();

// Create a g_lpdd pointer entity

IF (Failed (HR = DirectDrawcreateex (NULL, (VOID **) & g_lpdd, iid_idirectdraw7, null)))

Return E_FAIL;

// Get display mode

IF (Failed (HR = g_lpdd-> setcooperativeelevel (getActiveWindow (), DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))))

Return E_FAIL;

/ / Set display mode

IF (failed (hr = g_lpdd-> setDisplaymode (width, height, bpp, 0,0))))))

Return E_FAIL;

// Make page information

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 (FAILED (HR = g_lpdd-> createsssurface (& DDSD, & G_LPFRONTSURFACE, NULL)))

Return E_FAIL;

/ / Extract the background cache surface pointer

DDSCAPS.DWCAPS = DDSCAPS_BACKBUFFER;

IF (Failed (HR = g_lpfrontsurface-> getattachedsurface (& ddscaps, & g_lpbacksurface)))))

Return E_FAIL;

g_lpbacksurface-> addref (); // Don't you seem to work?

// Create an off-screen page with a picture

Hbitmap HBM = NULL;

Bitmap BMP;

// idb_bitmap1 is a picture resource

HBM = (hbitmap) loading (getModuleHandle (NULL), MakeintResource (IDB_bitmap1),

Image_bitmap, 1500, 280, // Picture After reading coming back, deformation is made into large size width, Height

LR_createdibsection;

GetObject (HBM, SIZEOF (BMP), & BMP); DDSD.DWFLAGS = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;

DDSD.DDSCAPS.DWCAPS = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEMEMORY

Ddsd.dwwidth = bmp.bmwidth;

DDSD.DWHEIGHT = Bmp.Bmheight;

HR = g_lpdd-> createssesurface (& DDSD, & G_LPoffScreen, NULL);

IF (Failed (HR))

{

IF (hr == DDERR_OUTOFVIDEOMEMORY)

{// This is the last chance

DDSD.DDSCAPS.DWCAPS = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMORY

IF (FAILED (HR = g_lpdd-> createsssurface (& DDSD, & G_LPOFFSCREEN, NULL)))

Return E_FAIL;

}

}

HDC HDC;

HDC HDCIMAGE;

HDCIMAGE = CREATECMOMPALDC (NULL);

SelectObject (HDCIMAGE, HBM);

g_lpoffscreen-> restore ();

g_lpoffscreen-> getSurfaceDesc (& DDSD);

IF ((hr = g_lpoffscreen-> getdc (& hdc)) == DD_OK)

{

Stretchblt (HDC, 0, 0, Ddsd.dwwidth, ddsd.dwheight, hdcimage, 0, 0,

Bmp.bmwidth, bmp.bmheight, srccopy;

g_lpoffscreen-> releasedc (hdc);

}

Deletedc (HDCIMAGE);

Return S_OK;

}

// The macro here is defined above.

Void freedraw ()

{

SAFE_RELEASE (G_LPFRONTSURFACE);

SAFE_RELEASE (G_LPBACKSURFACE);

SAFE_RELEASE (G_LPoffScreen);

SAFE_RELEASE (G_LPDD);

}

// function: mainloop ()

//

//

HRESULT MainLoop ()

{

HRESULT HRET;

RECT;

Rect.Left = 0;

RECT.TOP = 0;

Rect.right = 150;

Rect.bottom = 140;

// Remove the picture where Offscreen existing OFFSCREEN is removed from the remote page page, copy to the background cache

g_lpbacksurface-> Bltfast (0, 0, G_LPoffScreen, & Rect, DDBLTFAST_NOCOLORKEY);

/ / The background caucaso painted pictures, then flip put it until the front desk let him show

g_lpfrontsurface-> flip (null, 0);

Return S_OK;

}

//

转载请注明原文地址:https://www.9cbs.com/read-27931.html

New Post(0)