📄 ddex4.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//-----------------------------------------------------------------------------
// File: DDEx4.CPP
//
// Desc: Direct Draw example program 4. Adds functionality to
// example program 3. Creates a flipping surface and loads
// a bitmap image into an offscreen surface. Uses BltFast to
// copy portions of the offscreen surface to the back buffer
// to generate an animation. Illustrates watching return
// code from BltFast to prevent image tearing. This program
// requires 1.2 Meg of video ram.
//
//-----------------------------------------------------------------------------
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------
#include <windows.h>
#include <ddraw.h>
#include "resource.h"
#include "ddutil.h"
extern "C" IDirectDrawSurface4 *
DDLoadBitmapScaled(HINSTANCE hInstance, IDirectDraw4 * pdd, LPCTSTR szBitmap,
DWORD dwWidth, DWORD dwHeight);
//-----------------------------------------------------------------------------
// Local definitions
//-----------------------------------------------------------------------------
#define NAME TEXT("DDExample4")
#define TITLE TEXT("Direct Draw Example 4")
//-----------------------------------------------------------------------------
// Default settings
//-----------------------------------------------------------------------------
#define TIMER_ID 1
#define TIMER_RATE 18
//-----------------------------------------------------------------------------
// Global data
//-----------------------------------------------------------------------------
LPDIRECTDRAW4 g_pDD = NULL; // DirectDraw object
LPDIRECTDRAWSURFACE4 g_pDDSPrimary = NULL; // DirectDraw primary surface
LPDIRECTDRAWSURFACE4 g_pDDSBack = NULL; // DirectDraw back surface
LPDIRECTDRAWSURFACE4 g_pDDSBackground = NULL; // Offscreen surface 1
LPDIRECTDRAWSURFACE4 g_pDDSSprite = NULL; // Offscreen surface 1
BOOL g_bActive = FALSE; // Is application active?
//-----------------------------------------------------------------------------
// Local data
//-----------------------------------------------------------------------------
static LPCTSTR szBackground = TEXT("BACKGROUND");
static LPCTSTR szSprite = TEXT("SPRITE");
static HINSTANCE hInstance;
static DWORD g_dwWidth,
g_dwHeight;
//-----------------------------------------------------------------------------
// Name: ReleaseAllObjects()
// Desc: Finished with all objects we use; release them
//-----------------------------------------------------------------------------
static void
ReleaseAllObjects(void)
{
if (g_pDDSBack != NULL)
{
g_pDDSBack->Release();
g_pDDSBack = NULL;
}
if (g_pDDSPrimary != NULL)
{
g_pDDSPrimary->Release();
g_pDDSPrimary = NULL;
}
if (g_pDDSBackground != NULL)
{
g_pDDSBackground->Release();
g_pDDSBackground = NULL;
}
if (g_pDDSSprite != NULL)
{
g_pDDSSprite->Release();
g_pDDSSprite = NULL;
}
if (g_pDD != NULL)
{
g_pDD->Release();
g_pDD = NULL;
}
}
//-----------------------------------------------------------------------------
// Name: InitFail()
// Desc: This function is called if an initialization function fails
//-----------------------------------------------------------------------------
#define PREFIX TEXT("DDEX4: ")
#define PREFIX_LEN 7
HRESULT
InitFail(HWND hWnd, HRESULT hRet, LPCTSTR szError,...)
{
TCHAR szBuff[128];
va_list vl;
va_start(vl, szError);
wsprintf(szBuff, PREFIX);
wvsprintf(szBuff + PREFIX_LEN, szError, vl);
ReleaseAllObjects();
OutputDebugString(szBuff);
DestroyWindow(hWnd);
va_end(vl);
return hRet;
}
#undef PREFIX_LEN
#undef PREFIX
//-----------------------------------------------------------------------------
// Name: RestoreAll()
// Desc: Restore all lost objects
//-----------------------------------------------------------------------------
HRESULT
RestoreAll(void)
{
HRESULT hRet;
hRet = g_pDDSPrimary->Restore();
if (SUCCEEDED(hRet))
{
if (SUCCEEDED(hRet = g_pDDSBackground->Restore()))
{
DDReLoadBitmap(hInstance, g_pDDSBackground, szBackground);
}
if (SUCCEEDED(hRet = g_pDDSSprite->Restore()))
{
DDReLoadBitmap(hInstance, g_pDDSSprite, szSprite);
}
}
return hRet;
}
//-----------------------------------------------------------------------------
// Name: UpdateFrame()
// Desc: Decide what needs to be blitted next, wait for flip to complete,
// then flip the buffers.
//-----------------------------------------------------------------------------
void
UpdateFrame(void)
{
HRESULT hRet;
static DWORD lastTickCount[3] = {0, 0, 0};
static int currentFrame[3] = {0, 0, 0};
DWORD delay[3] = {50, 78, 13};
int xpos[3] = {288, 190, 416};
int ypos[3] = {128, 300, 256};
int i;
DWORD thisTickCount;
RECT rcRect;
// Decide which frame will be blitted next
thisTickCount = GetTickCount();
for (i = 0; i < 3; i++)
{
if ((thisTickCount - lastTickCount[i]) > delay[i])
{
// Move to next frame;
lastTickCount[i] = thisTickCount;
currentFrame[i]++;
if (currentFrame[i] > 59)
currentFrame[i] = 0;
}
}
// Blit the stuff for the next frame
rcRect.left = 0;
rcRect.top = 0;
rcRect.right = g_dwWidth;
rcRect.bottom = g_dwHeight;
while (TRUE)
{
hRet = g_pDDSBack->BltFast(0, 0, g_pDDSBackground,
&rcRect, DDBLTFAST_WAIT | DDBLTFAST_NOCOLORKEY);
if (SUCCEEDED(hRet))
break;
if (hRet == DDERR_SURFACELOST)
{
if (FAILED(RestoreAll()))
return;
}
if (hRet != DDERR_WASSTILLDRAWING)
return;
}
if (FAILED(hRet))
return;
for (i = 0; i < 3; i++)
{
rcRect.left = currentFrame[i] % 10 * 64;
rcRect.top = currentFrame[i] / 10 * 64;
rcRect.right = currentFrame[i] % 10 * 64 + 64;
rcRect.bottom = currentFrame[i] / 10 * 64 + 64;
while (TRUE)
{
hRet = g_pDDSBack->BltFast(xpos[i], ypos[i], g_pDDSSprite,
&rcRect, DDBLTFAST_SRCCOLORKEY);
if (SUCCEEDED(hRet))
break;
if (hRet == DDERR_SURFACELOST)
{
if (FAILED(RestoreAll()))
return;
}
if (hRet != DDERR_WASSTILLDRAWING)
return;
}
}
// Flip the surfaces
while (TRUE)
{
hRet = g_pDDSPrimary->Flip(NULL, 0);
if (SUCCEEDED(hRet))
break;
if (hRet == DDERR_SURFACELOST)
{
if (FAILED(RestoreAll()))
break;
}
if (hRet != DDERR_WASSTILLDRAWING)
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -