My Documents/dxframework/dxf/dxf-engine/dxf_statemanager.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 namespace dxf {
00034         /*      This is the callback given to the dialog.  This simply calls the OnGUIEvent
00035                 method on the game state (dxf::GUI) interface */
00036         void CALLBACK OnGameStateGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext ) {
00037                 GameState* pCurrentState = Model::Instance()->GetCurrentGameState();
00038                 if (pCurrentState) pCurrentState->OnGUIEvent(nEvent, nControlID, pControl, pUserContext);
00039         }
00040 
00041         CDXUTDialogResourceManager GameStateGUI::drm;
00042 
00043         HRESULT GameStateGUI::Load() {
00044                 dialog.Init(&drm);      // Initialize dialog with resource manager
00045                 dialog.SetCallback(OnGameStateGUIEvent); // Set the dialog callback for GUI events
00046                 return S_OK;
00047         }
00048 
00049         void GameStateGUI::Unload() {
00050                 dialog.RemoveAllControls();// Remove controls from dialog to assist unloading
00051                 drm.UnregisterDialog(&dialog);// Unregister the dialog from the resource manager
00052         }
00053 
00054         bool GameStateGUI::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
00055                 if (drm.MsgProc( hWnd, uMsg, wParam, lParam )) return true;             // if they return true, pass it on.
00056                 if (dialog.MsgProc( hWnd, uMsg, wParam, lParam )) return true;
00057                 return false;
00058         }
00059 
00060         HRESULT GameStateGUI::OnFrameRender(float fElapsedTime) {
00061                 HRESULT hr;
00062                 V_RETURN(dialog.OnRender(fElapsedTime));
00063                 return S_OK;
00064         }
00065 
00066         HRESULT GameStateGUI::OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc) {
00067                 HRESULT hr;
00068                 V_RETURN( drm.OnCreateDevice( pd3dDevice ) );
00069                 return S_OK;
00070         }
00071 
00072         HRESULT GameStateGUI::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc) {
00073                 HRESULT hr;
00074                 V_RETURN(drm.OnResetDevice());
00075                 return S_OK;
00076         }
00077 
00078         void GameStateGUI::OnLostDevice() {
00079                 drm.OnLostDevice();
00080         }
00081 
00082         void GameStateGUI::OnDestroyDevice() {
00083                 drm.OnDestroyDevice();
00084         }
00085 
00086         StateManager::StateManager() {
00087                 pCurrentState = 0;
00088         }
00089 
00090         void StateManager::Load() {
00091                 pCurrentState = 0;
00092         }
00093 
00094         void StateManager::Unload() {
00095                 if (pCurrentState) pCurrentState->Unload();
00096 
00097                 StateMapIterator iter = states.begin();
00098                 while (iter != states.end()) {
00099                         iter = states.erase(iter);
00100                 }
00101         }
00102 
00103         void StateManager::RegisterState(const std::wstring& name, GameState* pState) {
00104                 if (states.find(name) != states.end()) {
00105                         Console::output << L"Warning: overwriting state: " << name << std::endl;
00106                 }
00107                 if (newState.empty()) newState = name;
00108                 states[name] = pState;
00109         }
00110 
00111         void StateManager::ChangeState(const std::wstring& name) {
00112                 newState = name;
00113         }
00114 
00115         bool StateManager::JustChangedState() {
00116                 return newState.size() ? true : false;
00117         }
00118 
00119         void StateManager::Update() {
00120                 HRESULT hr;
00121                 if (newState.size()) {
00122                         // change states
00123                         if (pCurrentState) {
00124                                 pCurrentState->OnLostDevice();
00125                                 pCurrentState->OnDestroyDevice();
00126                                 pCurrentState->Unload();
00127                         }
00128                         assert(states.find(newState) != states.end());
00129                         pCurrentState = states[newState];
00130                         V(pCurrentState->Load());
00131                         V(pCurrentState->OnCreateDevice(DXUTGetD3DDevice(), DXUTGetBackBufferSurfaceDesc()));
00132                         V(pCurrentState->OnResetDevice(DXUTGetD3DDevice(), DXUTGetBackBufferSurfaceDesc()));
00133                         newState = L"";
00134                 }
00135         }
00136 
00137         void StateManager::Resize(const D3DSURFACE_DESC* pBackBufferSurfaceDesc) {
00138                 if (pCurrentState) pCurrentState->Resize(pBackBufferSurfaceDesc);
00139         }
00140 
00141         GameState* StateManager::GetCurrentGameState() {
00142                 return pCurrentState;
00143         }
00144 } // namespace dxf

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