|
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 : 10706 Date : 2010/04/19 08:47
|