Initializing DirectDraw with the CD3DFramework7 Object

[Previous] [Next]

With everything enumerated, we can now initialize the DirectDraw device that we want to use and create some of its main objects. When using the Direct3D framework, the class CD3DFramework7 is used to manage these main objects: the main window handle, the front and back buffers, and various state information. This chapter uses a slimmed-down version of the CD3DFramework7 class that focuses on DirectDraw and omits some of the Direct3D-related member variables and functions. The full version of CD3DFramework7 (which we'll use in later chapters) also manages the IDirect3D7 interface pointer, the IDirect3DDevice7 interface pointer, and the depth buffer. The slimmed-down CD3DFramework7 class is defined as follows:

 //------------------------------------------------------------------- // Name: CD3DFramework7 // Desc: The Direct3D sample framework class for DirectX7. Maintains  //       the Direct3D surfaces and device used for 3D rendering. //------------------------------------------------------------------- class CD3DFramework7 {     // Internal variables for the framework class     HWND                 m_hWnd;               // The window object     BOOL                 m_bIsFullscreen;      // Full-screen vs.                                                // windowed     BOOL                 m_bIsStereo;          // Stereo view mode     DWORD                m_dwRenderWidth;      // Dimensions of the                                                 // render target     DWORD                m_dwRenderHeight;     RECT                 m_rcScreenRect;       // Screen rectangle                                                // for window     LPDIRECTDRAW7        m_pDD;                // DirectDraw object     LPDIRECTDRAWSURFACE7 m_pddsFrontBuffer;    // Primary surface     LPDIRECTDRAWSURFACE7 m_pddsBackBuffer;     // Back-buffer surface     LPDIRECTDRAWSURFACE7 m_pddsBackBufferLeft; // For stereo modes     DWORD                m_dwDeviceMemType;     DDPIXELFORMAT        m_ddpfBackBufferPixelFormat;     // Internal functions for the framework class     HRESULT CreateFullscreenBuffers( DDSURFACEDESC2* );     HRESULT CreateWindowedBuffers();     HRESULT CreateDirectDraw( GUID*, DWORD );     HRESULT CreateEnvironment( GUID*, GUID*, DDSURFACEDESC2*, DWORD ); public:     // Access functions for DirectX objects     LPDIRECTDRAW7        GetDirectDraw()                                     { return m_pDD; }     LPDIRECTDRAWSURFACE7 GetFrontBuffer()                                           { return m_pddsFrontBuffer; }     LPDIRECTDRAWSURFACE7 GetBackBuffer()                                            { return m_pddsBackBuffer; }     LPDIRECTDRAWSURFACE7 GetRenderSurface()                                     { return m_pddsBackBuffer; }     LPDIRECTDRAWSURFACE7 GetRenderSurfaceLeft()                                     { return m_pddsBackBufferLeft; }     // Dimensions of the render target     DWORD                GetRenderWidth()                                             { return m_dwRenderWidth; }           DWORD                GetRenderHeight()                                             { return m_dwRenderHeight; }           // Functions to aid rendering     HRESULT RestoreSurfaces();     HRESULT ShowFrame();     HRESULT FlipToGDISurface( BOOL bDrawFrame = FALSE );     // Functions for managing screen and viewport bounds     BOOL    IsFullscreen()                { return m_bIsFullscreen; }     BOOL    IsStereo()                    { return m_bIsStereo; }     VOID    Move( INT x, INT y );     // Creates the framework     HRESULT Initialize( HWND hWnd, GUID* pDriverGUID,                          GUID* pDeviceGUID, DDSURFACEDESC2* pddsd,                         DWORD dwFlags );     HRESULT DestroyObjects();             CD3DFramework7();            ~CD3DFramework7(); }; 

After calling D3DEnum_EnumerateDevices and D3DEnum_SelectDefaultDevice, CD3DApplication::Create calls CD3DApplication::Initialize3DEnvironment. This member function initializes the framework and sets up either full-screen or windowed mode. The CD3DApplication::Initialize3DEnvironment function is defined as follows:

 //------------------------------------------------------------------- // Name: Initialize3DEnvironment // Desc: Initializes the sample framework and then calls the  //       application-specific function to initialize device-specific  //       objects. This code is structured to handle any errors that  //       might occur during initialization. //------------------------------------------------------------------- HRESULT CD3DApplication::Initialize3DEnvironment() {     HRESULT hr;     DWORD   dwFrameworkFlags = 0L;     dwFrameworkFlags |=          ( !m_pDeviceInfo->bWindowed ? D3DFW_FULLSCREEN : 0L );     dwFrameworkFlags |=          (  m_pDeviceInfo->bStereo   ? D3DFW_STEREO     : 0L );     dwFrameworkFlags |=          (  m_bAppUseZBuffer         ? D3DFW_ZBUFFER    : 0L );     // Initialize the Direct3D Framework.     if( SUCCEEDED( hr = m_pFramework->Initialize( m_hWnd,                      m_pDeviceInfo->pDriverGUID,                       m_pDeviceInfo->pDeviceGUID,                      &m_pDeviceInfo->ddsdFullscreenMode,                       dwFrameworkFlags ) ) )     {         m_pDD        = m_pFramework->GetDirectDraw();         m_pD3D       = m_pFramework->GetDirect3D();         m_pd3dDevice = m_pFramework->GetD3DDevice();         m_pddsRenderTarget     = m_pFramework->GetRenderSurface();         m_pddsRenderTargetLeft = m_pFramework->GetRenderSurfaceLeft();         m_ddsdRenderTarget.dwSize = sizeof(m_ddsdRenderTarget);         m_pddsRenderTarget->GetSurfaceDesc( &m_ddsdRenderTarget );         // Let the application run its startup code, which creates         // the 3D scene.         if( SUCCEEDED( hr = InitDeviceObjects() ) )             return S_OK;          else         {             DeleteDeviceObjects();             m_pFramework->DestroyObjects();         }     }     // If you get here, the first initialization pass failed. If      // that was with a hardware device, try again using a software     // rasterizer instead.     if( m_pDeviceInfo->bHardware )     {         // Try again with a software rasterizer.         DisplayFrameworkError( hr, MSGWARN_SWITCHEDTOSOFTWARE );         D3DEnum_SelectDefaultDevice( &m_pDeviceInfo,                                      D3DENUM_SOFTWAREONLY );         return Initialize3DEnvironment();     }     return hr; } 

The CD3DFramework7::Initialize member function sets up some framework member variables and then calls CD3DFramework7::CreateEnvironment to create the essential DirectX objects to be used in the game or application.

 //------------------------------------------------------------------- // Name: Initialize // Desc: Creates the internal objects for the framework //------------------------------------------------------------------- HRESULT CD3DFramework7::Initialize( HWND hWnd, GUID* pDriverGUID,                                     GUID* pDeviceGUID,                                      DDSURFACEDESC2* pMode,                                     DWORD dwFlags ) {     HRESULT hr;     // Check parameters. A NULL mode is valid only for windowed modes.     if( ( NULL==hWnd ) || ( NULL==pDeviceGUID ) ||          ( NULL==pMode && (dwFlags&D3DFW_FULLSCREEN) ) )         return E_INVALIDARG;     // Set up state for windowed/full-screen mode.     m_hWnd          = hWnd;     m_bIsStereo     = FALSE;     m_bIsFullscreen = ( dwFlags & D3DFW_FULLSCREEN ) ? TRUE : FALSE;     // Support stereoscopic viewing for full-screen modes that      // support it.     if( ( dwFlags & D3DFW_STEREO ) && ( dwFlags & D3DFW_FULLSCREEN ) )         if( pMode->ddsCaps.dwCaps2 & DDSCAPS2_STEREOSURFACELEFT )             m_bIsStereo = TRUE;     // Create the D3D rendering environment (surfaces, device,      // viewport, and so forth).     if( FAILED( hr = CreateEnvironment( pDriverGUID, pDeviceGUID,                                          pMode, dwFlags ) ) )     {         DestroyObjects();         return hr;     }     return S_OK; } 

The CD3DFramework7::CreateEnvironment member function lets you create the internal objects you'll be defining for your framework. It first chooses the display memory type you've selected. If you're running with a HAL device, choose video memory; otherwise, use system memory. Video memory is faster, but you're stuck with system memory if no accelerator is available on the target system.

You then call the routines to create the main DirectDraw object as well as the full-screen buffers if you're in full-screen mode or the windowed buffers if you're in windowed mode. The full version of CreateEnvironment also creates the Direct3D object, the Direct3D device, and the depth buffer.

 //------------------------------------------------------------------- // Name: CreateEnvironment // Desc: Creates the internal objects for the framework //------------------------------------------------------------------- HRESULT CD3DFramework7::CreateEnvironment( GUID* pDriverGUID,                                             GUID* pDeviceGUID,                                            DDSURFACEDESC2* pMode,                                             DWORD dwFlags ) {     HRESULT hr;     // Select the default memory type, depending on whether the      // device is hardware or software.     if( IsEqualIID( *pDeviceGUID, IID_IDirect3DHALDevice) )         m_dwDeviceMemType = DDSCAPS_VIDEOMEMORY;     else if( IsEqualIID( *pDeviceGUID, IID_IDirect3DTnLHalDevice) )         m_dwDeviceMemType = DDSCAPS_VIDEOMEMORY;     else         m_dwDeviceMemType = DDSCAPS_SYSTEMMEMORY;     // Create the DirectDraw object.     hr = CreateDirectDraw( pDriverGUID, dwFlags );     if( FAILED( hr ) )         return hr;     // Create the front and back buffers, and attach a clipper.     if( dwFlags & D3DFW_FULLSCREEN )         hr = CreateFullscreenBuffers( pMode );     else         hr = CreateWindowedBuffers();     if( FAILED( hr ) )         return hr;     return S_OK; } 

The first call is to the CD3DFramework7::CreateDirectDraw function, which calls DirectDrawCreateEx to create the DirectDraw interface using the IID_IDirectDraw7 interface. CD3DFramework7::CreateDirectDraw also sets the Windows cooperative level. (I'll explain how in the next section.) Finally, because the Direct3D framework doesn't use palettes, you need to verify that you're not running in palettized mode. This routine is defined as follows:

 //------------------------------------------------------------------- // Name: CreateDirectDraw // Desc: Creates the DirectDraw interface //------------------------------------------------------------------- HRESULT CD3DFramework7::CreateDirectDraw( GUID* pDriverGUID,                                            DWORD dwFlags ) {     // Create the DirectDraw interface.     if( FAILED( DirectDrawCreateEx( pDriverGUID, (VOID**)&m_pDD,                                     IID_IDirectDraw7, NULL ) ) )     {         DEBUG_MSG( _T("Could not create DirectDraw") );         return D3DFWERR_NODIRECTDRAW;     }     // Set the Windows cooperative level.     DWORD dwCoopFlags = DDSCL_NORMAL;     if( m_bIsFullscreen )         dwCoopFlags = DDSCL_ALLOWREBOOT|DDSCL_EXCLUSIVE|                           DDSCL_FULLSCREEN;     // By default, the flag is set to allow Direct3D to optimize      // floating-point calculations.     if( 0L == ( dwFlags & D3DFW_NO_FPUSETUP ) )         dwCoopFlags |= DDSCL_FPUSETUP;     if( FAILED( m_pDD->SetCooperativeLevel( m_hWnd, dwCoopFlags ) ) )     {         DEBUG_MSG( _T("Couldn't set coop level") );         return D3DFWERR_COULDNTSETCOOPLEVEL;     }     // Check that the display is not palettized. That case will fail      // because the Direct3D Framework doesn't use palettes.     DDSURFACEDESC2 ddsd;     ddsd.dwSize = sizeof(ddsd);     m_pDD->GetDisplayMode( &ddsd );     if( ddsd.ddpfPixelFormat.dwRGBBitCount <= 8 )         return D3DFWERR_INVALIDMODE;     return S_OK; } 



Inside Direct3D
Inside Direct3D (Dv-Mps Inside)
ISBN: 0735606137
EAN: 2147483647
Year: 1999
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net