axmol/cocos2dx/include/CCScheduler.h

224 lines
7.9 KiB
C
Raw Normal View History

2010-07-13 09:36:15 +08:00
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
2011-07-05 14:51:17 +08:00
Copyright (c) 2011 Zynga Inc.
2010-07-13 09:36:15 +08:00
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCSCHEDULER_H__
#define __CCSCHEDULER_H__
2011-06-20 17:31:38 +08:00
#include <string>
#include "CCObject.h"
#include "selector_protocol.h"
#include "support/data_support/uthash.h"
2011-06-20 17:31:38 +08:00
2010-08-04 15:46:12 +08:00
namespace cocos2d {
2010-07-13 09:36:15 +08:00
//
// CCTimer
//
2010-09-28 18:27:33 +08:00
/** @brief Light weight timer */
class CC_DLL CCTimer : public CCObject
2010-07-13 09:36:15 +08:00
{
public:
2011-06-20 17:31:38 +08:00
CCTimer(void);
2010-09-28 16:18:05 +08:00
/** get interval in seconds */
2010-08-02 12:00:06 +08:00
inline ccTime getInterval(void) { return m_fInterval; }
2010-09-28 16:18:05 +08:00
/** set interval in seconds */
2010-08-02 12:00:06 +08:00
inline void setInterval(ccTime fInterval){ m_fInterval = fInterval; }
2010-07-13 09:36:15 +08:00
2010-09-28 16:18:05 +08:00
/** Initializes a timer with a target and a selector. */
bool initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector);
2010-07-13 09:36:15 +08:00
2010-09-28 16:18:05 +08:00
/** Initializes a timer with a target, a selector and an interval in seconds. */
bool initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds);
2010-07-13 09:36:15 +08:00
2011-06-20 17:31:38 +08:00
bool initWithScriptFuncName(const char *pszFuncName, ccTime fSeconds);
2010-09-28 16:18:05 +08:00
/** triggers the timer */
2010-07-13 09:36:15 +08:00
void update(ccTime dt);
public:
2010-09-28 16:18:05 +08:00
/** Allocates a timer with a target and a selector. */
2010-07-13 09:36:15 +08:00
static CCTimer* timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector);
2011-06-20 17:31:38 +08:00
/** Allocates a timer with a script function name. */
static CCTimer* timerWithScriptFuncName(const char* pszFuncName, ccTime fSeconds);
2010-09-28 16:18:05 +08:00
/** Allocates a timer with a target, a selector and an interval in seconds. */
static CCTimer* timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds);
2010-07-13 09:36:15 +08:00
public:
SEL_SCHEDULE m_pfnSelector;
2010-09-24 18:10:32 +08:00
ccTime m_fInterval;
2011-06-20 17:31:38 +08:00
std::string m_scriptFunc;
2010-07-13 09:36:15 +08:00
protected:
SelectorProtocol *m_pTarget;
2011-06-20 17:31:38 +08:00
ccTime m_fElapsed;
2010-07-13 09:36:15 +08:00
};
//
// CCScheduler
//
2010-07-13 09:36:15 +08:00
struct _listEntry;
struct _hashSelectorEntry;
struct _hashUpdateEntry;
2011-06-20 17:31:38 +08:00
struct _hashScriptFuncEntry;
2010-07-13 09:36:15 +08:00
2010-09-28 18:27:33 +08:00
/** @brief Scheduler is responsible of triggering the scheduled callbacks.
You should not use NSTimer. Instead use this class.
There are 2 different types of callbacks (selectors):
- update selector: the 'update' selector will be called every frame. You can customize the priority.
- custom selector: A custom selector will be called every frame, or with a custom interval of time
The 'custom selectors' should be avoided when possible. It is faster, and consumes less memory to use the 'update selector'.
*/
class CC_DLL CCScheduler : public CCObject
2010-07-13 09:36:15 +08:00
{
public:
~CCScheduler(void);
2010-09-28 16:18:05 +08:00
2010-08-02 12:00:06 +08:00
inline ccTime getTimeScale(void) { return m_fTimeScale; }
2010-09-28 16:18:05 +08:00
/** Modifies the time of all scheduled callbacks.
You can use this property to create a 'slow motion' or 'fast forward' effect.
Default is 1.0. To create a 'slow motion' effect, use values below 1.0.
To create a 'fast forward' effect, use values higher than 1.0.
@since v0.8
@warning It will affect EVERY scheduled selector / action.
*/
2010-08-02 12:00:06 +08:00
inline void setTimeScale(ccTime fTimeScale) { m_fTimeScale = fTimeScale; }
2010-07-13 09:36:15 +08:00
/** 'tick' the scheduler.
You should NEVER call this method, unless you know what you are doing.
2010-07-13 09:36:15 +08:00
*/
void tick(ccTime dt);
/** The scheduled method will be called every 'interval' seconds.
If paused is YES, then it won't be called until it is resumed.
If 'interval' is 0, it will be called every frame, but if so, it recommened to use 'scheduleUpdateForTarget:' instead.
2010-09-24 18:10:32 +08:00
If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again.
@since v0.99.3
2010-07-13 09:36:15 +08:00
*/
2010-09-24 18:10:32 +08:00
void scheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol *pTarget, ccTime fInterval, bool bPaused);
2011-06-20 17:31:38 +08:00
/** Schedule the script function
*/
void scheduleScriptFunc(const char *pszFuncName, ccTime fInterval, bool bPaused);
/** Schedules the 'update' selector for a given target with a given priority.
The 'update' selector will be called every frame.
The lower the priority, the earlier it is called.
@since v0.99.3
*/
void scheduleUpdateForTarget(SelectorProtocol *pTarget, int nPriority, bool bPaused);
2010-07-13 09:36:15 +08:00
2010-09-28 16:18:05 +08:00
/** Unschedule a selector for a given target.
If you want to unschedule the "update", use unscheudleUpdateForTarget.
@since v0.99.3
2010-07-13 09:36:15 +08:00
*/
void unscheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol *pTarget);
2011-06-20 17:31:38 +08:00
/** Unschedule the script function
*/
void unscheduleScriptFunc(const char *pfzFuncName);
2010-07-13 09:36:15 +08:00
/** Unschedules the update selector for a given target
@since v0.99.3
2010-07-13 09:36:15 +08:00
*/
void unscheduleUpdateForTarget(const SelectorProtocol *pTarget);
2010-07-13 09:36:15 +08:00
/** Unschedules all selectors for a given target.
This also includes the "update" selector.
@since v0.99.3
2010-07-13 09:36:15 +08:00
*/
void unscheduleAllSelectorsForTarget(SelectorProtocol *pTarget);
/** Unschedules all selectors from all targets.
You should NEVER call this method, unless you know what you are doing.
@since v0.99.3
2010-07-13 09:36:15 +08:00
*/
void unscheduleAllSelectors(void);
/** Pauses the target.
All scheduled selectors/update for a given target won't be 'ticked' until the target is resumed.
If the target is not present, nothing happens.
@since v0.99.3
2010-07-13 09:36:15 +08:00
*/
void pauseTarget(SelectorProtocol *pTarget);
/** Resumes the target.
The 'target' will be unpaused, so all schedule selectors/update will be 'ticked' again.
If the target is not present, nothing happens.
@since v0.99.3
2010-07-13 09:36:15 +08:00
*/
void resumeTarget(SelectorProtocol *pTarget);
public:
2010-09-28 16:18:05 +08:00
/** returns a shared instance of the Scheduler */
2010-11-16 15:12:09 +08:00
static CCScheduler* sharedScheduler(void);
2010-07-13 09:36:15 +08:00
/** purges the shared scheduler. It releases the retained instance.
@since v0.99.0
2010-07-13 09:36:15 +08:00
*/
static void purgeSharedScheduler(void);
private:
void removeHashElement(struct _hashSelectorEntry *pElement);
2011-07-05 14:51:17 +08:00
void removeUpdateFromHash(struct _listEntry *entry);
CCScheduler();
bool init(void);
// update specific
void priorityIn(struct _listEntry **ppList, SelectorProtocol *pTarget, int nPriority, bool bPaused);
void appendIn(struct _listEntry **ppList, SelectorProtocol *pTarget, bool bPaused);
2010-07-13 09:36:15 +08:00
protected:
ccTime m_fTimeScale;
//
// "updates with priority" stuff
2010-07-13 09:36:15 +08:00
//
struct _listEntry *m_pUpdatesNegList; // list of priority < 0
struct _listEntry *m_pUpdates0List; // list priority == 0
struct _listEntry *m_pUpdatesPosList; // list priority > 0
struct _hashUpdateEntry *m_pHashForUpdates; // hash used to fetch quickly the list entries for pause,delete,etc
// Used for "selectors with interval"
struct _hashSelectorEntry *m_pHashForSelectors;
struct _hashSelectorEntry *m_pCurrentTarget;
bool m_bCurrentTargetSalvaged;
2011-07-05 14:51:17 +08:00
// If true unschedule will not remove anything from a hash. Elements will only be marked for deletion.
bool m_bUpdateHashLocked;
2011-06-20 17:31:38 +08:00
// Used for "script function call back with interval"
struct _hashScriptFuncEntry *m_pHashForScriptFunctions;
2010-07-13 09:36:15 +08:00
};
2010-08-04 15:46:12 +08:00
}//namespace cocos2d
2010-07-13 09:36:15 +08:00
#endif // __CCSCHEDULER_H__