⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zfxd3d_init.cpp

📁 This is a book introduce some tech about Game Engine 3D
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// File: ZFXD3D_init.cpp

#include <windows.h>       // type definitions
#include <vfw.h>           // show bmp's
#include "resource.h"      // control id's
#include "ZFX.h"           // return values and stuff
#include "ZFXD3D.h"        // class definition

#pragma comment(lib, "vfw32.lib")
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "dxguid.lib")


// we need this for dialog callback only
ZFXD3D *g_ZFXD3D=NULL;

// store result when dialog is closed
ZFXDEVICEINFO  g_xDevice;
D3DDISPLAYMODE g_Dspmd;
D3DFORMAT      g_fmtA;
D3DFORMAT      g_fmtB;
HBITMAP        g_hBMP;



/*-----------------------------------------------------------*/
/* DLL stuff implementation                                  *
/*-----------------------------------------------------------*/

/**
 * DLL Entry Point similar to WinMain()/main()
 */
BOOL WINAPI DllEntryPoint(HINSTANCE hDll,
                          DWORD     fdwReason,
                          LPVOID    lpvReserved) {
   switch(fdwReason) {
      // called when we attach to the DLL
      case DLL_PROCESS_ATTACH:
         /* dll init/setup stuff */
         break;
      case DLL_PROCESS_DETACH:
         /* dll shutdown/release stuff */
         break;
      default:
         break;
      };
   
   return TRUE;
   } // DllEntryPoint
/*----------------------------------------------------------------*/

/**
 * Exported create function: Creates a new ZFXRenderDevice object.
 */
HRESULT CreateRenderDevice(HINSTANCE hDLL, ZFXRenderDevice **pDevice) {
   if(!*pDevice) {
      *pDevice = new ZFXD3D(hDLL);
      return ZFX_OK;
      }
   return ZFX_FAIL;
   }
/*----------------------------------------------------------------*/

/**
 * Exported release function: Realeses the given ZFXRenderDevice object.
 */
HRESULT ReleaseRenderDevice(ZFXRenderDevice **pDevice) {
   if(!*pDevice) {
      return ZFX_FAIL;
      }
   delete *pDevice;
   *pDevice = NULL;
   return ZFX_OK;
   }
/*----------------------------------------------------------------*/



// give helping hand to class implementation of callback function
BOOL CALLBACK DlgProcWrap(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
   { return g_ZFXD3D->DlgProc(hDlg, message, wParam, lParam); }
/*----------------------------------------------------------------*/



/*-----------------------------------------------------------*/
/* ZFXD3D class implementation                               *
/*-----------------------------------------------------------*/

/**
 * Constructor
 */
ZFXD3D::ZFXD3D(HINSTANCE hDLL) {
   m_hDLL              = hDLL;
   m_pEnum             = NULL;
   m_pD3D              = NULL;
   m_pDevice           = NULL;
   m_pLog              = NULL;
   m_ClearColor        = D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f);
   m_bRunning          = false;
   m_bIsSceneRunning   = false;

   // dont use swapchain at first
   m_nActivehWnd       = 0;

   g_ZFXD3D = this;
   }
/*----------------------------------------------------------------*/


/**
 * Destructor
 */ 
ZFXD3D::~ZFXD3D() {
   Log("shutting down direct3d \n");
   if (m_pLog) fflush(m_pLog);
   this->Release();
   }
/*----------------------------------------------------------------*/


/**
 * Release all the Direct3D COM stuff.
 */
void ZFXD3D::Release() {
   if (m_pEnum) {
      delete m_pEnum;
      m_pEnum = NULL;
      }
   if(m_pDevice) {
      m_pDevice->Release();
      m_pDevice = NULL;
      }
   if(m_pD3D) {
      m_pD3D->Release();
      m_pD3D = NULL;
      }
   Log("shutdown completed \n");
   fclose(m_pLog);
   }
/*----------------------------------------------------------------*/


/**
 * Initialize dialogbox to select device and format. Dialog must
 * have ID using -"- signs and must have combobox "IDC_ADAPTER",
 * listbox "IDC_MODES", radiobuttons "IDC_FULL" and "IDC_WND",
 * and Buttons "IDOK" and "IDCANCEL".
 * -> IN: HWND      - handle of application window
 *        HWND      - array to render child-window HWNDs or NULL
 *        int       - number of child HWNDs in array
 *        int       - minimum number of depth bits
 *        int       - minimum number of stencil bits
 *        bool      - create a save but slow log?
 */
HRESULT ZFXD3D::Init(HWND hWnd, const HWND *hWnd3D, 
                     int nNumhWnd, int nMinDepth, 
                     int nMinStencil, bool bSaveLog) {
   int nResult;

   m_pLog = fopen("log_renderdevice.txt", "w");
   if (!m_pLog) return ZFX_FAIL;
   
   // are there any child windows to use?
   if (nNumhWnd > 0) {
      if (nNumhWnd > MAX_3DHWND) nNumhWnd = MAX_3DHWND;
      memcpy(&m_hWnd[0], hWnd3D, sizeof(HWND)*nNumhWnd);
      m_nNumhWnd = nNumhWnd;
      }
   // else store handle to main window at least
   else {
      m_hWnd[0] = hWnd;
      m_nNumhWnd = 0;
      }
   m_hWndMain = hWnd;

   if (nMinStencil > 0)
      m_bStencil = true;

   Log("ZFXEngine ZFXD3D-RenderDevice Log File:\n\n");

   // create enum object
   m_pEnum = new ZFXD3DEnum(nMinDepth, nMinStencil);

   Log("calling dialog... \n");

   // load bmp logo
   g_hBMP = (HBITMAP)LoadImage(NULL, "zfx.bmp",
                               IMAGE_BITMAP,0,0,
                               LR_LOADFROMFILE |
                               LR_CREATEDIBSECTION);

   // call device selection dialog
   nResult = DialogBox(m_hDLL, "dlgChangeDevice", hWnd, DlgProcWrap);
   
   // delete bmp if any
   if (g_hBMP) DeleteObject(g_hBMP);

   Log("returning from dialog... \n");

   // dialog failed somehow
   if (nResult == -1) {
      Log("selection dialog error \n");
      return ZFX_FAIL;
      }
   // dialog was canceled by user
   else if (nResult == 0) {
      Log("selection dialog canceled by user\n");
      return ZFX_CANCELED;
      }
   // dialog ended with ok button
   else {
      Log("selection dialog ok\nfiring up direct3d \n");
      return Go();
      }
   }
/*----------------------------------------------------------------*/


/**
 * -> IN: HWND      - handle of application window
 *        HWND      - array to render child-window HWNDs or NULL
 *        int       - number of child HWNDs in array
 *        bool      - create a save but slow log?
 */
HRESULT ZFXD3D::InitWindowed(HWND hWnd, const HWND *hWnd3D, 
                             int nNumhWnd, bool bSaveLog) {
   HRESULT hr;
   HWND    hwnd;
   
   // are there any child windows to use?
   if (nNumhWnd > 0) {
      if (nNumhWnd > MAX_3DHWND) nNumhWnd = MAX_3DHWND;
      memcpy(&m_hWnd[0], hWnd3D, sizeof(HWND)*nNumhWnd);
      m_nNumhWnd = nNumhWnd;
      }
   // else store handle to main window at least
   else {
      m_hWnd[0] = hWnd;
      m_nNumhWnd = 0;
      }
 
   m_hWndMain  = hWnd;
   m_bWindowed = true;

   // build main direct3d object
   if (m_pD3D) {
      m_pD3D->Release();
      m_pD3D = NULL;
      }
   m_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
   if(!m_pD3D) {
      Log("error: Direct3DCreate8()");
      return ZFX_CREATEAPI;
      }

   // prepare present parameters structure
   ZeroMemory(&m_d3dpp, sizeof(m_d3dpp));
   m_d3dpp.Windowed               = m_bWindowed;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -