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 namespace dxf {
00034
00035
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);
00045 dialog.SetCallback(OnGameStateGUIEvent);
00046 return S_OK;
00047 }
00048
00049 void GameStateGUI::Unload() {
00050 dialog.RemoveAllControls();
00051 drm.UnregisterDialog(&dialog);
00052 }
00053
00054 bool GameStateGUI::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
00055 if (drm.MsgProc( hWnd, uMsg, wParam, lParam )) return true;
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
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 }