2012-02-01 16:45:23 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-02-01 16:45:23 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCScheduler.h"
|
|
|
|
#include "ccMacros.h"
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "CCDirector.h"
|
2012-02-01 16:45:23 +08:00
|
|
|
#include "support/data_support/utlist.h"
|
|
|
|
#include "support/data_support/ccCArray.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCArray.h"
|
|
|
|
#include "script_support/CCScriptSupport.h"
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
// data structures
|
|
|
|
|
|
|
|
// A list double-linked list used for "updates with priority"
|
|
|
|
typedef struct _listEntry
|
|
|
|
{
|
2012-12-10 13:48:27 +08:00
|
|
|
struct _listEntry *prev, *next;
|
|
|
|
CCObject *target; // not retained (retained by hashUpdateEntry)
|
|
|
|
int priority;
|
2012-04-19 14:35:52 +08:00
|
|
|
bool paused;
|
|
|
|
bool markedForDeletion; // selector will no longer be called and entry will be removed at end of the next tick
|
2012-02-01 16:45:23 +08:00
|
|
|
} tListEntry;
|
|
|
|
|
|
|
|
typedef struct _hashUpdateEntry
|
|
|
|
{
|
2012-12-10 13:48:27 +08:00
|
|
|
tListEntry **list; // Which list does it belong to ?
|
|
|
|
tListEntry *entry; // entry in the list
|
|
|
|
CCObject *target; // hash key (retained)
|
|
|
|
UT_hash_handle hh;
|
2012-02-01 16:45:23 +08:00
|
|
|
} tHashUpdateEntry;
|
|
|
|
|
|
|
|
// Hash Element used for "selectors with interval"
|
|
|
|
typedef struct _hashSelectorEntry
|
|
|
|
{
|
2012-12-10 13:48:27 +08:00
|
|
|
ccArray *timers;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCObject *target; // hash key (retained)
|
2012-12-10 13:48:27 +08:00
|
|
|
unsigned int timerIndex;
|
|
|
|
CCTimer *currentTimer;
|
|
|
|
bool currentTimerSalvaged;
|
|
|
|
bool paused;
|
|
|
|
UT_hash_handle hh;
|
2012-11-14 18:05:15 +08:00
|
|
|
} tHashTimerEntry;
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
// implementation CCTimer
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
CCTimer::CCTimer()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _target(NULL)
|
|
|
|
, _elapsed(-1)
|
|
|
|
, _runForever(false)
|
|
|
|
, _useDelay(false)
|
|
|
|
, _timesExecuted(0)
|
|
|
|
, _repeat(0)
|
|
|
|
, _delay(0.0f)
|
|
|
|
, _interval(0.0f)
|
|
|
|
, _selector(NULL)
|
|
|
|
, _scriptHandler(0)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
CCTimer* CCTimer::timerWithTarget(CCObject *pTarget, SEL_SCHEDULE pfnSelector)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2012-06-12 17:07:54 +08:00
|
|
|
CCTimer *pTimer = new CCTimer();
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
pTimer->initWithTarget(pTarget, pfnSelector, 0.0f, kCCRepeatForever, 0.0f);
|
|
|
|
pTimer->autorelease();
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pTimer;
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
CCTimer* CCTimer::timerWithTarget(CCObject *pTarget, SEL_SCHEDULE pfnSelector, float fSeconds)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2012-06-12 17:07:54 +08:00
|
|
|
CCTimer *pTimer = new CCTimer();
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
pTimer->initWithTarget(pTarget, pfnSelector, fSeconds, kCCRepeatForever, 0.0f);
|
|
|
|
pTimer->autorelease();
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pTimer;
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
CCTimer* CCTimer::timerWithScriptHandler(int nHandler, float fSeconds)
|
2012-02-02 15:58:10 +08:00
|
|
|
{
|
2012-06-12 17:07:54 +08:00
|
|
|
CCTimer *pTimer = new CCTimer();
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
pTimer->initWithScriptHandler(nHandler, fSeconds);
|
2012-04-19 14:35:52 +08:00
|
|
|
pTimer->autorelease();
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pTimer;
|
2012-02-02 15:58:10 +08:00
|
|
|
}
|
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
bool CCTimer::initWithScriptHandler(int nHandler, float fSeconds)
|
2012-02-02 15:58:10 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scriptHandler = nHandler;
|
|
|
|
_elapsed = -1;
|
|
|
|
_interval = fSeconds;
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-02-02 15:58:10 +08:00
|
|
|
}
|
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
bool CCTimer::initWithTarget(CCObject *pTarget, SEL_SCHEDULE pfnSelector)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2012-03-14 14:55:17 +08:00
|
|
|
return initWithTarget(pTarget, pfnSelector, 0, kCCRepeatForever, 0.0f);
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
bool CCTimer::initWithTarget(CCObject *pTarget, SEL_SCHEDULE pfnSelector, float fSeconds, unsigned int nRepeat, float fDelay)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_target = pTarget;
|
|
|
|
_selector = pfnSelector;
|
|
|
|
_elapsed = -1;
|
|
|
|
_interval = fSeconds;
|
|
|
|
_delay = fDelay;
|
|
|
|
_useDelay = (fDelay > 0.0f) ? true : false;
|
|
|
|
_repeat = nRepeat;
|
|
|
|
_runForever = (nRepeat == kCCRepeatForever) ? true : false;
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
void CCTimer::update(float dt)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_elapsed == -1)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_elapsed = 0;
|
|
|
|
_timesExecuted = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_runForever && !_useDelay)
|
2012-04-19 14:35:52 +08:00
|
|
|
{//standard timer usage
|
2013-06-15 14:03:30 +08:00
|
|
|
_elapsed += dt;
|
|
|
|
if (_elapsed >= _interval)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_target && _selector)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
(_target->*_selector)(_elapsed);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_scriptHandler)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(_scriptHandler, _elapsed);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_elapsed = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{//advanced usage
|
2013-06-15 14:03:30 +08:00
|
|
|
_elapsed += dt;
|
|
|
|
if (_useDelay)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _elapsed >= _delay )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_target && _selector)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
(_target->*_selector)(_elapsed);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_scriptHandler)
|
2012-05-03 10:12:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(_scriptHandler, _elapsed);
|
2012-05-03 10:12:00 +08:00
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_elapsed = _elapsed - _delay;
|
|
|
|
_timesExecuted += 1;
|
|
|
|
_useDelay = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_elapsed >= _interval)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_target && _selector)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
(_target->*_selector)(_elapsed);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_scriptHandler)
|
2012-05-03 10:12:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(_scriptHandler, _elapsed);
|
2012-05-03 10:12:00 +08:00
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_elapsed = 0;
|
|
|
|
_timesExecuted += 1;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_runForever && _timesExecuted > _repeat)
|
2012-04-19 14:35:52 +08:00
|
|
|
{ //unschedule timer
|
2013-06-15 14:03:30 +08:00
|
|
|
CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(_selector, _target);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
float CCTimer::getInterval() const
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _interval;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
void CCTimer::setInterval(float fInterval)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_interval = fInterval;
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
SEL_SCHEDULE CCTimer::getSelector() const
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _selector;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
// implementation of CCScheduler
|
|
|
|
|
|
|
|
CCScheduler::CCScheduler(void)
|
2013-06-15 14:03:30 +08:00
|
|
|
: _timeScale(1.0f)
|
|
|
|
, _updatesNegList(NULL)
|
|
|
|
, _updates0List(NULL)
|
|
|
|
, _updatesPosList(NULL)
|
|
|
|
, _hashForUpdates(NULL)
|
|
|
|
, _hashForTimers(NULL)
|
|
|
|
, _currentTarget(NULL)
|
|
|
|
, _currentTargetSalvaged(false)
|
|
|
|
, _updateHashLocked(false)
|
|
|
|
, _scriptHandlerEntries(NULL)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2012-03-14 18:11:25 +08:00
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCScheduler::~CCScheduler(void)
|
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
unscheduleAll();
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_scriptHandlerEntries);
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScheduler::removeHashElement(_hashSelectorEntry *pElement)
|
|
|
|
{
|
2013-03-27 05:55:26 +08:00
|
|
|
|
|
|
|
cocos2d::CCObject *target = pElement->target;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccArrayFree(pElement->timers);
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_DEL(_hashForTimers, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
free(pElement);
|
2013-03-27 05:55:26 +08:00
|
|
|
|
|
|
|
// make sure the target is released after we have removed the hash element
|
|
|
|
// otherwise we access invalid memory when the release call deletes the target
|
|
|
|
// and the target calls removeAllSelectors() during its destructor
|
|
|
|
target->release();
|
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScheduler::scheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget, float fInterval, bool bPaused)
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2012-11-22 12:05:38 +08:00
|
|
|
this->scheduleSelector(pfnSelector, pTarget, fInterval, kCCRepeatForever, 0.0f, bPaused);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
void CCScheduler::scheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget, float fInterval, unsigned int repeat, float delay, bool bPaused)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2012-06-08 14:11:48 +08:00
|
|
|
CCAssert(pfnSelector, "Argument selector must be non-NULL");
|
|
|
|
CCAssert(pTarget, "Argument target must be non-NULL");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
tHashTimerEntry *pElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForTimers, &pTarget, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (! pElement)
|
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
pElement = (tHashTimerEntry *)calloc(sizeof(*pElement), 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
pElement->target = pTarget;
|
|
|
|
if (pTarget)
|
|
|
|
{
|
|
|
|
pTarget->retain();
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_ADD_INT(_hashForTimers, target, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Is this the 1st element ? Then set the pause level to all the selectors of this target
|
|
|
|
pElement->paused = bPaused;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCAssert(pElement->paused == bPaused, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pElement->timers == NULL)
|
|
|
|
{
|
|
|
|
pElement->timers = ccArrayNew(10);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < pElement->timers->num; ++i)
|
|
|
|
{
|
2012-06-12 17:07:54 +08:00
|
|
|
CCTimer *timer = (CCTimer*)pElement->timers->arr[i];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
if (pfnSelector == timer->getSelector())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-08 14:11:48 +08:00
|
|
|
CCLOG("CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: %.4f to %.4f", timer->getInterval(), fInterval);
|
2012-11-14 18:05:15 +08:00
|
|
|
timer->setInterval(fInterval);
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ccArrayEnsureExtraCapacity(pElement->timers, 1);
|
|
|
|
}
|
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
CCTimer *pTimer = new CCTimer();
|
2012-04-19 14:35:52 +08:00
|
|
|
pTimer->initWithTarget(pTarget, pfnSelector, fInterval, repeat, delay);
|
|
|
|
ccArrayAppendObject(pElement->timers, pTimer);
|
|
|
|
pTimer->release();
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScheduler::unscheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// explicity handle nil arguments when removing an object
|
|
|
|
if (pTarget == 0 || pfnSelector == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//CCAssert(pTarget);
|
|
|
|
//CCAssert(pfnSelector);
|
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
tHashTimerEntry *pElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForTimers, &pTarget, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pElement)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < pElement->timers->num; ++i)
|
|
|
|
{
|
2012-06-12 17:07:54 +08:00
|
|
|
CCTimer *pTimer = (CCTimer*)(pElement->timers->arr[i]);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
if (pfnSelector == pTimer->getSelector())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (pTimer == pElement->currentTimer && (! pElement->currentTimerSalvaged))
|
|
|
|
{
|
|
|
|
pElement->currentTimer->retain();
|
|
|
|
pElement->currentTimerSalvaged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccArrayRemoveObjectAtIndex(pElement->timers, i, true);
|
|
|
|
|
|
|
|
// update timerIndex in case we are in tick:, looping over the actions
|
|
|
|
if (pElement->timerIndex >= i)
|
|
|
|
{
|
|
|
|
pElement->timerIndex--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pElement->timers->num == 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_currentTarget == pElement)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTargetSalvaged = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
removeHashElement(pElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScheduler::priorityIn(tListEntry **ppList, CCObject *pTarget, int nPriority, bool bPaused)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
tListEntry *pListElement = (tListEntry *)malloc(sizeof(*pListElement));
|
|
|
|
|
|
|
|
pListElement->target = pTarget;
|
|
|
|
pListElement->priority = nPriority;
|
|
|
|
pListElement->paused = bPaused;
|
|
|
|
pListElement->next = pListElement->prev = NULL;
|
|
|
|
pListElement->markedForDeletion = false;
|
|
|
|
|
2012-09-16 05:19:14 +08:00
|
|
|
// empty list ?
|
2012-04-19 14:35:52 +08:00
|
|
|
if (! *ppList)
|
|
|
|
{
|
|
|
|
DL_APPEND(*ppList, pListElement);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool bAdded = false;
|
|
|
|
|
|
|
|
for (tListEntry *pElement = *ppList; pElement; pElement = pElement->next)
|
|
|
|
{
|
|
|
|
if (nPriority < pElement->priority)
|
|
|
|
{
|
|
|
|
if (pElement == *ppList)
|
|
|
|
{
|
|
|
|
DL_PREPEND(*ppList, pListElement);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pListElement->next = pElement;
|
|
|
|
pListElement->prev = pElement->prev;
|
|
|
|
|
|
|
|
pElement->prev->next = pListElement;
|
|
|
|
pElement->prev = pListElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
bAdded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not added? priority has the higher value. Append it.
|
|
|
|
if (! bAdded)
|
|
|
|
{
|
|
|
|
DL_APPEND(*ppList, pListElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update hash entry for quick access
|
|
|
|
tHashUpdateEntry *pHashElement = (tHashUpdateEntry *)calloc(sizeof(*pHashElement), 1);
|
|
|
|
pHashElement->target = pTarget;
|
|
|
|
pTarget->retain();
|
|
|
|
pHashElement->list = ppList;
|
|
|
|
pHashElement->entry = pListElement;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_ADD_INT(_hashForUpdates, target, pHashElement);
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScheduler::appendIn(_listEntry **ppList, CCObject *pTarget, bool bPaused)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
tListEntry *pListElement = (tListEntry *)malloc(sizeof(*pListElement));
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
pListElement->target = pTarget;
|
|
|
|
pListElement->paused = bPaused;
|
|
|
|
pListElement->markedForDeletion = false;
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
DL_APPEND(*ppList, pListElement);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// update hash entry for quicker access
|
|
|
|
tHashUpdateEntry *pHashElement = (tHashUpdateEntry *)calloc(sizeof(*pHashElement), 1);
|
|
|
|
pHashElement->target = pTarget;
|
|
|
|
pTarget->retain();
|
|
|
|
pHashElement->list = ppList;
|
|
|
|
pHashElement->entry = pListElement;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_ADD_INT(_hashForUpdates, target, pHashElement);
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScheduler::scheduleUpdateForTarget(CCObject *pTarget, int nPriority, bool bPaused)
|
|
|
|
{
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
tHashUpdateEntry *pHashElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForUpdates, &pTarget, pHashElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pHashElement)
|
|
|
|
{
|
2012-02-01 16:45:23 +08:00
|
|
|
#if COCOS2D_DEBUG >= 1
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(pHashElement->entry->markedForDeletion,"");
|
2012-02-01 16:45:23 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
// TODO: check if priority has changed!
|
|
|
|
|
|
|
|
pHashElement->entry->markedForDeletion = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// most of the updates are going to be 0, that's way there
|
|
|
|
// is an special list for updates with priority 0
|
|
|
|
if (nPriority == 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
appendIn(&_updates0List, pTarget, bPaused);
|
2013-03-18 15:29:53 +08:00
|
|
|
}
|
|
|
|
else if (nPriority < 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
priorityIn(&_updatesNegList, pTarget, nPriority, bPaused);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// priority > 0
|
2013-06-15 14:03:30 +08:00
|
|
|
priorityIn(&_updatesPosList, pTarget, nPriority, bPaused);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScheduler::removeUpdateFromHash(struct _listEntry *entry)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
tHashUpdateEntry *element = NULL;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForUpdates, &entry->target, element);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (element)
|
|
|
|
{
|
|
|
|
// list entry
|
|
|
|
DL_DELETE(*element->list, element->entry);
|
|
|
|
free(element->entry);
|
|
|
|
|
|
|
|
// hash entry
|
2012-07-01 21:17:44 +08:00
|
|
|
CCObject* pTarget = element->target;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_DEL(_hashForUpdates, element);
|
2012-04-19 14:35:52 +08:00
|
|
|
free(element);
|
2012-07-01 21:17:44 +08:00
|
|
|
|
2012-07-23 22:49:11 +08:00
|
|
|
// target#release should be the last one to prevent
|
2012-07-01 21:17:44 +08:00
|
|
|
// a possible double-free. eg: If the [target dealloc] might want to remove it itself from there
|
|
|
|
pTarget->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScheduler::unscheduleUpdateForTarget(const CCObject *pTarget)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pTarget == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tHashUpdateEntry *pElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForUpdates, &pTarget, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pElement)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_updateHashLocked)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pElement->entry->markedForDeletion = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->removeUpdateFromHash(pElement->entry);
|
|
|
|
}
|
|
|
|
}
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
void CCScheduler::unscheduleAll(void)
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
unscheduleAllWithMinPriority(kCCPrioritySystem);
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
void CCScheduler::unscheduleAllWithMinPriority(int nMinPriority)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// Custom Selectors
|
2012-11-14 18:05:15 +08:00
|
|
|
tHashTimerEntry *pElement = NULL;
|
|
|
|
tHashTimerEntry *pNextElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
for (pElement = _hashForTimers; pElement != NULL;)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// pElement may be removed in unscheduleAllSelectorsForTarget
|
2012-11-14 18:05:15 +08:00
|
|
|
pNextElement = (tHashTimerEntry *)pElement->hh.next;
|
|
|
|
unscheduleAllForTarget(pElement->target);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
pElement = pNextElement;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Updates selectors
|
|
|
|
tListEntry *pEntry, *pTmp;
|
2012-06-08 14:11:48 +08:00
|
|
|
if(nMinPriority < 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE(_updatesNegList, pEntry, pTmp)
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
|
|
|
if(pEntry->priority >= nMinPriority)
|
|
|
|
{
|
|
|
|
unscheduleUpdateForTarget(pEntry->target);
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-08 14:11:48 +08:00
|
|
|
|
|
|
|
if(nMinPriority <= 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE(_updates0List, pEntry, pTmp)
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
|
|
|
unscheduleUpdateForTarget(pEntry->target);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE(_updatesPosList, pEntry, pTmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-08 14:11:48 +08:00
|
|
|
if(pEntry->priority >= nMinPriority)
|
|
|
|
{
|
|
|
|
unscheduleUpdateForTarget(pEntry->target);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-02-06 10:54:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_scriptHandlerEntries)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scriptHandlerEntries->removeAllObjects();
|
2012-02-09 14:07:11 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
void CCScheduler::unscheduleAllForTarget(CCObject *pTarget)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// explicit NULL handling
|
|
|
|
if (pTarget == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom Selectors
|
2012-11-14 18:05:15 +08:00
|
|
|
tHashTimerEntry *pElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForTimers, &pTarget, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pElement)
|
|
|
|
{
|
|
|
|
if (ccArrayContainsObject(pElement->timers, pElement->currentTimer)
|
|
|
|
&& (! pElement->currentTimerSalvaged))
|
|
|
|
{
|
|
|
|
pElement->currentTimer->retain();
|
|
|
|
pElement->currentTimerSalvaged = true;
|
|
|
|
}
|
|
|
|
ccArrayRemoveAllObjects(pElement->timers);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_currentTarget == pElement)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTargetSalvaged = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
removeHashElement(pElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update selector
|
|
|
|
unscheduleUpdateForTarget(pTarget);
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
unsigned int CCScheduler::scheduleScriptFunc(unsigned int nHandler, float fInterval, bool bPaused)
|
2012-02-02 15:58:10 +08:00
|
|
|
{
|
2012-08-28 12:08:15 +08:00
|
|
|
CCSchedulerScriptHandlerEntry* pEntry = CCSchedulerScriptHandlerEntry::create(nHandler, fInterval, bPaused);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_scriptHandlerEntries)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scriptHandlerEntries = CCArray::createWithCapacity(20);
|
|
|
|
_scriptHandlerEntries->retain();
|
2012-02-09 14:07:11 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_scriptHandlerEntries->addObject(pEntry);
|
2012-08-28 12:08:15 +08:00
|
|
|
return pEntry->getEntryId();
|
2012-02-02 15:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScheduler::unscheduleScriptEntry(unsigned int uScheduleScriptEntryID)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for (int i = _scriptHandlerEntries->count() - 1; i >= 0; i--)
|
2012-02-02 15:58:10 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCSchedulerScriptHandlerEntry* pEntry = static_cast<CCSchedulerScriptHandlerEntry*>(_scriptHandlerEntries->objectAtIndex(i));
|
2013-03-03 10:32:09 +08:00
|
|
|
if (pEntry->getEntryId() == (int)uScheduleScriptEntryID)
|
2012-02-02 15:58:10 +08:00
|
|
|
{
|
|
|
|
pEntry->markedForDeletion();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
void CCScheduler::resumeTarget(CCObject *pTarget)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(pTarget != NULL, "");
|
|
|
|
|
|
|
|
// custom selectors
|
2012-11-14 18:05:15 +08:00
|
|
|
tHashTimerEntry *pElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForTimers, &pTarget, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pElement)
|
|
|
|
{
|
|
|
|
pElement->paused = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update selector
|
|
|
|
tHashUpdateEntry *pElementUpdate = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForUpdates, &pTarget, pElementUpdate);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pElementUpdate)
|
|
|
|
{
|
|
|
|
CCAssert(pElementUpdate->entry != NULL, "");
|
|
|
|
pElementUpdate->entry->paused = false;
|
|
|
|
}
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCScheduler::pauseTarget(CCObject *pTarget)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(pTarget != NULL, "");
|
|
|
|
|
|
|
|
// custom selectors
|
2012-11-14 18:05:15 +08:00
|
|
|
tHashTimerEntry *pElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForTimers, &pTarget, pElement);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pElement)
|
|
|
|
{
|
|
|
|
pElement->paused = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update selector
|
|
|
|
tHashUpdateEntry *pElementUpdate = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForUpdates, &pTarget, pElementUpdate);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pElementUpdate)
|
|
|
|
{
|
|
|
|
CCAssert(pElementUpdate->entry != NULL, "");
|
|
|
|
pElementUpdate->entry->paused = true;
|
|
|
|
}
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCScheduler::isTargetPaused(CCObject *pTarget)
|
|
|
|
{
|
|
|
|
CCAssert( pTarget != NULL, "target must be non nil" );
|
|
|
|
|
|
|
|
// Custom selectors
|
2012-11-14 18:05:15 +08:00
|
|
|
tHashTimerEntry *pElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForTimers, &pTarget, pElement);
|
2012-02-01 16:45:23 +08:00
|
|
|
if( pElement )
|
|
|
|
{
|
|
|
|
return pElement->paused;
|
|
|
|
}
|
2012-11-14 18:05:15 +08:00
|
|
|
|
|
|
|
// We should check update selectors if target does not have custom selectors
|
|
|
|
tHashUpdateEntry *elementUpdate = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_INT(_hashForUpdates, &pTarget, elementUpdate);
|
2012-11-14 18:05:15 +08:00
|
|
|
if ( elementUpdate )
|
|
|
|
{
|
|
|
|
return elementUpdate->entry->paused;
|
|
|
|
}
|
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
return false; // should never get here
|
|
|
|
}
|
|
|
|
|
2012-06-14 05:26:28 +08:00
|
|
|
CCSet* CCScheduler::pauseAllTargets()
|
|
|
|
{
|
|
|
|
return pauseAllTargetsWithMinPriority(kCCPrioritySystem);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCSet* CCScheduler::pauseAllTargetsWithMinPriority(int nMinPriority)
|
|
|
|
{
|
|
|
|
CCSet* idsWithSelectors = new CCSet();// setWithCapacity:50];
|
|
|
|
idsWithSelectors->autorelease();
|
|
|
|
|
|
|
|
// Custom Selectors
|
2013-06-15 14:03:30 +08:00
|
|
|
for(tHashTimerEntry *element = _hashForTimers; element != NULL;
|
2012-11-14 18:05:15 +08:00
|
|
|
element = (tHashTimerEntry*)element->hh.next)
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
element->paused = true;
|
|
|
|
idsWithSelectors->addObject(element->target);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates selectors
|
|
|
|
tListEntry *entry, *tmp;
|
|
|
|
if(nMinPriority < 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE( _updatesNegList, entry, tmp )
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
if(entry->priority >= nMinPriority)
|
|
|
|
{
|
|
|
|
entry->paused = true;
|
|
|
|
idsWithSelectors->addObject(entry->target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nMinPriority <= 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE( _updates0List, entry, tmp )
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
entry->paused = true;
|
|
|
|
idsWithSelectors->addObject(entry->target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE( _updatesPosList, entry, tmp )
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
if(entry->priority >= nMinPriority)
|
|
|
|
{
|
|
|
|
entry->paused = true;
|
|
|
|
idsWithSelectors->addObject(entry->target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return idsWithSelectors;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCScheduler::resumeTargets(CCSet* pTargetsToResume)
|
|
|
|
{
|
|
|
|
CCSetIterator iter;
|
2012-06-08 14:11:48 +08:00
|
|
|
for (iter = pTargetsToResume->begin(); iter != pTargetsToResume->end(); ++iter)
|
|
|
|
{
|
|
|
|
resumeTarget(*iter);
|
2012-06-14 05:26:28 +08:00
|
|
|
}
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
// main loop
|
2012-06-08 13:55:28 +08:00
|
|
|
void CCScheduler::update(float dt)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_updateHashLocked = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_timeScale != 1.0f)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
dt *= _timeScale;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-09-16 05:19:14 +08:00
|
|
|
// Iterate over all the Updates' selectors
|
2012-04-19 14:35:52 +08:00
|
|
|
tListEntry *pEntry, *pTmp;
|
|
|
|
|
|
|
|
// updates with priority < 0
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE(_updatesNegList, pEntry, pTmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if ((! pEntry->paused) && (! pEntry->markedForDeletion))
|
|
|
|
{
|
|
|
|
pEntry->target->update(dt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updates with priority == 0
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE(_updates0List, pEntry, pTmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if ((! pEntry->paused) && (! pEntry->markedForDeletion))
|
|
|
|
{
|
2012-12-10 13:48:27 +08:00
|
|
|
pEntry->target->update(dt);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updates with priority > 0
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE(_updatesPosList, pEntry, pTmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if ((! pEntry->paused) && (! pEntry->markedForDeletion))
|
|
|
|
{
|
2012-12-10 13:48:27 +08:00
|
|
|
pEntry->target->update(dt);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-16 05:19:14 +08:00
|
|
|
// Iterate over all the custom selectors
|
2013-06-15 14:03:30 +08:00
|
|
|
for (tHashTimerEntry *elt = _hashForTimers; elt != NULL; )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTarget = elt;
|
|
|
|
_currentTargetSalvaged = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _currentTarget->paused)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// The 'timers' array may change while inside this loop
|
|
|
|
for (elt->timerIndex = 0; elt->timerIndex < elt->timers->num; ++(elt->timerIndex))
|
|
|
|
{
|
2012-06-12 17:07:54 +08:00
|
|
|
elt->currentTimer = (CCTimer*)(elt->timers->arr[elt->timerIndex]);
|
2012-04-19 14:35:52 +08:00
|
|
|
elt->currentTimerSalvaged = false;
|
|
|
|
|
|
|
|
elt->currentTimer->update(dt);
|
|
|
|
|
|
|
|
if (elt->currentTimerSalvaged)
|
|
|
|
{
|
|
|
|
// The currentTimer told the remove itself. To prevent the timer from
|
|
|
|
// accidentally deallocating itself before finishing its step, we retained
|
|
|
|
// it. Now that step is done, it's safe to release it.
|
|
|
|
elt->currentTimer->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
elt->currentTimer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// elt, at this moment, is still valid
|
|
|
|
// so it is safe to ask this here (issue #490)
|
2012-11-14 18:05:15 +08:00
|
|
|
elt = (tHashTimerEntry *)elt->hh.next;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// only delete currentTarget if no actions were scheduled during the cycle (issue #481)
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_currentTargetSalvaged && _currentTarget->timers->num == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
removeHashElement(_currentTarget);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-06 10:54:51 +08:00
|
|
|
|
2012-09-16 05:19:14 +08:00
|
|
|
// Iterate over all the script callbacks
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_scriptHandlerEntries)
|
2012-02-02 15:58:10 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for (int i = _scriptHandlerEntries->count() - 1; i >= 0; i--)
|
2012-02-02 15:58:10 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCSchedulerScriptHandlerEntry* pEntry = static_cast<CCSchedulerScriptHandlerEntry*>(_scriptHandlerEntries->objectAtIndex(i));
|
2012-02-09 14:07:11 +08:00
|
|
|
if (pEntry->isMarkedForDeletion())
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scriptHandlerEntries->removeObjectAtIndex(i);
|
2012-02-09 14:07:11 +08:00
|
|
|
}
|
|
|
|
else if (!pEntry->isPaused())
|
|
|
|
{
|
|
|
|
pEntry->getTimer()->update(dt);
|
|
|
|
}
|
2012-02-02 15:58:10 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-09-16 05:19:14 +08:00
|
|
|
// delete all updates that are marked for deletion
|
2012-04-19 14:35:52 +08:00
|
|
|
// updates with priority < 0
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE(_updatesNegList, pEntry, pTmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (pEntry->markedForDeletion)
|
|
|
|
{
|
|
|
|
this->removeUpdateFromHash(pEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updates with priority == 0
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE(_updates0List, pEntry, pTmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (pEntry->markedForDeletion)
|
|
|
|
{
|
|
|
|
this->removeUpdateFromHash(pEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updates with priority > 0
|
2013-06-15 14:03:30 +08:00
|
|
|
DL_FOREACH_SAFE(_updatesPosList, pEntry, pTmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (pEntry->markedForDeletion)
|
|
|
|
{
|
|
|
|
this->removeUpdateFromHash(pEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_updateHashLocked = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTarget = NULL;
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|