My Documents/dxframework/dxf/dxf-engine/dxf_view.cpp

Go to the documentation of this file.
00001 /*      DXFramework Copyright (c) 2006, Jonathan Voigt, University of Michigan.
00002         See http://dxframework.sourceforge.net/ for a list of contributors.
00003         All rights reserved.
00004 
00005         Redistribution and use in source and binary forms, with or without modification, 
00006         are permitted provided that the following conditions are met:
00007 
00008                 * Redistributions of source code must retain the above copyright notice, 
00009                 this list of conditions and the following disclaimer.
00010 
00011                 * Redistributions in binary form must reproduce the above copyright notice, 
00012                 this list of conditions and the following disclaimer in the documentation 
00013                 and/or other materials provided with the distribution.
00014 
00015                 * Neither the name of the DXFramework project nor the names of its 
00016                 contributors may be used to endorse or promote products derived from this 
00017                 software without specific prior written permission.
00018 
00019         THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
00020         ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
00021         WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00022         DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
00023         ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00024         (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00025         LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
00026         ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00027         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00028         SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 */
00030 
00031 #include "dxframework.h"
00032 
00033 HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) {
00034         return dxf::View::Instance()->OnCreateDevice(pd3dDevice, pBackBufferSurfaceDesc, pUserContext);
00035 }
00036 
00037 HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) {
00038         return dxf::View::Instance()->OnResetDevice(pd3dDevice, pBackBufferSurfaceDesc, pUserContext);
00039 }
00040 
00041 void CALLBACK OnLostDevice( void* pUserContext ) {
00042         // FIXME this is invalid
00043         if (dxf::View::Instance()->Ready()) dxf::View::Instance()->OnLostDevice(pUserContext);
00044 }
00045 
00046 void CALLBACK OnDestroyDevice( void* pUserContext ){
00047         // FIXME this is invalid
00048         if (dxf::View::Instance()->Ready()) dxf::View::Instance()->OnDestroyDevice(pUserContext);
00049 }
00050 
00051 void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) {
00052         dxf::View::Instance()->OnFrameRender(pd3dDevice, fTime, fElapsedTime, pUserContext);
00053 }
00054 
00055 bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
00056                                   D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
00057 {
00058     // Skip backbuffer formats that don't support alpha blending
00059     IDirect3D9* pD3D = DXUTGetD3DObject(); 
00060     if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
00061                     AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, 
00062                     D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
00063 
00064         return false;
00065 
00066     return true;
00067 }
00068 
00069 bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext ) {
00070         return dxf::View::Instance()->ModifyDeviceSettings(pDeviceSettings, pCaps, pUserContext);
00071 }
00072 
00073 namespace dxf {
00074         View::View() {
00075                 pd3dxSprite = 0;
00076                 ready = false;
00077         }
00078 
00079         View* const View::Instance() {
00080                 static View instance;
00081                 return &instance;
00082         }
00083 
00084         bool View::Ready() {
00085                 return ready;
00086         }
00087 
00088         void View::Load(int initialWidth, int initialHeight, bool forceResolution, bool startWindowed) {
00089 
00090                 this->initialWidth = initialWidth;
00091                 this->initialHeight = initialHeight;
00092                 this->forceResolution = forceResolution;
00093                 this->startWindowed = startWindowed;
00094 
00095                 clear = true;
00096                 clearRect.x1 = 0;
00097                 clearRect.x2 = initialWidth;
00098                 clearRect.y1 = 0;
00099                 clearRect.y2 = initialHeight;
00100                 clearColor = BLACK;
00101 
00102                 DXUTSetCallbackDeviceCreated( ::OnCreateDevice );
00103                 DXUTSetCallbackDeviceReset( ::OnResetDevice );
00104                 DXUTSetCallbackDeviceLost( ::OnLostDevice );
00105                 DXUTSetCallbackDeviceDestroyed( ::OnDestroyDevice );
00106                 DXUTSetCallbackFrameRender( ::OnFrameRender );
00107         }
00108 
00109         HRESULT View::Init(const wchar_t* windowName) {
00110                 HRESULT hr;
00111                 V_RETURN(DXUTCreateWindow(windowName));
00112                 DXUTGetEnumeration()->SetRequirePostPixelShaderBlending(false);
00113                 V_RETURN(DXUTCreateDevice( D3DADAPTER_DEFAULT, startWindowed, initialWidth, initialHeight, IsDeviceAcceptable, ::ModifyDeviceSettings));
00114         
00115                 ready = true;
00116 
00117                 return S_OK;
00118         }
00119 
00120         void View::Unload() {
00121                 SAFE_RELEASE(pd3dxSprite);
00122         }
00123 
00124         bool View::ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext ) {
00125                 // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW 
00126                 // then switch to SWVP.
00127                 if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
00128                         pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) )
00129                 {
00130                         pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
00131                 }
00132 
00133                 // Debugging vertex shaders requires either REF or software vertex processing 
00134                 // and debugging pixel shaders requires REF.  
00135 #ifdef DEBUG_VS
00136                 if( pDeviceSettings->DeviceType != D3DDEVTYPE_REF )
00137                 {
00138                         pDeviceSettings->BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
00139                         pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE;                            
00140                         pDeviceSettings->BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
00141                 }
00142 #endif
00143 #ifdef DEBUG_PS
00144                 pDeviceSettings->DeviceType = D3DDEVTYPE_REF;
00145 #endif
00146                 // For the first device created if its a REF device, optionally display a warning dialog box
00147                 static bool s_bFirstTime = true;
00148                 if( s_bFirstTime )
00149                 {
00150                         s_bFirstTime = false;
00151                         if( pDeviceSettings->DeviceType == D3DDEVTYPE_REF )
00152                                 DXUTDisplaySwitchingToREFWarning();
00153                 }
00154 
00155                 if (forceResolution) {
00156                         pDeviceSettings->pp.BackBufferWidth = initialWidth;
00157                         pDeviceSettings->pp.BackBufferHeight = initialHeight;
00158                 }
00159 
00160                 return true;
00161         }
00162 
00163         HRESULT View::OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) {
00164                 HRESULT hr;
00165                 
00166                 GameState* pCurrentState = Model::Instance()->GetCurrentGameState();
00167                 if (pCurrentState) V_RETURN(pCurrentState->OnCreateDevice(pd3dDevice, pBackBufferSurfaceDesc));
00168                 V_RETURN(Font::OnCreateDevice());
00169                 V_RETURN(Texture::OnCreateDevice());
00170 
00171                 V_RETURN(D3DXCreateSprite(DXUTGetD3DDevice(), &pd3dxSprite));
00172                 return S_OK;
00173         }
00174 
00175         HRESULT View::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) {
00176                 HRESULT hr;
00177 
00178                 D3DRECT r;
00179                 r.x1 = 0;
00180                 r.x2 = pBackBufferSurfaceDesc->Width;
00181                 r.y1 = 0;
00182                 r.y2 = pBackBufferSurfaceDesc->Height;
00183                 SetClearRect(r);
00184 
00185                 GameState* pCurrentState = Model::Instance()->GetCurrentGameState();
00186                 if (pCurrentState) V_RETURN(pCurrentState->OnResetDevice(pd3dDevice, pBackBufferSurfaceDesc));
00187                 V_RETURN(pd3dxSprite->OnResetDevice());
00188                 V_RETURN(Font::OnResetDevice());
00189                 if (Console::Instance()->Ready()) Console::Instance()->OnResetDevice(pBackBufferSurfaceDesc);
00190                 Model::Instance()->Resize(pBackBufferSurfaceDesc);
00191                 return S_OK;
00192         }
00193 
00194         void View::OnLostDevice( void* pUserContext ) {
00195                 GameState* pCurrentState = Model::Instance()->GetCurrentGameState();
00196                 if (pCurrentState) pCurrentState->OnLostDevice();
00197                 if (pd3dxSprite) pd3dxSprite->OnLostDevice();
00198                 Font::OnLostDevice();
00199         }
00200 
00201         void View::OnDestroyDevice( void* pUserContext ) {
00202                 GameState* pCurrentState = Model::Instance()->GetCurrentGameState();
00203                 if (pCurrentState) pCurrentState->OnDestroyDevice();
00204                 Font::OnDestroyDevice();
00205                 Texture::OnDestroyDevice();
00206 
00207                 SAFE_RELEASE(pd3dxSprite);
00208         }
00209 
00210         void View::OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) {
00211                 HRESULT hr;
00212 
00213                 // Clear the render target and the zbuffer 
00214                 if (clear) {
00215                         V( pd3dDevice->Clear(1, &clearRect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clearColor, 1.0f, 0) );
00216                 }
00217 
00218                 // Render the scene
00219                 if( SUCCEEDED( pd3dDevice->BeginScene() ) ) {
00220                         pd3dDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE);
00221 
00222                         // Background 2D
00223                         V(pd3dxSprite->Begin(D3DXSPRITE_ALPHABLEND));
00224                         DXFRenderPre2D(fTime, fElapsedTime);
00225                         V(pd3dxSprite->End());
00226 
00227                         // 3D
00228                         pd3dDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
00229                         DXFRender3D(fTime, fElapsedTime);
00230 
00231                         // Foreground 2D
00232                         V(pd3dxSprite->Begin(D3DXSPRITE_ALPHABLEND));   
00233                         DXFRender2D(fTime, fElapsedTime);
00234                         V(pd3dxSprite->End());
00235 
00236                         // GUI
00237                         GameState* pCurrentState = Model::Instance()->GetCurrentGameState();
00238                         if (pCurrentState) V(pCurrentState->OnFrameRender(fElapsedTime));
00239 
00240                         // Console
00241                         V(pd3dxSprite->Begin(D3DXSPRITE_ALPHABLEND));
00242                         DXFRenderConsole();
00243                         V(pd3dxSprite->End());
00244 
00245                         V(pd3dDevice->EndScene());
00246                 }
00247         }
00248 
00249         void View::SetClear(bool clear) { 
00250                 this->clear = clear; 
00251         }
00252 
00253         void View::SetClearColor(D3DCOLOR clearColor) { this->clearColor = clearColor; }
00254         void View::SetClearRect(const D3DRECT& clearRect) { this->clearRect = clearRect; }
00255         ID3DXSprite* View::GetD3DXSprite() { return pd3dxSprite; }
00256         ID3DXSprite* DXFGetD3DXSprite() { return View::Instance()->GetD3DXSprite(); }
00257         void DXFSetClear(bool clearEachFrame) { View::Instance()->SetClear(clearEachFrame); }
00258         void DXFSetClearColor(D3DCOLOR clearColor) { View::Instance()->SetClearColor(clearColor); }
00259         void DXFSetClearRect(const D3DRECT& clearRect) { View::Instance()->SetClearRect(clearRect); }
00260 } // namespace dxf

Generated on Fri Aug 18 12:01:27 2006 for DXFramework by  doxygen 1.4.7