00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #if !defined(AFX_GAMESTATE_H__00A5F6B7_02B2_45B3_9D9B_F8B0AC9F5614__INCLUDED_)
00022 #define AFX_GAMESTATE_H__00A5F6B7_02B2_45B3_9D9B_F8B0AC9F5614__INCLUDED_
00023
00024 #include <string>
00025 #include <map>
00026 #include <list>
00027 #include <engine/MainLoopI.h>
00028 #include <common/Clock.h>
00029
00030 class GameStateI;
00031 class GameStateStimulusI;
00032 class GameState;
00033
00034 #define GAMESTATE_PERF_COUNTER_START(x, y) { static int __counter__ = x.getPerfCounter(y); x.startPerfCount(__counter__); }
00035 #define GAMESTATE_PERF_COUNTER_END(x, y) { static int __counter__ = x.getPerfCounter(y); x.endPerfCount(__counter__); }
00036
00037 class GameStatePerfCounter
00038 {
00039 public:
00040 GameStatePerfCounter(const char *name);
00041 ~GameStatePerfCounter();
00042
00043 void start();
00044 void end();
00045
00046 const char *getName() { return name_.c_str(); }
00047 unsigned int getTotal();
00048 bool getUsed() { return used_; }
00049
00050 protected:
00051 bool used_;
00052 std::string name_;
00053 unsigned int start_;
00054 unsigned int total_;
00055 };
00056
00057 class GameState : public MainLoopI
00058 {
00059 public:
00060 enum MouseButton
00061 {
00062 MouseButtonLeft = 0x1,
00063 MouseButtonMiddle = 0x2,
00064 MouseButtonRight = 0x4,
00065 MouseButtonLeftDoubleClick = 0x8,
00066 MouseButtonMiddleDoubleClick = 0x16,
00067 MouseButtonRightDoubleClick = 0x32
00068 };
00069
00070 GameState(const char *name);
00071 virtual ~GameState();
00072
00073
00074 virtual void simulate(float simTime);
00075 virtual void draw();
00076
00077 bool checkStimulate();
00078 void clear();
00079
00080
00081 void mouseDown(MouseButton button, int x, int y);
00082 void mouseUp(MouseButton button, int x, int y);
00083 void mouseMove(int x, int y);
00084 void mouseWheel(short z);
00085
00086
00087 void setState(const unsigned state);
00088 void stimulate(const unsigned stimulus);
00089 void setFakeMiddleButton(bool fake);
00090 const unsigned getState() { return currentState_; }
00091 int getMouseX() { return currentMouseX_; }
00092 int getMouseY() { return currentMouseY_; }
00093 bool &getStateLogging() { return stateLogging_; }
00094 float &getStateTimeLogging() { return stateTimeLogging_; }
00095
00096 int getPerfCounter(const char *name);
00097 void startPerfCount(int counter);
00098 void endPerfCount(int counter);
00099
00100
00101 void addStateStimulus(const unsigned state,
00102 const unsigned stim,
00103 const unsigned nexts);
00104 void addStateStimulus(const unsigned state,
00105 GameStateStimulusI *check,
00106 const unsigned nexts);
00107 void addStateEntry(const unsigned state,
00108 GameStateI *entry);
00109 void addStateLoop(const unsigned state,
00110 GameStateI *entry,
00111 GameStateI *subEntry);
00112 void addStateKeyEntry(const unsigned state,
00113 GameStateI *subEntry);
00114 void addStateMouseDownEntry(const unsigned state,
00115 const unsigned buttons,
00116 GameStateI *subEntry);
00117 void addStateMouseUpEntry(const unsigned state,
00118 const unsigned buttons,
00119 GameStateI *subEntry);
00120 void addStateMouseDragEntry(const unsigned state,
00121 const unsigned buttons,
00122 GameStateI *subEntry);
00123 void addStateMouseWheelEntry(const unsigned state,
00124 GameStateI *subEntry);
00125
00126 protected:
00127 typedef std::list<GameStateI *> StateIList;
00128 typedef std::pair<GameStateStimulusI *, unsigned> SimulusIPair;
00129 typedef std::list<SimulusIPair> StiulusIList;
00130
00131 struct TimerInfo
00132 {
00133 GameStateI *gameStateI;
00134 unsigned int drawTime;
00135 unsigned int simulateTime;
00136 };
00137
00138 struct GameStateSubEntry
00139 {
00140 GameStateI *current;
00141 StateIList subLoopList;
00142 };
00143
00144 struct GameStateEntry
00145 {
00146
00147 std::list<GameStateSubEntry> loopList;
00148
00149
00150 std::map<unsigned, unsigned> stimList;
00151
00152 StiulusIList condStimList;
00153
00154
00155 StateIList subKeyList;
00156
00157 StateIList subMouseDownLeftList;
00158
00159 StateIList subMouseDownMiddleList;
00160
00161 StateIList subMouseDownRightList;
00162
00163 StateIList subMouseUpLeftList;
00164
00165 StateIList subMouseUpMiddleList;
00166
00167 StateIList subMouseUpRightList;
00168
00169 StateIList subMouseDragLeftList;
00170
00171 StateIList subMouseDragMiddleList;
00172
00173 StateIList subMouseDragRightList;
00174
00175 StateIList subMouseWheelList;
00176
00177 StateIList enterStateList;
00178 };
00179
00180 unsigned currentState_;
00181 GameStateI *currentStateI_;
00182 GameStateEntry *currentEntry_;
00183 std::map<unsigned, GameStateEntry> stateList_;
00184 std::string name_;
00185 unsigned pendingStimulus_;
00186 bool fakeMiddleButton_;
00187 bool stateLogging_;
00188 float stateTimeLogging_;
00189 int frameCount_;
00190 Clock timerClock_;
00191 Clock overallTimerClock_;
00192 Clock doubleClickClock_;
00193 TimerInfo timers_[50];
00194
00195
00196
00197 unsigned currentMouseState_;
00198 int mouseLDragX_, mouseLDragY_;
00199 int mouseMDragX_, mouseMDragY_;
00200 int mouseRDragX_, mouseRDragY_;
00201 int mouseDoubleX_, mouseDoubleY_;
00202 int currentMouseX_, currentMouseY_;
00203
00204 void mouseMoveCall(const unsigned state, MouseButton button,
00205 StateIList ¤tList,
00206 int mx, int my,
00207 int dx, int dy);
00208 void mouseUpDown(MouseButton button, bool down, int x, int y);
00209 GameState::GameStateEntry*
00210 getEntry(const unsigned state);
00211 GameState::GameStateSubEntry*
00212 getSubEntry(const unsigned state, GameStateI *entry);
00213 void clearTimers(bool printTimers = false);
00214
00215 };
00216
00217 #endif // !defined(AFX_GAMESTATE_H__00A5F6B7_02B2_45B3_9D9B_F8B0AC9F5614__INCLUDED_)