FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
timer.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library 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 GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 
00026 // FIFE includes
00027 // These includes are split up in two parts, separated by one empty line
00028 // First block: files included from the FIFE root src directory
00029 // Second block: files included from the same folder
00030 #include "timemanager.h"
00031 #include "timer.h"
00032 
00033 namespace FIFE {
00034 
00035     // Remember TimeEvent(-1) means that even an registered event won't be called
00036     Timer::Timer() 
00037         : TimeEvent(-1), 
00038         m_active(false) {
00039     }
00040 
00041     Timer::~Timer() {
00042         stop();
00043     }
00044 
00045     void Timer::start() {
00046         if(m_active)
00047             return;
00048         setLastUpdateTime( TimeManager::instance()->getTime() );
00049         TimeManager::instance()->registerEvent(this);
00050         m_active = true;
00051     }
00052 
00053     void Timer::stop() {
00054         if(!m_active)
00055             return;
00056         TimeManager::instance()->unregisterEvent(this);
00057         m_active = false;
00058     }
00059 
00060     void Timer::setInterval(unsigned long msec) {
00061         setPeriod(msec);
00062         stop();
00063     }
00064     void Timer::setCallback(const type_callback& cb) {
00065         m_callback = cb;
00066     }
00067 
00068     void Timer::updateEvent(unsigned long) {
00069         if(!m_active)
00070             return;
00071 
00072         if( m_callback ) {
00073             m_callback();
00074         }
00075     }
00076 }