home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Extras / Sensaura / SDK1.0 / data1.cab / Example_Files / DS3DDemo / Source / cd3drm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-13  |  7.4 KB  |  246 lines

  1. /*
  2.     Company:            Sensaura
  3.     Copyright:          (C) 1998
  4.  
  5.     File Name:            cd3drm.cpp
  6.     File Description:    Source file for implementation of Direct3DRMObject class.
  7.                         This class supports an IDirect3DRM object rendered in a Window.
  8.     Author:                Adam Philp
  9.     File Version:        1.01.000
  10.     Last Update:        02-DEC-98
  11.  
  12.     Target Compiler:    Microsoft Visual C++ Version 5.0
  13. */
  14.  
  15. ///////////////////////    Included files ////////////////////////////////////////////////////////////
  16.  
  17. #include <windows.h>
  18.  
  19. #include "cd3drm.h"                        // Direct3DRMObject class declarations
  20. #include "directx.h"                    // DirectX functions and macros
  21. #include "debug.h"                        // Debugging support macros and functions
  22.  
  23. /////////////////////// Direct3DRMObject class implementation /////////////////////////////////////
  24.  
  25. Direct3DRMObject::Direct3DRMObject(D3DVALUE BackDistance)
  26. {
  27.     LPDIRECT3DRM pD3DRM;
  28.  
  29.     m_pD3DRM = NULL;
  30.     m_pScene = NULL;
  31.     m_pDevice = NULL;
  32.     m_pDDClipper = NULL;
  33.  
  34.     m_Model = D3DCOLOR_RGB;                // Set default values for color model, render quality and
  35.     m_Quality = D3DRMRENDER_GOURAUD;    // back distance
  36.     m_BackDistance = BackDistance;
  37.                                         // Create an IDirect3DRM2 object.  It is created in the
  38.                                         // constructor so that other Direct3DRM objects can be 
  39.                                         // created from it by the application
  40.     TRY_D3DRM(Direct3DRMCreate(&pD3DRM))
  41.     TRY_D3DRM(pD3DRM->QueryInterface(IID_IDirect3DRM2, (void**)&m_pD3DRM))
  42.  
  43. D3DRM_ERROR:
  44.     return;
  45. }
  46.  
  47. Direct3DRMObject::~Direct3DRMObject()
  48. {
  49.     Release();
  50.     RELEASE(m_pD3DRM)
  51. }
  52.  
  53. /*
  54.     Function:        Create
  55.     Description:    Create a windowed IDirect3DRM device and the main (scene) frame
  56.     Parameters:        hwnd - handle of the window to which to clip the Direct3DRM object
  57.     Return value:    true if Direct3DRM object and scene frame were created, false otherwise
  58. */
  59.  
  60. bool Direct3DRMObject::Create(HWND hwnd)
  61. {
  62.     RECT    rect;
  63.     int     bpp;
  64.     HDC     hdc;
  65.  
  66.     ASSERT(m_pD3DRM);                    // IDirect3DRM object is created by the constructor
  67.     ASSERT(!m_pDDClipper);                // All other objects are created here
  68.     ASSERT(!m_pDevice);
  69.     ASSERT(hwnd);
  70.  
  71.     GetClientRect(hwnd, &rect);
  72.                                         // Create a device which is clipped to the specified HWND
  73.     TRY_DD(DirectDrawCreateClipper(0, &m_pDDClipper, NULL))
  74.     TRY_DD(m_pDDClipper->SetHWnd(0, hwnd))
  75.     TRY_D3DRM(m_pD3DRM->CreateDeviceFromClipper(m_pDDClipper,
  76.                                                 FindDirect3DDevice(),
  77.                                                 rect.right, rect.bottom,
  78.                                                 &m_pDevice))
  79.     hdc = GetDC(hwnd);
  80.     bpp = GetDeviceCaps(hdc, BITSPIXEL);
  81.     ReleaseDC(hwnd, hdc);
  82.  
  83.     switch(bpp)                            // Set properties of the device to match the capabilities 
  84.     {                                    // of the display device
  85.     case 8:
  86.         TRY_D3DRM(m_pDevice->SetShades(4))
  87.         TRY_D3DRM(m_pD3DRM->SetDefaultTextureShades(4))
  88.         break;
  89.  
  90.     case 16:
  91.         TRY_D3DRM(m_pDevice->SetShades(32))
  92.         TRY_D3DRM(m_pD3DRM->SetDefaultTextureColors(64))
  93.         TRY_D3DRM(m_pD3DRM->SetDefaultTextureShades(32))
  94.         TRY_D3DRM(m_pDevice->SetDither(FALSE))
  95.         break;
  96.  
  97.     case 24:
  98.     case 32:
  99.         TRY_D3DRM(m_pDevice->SetShades(256))
  100.         TRY_D3DRM(m_pD3DRM->SetDefaultTextureColors(64))
  101.         TRY_D3DRM(m_pD3DRM->SetDefaultTextureShades(256))
  102.         TRY_D3DRM(m_pDevice->SetDither(FALSE))
  103.         break;
  104.  
  105.     default:
  106.         TRY_D3DRM(m_pDevice->SetDither(FALSE))
  107.     }
  108.     TRY_D3DRM(m_pDevice->SetQuality(m_Quality))
  109.                                         // Create the scene frame here
  110.     TRY_D3DRM(m_pD3DRM->CreateFrame(NULL, &m_pScene))
  111.     return true;
  112.  
  113. D3DRM_ERROR:
  114. DD_ERROR:
  115.     Release();
  116.     return false;
  117. }
  118.  
  119. /*
  120.     Function:        Release
  121.     Description:    Release the Direct3DRM scene and Device and the clipper object
  122. */
  123.  
  124. void Direct3DRMObject::Release()
  125. {
  126.     RELEASE(m_pScene)
  127.     RELEASE(m_pDevice)
  128.     RELEASE(m_pDDClipper)
  129. }
  130.  
  131. /*
  132.     Function:        Resize
  133.     Description:    Resize the Direct3DRM device.  Call this function when the window is resized 
  134.                     so that the device is resized to fit in the window.  The device is actually
  135.                     destroyed and a new device created
  136.     Parameters:        cx - the width in pixels of the new device
  137.                     cy - the height in pixels of the new device
  138.     Return value:    true if a new device was successfully created, false otherwise
  139. */
  140.  
  141. bool Direct3DRMObject::Resize(int cx, int cy)
  142. {
  143.     int                 oldDither;
  144.     int                 oldShades;
  145.     D3DRMRENDERQUALITY  oldQuality;
  146.  
  147.     ASSERT(m_pDevice);                    // Make sure we have a device to resize
  148.     oldDither = m_pDevice->GetDither();    // Save the old device properties
  149.     oldQuality = m_pDevice->GetQuality();
  150.     oldShades = m_pDevice->GetShades();
  151.     RELEASE(m_pDevice)                    // Destroy the old device
  152.                                         // Create a new device of the specified size
  153.     TRY_D3DRM(m_pD3DRM->CreateDeviceFromClipper(m_pDDClipper, 
  154.                                                 FindDirect3DDevice(),
  155.                                                 cx, cy, &m_pDevice))
  156.                                         // Restore the device properties
  157.     TRY_D3DRM(m_pDevice->SetDither(oldDither))
  158.     TRY_D3DRM(m_pDevice->SetQuality(oldQuality))
  159.     TRY_D3DRM(m_pDevice->SetShades(oldShades))
  160.     return true;
  161.  
  162. D3DRM_ERROR:
  163.     return false;
  164. }
  165.  
  166. /*
  167.     Function:        FindDirect3DDevice
  168.     Description:    Find a Direct3D device which supports the desired color model
  169.     Return value:    Pointer to the GUID of the Direct3D device
  170. */
  171.  
  172. LPGUID Direct3DRMObject::FindDirect3DDevice()
  173. {
  174.     LPDIRECTDRAW        lpDD;
  175.     LPDIRECT3D          lpD3D;
  176.     D3DFINDDEVICESEARCH devSearch;
  177.     D3DFINDDEVICERESULT devResult;
  178.     HDC                    hdcScreen; // Get the capabilities of the whole screen
  179.     int                 bpp;
  180.     LPGUID              lpGuid;
  181.     HRESULT             hrError;
  182.     DWORD                dwBitDepth;
  183.  
  184.  
  185.     hdcScreen = GetDC (NULL);
  186.     bpp = GetDeviceCaps (hdcScreen, BITSPIXEL);
  187.     ReleaseDC (NULL, hdcScreen);
  188.  
  189.     if (DirectDrawCreate (NULL, &lpDD, NULL))
  190.         return NULL;
  191.  
  192.     if (lpDD->QueryInterface (IID_IDirect3D, (void**) &lpD3D)) {
  193.         lpDD->Release ();
  194.         return NULL;
  195.     }
  196.  
  197.     memset (&devSearch, 0, sizeof (devSearch));
  198.     memset (&devResult, 0, sizeof (devResult));
  199.     devSearch.dwSize = sizeof (devSearch);
  200.     devSearch.dwFlags = D3DFDS_COLORMODEL;
  201.     devSearch.dcmColorModel = m_Model == D3DCOLOR_MONO ? D3DCOLOR_MONO : D3DCOLOR_RGB;
  202.     devResult.dwSize = sizeof (devResult);
  203.  
  204.     hrError = lpD3D->FindDevice (&devSearch, &devResult);
  205.     if (hrError == DD_OK) {
  206.  
  207.     switch (bpp) {
  208.     case 1:
  209.         dwBitDepth = DDBD_1;
  210.     case 2:
  211.         dwBitDepth = DDBD_2;
  212.     case 4:
  213.         dwBitDepth = DDBD_4;
  214.     case 8:
  215.         dwBitDepth = DDBD_8;
  216.     case 16:
  217.         dwBitDepth = DDBD_16;
  218.     case 24:
  219.         dwBitDepth = DDBD_24;
  220.     case 32:
  221.         dwBitDepth = DDBD_32;
  222.     default:
  223.         dwBitDepth = 0;
  224.     } 
  225.  
  226.         if (devResult.ddHwDesc.dwFlags &&
  227.             !(devResult.ddHwDesc.dwDeviceRenderBitDepth & dwBitDepth)) {
  228.  
  229.             devSearch.dwFlags |= D3DFDS_HARDWARE;
  230.             devSearch.bHardware = FALSE;
  231.             memset (&devResult, 0, sizeof (devResult));
  232.             devResult.dwSize = sizeof (devResult);
  233.             hrError = lpD3D->FindDevice (&devSearch, &devResult);
  234.         }
  235.     }
  236.     lpD3D->Release ();
  237.     lpDD->Release ();
  238.  
  239.     if (hrError)                              // If we had an error, return NULL,
  240.         lpGuid = NULL;
  241.     else
  242.         lpGuid = &(devResult.guid);             // Otherwise, return the result
  243.     return lpGuid;
  244. }
  245.  
  246.