Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 26, 2008, 4:26:04 PM (16 years ago)
Author:
rgrieder
Message:

Still working on the GameStates, but I have to save the work because of some major changes.

  • Exported InputManager- and TclThreadManager-tick to GSGraphics instead of Core
  • Fixed a few bugs in GameState by adding an internal state variable as bitfield (quite practical)
  • Fixed a bug in InputManager that occurred when destroying an active InputState
  • Added GSIO and GSIOConsole (3 lines of loop code with std::cin, but works ;))
  • Added more boost thread includes to OrxonoxStableHeaders.h
  • Many changes in all GameState derived classes
Location:
code/branches/gui/src/core
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gui/src/core/CommandLine.h

    r1664 r1670  
    3838#define SetCommandLineArgument(name, defaultValue) \
    3939    BaseCommandLineArgument& CmdArgumentDummyBoolVar##name \
    40     = orxonox::CommandLine::addCommandLineArgument(#name, defaultValue)
     40    = orxonox::CommandLine::addArgument(#name, defaultValue)
    4141#define SetCommandLineSwitch(name) \
    4242    BaseCommandLineArgument& CmdArgumentDummyBoolVar##name \
    43     = orxonox::CommandLine::addCommandLineArgument(#name, false)
     43    = orxonox::CommandLine::addArgument(#name, false)
    4444
    4545
     
    8989    protected:
    9090        BaseCommandLineArgument(const std::string& name)
    91             : name_(name)
    92             , bHasDefaultValue_(true)
     91            : bHasDefaultValue_(true)
     92            , name_(name)
    9393        { }
    9494
     
    216216
    217217        template <class T>
    218         static const CommandLineArgument<T>* getCommandLineArgument(const std::string& name);
     218        static const CommandLineArgument<T>* getArgument(const std::string& name);
    219219        //! Writes the argument value in the given parameter.
    220220        template <class T>
    221         static void getCommandLineValue(const std::string& name, T* value)
    222         { *value = getCommandLineArgument<T>(name)->getValue(); }
    223         template <class T>
    224         static BaseCommandLineArgument& addCommandLineArgument(const std::string& name, T defaultValue);
     221        static void getValue(const std::string& name, T* value)
     222        { *value = getArgument<T>(name)->getValue(); }
     223        template <class T>
     224        static BaseCommandLineArgument& addArgument(const std::string& name, T defaultValue);
    225225
    226226    private:
     
    264264    */
    265265    template <class T>
    266     const CommandLineArgument<T>* CommandLine::getCommandLineArgument(const std::string& name)
     266    const CommandLineArgument<T>* CommandLine::getArgument(const std::string& name)
    267267    {
    268268        std::map<std::string, BaseCommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
     
    302302    */
    303303    template <class T>
    304     BaseCommandLineArgument& CommandLine::addCommandLineArgument(const std::string& name, T defaultValue)
     304    BaseCommandLineArgument& CommandLine::addArgument(const std::string& name, T defaultValue)
    305305    {
    306306        std::map<std::string, BaseCommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
  • code/branches/gui/src/core/Core.cc

    r1642 r1670  
    3636#include "CoreIncludes.h"
    3737#include "ConfigValueIncludes.h"
    38 #include "input/InputManager.h"
    39 #include "TclThreadManager.h"
     38//#include "input/InputManager.h"
     39//#include "TclThreadManager.h"
    4040
    4141namespace orxonox
     
    188188    }
    189189
    190     /**
    191         @brief Ticks every core class in a specified sequence. Has to be called
    192                every Orxonox tick!
    193         @param dt Delta Time
    194     */
    195     void Core::tick(float dt)
    196     {
    197         TclThreadManager::getInstance().tick(dt);
    198         InputManager::getInstance().tick(dt);
    199     }
     190    ///**
     191    //    @brief Ticks every core class in a specified sequence. Has to be called
     192    //           every Orxonox tick!
     193    //    @param dt Delta Time
     194    //*/
     195    //void Core::tick(float dt)
     196    //{
     197    //    TclThreadManager::getInstance().tick(dt);
     198    //    InputManager::getInstance().tick(dt);
     199    //}
    200200}
    201201
  • code/branches/gui/src/core/Core.h

    r1638 r1670  
    5858            static void resetLanguage();
    5959
    60             static void tick(float dt);
     60            //static void tick(float dt);
    6161
    6262        private:
  • code/branches/gui/src/core/Exception.h

    r1664 r1670  
    8888        std::string functionName_;
    8989        std::string fileName_;
    90         // mutable because of "what()" is a const method
     90        // mutable because "what()" is a const method
    9191        mutable std::string fullDescription_;
    9292    };
     
    101101                  : Exception(description, lineNumber, fileName, functionName)
    102102        {
    103             // let the catcher decide whether to display the message or not
    104             //COUT(1) << this->getFullDescription() << std::endl;
     103            // let the catcher decide whether to display the message below level 3
     104            COUT(3) << this->getFullDescription() << std::endl;
    105105        }
    106106
     
    108108            : Exception(description)
    109109        {
    110             // let the catcher decide whether to display the message or not
    111             //COUT(1) << this->getFullDescription() << std::endl;
     110            // let the catcher decide whether to display the message below level 3
     111            COUT(3) << this->getFullDescription() << std::endl;
    112112        }
    113113
  • code/branches/gui/src/core/GameState.cc

    r1661 r1670  
    4747        : name_(name)
    4848        , bPauseParent_(false)
    49         , bActive_(false)
    50         , bSuspended_(false)
     49        //, bActive_(false)
     50        //, bSuspended_(false)
     51        //, bRunning_(false)
     52        , scheduledTransition_(0)
    5153        , parent_(0)
    5254        , activeChild_(0)
    5355    {
     56        Operations temp = {false, false, false, false, false};
     57        this->operation_ = temp;
    5458    }
    5559
     
    6064    GameState::~GameState()
    6165    {
    62         if (this->bActive_)
     66        if (this->operation_.active)
    6367        {
    6468            if (this->parent_)
     
    133137        if (it != this->grandchildrenToChildren_.end())
    134138        {
    135             if (state->isActive())
     139            if (state->getOperation().active)
    136140            {
    137141                ThrowException(GameState, "Cannot remove active game state child '"
     
    247251    GameState* GameState::getCurrentState()
    248252    {
    249         if (this->bActive_)
     253        if (this->operation_.active)
    250254        {
    251255            if (this->activeChild_)
     
    284288    void GameState::requestState(const std::string& name)
    285289    {
    286         if (name == "")
    287         {
    288             // user would like to leave every state.
    289             GameState* current = getCurrentState();
    290             if (current)
    291             {
    292                 // Deactivate all states but root
    293                 GameState* root = getRootNode();
    294                 current->makeTransition(root);
    295                 // Kick root too
    296                 root->deactivate();
    297             }
    298         }
     290        GameState* current = getCurrentState();
     291        if (current != 0 && (current->getOperation().entering || current->getOperation().leaving))
     292        {
     293            ThrowException(GameState, "Making state transitions during enter()/leave() is forbidden.");
     294        }
     295        //if (name == "")
     296        //{
     297        //    // user would like to leave every state.
     298        //    if (current)
     299        //    {
     300        //        // Deactivate all states but root
     301        //        GameState* root = getRootNode();
     302        //        current->makeTransition(root);
     303        //        //// Kick root too
     304        //        //assert(!(root->getOperation().entering || root->getOperation().leaving));
     305        //        //if (root->operation_.running)
     306        //        //    root->scheduledTransition_ = 0;
     307        //        //else
     308        //        //    root->deactivate();
     309        //    }
     310        //}
    299311        else
    300312        {
    301313            GameState* request = checkState(name);
    302             GameState* current = getCurrentState();
    303314            if (request)
    304315            {
     
    331342    {
    332343        // we're always already active
    333         assert(this->bActive_);
     344        assert(this->operation_.active);
    334345
    335346        if (state == this)
     
    349360            assert(this->parent_ != 0);
    350361
    351             // first, leave this state.
     362            // only do the transition if we're not currently running
     363            if (this->operation_.running)
     364            {
     365                //this->bDeactivationScheduled_ = true;
     366                this->scheduledTransition_ = state;
     367            }
     368            else
     369            {
     370                this->deactivate();
     371                this->parent_->makeTransition(state);
     372            }
     373
     374        }
     375    }
     376
     377    /**
     378    @brief
     379        Activates the state. Only sets bActive_ to true and notifies the parent.
     380    */
     381    void GameState::activate()
     382    {
     383        if (this->parent_)
     384            this->parent_->activeChild_ = this;
     385        this->operation_.active = true;
     386        this->operation_.entering = true;
     387        this->enter();
     388        this->operation_.entering = false;
     389    }
     390
     391    /**
     392        Activates the state. Only sets bActive_ to false and notifies the parent.
     393    */
     394    void GameState::deactivate()
     395    {
     396        this->operation_.leaving = true;
     397        this->leave();
     398        this->operation_.leaving = false;
     399        this->operation_.active = false;
     400        if (this->parent_)
     401            this->parent_->activeChild_ = 0;
     402    }
     403
     404    /**
     405    @brief
     406        Update method that calls ticked() with enclosed bRunning_ = true
     407        If there was a state transition request within ticked() then this
     408        method will transition in the end.
     409    @param dt Delta time
     410    @note
     411        This method is not virtual! You cannot override it therefore.
     412    */
     413    void GameState::tick(float dt)
     414    {
     415        this->operation_.running = true;
     416        this->ticked(dt);
     417        this->operation_.running = false;
     418
     419        if (this->scheduledTransition_)
     420        {
     421            // state was requested to be deactivated when ticked.
     422            this->makeTransition(this->scheduledTransition_);
     423            this->scheduledTransition_ = 0;
    352424            this->deactivate();
    353             this->parent_->makeTransition(state);
    354         }
    355     }
    356 
    357     /**
    358     @brief
    359         Activates the state. Only sets bActive_ to true and notifies the parent.
    360     */
    361     void GameState::activate()
    362     {
    363         this->bActive_ = true;
    364         if (this->parent_)
    365             this->parent_->activeChild_ = this;
    366         this->enter();
    367     }
    368 
    369     /**
    370         Activates the state. Only sets bActive_ to false and notifies the parent.
    371     */
    372     void GameState::deactivate()
    373     {
    374         this->leave();
    375         this->bActive_ = false;
    376         if (this->parent_)
    377             this->parent_->activeChild_ = 0;
     425        }
    378426    }
    379427
  • code/branches/gui/src/core/GameState.h

    r1661 r1670  
    6161    {
    6262    public:
     63        struct Operations
     64        {
     65            unsigned active    : 1;
     66            unsigned entering  : 1;
     67            unsigned leaving   : 1;
     68            unsigned running   : 1;
     69            unsigned suspended : 1;
     70        };
     71
    6372        GameState(const std::string& name);
    6473        virtual ~GameState();
     
    7180        void requestState(const std::string& name);
    7281
    73         //! Determines whether the state is active.
    74         bool isActive()       { return this->bActive_; }
    75         //! Determines whether the state is suspended.
    76         bool isSuspended()    { return this->bSuspended_; }
    77         //! Determines whether the state is the current
    78         bool isCurrentState() { return this->bActive_ && !this->activeChild_; }
     82        ////! Determines whether the state is active.
     83        //bool isActive()       { return this->bActive_; }
     84        ////! Determines whether the state is suspended.
     85        //bool isSuspended()    { return this->bSuspended_; }
     86        ////! Determines whether the state is the current
     87        //bool isCurrentState() { return this->bActive_ && !this->activeChild_; }
     88        const Operations getOperation() { return this->operation_; }
    7989
    80         virtual bool tick(float dt) { return true; }
     90        void tick(float dt);
     91        void tickChild(float dt) { if (this->activeChild_) this->activeChild_->tick(dt); }
    8192
    8293    protected:
    83         //virtual void enter() = 0;
    84         //virtual void leave() = 0;
    85         //virtual void tick(float dt) = 0;
    86         virtual void enter() { }
    87         virtual void leave() { }
     94        virtual void enter() = 0;
     95        virtual void leave() = 0;
     96        virtual void ticked(float dt) = 0;
     97        //virtual void enter() { }
     98        //virtual void leave() { }
     99        //virtual void ticked(float dt) { }
    88100
    89101        GameState* getActiveChild() { return this->activeChild_; }
     102        bool hasScheduledTransition() { return this->scheduledTransition_; }
    90103
    91104    private:
     
    102115        bool                                 bPauseParent_;
    103116
    104         bool                                 bActive_;
    105         bool                                 bSuspended_;
     117        Operations                           operation_;
    106118
    107119        GameState*                           parent_;
    108120        GameState*                           activeChild_;
     121        GameState*                           scheduledTransition_;
    109122        std::map<std::string, GameState*>    allChildren_;
    110123        std::map<GameState*, GameState*>     grandchildrenToChildren_;
  • code/branches/gui/src/core/Script.cc

    r1638 r1670  
    198198      for (std::map<unsigned int, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it)
    199199      {
    200         if ((*it).second == expectedValue)
     200        if (it->second == expectedValue)
    201201          expectedValue = !expectedValue;
    202202        else
  • code/branches/gui/src/core/input/InputManager.cc

    r1660 r1670  
    348348        for (std::map<int, InputState*>::const_iterator it = inputStatesByPriority_.begin();
    349349            it != inputStatesByPriority_.end(); ++it)
    350             (*it).second->setNumOfJoySticks(joySticksSize_);
     350            it->second->setNumOfJoySticks(joySticksSize_);
    351351    }
    352352
     
    506506    }
    507507
     508    /**
     509    @brief
     510        Removes and destroys an InputState.
     511    @return
     512        True if state was removed immediately, false if postponed.
     513    */
    508514    void InputManager::_destroyState(InputState* state)
    509515    {
    510         assert(state);
     516        assert(state && !(this->internalState_ & Ticking));
    511517        std::map<int, InputState*>::iterator it = this->activeStates_.find(state->getPriority());
    512518        if (it != this->activeStates_.end())
     
    633639        internalState_ |= Ticking;
    634640
    635         // check for states to leave (don't use unsigned int!)
    636         for (int i = stateLeaveRequests_.size() - 1; i >= 0; --i)
    637         {
    638             stateLeaveRequests_[i]->onLeave();
     641        // check for states to leave
     642        for (std::set<InputState*>::reverse_iterator it = stateLeaveRequests_.rbegin();
     643            it != stateLeaveRequests_.rend(); ++it)
     644        {
     645            (*it)->onLeave();
    639646            // just to be sure that the state actually is registered
    640             assert(inputStatesByName_.find(stateLeaveRequests_[i]->getName()) != inputStatesByName_.end());
    641            
    642             activeStates_.erase(stateLeaveRequests_[i]->getPriority());
     647            assert(inputStatesByName_.find((*it)->getName()) != inputStatesByName_.end());
     648
     649            activeStates_.erase((*it)->getPriority());
    643650            _updateActiveStates();
    644             stateLeaveRequests_.pop_back();
    645         }
    646 
    647 
    648         // check for states to enter (don't use unsigned int!)
    649         for (int i = stateEnterRequests_.size() - 1; i >= 0; --i)
     651        }
     652        stateLeaveRequests_.clear();
     653
     654        // check for states to enter
     655        for (std::set<InputState*>::reverse_iterator it = stateEnterRequests_.rbegin();
     656            it != stateEnterRequests_.rend(); ++it)
    650657        {
    651658            // just to be sure that the state actually is registered
    652             assert(inputStatesByName_.find(stateEnterRequests_[i]->getName()) != inputStatesByName_.end());
    653            
    654             activeStates_[stateEnterRequests_[i]->getPriority()] = stateEnterRequests_[i];
     659            assert(inputStatesByName_.find((*it)->getName()) != inputStatesByName_.end());
     660
     661            activeStates_[(*it)->getPriority()] = (*it);
    655662            _updateActiveStates();
    656             stateEnterRequests_[i]->onEnter();
    657             stateEnterRequests_.pop_back();
     663            (*it)->onEnter();
     664        }
     665        stateEnterRequests_.clear();
     666
     667        // check for states to destroy
     668        for (std::set<InputState*>::reverse_iterator it = stateDestroyRequests_.rbegin();
     669            it != stateDestroyRequests_.rend(); ++it)
     670        {
     671            _destroyState((*it));
    658672        }
    659673
     
    703717        for (std::map<int, InputState*>::const_iterator it = activeStates_.begin(); it != activeStates_.end(); ++it)
    704718            for (unsigned int i = 0; i < devicesNum_; ++i)
    705                 if ((*it).second->isInputDeviceEnabled(i))
    706                     activeStatesTop_[i] = (*it).second;
     719                if (it->second->isInputDeviceEnabled(i))
     720                    activeStatesTop_[i] = it->second;
    707721
    708722        // update tickables (every state will only appear once)
     
    11351149    /**
    11361150    @brief
    1137         Removes an input state internally.
     1151        Removes and destroys an input state internally.
    11381152    @param name
    11391153        Name of the handler.
     
    11421156    @remarks
    11431157        You can't remove the internal states "empty", "calibrator" and "detector".
    1144     */
    1145     bool InputManager::destroyState(const std::string& name)
     1158        The removal process is being postponed if InputManager::tick() is currently running.
     1159    */
     1160    bool InputManager::requestDestroyState(const std::string& name)
    11461161    {
    11471162        if (name == "empty" || name == "calibrator" || name == "detector")
     
    11531168        if (it != inputStatesByName_.end())
    11541169        {
    1155             _destroyState((*it).second);
     1170            if (activeStates_.find(it->second->getPriority()) != activeStates_.end())
     1171            {
     1172                // The state is still active. We have to postpone
     1173                stateLeaveRequests_.insert(it->second);
     1174                stateDestroyRequests_.insert(it->second);
     1175            }
     1176            else if (this->internalState_ & Ticking)
     1177            {
     1178                // cannot remove state while ticking
     1179                stateDestroyRequests_.insert(it->second);
     1180            }
     1181            else
     1182                _destroyState(it->second);
     1183
    11561184            return true;
    11571185        }
     
    11711199        std::map<std::string, InputState*>::iterator it = inputStatesByName_.find(name);
    11721200        if (it != inputStatesByName_.end())
    1173             return (*it).second;
     1201            return it->second;
    11741202        else
    11751203            return 0;
     
    12021230        if (it != inputStatesByName_.end())
    12031231        {
    1204             stateEnterRequests_.push_back((*it).second);
    1205             return true;
     1232            // exists
     1233            if (activeStates_.find(it->second->getPriority()) == activeStates_.end())
     1234            {
     1235                // not active
     1236                if (stateDestroyRequests_.find(it->second) == stateDestroyRequests_.end())
     1237                {
     1238                    // not scheduled for destruction
     1239                    // set prevents a state being added multiple times
     1240                    stateEnterRequests_.insert(it->second);
     1241                    return true;
     1242                }
     1243            }
    12061244        }
    12071245        return false;
     
    12221260        if (it != inputStatesByName_.end())
    12231261        {
    1224             stateLeaveRequests_.push_back((*it).second);
    1225             return true;
     1262            // exists
     1263            if (activeStates_.find(it->second->getPriority()) != activeStates_.end())
     1264            {
     1265                // active
     1266                stateLeaveRequests_.insert(it->second);
     1267                return true;
     1268            }
    12261269        }
    12271270        return false;
  • code/branches/gui/src/core/input/InputManager.h

    r1659 r1670  
    127127        }
    128128
    129         bool destroyState          (const std::string& name);
    130129        InputState* getState       (const std::string& name);
    131130        InputState* getCurrentState();
     131        bool requestDestroyState   (const std::string& name);
    132132        bool requestEnterState     (const std::string& name);
    133133        bool requestLeaveState     (const std::string& name);
     134
     135        void tick(float dt);
    134136
    135137        static InputManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; }
     
    167169        void _updateActiveStates();
    168170        bool _configureInputState(InputState* state, const std::string& name, int priority);
    169 
    170         void tick(float dt);
    171171
    172172        // input events
     
    204204        std::map<int, InputState*>          inputStatesByPriority_;
    205205
    206         std::vector<InputState*>            stateEnterRequests_;   //!< Request to enter a new state
    207         std::vector<InputState*>            stateLeaveRequests_;   //!< Request to leave the current state
     206        std::set<InputState*>               stateEnterRequests_;   //!< Request to enter a new state
     207        std::set<InputState*>               stateLeaveRequests_;   //!< Request to leave a running state
     208        std::set<InputState*>               stateDestroyRequests_; //!< Request to destroy a state
    208209
    209210        std::map<int, InputState*>          activeStates_;
Note: See TracChangeset for help on using the changeset viewer.