首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
 您现在的位置: 中国IT实验室 >> 游戏开发 >> 游戏开发入门 >> 正文
详细讲解如何显示 Hello DirectX 8
来源:ChinaItLab 作者: 时间:2004-12-26


  下面就该用 DirectX 8 来写字了。(●^o^●)
  MS/DOS 下一行就办了的程序,现在都长成这样。(^_^;
  忍着点儿,慢慢往后学。(f^_^)
  
  跟以前的版本比起来, DirectX 8 还算是托了 Common 文件夹下那4个文件的福,大幅简化了源程序哩。
  程序的执行过程如下:
  
  1. 创建窗口;
  2. 初始化 DirectDraw ;
  3. 创建 Sueface(表面) 储存字符串 "Hello DirectX 8 !" ;
  4. 进入消息循环;
  5. 循环中反复描绘 "Hello DirectX 8 !" ;
  6. 按任意键退出。
  下面是 DirectDraw 的 global 区域。
  Display 和储存字符串的 Sueface(用于描绘的内存区域),
  g_bActive 是 DirectDraw 初始化成功的标志。
  CDisplay*  g_pDisplay  = NULL;
  CSurface*  g_pTextSurface= NULL;
  BOOL    g_bActive   = FALSE;
  
  下面是初始化 DirectDraw 的代码。
  若初始化失败就显示错误信 息并返回;
  创建储存文本的 Sueface(用于描绘的内存区域) 并以点阵图的形式储存 "Hello DirectX 8 !" ;
  RGB(0,0,0)、RGB(255,255,0) 分别是背景色和前景色,R(红)、G(绿)、B(蓝)在 0~255 的范围内分别指定。这里设定背景色为黑色、文字色(前景色)为黄色。
  HRESULT InitDirectDraw(HWND hWnd)
  {  HRESULT       hr;
  
  g_pDisplay = new CDisplay();
  if (FAILED(hr= g_pDisplay->CreateFullScreenDisplay(hWnd, 640, 480, 16)))
  {  ERMSG("This display card does not support 640x480x16.");
  return hr;
  }
  
  // Create a surface, and draw text to it.
  if (FAILED(hr= g_pDisplay->CreateSurfaceFromText(&g_pTextSurface,
  NULL,"Hello DirectX 8 !",RGB(0,0,0),RGB(255,255,0))))
  return hr;
  
  return S_OK;
  }
  
  下面是 Message Loop(消息循环) 。
  用 DisplayFrame() 逐帧描绘。
  while(TRUE)  {  if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))    {  if (0 == GetMessage(&msg, NULL, 0, 0)) 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(0);
  // Blt all the sprites onto the back buffer.
  g_pDisplay->Blt(0, 0, 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;
  }
  
  下面说明创建工程的步骤:
  
  1. 新建一个 Win32 Application 空白工程,命名为 "Hello"。
  
  2. 向工程中新建一个 C++ Source File ,命名为 "hello" ,向其中键入篇末附带的源程序。
  
  3. 选择菜单 [Project|工程]-[Settings...|设定] 打开[Project Settings|工程设定] 面板,点击 [Link|链接] 标签,向 [Object/library modules|对象、库模块] 栏内添加下面四个库文件:
  dxguid.lib
  ddraw.lib
  dxerr8.lib
  winmm.lib
  (文件名之间用半角空格分隔)
  
 

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

  5. 新建 Common 文件夹并向其中添加下面4个文件:
  ddutil.h
  dxutil.h
  ddutil.cpp
  dxutil.cpp
  (参见 §04. 移动样品到另外的文件夹)
  
  6. 编译并执行!
  源程序:
  /******************************************************/
  /*★ 显示 Hello DirectX 8 !   2001-01-02 前田 稔 ★*/
  /******************************************************/
  #define   STRICT
  #include  
  #include  
  #include  
  #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, 0, 0, PM_NOREMOVE))
  {  if (0 == GetMessage(&msg, NULL, 0, 0)) 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 8 !");
  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+1);
  wc.lpszMenuName = NULL;
  wc.cbClsExtra  = 0;
  wc.cbWndExtra  = 0;
  
  if (RegisterClass(&wc)==0)   return E_FAIL;
  
  // Create and show the main window
  hWnd = CreateWindowEx(0, TEXT("Hello DirectX 8 !"), 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, 640, 480, 16)))
  {  ERMSG("This display card does not support 640x480x16.");
  return hr;
  }
  
  // Create a surface, and draw text to it.
  if (FAILED(hr= g_pDisplay->CreateSurfaceFromText(&g_pTextSurface,
  NULL,"Hello DirectX 8 !",RGB(0,0,0),RGB(255,255,0))))
  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, 0, 0);
  return 0L;
  case WM_SIZE:
  // Check to se
【责编:admin】

中国IT教育热线咨询

相关文章
游戏开发新手入门之位图化图形
给希望成为游戏美术设计师的朋友
Ogre游戏引擎鼠标选取物体演示  
所有绘画的核心灵魂——素描知识
DirectDraw与DirectInput游戏编程体验
推荐文章

 精彩友情推荐
·Asp源码 PHP源码
·CGI源码 JSP源码
·建站书籍教程
·服务器软件 .net源码
·建站工具软件
·IDC资讯大全
·机房品质万里行
·IDC托管必备知识
·全国IDC报价
·网站推广优化
 基础入门  开发文档
 最新推荐
  多数的Windows程序都需要Windows.h和Windowsx.h这两个头文件,要确保使用它们。当然,你还需要其它......
游戏引擎演化史
在Windows上安装OGRE的方法
关于滤镜遮罩概念,Sobel 遮罩
游戏开发新手入门之Windows编程
游戏开发新手入门之位图化图形
教你实现卡通渲染的另类勾边方法
游戏设计大师谈如何成为一名游戏设
Visual C#编写 3D游戏框架示例
真正的 Java 学习从入门到精通
游戏开发经验——游戏开发的基本常
  为什么要研究攻击行为在人类有记载的5600年的历史中,共计发生了14,400次战争;今天,平均一天要发生............
游戏开发中显示对话的特殊句法
游戏原型设计的介绍
网络游戏中的攻击行为
谈动作类游戏的必要条件
规则的多元分析模式
载入位图文件到DirectDraw
Archer Game Suite 是什么?
浅谈游戏企划-新手入门篇
暴雪称霸游戏业界的六大秘密绝招
骨骼动画及示例Skinned Mesh的解析
  培训中心