home *** CD-ROM | disk | FTP | other *** search
- /*
- Company: Sensaura
- Copyright: (C) 1998
-
- File Name: cd3drm.cpp
- File Description: Source file for implementation of Direct3DRMObject class.
- This class supports an IDirect3DRM object rendered in a Window.
- Author: Adam Philp
- File Version: 1.01.000
- Last Update: 02-DEC-98
-
- Target Compiler: Microsoft Visual C++ Version 5.0
- */
-
- /////////////////////// Included files ////////////////////////////////////////////////////////////
-
- #include <windows.h>
-
- #include "cd3drm.h" // Direct3DRMObject class declarations
- #include "directx.h" // DirectX functions and macros
- #include "debug.h" // Debugging support macros and functions
-
- /////////////////////// Direct3DRMObject class implementation /////////////////////////////////////
-
- Direct3DRMObject::Direct3DRMObject(D3DVALUE BackDistance)
- {
- LPDIRECT3DRM pD3DRM;
-
- m_pD3DRM = NULL;
- m_pScene = NULL;
- m_pDevice = NULL;
- m_pDDClipper = NULL;
-
- m_Model = D3DCOLOR_RGB; // Set default values for color model, render quality and
- m_Quality = D3DRMRENDER_GOURAUD; // back distance
- m_BackDistance = BackDistance;
- // Create an IDirect3DRM2 object. It is created in the
- // constructor so that other Direct3DRM objects can be
- // created from it by the application
- TRY_D3DRM(Direct3DRMCreate(&pD3DRM))
- TRY_D3DRM(pD3DRM->QueryInterface(IID_IDirect3DRM2, (void**)&m_pD3DRM))
-
- D3DRM_ERROR:
- return;
- }
-
- Direct3DRMObject::~Direct3DRMObject()
- {
- Release();
- RELEASE(m_pD3DRM)
- }
-
- /*
- Function: Create
- Description: Create a windowed IDirect3DRM device and the main (scene) frame
- Parameters: hwnd - handle of the window to which to clip the Direct3DRM object
- Return value: true if Direct3DRM object and scene frame were created, false otherwise
- */
-
- bool Direct3DRMObject::Create(HWND hwnd)
- {
- RECT rect;
- int bpp;
- HDC hdc;
-
- ASSERT(m_pD3DRM); // IDirect3DRM object is created by the constructor
- ASSERT(!m_pDDClipper); // All other objects are created here
- ASSERT(!m_pDevice);
- ASSERT(hwnd);
-
- GetClientRect(hwnd, &rect);
- // Create a device which is clipped to the specified HWND
- TRY_DD(DirectDrawCreateClipper(0, &m_pDDClipper, NULL))
- TRY_DD(m_pDDClipper->SetHWnd(0, hwnd))
- TRY_D3DRM(m_pD3DRM->CreateDeviceFromClipper(m_pDDClipper,
- FindDirect3DDevice(),
- rect.right, rect.bottom,
- &m_pDevice))
- hdc = GetDC(hwnd);
- bpp = GetDeviceCaps(hdc, BITSPIXEL);
- ReleaseDC(hwnd, hdc);
-
- switch(bpp) // Set properties of the device to match the capabilities
- { // of the display device
- case 8:
- TRY_D3DRM(m_pDevice->SetShades(4))
- TRY_D3DRM(m_pD3DRM->SetDefaultTextureShades(4))
- break;
-
- case 16:
- TRY_D3DRM(m_pDevice->SetShades(32))
- TRY_D3DRM(m_pD3DRM->SetDefaultTextureColors(64))
- TRY_D3DRM(m_pD3DRM->SetDefaultTextureShades(32))
- TRY_D3DRM(m_pDevice->SetDither(FALSE))
- break;
-
- case 24:
- case 32:
- TRY_D3DRM(m_pDevice->SetShades(256))
- TRY_D3DRM(m_pD3DRM->SetDefaultTextureColors(64))
- TRY_D3DRM(m_pD3DRM->SetDefaultTextureShades(256))
- TRY_D3DRM(m_pDevice->SetDither(FALSE))
- break;
-
- default:
- TRY_D3DRM(m_pDevice->SetDither(FALSE))
- }
- TRY_D3DRM(m_pDevice->SetQuality(m_Quality))
- // Create the scene frame here
- TRY_D3DRM(m_pD3DRM->CreateFrame(NULL, &m_pScene))
- return true;
-
- D3DRM_ERROR:
- DD_ERROR:
- Release();
- return false;
- }
-
- /*
- Function: Release
- Description: Release the Direct3DRM scene and Device and the clipper object
- */
-
- void Direct3DRMObject::Release()
- {
- RELEASE(m_pScene)
- RELEASE(m_pDevice)
- RELEASE(m_pDDClipper)
- }
-
- /*
- Function: Resize
- Description: Resize the Direct3DRM device. Call this function when the window is resized
- so that the device is resized to fit in the window. The device is actually
- destroyed and a new device created
- Parameters: cx - the width in pixels of the new device
- cy - the height in pixels of the new device
- Return value: true if a new device was successfully created, false otherwise
- */
-
- bool Direct3DRMObject::Resize(int cx, int cy)
- {
- int oldDither;
- int oldShades;
- D3DRMRENDERQUALITY oldQuality;
-
- ASSERT(m_pDevice); // Make sure we have a device to resize
- oldDither = m_pDevice->GetDither(); // Save the old device properties
- oldQuality = m_pDevice->GetQuality();
- oldShades = m_pDevice->GetShades();
- RELEASE(m_pDevice) // Destroy the old device
- // Create a new device of the specified size
- TRY_D3DRM(m_pD3DRM->CreateDeviceFromClipper(m_pDDClipper,
- FindDirect3DDevice(),
- cx, cy, &m_pDevice))
- // Restore the device properties
- TRY_D3DRM(m_pDevice->SetDither(oldDither))
- TRY_D3DRM(m_pDevice->SetQuality(oldQuality))
- TRY_D3DRM(m_pDevice->SetShades(oldShades))
- return true;
-
- D3DRM_ERROR:
- return false;
- }
-
- /*
- Function: FindDirect3DDevice
- Description: Find a Direct3D device which supports the desired color model
- Return value: Pointer to the GUID of the Direct3D device
- */
-
- LPGUID Direct3DRMObject::FindDirect3DDevice()
- {
- LPDIRECTDRAW lpDD;
- LPDIRECT3D lpD3D;
- D3DFINDDEVICESEARCH devSearch;
- D3DFINDDEVICERESULT devResult;
- HDC hdcScreen; // Get the capabilities of the whole screen
- int bpp;
- LPGUID lpGuid;
- HRESULT hrError;
- DWORD dwBitDepth;
-
-
- hdcScreen = GetDC (NULL);
- bpp = GetDeviceCaps (hdcScreen, BITSPIXEL);
- ReleaseDC (NULL, hdcScreen);
-
- if (DirectDrawCreate (NULL, &lpDD, NULL))
- return NULL;
-
- if (lpDD->QueryInterface (IID_IDirect3D, (void**) &lpD3D)) {
- lpDD->Release ();
- return NULL;
- }
-
- memset (&devSearch, 0, sizeof (devSearch));
- memset (&devResult, 0, sizeof (devResult));
- devSearch.dwSize = sizeof (devSearch);
- devSearch.dwFlags = D3DFDS_COLORMODEL;
- devSearch.dcmColorModel = m_Model == D3DCOLOR_MONO ? D3DCOLOR_MONO : D3DCOLOR_RGB;
- devResult.dwSize = sizeof (devResult);
-
- hrError = lpD3D->FindDevice (&devSearch, &devResult);
- if (hrError == DD_OK) {
-
- switch (bpp) {
- case 1:
- dwBitDepth = DDBD_1;
- case 2:
- dwBitDepth = DDBD_2;
- case 4:
- dwBitDepth = DDBD_4;
- case 8:
- dwBitDepth = DDBD_8;
- case 16:
- dwBitDepth = DDBD_16;
- case 24:
- dwBitDepth = DDBD_24;
- case 32:
- dwBitDepth = DDBD_32;
- default:
- dwBitDepth = 0;
- }
-
- if (devResult.ddHwDesc.dwFlags &&
- !(devResult.ddHwDesc.dwDeviceRenderBitDepth & dwBitDepth)) {
-
- devSearch.dwFlags |= D3DFDS_HARDWARE;
- devSearch.bHardware = FALSE;
- memset (&devResult, 0, sizeof (devResult));
- devResult.dwSize = sizeof (devResult);
- hrError = lpD3D->FindDevice (&devSearch, &devResult);
- }
- }
- lpD3D->Release ();
- lpDD->Release ();
-
- if (hrError) // If we had an error, return NULL,
- lpGuid = NULL;
- else
- lpGuid = &(devResult.guid); // Otherwise, return the result
- return lpGuid;
- }
-
-