电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

详细讲解如何显示 Hello DirectX 8


发布日期:2018/11/6
 

下面就该用 DirectX 来写字了(●^o^●)

MS/DOS 下一行就办了的程序现在都长成这样(^_^;

忍着点儿慢慢往后学(f^_^)

跟以前的版本比起来 DirectX 还算是托了 Common 文件夹下那个文件的福大幅简化了源程序哩

程序的执行过程如下

创建窗口

初始化 DirectDraw

创建 Sueface(表面) 储存字符串 Hello DirectX !

进入消息循环

循环中反复描绘 Hello DirectX !

按任意键退出

下面是 DirectDraw 的 global 区域

Display 和储存字符串的 Sueface(用于描绘的内存区域)

g_bActive 是 DirectDraw 初始化成功的标志

CDisplay* g_pDisplay= NULL;

CSurface* g_pTextSurface= NULL;

BOOLg_bActive = FALSE;

下面是初始化 DirectDraw 的代码

若初始化失败就显示错误信 息并返回

创建储存文本的 Sueface(用于描绘的内存区域) 并以点阵图的形式储存 Hello DirectX !

RGB()RGB() 分别是背景色和前景色R(红)G(绿)B(蓝)在 的范围内分别指定这里设定背景色为黑色文字色(前景色)为黄色

HRESULT InitDirectDraw(HWND hWnd)

{ HRESULT hr;

g_pDisplay = new CDisplay();

if (FAILED(hr= g_pDisplay>CreateFullScreenDisplay(hWnd )))

{ ERMSG(This display card does not support xx);

return hr;

}

// Create a surface and draw text to it

if (FAILED(hr= g_pDisplay>CreateSurfaceFromText(&g_pTextSurface

NULLHello DirectX !RGB()RGB())))

return hr;

return S_OK;

}

下面是 Message Loop(消息循环)

用 DisplayFrame() 逐帧描绘

while(TRUE){ if (PeekMessage(&msg NULL PM_NOREMOVE)){ if ( == GetMessage(&msg NULL ))return (int)msgwParam;TranslateMessage(&msg);DispatchMessage(&msg);}else{ if (g_bActive){ if (FAILED(DisplayFrame())){ SAFE_DELETE(g_pDisplay);ERMSG(Displaying the next frame failed The sample will now exit);return FALSE;}}elseWaitMessage();}}

下面是描绘代码

用 Clear() 清空 DisplaySurface

用 Blt() 描绘已被转换为点阵图的字符串

用 Present() 换帧

HRESULT DisplayFrame()

{ HRESULT hr;

// Fill the back buffer with black ignoring errors until the flip

g_pDisplay>Clear();

// Blt all the sprites onto the back buffer

g_pDisplay>Blt( g_pTextSurface NULL);

// We are in fullscreen mode so perform a flip and return

if (FAILED(hr= g_pDisplay>Present())) return hr;

return S_OK;

}

下面说明创建工程的步骤

新建一个 Win Application 空白工程命名为 Hello

向工程中新建一个 C++ Source File 命名为 hello 向其中键入篇末附带的源程序

选择菜单 [Project|工程][Settings|设定] 打开[Project Settings|工程设定] 面板点击 [Link|链接] 标签向 [Object/library modules|对象库模块] 栏内添加下面四个库文件

dxguidlib

ddrawlib

dxerrlib

winmmlib

(文件名之间用半角空格分隔)

educitycn/img_///gif >

点击 [C/C++] 标签设定 include 文件的路径(参见 § 移动样品到另外的文件夹)

educitycn/img_///gif >

新建 Common 文件夹并向其中添加下面个文件

ddutilh

dxutilh

ddutilcpp

dxutilcpp

(参见 § 移动样品到另外的文件夹)

编译并执行!

源程序

/******************************************************/

/*★ 显示 Hello DirectX !前田 稔 ★*/

/******************************************************/

#define STRICT

#includeh>

#includeh>

#includeh>

#includeddutilh

// Defines constants and global variables

#define SAFE_DELETE(p){ if (p) { delete (p); (p)=NULL; } }

#define SAFE_RELEASE(p) { if (p) { (p)>Release(); (p)=NULL; } }

#define ERMSG(x)MessageBox(hWndxDirectDraw ProgramMB_OK);

CDisplay* g_pDisplay= NULL;

CSurface* g_pTextSurface= NULL;

BOOLg_bActive = FALSE;

// Functionprototypes

LRESULT CALLBACK MainWndProc(HWND UINT WPARAM LPARAM);

HRESULT WinInit(HINSTANCE hInst int nCmdShow HWND* phWnd);

HRESULT InitDirectDraw(HWND hWnd);

VOIDFreeDirectDraw();

HRESULT DisplayFrame();

//★ Windows Main

int APIENTRY WinMain(HINSTANCE hInst HINSTANCE hPrevInst LPSTR pCmdLine int nCmdShow)

{ MSG msg;

HWNDhWnd;

if (FAILED(WinInit(hInst nCmdShow &hWnd)))return FALSE;

if (FAILED(InitDirectDraw(hWnd)))

{ if (g_pDisplay)

g_pDisplay>GetDirectDraw()>SetCooperativeLevel(NULL DDSCL_NORMAL);

ERMSG(DirectDraw init failed The sample will now exit);

return FALSE;

}

while(TRUE)

{ if (PeekMessage(&msg NULL PM_NOREMOVE))

{ if ( == GetMessage(&msg NULL ))return (int)msgwParam;

TranslateMessage(&msg);

DispatchMessage(&msg);

}

else

{ if (g_bActive)

{ if (FAILED(DisplayFrame()))

{ SAFE_DELETE(g_pDisplay);

ERMSG(Displaying the next frame failed The sample will now exit);

return FALSE;

}

}

elseWaitMessage();

}

}

}

//★ WinInit()

HRESULT WinInit(HINSTANCE hInst int nCmdShow HWND* phWnd)

{ WNDCLASS wc;

HWND hWnd;

// Register the Window Class

wclpszClassName = TEXT(Hello DirectX !);

wclpfnWndProc = MainWndProc;

wcstyle = CS_VREDRAW | CS_HREDRAW;

wchInstance = hInst;

wchIcon = LoadIcon(NULLIDI_APPLICATION);

wchCursor = LoadCursor(NULL IDC_ARROW);

wchbrBackground = (HBRUSH)(COLOR_WINDOW+);

wclpszMenuName= NULL;

wccbClsExtra= ;

wccbWndExtra= ;

if (RegisterClass(&wc)==)return E_FAIL;

// Create and show the main window

hWnd = CreateWindowEx( TEXT(Hello DirectX !) TEXT(DirectDraw TEXT View)

WS_POPUP CW_USEDEFAULT CW_USEDEFAULT

CW_USEDEFAULT CW_USEDEFAULT NULL NULL hInst NULL);

if (hWnd==NULL) return E_FAIL;

ShowWindow(hWnd nCmdShow);

UpdateWindow(hWnd);

*phWnd = hWnd;

return S_OK;

}

//★ InitDirectDraw()

HRESULT InitDirectDraw(HWND hWnd)

{ HRESULT hr;

g_pDisplay = new CDisplay();

if (FAILED(hr= g_pDisplay>CreateFullScreenDisplay(hWnd )))

{ ERMSG(This display card does not support xx);

return hr;

}

// Create a surface and draw text to it

if (FAILED(hr= g_pDisplay>CreateSurfaceFromText(&g_pTextSurface

NULLHello DirectX !RGB()RGB())))

return hr;

return S_OK;

}

//★ FreeDirectDraw()

VOID FreeDirectDraw()

{ SAFE_DELETE(g_pTextSurface);

SAFE_DELETE(g_pDisplay);

}

//★ MainWndProc()

LRESULT CALLBACK MainWndProc(HWND hWnd UINT msg WPARAM wParam LPARAM lParam)

{ switch(msg)

{ case WM_KEYDOWN:

PostMessage(hWnd WM_CLOSE );

return L;

case WM_SIZE:

// Check to se

上一篇:分享:N-Gage上各种游戏的安装方法

下一篇:《QQ五星坦克》