celtool/ticktimer.h
00001 /* 00002 Crystal Space Entity Layer 00003 Copyright (C) 2005-2011 by Jorrit Tyberghein 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #ifndef __CEL_TOOL_TICK_TIMER__ 00021 #define __CEL_TOOL_TICK_TIMER__ 00022 00023 #include <csutil/scf_implementation.h> 00024 #include <csutil/weakref.h> 00025 #include <physicallayer/pl.h> 00026 00027 class celPeriodicTimer 00028 { 00029 public: 00030 celPeriodicTimer (csTicks tick = 100, bool repeat = true) 00031 : tick (tick), repeat (repeat) 00032 { 00033 scfiCelTimerListener = new CelTimerListener (this); 00034 } 00035 celPeriodicTimer (csWeakRef<iCelPlLayer> pl, csTicks tick = 100, bool repeat = true) 00036 : tick (tick), repeat (repeat), wref_physical_layer (pl) 00037 { 00038 scfiCelTimerListener = new CelTimerListener (this); 00039 pl->CallbackOnce (scfiCelTimerListener, tick, CEL_EVENT_PRE); 00040 } 00041 virtual ~celPeriodicTimer () { scfiCelTimerListener->DecRef (); } 00042 00043 virtual void Tick () = 0; 00044 void SetTickTime (csTicks tck) { tick = tck; } 00045 void SetRepeat (bool repeat) { celPeriodicTimer::repeat = repeat; } 00046 void SetPL (iCelPlLayer* pl) 00047 { 00048 if (wref_physical_layer) 00049 wref_physical_layer->RemoveCallbackOnce (scfiCelTimerListener, CEL_EVENT_PRE); 00050 wref_physical_layer = pl; 00051 if (wref_physical_layer) 00052 wref_physical_layer->CallbackOnce (scfiCelTimerListener, tick, CEL_EVENT_PRE); 00053 } 00054 00055 private: 00056 // Made independent to avoid circular refs and leaks. 00057 struct CelTimerListener : public scfImplementation1< 00058 CelTimerListener, iCelTimerListener> 00059 { 00060 celPeriodicTimer* parent; 00061 CelTimerListener (celPeriodicTimer* parent) : 00062 scfImplementationType (this), parent (parent) { } 00063 virtual ~CelTimerListener () { } 00064 virtual void TickEveryFrame () 00065 { 00066 return; 00067 } 00068 virtual void TickOnce () 00069 { 00070 if (parent->repeat) 00071 parent->wref_physical_layer->CallbackOnce (this, parent->tick, CEL_EVENT_PRE); 00072 parent->Tick (); 00073 } 00074 } * scfiCelTimerListener; 00075 00076 csTicks tick; 00077 bool repeat; 00078 csWeakRef<iCelPlLayer> wref_physical_layer; 00079 }; 00080 00081 #endif
Generated for CEL: Crystal Entity Layer 2.1 by doxygen 1.6.1
