Back to index

supertuxkart  0.5+dfsg1
callback_manager.cpp
Go to the documentation of this file.
00001 //  $Id: callback_manager.cpp 796 2006-09-27 07:06:34Z hiker $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 Joerg Henrichs
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program 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 this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include "callback_manager.hpp"
00021 #include "moving_physics.hpp"
00022 
00023 CallbackManager *callback_manager=NULL;
00024 
00025 CallbackManager::CallbackManager()
00026 {
00027     for(int i=0; i<CB_MAX; i++)
00028     {
00029         m_allCallbacks[i].clear();
00030     }
00031 }   // CallbackManager
00032 
00033 //-----------------------------------------------------------------------------
00034 CallbackManager::~CallbackManager()
00035 {
00036     for(int i=0; i<CB_MAX; i++)
00037     {
00038         for(std::vector<Callback*>::const_iterator c = m_allCallbacks[i].begin();
00039             c != m_allCallbacks[i].end(); c++)
00040         {
00041             MovingPhysics *mp = dynamic_cast<MovingPhysics*>(*c);
00042             if(mp)
00043             {
00044                 ssgDeRefDelete(mp);
00045             }
00046             else
00047             {
00048                 delete *c;
00049             }
00050             m_allCallbacks[i].clear();
00051         }   // for c in m_allCallbacks[i]
00052     }   // for i <CB_MAX
00053 }   // ~CallbackManager
00054 
00055 //-----------------------------------------------------------------------------
00056 void CallbackManager::addCallback(Callback *c, CallbackType t) 
00057 {
00058     m_allCallbacks[t].push_back(c);
00059         MovingPhysics *mp = dynamic_cast<MovingPhysics*>(c);
00060         if(mp)
00061         {
00062             mp->ref();
00063         }
00064 }   // addCallback
00065 
00066 //-----------------------------------------------------------------------------
00067 
00068 
00069 void CallbackManager::clear(CallbackType cbType)
00070 {
00071     for(std::vector<Callback*>::const_iterator c = m_allCallbacks[cbType].begin();
00072         c != m_allCallbacks[cbType].end(); c++)
00073     {
00074         MovingPhysics *mp = dynamic_cast<MovingPhysics*>(*c);
00075         if(mp)
00076         {
00077             ssgDeRefDelete(mp);
00078         }
00079         else
00080         {
00081             delete *c;
00082         }
00083     }
00084 
00085     m_allCallbacks[cbType].clear();
00086 }  // clear
00087 
00088 //-----------------------------------------------------------------------------
00089 void CallbackManager::update(float dt) const
00090 {
00091     for(int i=0; i<CB_MAX; i++)
00092     {
00093         for(std::vector<Callback*>::const_iterator c = m_allCallbacks[i].begin();
00094             c != m_allCallbacks[i].end(); c++)
00095             (*c)->update(dt);
00096     }   // for i
00097 
00098 }   // update
00099 //-----------------------------------------------------------------------------
00100 void CallbackManager::initAll() const
00101 {
00102     for(int i=0; i<CB_MAX; i++)
00103     {
00104         for(std::vector<Callback*>::const_iterator c = m_allCallbacks[i].begin();
00105             c != m_allCallbacks[i].end(); c++)
00106             (*c)->init();
00107     }   // for i
00108 
00109 }   // initAll
00110 
00111 //-----------------------------------------------------------------------------
00112 // Called when restarting a race
00113 void CallbackManager::reset() const
00114 {
00115     for(int i=0; i<CB_MAX; i++)
00116     {
00117         for(std::vector<Callback*>::const_iterator c = m_allCallbacks[i].begin();
00118             c != m_allCallbacks[i].end(); c++)
00119             (*c)->reset();
00120     }   // for i
00121 
00122 }   // initAll
00123 
00124 //-----------------------------------------------------------------------------
00125 void CallbackManager::handleExplosion(const btVector3& pos, 
00126                                       const MovingPhysics* mp) const
00127 {
00128     for(int i=0; i<CB_MAX; i++)
00129     {
00130         for(std::vector<Callback*>::const_iterator c = m_allCallbacks[i].begin();
00131             c != m_allCallbacks[i].end(); c++)
00132             (*c)->handleExplosion(pos, mp==(*c));
00133     }   // for i
00134 
00135 }   // handleExplosion
00136