下面就該用 DirectX
MS/DOS 下一行就辦了的程序
忍著點兒
跟以前的版本比起來
程序的執行過程如下
下面是 DirectDraw 的 global 區域
Display 和儲存字符串的 Sueface(用於描繪的內存區域)
g_bActive 是 DirectDraw 初始化成功的標志
CDisplay* g_pDisplay = NULL;
CSurface* g_pTextSurface= NULL;
BOOL g_bActive = FALSE;
下面是初始化 DirectDraw 的代碼
若初始化失敗就顯示錯誤信 息並返回
創建儲存文本的 Sueface(用於描繪的內存區域) 並以點陣圖的形式儲存
RGB(
HRESULT InitDirectDraw(HWND hWnd)
{ HRESULT hr;
g_pDisplay = new CDisplay();
if (FAILED(hr= g_pDisplay
{ ERMSG(
return hr;
}
// Create a surface
if (FAILED(hr= g_pDisplay
NULL
return hr;
return S_OK;
}
下面是 Message Loop(消息循環)
用 DisplayFrame() 逐幀描繪
while(TRUE) { if (PeekMessage(&msg
下面是描繪代碼
用 Clear() 清空 DisplaySurface
用 Blt() 描繪已被轉換為點陣圖的字符串
用 Present() 換幀
HRESULT DisplayFrame()
{ HRESULT hr;
// Fill the back buffer with black
g_pDisplay
// Blt all the sprites onto the back buffer
g_pDisplay
// We are in fullscreen mode
if (FAILED(hr= g_pDisplay
return S_OK;
}
下面說明創建工程的步驟
dxguid
ddraw
dxerr
winmm
(文件名之間用半角空格分隔)
ddutil
dxutil
ddutil
dxutil
(參見 §
源程序
/******************************************************/
/*★ 顯示 Hello DirectX
/******************************************************/
#define STRICT
#include
#include
#include
#include
// Defines
#define SAFE_DELETE(p) { if (p) { delete (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if (p) { (p)
#define ERMSG(x) MessageBox(hWnd
CDisplay* g_pDisplay = NULL;
CSurface* g_pTextSurface= NULL;
BOOL g_bActive = FALSE;
// Function
LRESULT CALLBACK MainWndProc(HWND
HRESULT WinInit(HINSTANCE hInst
HRESULT InitDirectDraw(HWND hWnd);
VOID FreeDirectDraw();
HRESULT DisplayFrame();
//★ Windows Main
int APIENTRY WinMain(HINSTANCE hInst
{ MSG msg;
HWND hWnd;
if (FAILED(WinInit(hInst
if (FAILED(InitDirectDraw(hWnd)))
{ if (g_pDisplay)
g_pDisplay
ERMSG(
return FALSE;
}
while(TRUE)
{ if (PeekMessage(&msg
{ if (
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{ if (g_bActive)
{ if (FAILED(DisplayFrame()))
{ SAFE_DELETE(g_pDisplay);
ERMSG(
return FALSE;
}
}
else WaitMessage();
}
}
}
//★ WinInit()
HRESULT WinInit(HINSTANCE hInst
{ WNDCLASS wc;
HWND hWnd;
// Register the Window Class
wc
wc
wc
wc
wc
wc
wc
wc
wc
wc
if (RegisterClass(&wc)==
// Create and show the main window
hWnd = CreateWindowEx(
WS_POPUP
CW_USEDEFAULT
if (hWnd==NULL) return E_FAIL;
ShowWindow(hWnd
UpdateWindow(hWnd);
*phWnd = hWnd;
return S_OK;
}
//★ InitDirectDraw()
HRESULT InitDirectDraw(HWND hWnd)
{ HRESULT hr;
g_pDisplay = new CDisplay();
if (FAILED(hr= g_pDisplay
{ ERMSG(
return hr;
}
// Create a surface
if (FAILED(hr= g_pDisplay
NULL
return hr;
return S_OK;
}
//★ FreeDirectDraw()
VOID FreeDirectDraw()
{ SAFE_DELETE(g_pTextSurface);
SAFE_DELETE(g_pDisplay);
}
//★ MainWndProc()
LRESULT CALLBACK MainWndProc(HWND hWnd
{ switch(msg)
{ case WM_KEYDOWN:
PostMessage(hWnd
return
case WM_SIZE:
// Check to se
From:http://tw.wingwit.com/Article/program/yxkf/201311/11087.html