00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
00043 if (dxf::View::Instance()->Ready()) dxf::View::Instance()->OnLostDevice(pUserContext);
00044 }
00045
00046 void CALLBACK OnDestroyDevice( void* pUserContext ){
00047
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
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
00126
00127 if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
00128 pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) )
00129 {
00130 pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
00131 }
00132
00133
00134
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
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
00214 if (clear) {
00215 V( pd3dDevice->Clear(1, &clearRect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clearColor, 1.0f, 0) );
00216 }
00217
00218
00219 if( SUCCEEDED( pd3dDevice->BeginScene() ) ) {
00220 pd3dDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE);
00221
00222
00223 V(pd3dxSprite->Begin(D3DXSPRITE_ALPHABLEND));
00224 DXFRenderPre2D(fTime, fElapsedTime);
00225 V(pd3dxSprite->End());
00226
00227
00228 pd3dDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
00229 DXFRender3D(fTime, fElapsedTime);
00230
00231
00232 V(pd3dxSprite->Begin(D3DXSPRITE_ALPHABLEND));
00233 DXFRender2D(fTime, fElapsedTime);
00234 V(pd3dxSprite->End());
00235
00236
00237 GameState* pCurrentState = Model::Instance()->GetCurrentGameState();
00238 if (pCurrentState) V(pCurrentState->OnFrameRender(fElapsedTime));
00239
00240
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 }