GameState.h

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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         // Called by the simulator
00074         virtual void simulate(float simTime);
00075         virtual void draw();
00076 
00077         bool checkStimulate();
00078         void clear();
00079 
00080         // Called by SDL subsystem 
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         // User fns to change + set state
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         // User fns to add classes to state management
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                 // Classes called for every loop
00147                 std::list<GameStateSubEntry> loopList; 
00148 
00149                  // Possible stimuli in this state
00150                 std::map<unsigned, unsigned> stimList;
00151                 // Stimili checked every loop
00152                 StiulusIList condStimList; 
00153 
00154                 // Classes called on key events
00155                 StateIList subKeyList; 
00156                 // Classes called on mouse down L
00157                 StateIList subMouseDownLeftList; 
00158                 // Classes called on mouse down M
00159                 StateIList subMouseDownMiddleList; 
00160                 // Classes called on mouse down R
00161                 StateIList subMouseDownRightList;
00162                 // Classes called on mouse up L
00163                 StateIList subMouseUpLeftList;
00164                 // Classes called on mouse up M
00165                 StateIList subMouseUpMiddleList; 
00166                 // Classes called on mouse up R
00167                 StateIList subMouseUpRightList;
00168                 // Classes called on mouse drag L
00169                 StateIList subMouseDragLeftList; 
00170                 // Classes called on mouse drag M
00171                 StateIList subMouseDragMiddleList;
00172                 // Classes called on mouse drag R
00173                 StateIList subMouseDragRightList;
00174                 // Classes called on mouse down W
00175                 StateIList subMouseWheelList;
00176                 // Classes called when entering state
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         // Dragging stuff
00196         // Up or down for each button (bit field)
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 &currentList, 
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_)

Generated on Mon Feb 16 15:14:49 2009 for Scorched3D by  doxygen 1.5.3