public/ITimerSystem.h
author David Anderson <dvander@alliedmods.net>
Tue Sep 11 18:02:59 2007 +0000 (2007-09-11)
changeset 1407 bb6516a50eea
parent 1330 ab94316bb52b
child 1414 bb258bffc7df
permissions -rw-r--r--
removed tick simulator and added api to help with mapchanges
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_TIMER_SYSTEM_H_
    33 #define _INCLUDE_SOURCEMOD_TIMER_SYSTEM_H_
    34 
    35 /**
    36  * @file ITimerSystem.h
    37  * @brief Contains functions for creating and managing timers.
    38  */
    39 
    40 
    41 #include <IShareSys.h>
    42 #include <IForwardSys.h>
    43 
    44 #define SMINTERFACE_TIMERSYS_NAME		"ITimerSys"
    45 #define SMINTERFACE_TIMERSYS_VERSION	1
    46 
    47 namespace SourceMod
    48 {
    49 	class ITimer;
    50 
    51 	typedef float (*SM_TIMELEFT_FUNCTION)();
    52 
    53 	/**
    54 	 * @brief Event callbacks for when a timer is executed.
    55 	 */
    56 	class ITimedEvent
    57 	{
    58 	public:
    59 		/**
    60 		 * @brief Called when a timer is executed.
    61 		 *
    62 		 * @param pTimer		Pointer to the timer instance.
    63 		 * @param pData			Private pointer passed from host.
    64 		 * @return				Pl_Stop to stop timer, Pl_Continue to continue.
    65 		 */
    66 		virtual ResultType OnTimer(ITimer *pTimer, void *pData) =0;
    67 
    68 		/**
    69 		 * @brief Called when the timer has been killed.
    70 		 *
    71 		 * @param pTimer		Pointer to the timer instance.
    72 		 * @param pData			Private data pointer passed from host.
    73 		 */
    74 		virtual void OnTimerEnd(ITimer *pTimer, void *pData) =0;
    75 	};
    76 
    77 	#define TIMER_FLAG_REPEAT			(1<<0)		/**< Timer will repeat until stopped */
    78 	#define TIMER_FLAG_NO_MAPCHANGE		(1<<1)		/**< Timer will not carry over mapchanges */
    79 	#define TIMER_FLAG_BEFORE_MAP_END	(1<<2)		/**< Timer will fire <interval> seconds before map end.
    80 														 This flag is cannot be used with REPEAT, 
    81 														 and TIMER_FLAG_NO_MAPCHANGE is implied.
    82 														 */
    83 
    84 	class ITimerSystem : public SMInterface
    85 	{
    86 	public:
    87 		const char *GetInterfaceName()
    88 		{
    89 			return SMINTERFACE_TIMERSYS_NAME;
    90 		}
    91 		unsigned int GetInterfaceVersion()
    92 		{
    93 			return SMINTERFACE_TIMERSYS_VERSION;
    94 		}
    95 	public:
    96 		/**
    97 		 * @brief Creates a timed event.
    98 		 *
    99 		 * @param pCallbacks		Pointer to ITimedEvent callbacks.
   100 		 * @param fInterval			Interval, in seconds, of the timed event to occur.
   101 		 *							The smallest allowed interval is 0.1 seconds.
   102 		 * @param pData				Private data to pass on to the timer.
   103 		 * @param flags				Extra flags to pass on to the timer.
   104 		 * @return					An ITimer pointer on success, NULL on 
   105 		 *							failure.
   106 		 */
   107 		virtual ITimer *CreateTimer(ITimedEvent *pCallbacks, 
   108 										 float fInterval, 
   109 										 void *pData,
   110 										 int flags) =0;
   111 
   112 		/**
   113 		 * @brief Kills a timer.
   114 		 *
   115 		 * @param pTimer			Pointer to the ITimer structure.
   116 		 * @return
   117 		 */
   118 		virtual void KillTimer(ITimer *pTimer) =0;
   119 
   120 		/**
   121 		 * @brief Arbitrarily fires a timer.  If the timer is not a repeating 
   122 		 * timer, this will also kill the timer.
   123 		 *
   124 		 * @param pTimer			Pointer to the ITimer structure.
   125 		 * @param delayExec			If true, and the timer is repeating, the
   126 		 *							next execution will be delayed by its 
   127 		 *							interval.
   128 		 * @return
   129 		 */
   130 		virtual void FireTimerOnce(ITimer *pTimer, bool delayExec=false) =0;
   131 
   132 		/**
   133 		 * @brief Sets the function which is used to find the time left in the map.
   134 		 *
   135 		 * @param fn				Function that returns the time left in seconds.
   136 		 * @return					The previous SM_TIMELEFT_FUNCTION pointer.
   137 		 */
   138 		virtual SM_TIMELEFT_FUNCTION SetTimeLeftFunction(SM_TIMELEFT_FUNCTION fn) =0;
   139 	};
   140 }
   141 
   142 #endif //_INCLUDE_SOURCEMOD_TIMER_SYSTEM_H_
   143