熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> 游戲開發 >> 正文

詳細講解如何顯示 Hello DirectX 8

2022-06-13   來源: 游戲開發 

  下面就該用 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 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;        }      }      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|對象庫模塊] 欄內添加下面四個庫文件
  dxguidlib
  ddrawlib
  dxerrlib
  winmmlib
  (文件名之間用半角空格分隔)
  
 educitycn/img_///gif >

   點擊 [C/C++] 標簽設定 include 文件的路徑(參見 § 移動樣品到另外的文件夾)
  
educitycn/img_///gif >

   新建 Common 文件夾並向其中添加下面個文件
  ddutilh
  dxutilh
  ddutilcpp
  dxutilcpp
  (參見 § 移動樣品到另外的文件夾)
  
   編譯並執行!
  源程序
  /******************************************************/
  /*★ 顯示 Hello DirectX !    前田 稔 ★*/
  /******************************************************/
  #define   STRICT
  #include  h>
  #include  h>
  #include  h>
  #include  ddutilh
  
  // 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;
  BOOL    g_bActive   = FALSE;
  
  // Functionprototypes
  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)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;
  }
  }
  else  WaitMessage();
  }
  }
  }
  
  //★ 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
From:http://tw.wingwit.com/Article/program/yxkf/201311/11087.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.