|
http://www.hackerschool.org/HS_Boards/zboard.php?id=Free_Board&no=15750 [복사]
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <ddraw.h>
#include <stdio.h>
#include <stdarg.h>
#include "resource.h"
#define NAME "DDExample1"
#define TITLE "Direct Draw Example 1"
#define TIMER_ID 1
#define TIMER_RATE 500
LPDIRECTDRAW7 g_pDD = NULL; // DirectDraw object
LPDIRECTDRAWSURFACE7 g_pDDSPrimary = NULL;// DirectDraw primary surface
LPDIRECTDRAWSURFACE7 g_pDDSBack = NULL; // DirectDraw back surface
BOOL g_bActive = FALSE; // Is application active?
static char szMsg[] = "Page Flipping Test: Press F12 to exit";
static char szFrontMsg[] = "Front buffer (F12 to quit)";
static char szBackMsg[] = "Back buffer (F12 to quit)";
struct _5 {};
static void
ReleaseAllObjects(void)
{
if (g_pDD != NULL)
{
if (g_pDDSPrimary != NULL)
{
//// 생성된 표면 해제
g_pDDSPrimary->Release();
g_pDDSPrimary = NULL;
}
g_pDD->Release();
g_pDD = NULL;
}
}
HRESULT
InitFail(HWND hWnd, HRESULT hRet, LPCTSTR szError,...)
{
char szBuff[128];
va_list vl;
va_start(vl, szError);
vsprintf(szBuff, szError, vl);
ReleaseAllObjects();
MessageBox(hWnd, szBuff, TITLE, MB_OK);
DestroyWindow(hWnd);
va_end(vl);
return hRet;
}
struct _4 {};
static void
UpdateFrame(HWND hWnd)
{
static BYTE phase = 0;
HDC hdc;
DDBLTFX ddbltfx;
RECT rc;
SIZE size;
// Use the blter to do a color fill to clear the back buffer
ZeroMemory(&ddbltfx, sizeof(ddbltfx));
ddbltfx.dwSize = sizeof(ddbltfx);
ddbltfx.dwFillColor = 0; // 픽셀 RGB 값 또는 팔레트
g_pDDSBack->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
if (g_pDDSBack->GetDC(&hdc) == DD_OK)
{
SetBkColor(hdc, RGB(0, 0, 255));
SetTextColor(hdc, RGB(255, 255, 0));
//// 플래그 전환
if (phase)
{
GetClientRect(hWnd, &rc);
GetTextExtentPoint(hdc, szMsg, lstrlen(szMsg), &size);
TextOut(hdc, (rc.right - size.cx) / 2, (rc.bottom - size.cy) / 2,
szMsg, sizeof(szMsg) - 1);
TextOut(hdc, 0, 0, szFrontMsg, lstrlen(szFrontMsg));
phase = 0;
}
else
{
TextOut(hdc, 0, 0, szBackMsg, lstrlen(szBackMsg));
phase = 1;
}
g_pDDSBack->ReleaseDC(hdc);
}
}
struct _3 {};
long FAR PASCAL
WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HRESULT hRet;
switch (message)
{
case WM_ACTIVATE:
// Pause if minimized
// wParam 상위 2바이트 : 최소화된 상태이면 0이 아니다
g_bActive = !((BOOL)HIWORD(wParam));
return 0L;
//// 어플리케이션이 종료되기전에 발생
// WM_CLOSE -> DefWindowProc() -> DestroyWindow()
// -> WM_NCDESTROY / WM_DESTROY
case WM_DESTROY:
// Clean up and close the app
ReleaseAllObjects();
// WM_QUIT 메시지 발생
// 일반적으로 WM_DESTROY에서 사용
PostQuitMessage(0);
return 0L;
case WM_KEYDOWN:
// Handle any non-accelerated key commands
switch (wParam)
{
case VK_ESCAPE:
case VK_F12:
// 윈도우 or 어플리케이션을 종료 시키는 메시지 발생
PostMessage(hWnd, WM_CLOSE, 0, 0);
return 0L;
}
break;
case WM_SETCURSOR:
// Turn off the cursor since this is a full-screen app
SetCursor(NULL);
return TRUE;
case WM_TIMER:
// Update and flip surfaces
if (g_bActive && TIMER_ID == wParam)
{
UpdateFrame(hWnd);
while (TRUE)
{
hRet = g_pDDSPrimary->Flip(NULL, // 플립핑 체인의 표면 주소
// NULL이면 부가 ( 연계된) 표면을 순차 플립
0); // 0이면 플립핑 작동과 상관없이 즉시 리턴
if (hRet == DD_OK)
break;
if (hRet == DDERR_SURFACELOST)
{
//// 표면을 복구
hRet = g_pDDSPrimary->Restore();
if (hRet != DD_OK)
break;
}
// 드로우 중이면 루프
if (hRet != DDERR_WASSTILLDRAWING)
break;
}
}
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
struct _2 {};
static HRESULT
InitApp(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
WNDCLASS wc;
DDSURFACEDESC2 ddsd; // 표면에 대한 속성 설정
DDSCAPS2 ddscaps; // 표면 능력 설정
HRESULT hRet;
// Set up and register window class
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MAIN_ICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NAME;
wc.lpszClassName = NAME;
RegisterClass(&wc);
// Create a window
hWnd = CreateWindowEx(WS_EX_TOPMOST,
NAME,
TITLE,
WS_POPUP,
0,
0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hInstance,
NULL);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
SetFocus(hWnd);
hRet = DirectDrawCreateEx(NULL, // NULL == 주장치사용
(VOID**)&g_pDD, // directdraw 객체
IID_IDirectDraw7,
NULL); // 확장성 인자 ( X )
if (hRet != DD_OK)
return InitFail(hWnd, hRet, "DirectDrawCreateEx FAILED");
// 실행수준
// Get exclusive mode
hRet = g_pDD->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, "SetCooperativeLevel FAILED");
// Set the video mode to 640x480x8
hRet = g_pDD->SetDisplayMode(640, 480, 8,
0, // 리프레쉬율 ( 0 == 디폴트 )
0);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, "SetDisplayMode FAILED");
// Create the primary surface with 1 back buffer
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; // 지정 멤버의 유효성 설정
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP |
DDSCAPS_COMPLEX; // 하나 이상의 표면
// 즉, 생성될 표면에 연계되는 표면의 생성
ddsd.dwBackBufferCount = 1;
hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSPrimary, NULL);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, "CreateSurface FAILED");
// Get a pointer to the back buffer
ZeroMemory(&ddscaps, sizeof(ddscaps));
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
hRet = g_pDDSPrimary->GetAttachedSurface(&ddscaps, &g_pDDSBack);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, "GetAttachedSurface FAILED");
// Create a timer to flip the pages
if (TIMER_ID != SetTimer(hWnd, TIMER_ID, TIMER_RATE, NULL))
return InitFail(hWnd, hRet, "SetTimer FAILED");
return DD_OK;
}
struct _1 {};
int PASCAL
WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
if (InitApp(hInstance, nCmdShow) != DD_OK)
return FALSE;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
DirectX 페이지 플립핑예제인데요..
함수라던지.. 등등 조금만 설명 부탁드립니다..
|
Hit : 11604 Date : 2010/04/19 08:47
|