您现在的位置是:首页
>
怎么样qq资料不显示详细资料 详细讲解如何显示 Hello DirectX 8
详细讲解如何显示 Hello DirectX 8 下面就该用 DirectX 来写字了 ●^o^● MS/DOS 下一行就办了的程序 现在都长成这样 ^_^ 忍着点儿 慢慢往后学 f^_^
详细讲解如何显示 Hello DirectX 8

下面就该用 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; BOOL g_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 x x ); return hr; } // Create a surface and draw text to it if (FAILED(hr= g_pDisplay >CreateSurfaceFromText(&g_pTextSurface NULL Hello 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)msg wParam; 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; } } else WaitMessage(); } } 下面是描绘代码 用 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|对象 库模块] 栏内添加下面四个库文件 dxguid lib ddraw lib dxerr lib winmm lib (文件名之间用半角空格分隔) educity cn/img_ / / / gif > 点击 [C/C++] 标签 设定 include 文件的路径(参见 § 移动样品到另外的文件夹) educity cn/img_ / / / gif > 新建 Common 文件夹并向其中添加下面 个文件 ddutil h dxutil h ddutil cpp dxutil cpp (参见 § 移动样品到另外的文件夹) 编译并执行! 源程序 /******************************************************/ /******************************************************/ #define STRICT #include h> #include h> #include h> #include ddutil h // 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(hWnd x DirectDraw Program MB_OK); CDisplay* g_pDisplay = NULL; CSurface* g_pTextSurface= NULL; BOOL g_bActive = FALSE; // Function prototypes LRESULT CALLBACK MainWndProc(HWND UINT WPARAM LPARAM); HRESULT WinInit(HINSTANCE hInst int nCmdShow HWND* phWnd); HRESULT InitDirectDraw(HWND hWnd); VOID FreeDirectDraw(); HRESULT DisplayFrame(); // Windows Main int APIENTRY WinMain(HINSTANCE hInst HINSTANCE hPrevInst LPSTR pCmdLine int nCmdShow) { MSG msg; HWND hWnd; 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)msg wParam; 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; } } else WaitMessage(); } } } // WinInit() HRESULT WinInit(HINSTANCE hInst int nCmdShow HWND* phWnd) { WNDCLASS wc; HWND hWnd; // Register the Window Class wc lpszClassName = TEXT( Hello DirectX ! ); wc lpfnWndProc = MainWndProc; wc style = CS_VREDRAW | CS_HREDRAW; wc hInstance = hInst; wc hIcon = LoadIcon(NULL IDI_APPLICATION); wc hCursor = LoadCursor(NULL IDC_ARROW); wc hbrBackground = (HBRUSH)(COLOR_WINDOW+ ); wc lpszMenuName = NULL; wc cbClsExtra = ; wc cbWndExtra = ; 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 x x ); return hr; } // Create a surface and draw text to it if (FAILED(hr= g_pDisplay >CreateSurfaceFromText(&g_pTextSurface NULL Hello 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 lishixinzhi/Article/program/yxkf/201311/11087
很赞哦! (1051)