00001 #include "AppInterface.h" 00002 #include "RakAssert.h" 00003 #include "FSM.h" 00004 #include "RunnableState.h" 00005 00006 #ifdef _CONSOLE 00007 #include <stdio.h> 00008 #include <string.h> 00009 #include <stdarg.h> 00010 00011 #if defined(__GNUC__) 00012 #define _vsnprintf vsnprintf 00013 #endif 00014 #endif 00015 00016 AppInterface::AppInterface() 00017 { 00018 hasFocus=false; 00019 primaryFSM=0; 00020 primaryStates=0; 00021 quit=false; 00022 primaryStatesLength=0; 00023 lastElapsedTimeMS=0; 00024 lastCurTimeMS=0; 00025 } 00026 AppInterface::~AppInterface() 00027 { 00028 00029 00030 } 00031 void AppInterface::PreConfigure(void) 00032 { 00033 primaryFSM = new FSM; 00034 } 00035 void AppInterface::PostConfigure(const char *defaultResourceConfigurationPath) 00036 { 00037 } 00038 void AppInterface::Update(AppTime curTimeMS,AppTime elapsedTimeMS) 00039 { 00040 lastCurTimeMS=curTimeMS; 00041 lastElapsedTimeMS=elapsedTimeMS; 00042 } 00043 void AppInterface::OnAppShutdown(void) 00044 { 00045 delete primaryFSM; 00046 if (primaryStates) 00047 { 00048 int i; 00049 for (i=0; i < primaryStatesLength; i++) 00050 delete primaryStates[i]; 00051 delete [] primaryStates; 00052 } 00053 } 00054 void AppInterface::DebugOut(unsigned int lifetimeMS, const char *format, ...) 00055 { 00056 #ifdef _CONSOLE 00057 char text[8096]; 00058 va_list ap; 00059 va_start(ap, format); 00060 _vsnprintf(text, 8096-1, format, ap); 00061 va_end(ap); 00062 strcat(text, "\n"); 00063 text[8096-1]=0; 00064 printf(text); 00065 #else 00066 // Don't call this without an implementation. Perhaps you meant to use MainApp() instead 00067 RakAssert(0); 00068 #endif 00069 } 00070 bool AppInterface::HasFocus(void) const 00071 { 00072 return hasFocus; 00073 } 00074 void AppInterface::SetFocus(bool hasFocus) 00075 { 00076 if (this->hasFocus!=hasFocus) 00077 { 00078 if (primaryFSM->CurrentState()) 00079 ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(hasFocus); 00080 this->hasFocus=hasFocus; 00081 } 00082 } 00083 void AppInterface::PushState(RunnableState* state) 00084 { 00085 if (hasFocus && primaryFSM->CurrentState()) 00086 ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(false); 00087 primaryFSM->AddState(state); 00088 if (hasFocus) 00089 ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(true); 00090 else 00091 ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(false); 00092 } 00093 void AppInterface::PushState(int stateType) 00094 { 00095 PushState(primaryStates[stateType]); 00096 } 00097 void AppInterface::PopState(int popCount) 00098 { 00099 RakAssert(popCount>=1); 00100 RakAssert(primaryFSM->GetStateHistorySize()>=1+popCount); 00101 if (hasFocus && primaryFSM->CurrentState()) 00102 ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(false); 00103 primaryFSM->SetPriorState(primaryFSM->GetStateHistorySize()-1-popCount); 00104 if (hasFocus) 00105 ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(true); 00106 } 00107 RunnableState* AppInterface::GetCurrentState(void) const 00108 { 00109 return (RunnableState*) primaryFSM->CurrentState(); 00110 } 00111 int AppInterface::GetStateHistorySize(void) const 00112 { 00113 return primaryFSM->GetStateHistorySize(); 00114 } 00115 void AppInterface::AllocateStates(int numStates) 00116 { 00117 RakAssert(primaryStates==0); 00118 primaryStates = new RunnableState*[numStates]; 00119 00120 primaryStatesLength=numStates; 00121 } 00122 void AppInterface::SetState(int stateType, RunnableState* state) 00123 { 00124 primaryStates[stateType] = state; 00125 state->SetParentApp(this); 00126 state->OnStateReady(); 00127 } 00128 RunnableState* AppInterface::GetState(int stateType) const 00129 { 00130 return primaryStates[stateType]; 00131 } 00132 bool AppInterface::ShouldQuit(void) const 00133 { 00134 return quit; 00135 } 00136 void AppInterface::Quit(void) 00137 { 00138 quit=true; 00139 } 00140 AppTime AppInterface::GetLastCurrentTime(void) const 00141 { 00142 return lastCurTimeMS; 00143 } 00144 AppTime AppInterface::GetLastElapsedTime(void) const 00145 { 00146 return lastElapsedTimeMS; 00147 }
1.7.1