* add CCLuaSupport.cpp/.h, CCSchedulerFuncEntry class

* move CCTimer from CCScheduler.cpp/.h to CCTimer.cpp/.h
This commit is contained in:
YuLei Liao 2011-11-20 16:38:27 +08:00
parent 01bf6b57f4
commit 57bf9453b7
7 changed files with 371 additions and 227 deletions

47
cocos2dx/CCLuaSupport.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "CCLuaSupport.h"
#include "CCArray.h"
#include "CCTimer.h"
#include "LuaEngine.h"
using namespace cocos2d;
CCSchedulerFuncEntry* CCSchedulerFuncEntry::entryWithRefID(int refID, ccTime fInterval, bool bPaused)
{
CCSchedulerFuncEntry* entry = new CCSchedulerFuncEntry();
entry->initWithRefID(refID, fInterval, bPaused);
entry->autorelease();
return entry;
}
bool CCSchedulerFuncEntry::initWithRefID(int refID, ccTime fInterval, bool bPaused)
{
LuaEngine::sharedEngine()->retainRefID(refID);
m_timer = new CCTimer();
m_timer->initWithScriptFunc(refID, fInterval);
m_timer->autorelease();
m_timer->retain();
m_refID = refID;
m_paused = bPaused;
return true;
}
CCSchedulerFuncEntry::CCSchedulerFuncEntry(void)
: m_timer(NULL)
, m_refID(0)
, m_paused(true)
, m_isMarkDeleted(false)
{
static int handleCount = 0;
++handleCount;
m_handle = handleCount;
}
CCSchedulerFuncEntry::~CCSchedulerFuncEntry(void)
{
m_timer->release();
LuaEngine::sharedEngine()->releaseRefID(m_refID);
CCLOG("CCSchedulerFuncEntry::~CCSchedulerFuncEntry() - remove script entry, handle: %d, refid: %d",
m_handle, m_refID);
}

View File

@ -25,12 +25,13 @@
****************************************************************************/
#include "CCScheduler.h"
#include "CCTimer.h"
#include "ccMacros.h"
#include "support/data_support/utlist.h"
#include "support/data_support/ccCArray.h"
#include "CCMutableArray.h"
#include <assert.h>
#include "support/data_support/uthash.h"
#include "CCArray.h"
#include "CCLuaSupport.h"
#include "LuaEngine.h"
using namespace std;
@ -69,132 +70,21 @@ typedef struct _hashSelectorEntry
UT_hash_handle hh;
} tHashSelectorEntry;
typedef struct CCSchedulerFuncEntry
{
CCTimer* timer;
bool paused;
int refID;
} _CCSchedulerFuncEntry;
// implementation CCTimer
CCTimer::CCTimer()
: m_pTarget(NULL)
, m_refID(0)
, m_fInterval(0.0f)
, m_fElapsed(0.0f)
, m_pfnSelector(NULL)
{
}
CCTimer::~CCTimer()
{
if (m_refID)
{
LuaEngine::sharedEngine()->releaseRefID(m_refID);
}
}
CCTimer* CCTimer::timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector)
{
CCTimer *pTimer = new CCTimer();
pTimer->initWithTarget(pTarget, pfnSelector);
pTimer->autorelease();
return pTimer;
}
CCTimer* CCTimer::timerWithScriptFunc(int refid, ccTime fSeconds)
{
CCTimer *pTimer = new CCTimer();
pTimer->initWithScriptFunc(refid, fSeconds);
pTimer->autorelease();
return pTimer;
}
CCTimer* CCTimer::timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds)
{
CCTimer *pTimer = new CCTimer();
pTimer->initWithTarget(pTarget, pfnSelector, fSeconds);
pTimer->autorelease();
return pTimer;
}
bool CCTimer::initWithScriptFunc(int newRefID, ccTime fSeconds)
{
LuaEngine::sharedEngine()->retainRefID(newRefID);
if (m_refID)
{
LuaEngine::sharedEngine()->releaseRefID(m_refID);
}
m_refID = newRefID;
m_fInterval = fSeconds;
m_fElapsed = -1;
return true;
}
bool CCTimer::initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector)
{
return initWithTarget(pTarget, pfnSelector, 0);
}
bool CCTimer::initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds)
{
m_pTarget = pTarget;
m_pfnSelector = pfnSelector;
m_fElapsed = -1;
m_fInterval = fSeconds;
return true;
}
void CCTimer::update(ccTime dt)
{
if (m_fElapsed == -1)
{
m_fElapsed = 0;
}
else
{
m_fElapsed += dt;
}
if (m_fElapsed >= m_fInterval)
{
if (m_pfnSelector)
{
(m_pTarget->*m_pfnSelector)(m_fElapsed);
m_fElapsed = 0;
}
if (m_refID)
{
LuaEngine::sharedEngine()->executeSchedule(m_refID, m_fElapsed);
m_fElapsed = 0;
}
}
}
// implementation of CCScheduler
static CCScheduler *pSharedScheduler;
CCScheduler::CCScheduler(void)
: m_fTimeScale(0.0)
, m_pUpdatesNegList(NULL)
, m_pUpdates0List(NULL)
, m_pUpdatesPosList(NULL)
, m_pHashForUpdates(NULL)
, m_pHashForSelectors(NULL)
, m_pCurrentTarget(NULL)
, m_bCurrentTargetSalvaged(false)
, m_scriptHandleCount(0)
: m_fTimeScale(0.0)
, m_pUpdatesNegList(NULL)
, m_pUpdates0List(NULL)
, m_pUpdatesPosList(NULL)
, m_pHashForUpdates(NULL)
, m_pHashForSelectors(NULL)
, m_pCurrentTarget(NULL)
, m_bCurrentTargetSalvaged(false)
, m_scriptFunctions(NULL)
{
assert(pSharedScheduler == NULL);
}
@ -202,7 +92,9 @@ CCScheduler::CCScheduler(void)
CCScheduler::~CCScheduler(void)
{
unscheduleAllSelectors();
unscheduleScriptFunctions();
pSharedScheduler = NULL;
m_scriptFunctions->release();
}
CCScheduler* CCScheduler::sharedScheduler(void)
@ -236,7 +128,8 @@ bool CCScheduler::init(void)
m_pHashForSelectors = NULL;
m_bUpdateHashLocked = false;
clearScriptFunctions();
m_scriptFunctions = CCArray::arrayWithCapacity(20);
m_scriptFunctions->retain();
return true;
}
@ -304,47 +197,28 @@ void CCScheduler::scheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol *p
int CCScheduler::scheduleScriptFunc(int refID, ccTime fInterval, bool bPaused)
{
m_scriptHandleCount++;
LuaEngine::sharedEngine()->retainRefID(refID);
CCSchedulerFuncEntry* entry = new CCSchedulerFuncEntry();
entry->refID = refID;
entry->paused = bPaused;
entry->timer = new CCTimer();
entry->timer->initWithScriptFunc(refID, fInterval);
m_scriptFunctions[m_scriptHandleCount] = entry;
CCSchedulerFuncEntry* entry = CCSchedulerFuncEntry::entryWithRefID(refID, fInterval, bPaused);
m_scriptFunctions->addObject(entry);
CCLOG("CCScheduler::scheduleScriptFunc() - add script entry, handle: %d, refid: %d", m_scriptHandleCount, refID);
return m_scriptHandleCount;
CCLOG("CCScheduler::scheduleScriptFunc() - add script entry, handle: %d, refid: %d", entry->getHandle(), refID);
return entry->getHandle();
}
void CCScheduler::unscheduleScriptFunc(int handle)
{
std::map<int, CCSchedulerFuncEntry*>::iterator it = m_scriptFunctions.find(handle);
if (it != m_scriptFunctions.end())
for (int i = m_scriptFunctions->count() - 1; i >= 0; i--)
{
CCSchedulerFuncEntry* entry = it->second;
CCLOG("CCScheduler::unscheduleScriptFunc() - remove script entry, handle: %d", it->first);
LuaEngine::sharedEngine()->releaseRefID(entry->refID);
delete entry->timer;
m_scriptFunctions.erase(it);
CCSchedulerFuncEntry* entry = (CCSchedulerFuncEntry*)m_scriptFunctions->objectAtIndex(i);
if (entry->getHandle() == handle)
{
entry->markDeleted();
}
}
}
void CCScheduler::clearScriptFunctions()
void CCScheduler::unscheduleScriptFunctions()
{
std::map<int, CCSchedulerFuncEntry*>::iterator it = m_scriptFunctions.begin();
while (it != m_scriptFunctions.end())
{
CCSchedulerFuncEntry* entry = it->second;
if (entry->refID)
{
LuaEngine::sharedEngine()->releaseRefID(entry->refID);
}
if (entry->timer) delete entry->timer;
delete entry;
}
m_scriptFunctions.clear();
m_scriptFunctions->removeAllObjects();
}
void CCScheduler::unscheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol *pTarget)
@ -578,7 +452,7 @@ void CCScheduler::unscheduleAllSelectors(void)
unscheduleUpdateForTarget(pEntry->target);
}
clearScriptFunctions();
unscheduleScriptFunctions();
}
void CCScheduler::unscheduleAllSelectorsForTarget(SelectorProtocol *pTarget)
@ -769,7 +643,7 @@ void CCScheduler::tick(ccTime dt)
{
if (pEntry->markedForDeletion)
{
// this->removeUpdateFromHash(pEntry);
// this->removeUpdateFromHash(pEntry);
}
}
@ -787,19 +661,20 @@ void CCScheduler::tick(ccTime dt)
m_pCurrentTarget = NULL;
// Interate all script functions
if (m_scriptFunctions.size())
for (int i = m_scriptFunctions->count() - 1; i >= 0; i--)
{
std::map<int, CCSchedulerFuncEntry*>::iterator it = m_scriptFunctions.begin();
while (it != m_scriptFunctions.end())
CCSchedulerFuncEntry* entry = (CCSchedulerFuncEntry*)m_scriptFunctions->objectAtIndex(i);
if (!entry->isMarkDeleted() && !entry->isPaused())
{
CCSchedulerFuncEntry* entry = it->second;
if (!entry->paused)
{
entry->timer->update(dt);
}
it++;
entry->getTimer()->update(dt);
}
}
for (int i = m_scriptFunctions->count() - 1; i >= 0; i--)
{
CCSchedulerFuncEntry* entry = (CCSchedulerFuncEntry*)m_scriptFunctions->objectAtIndex(i);
if (entry->isMarkDeleted()) m_scriptFunctions->removeObjectAtIndex(i);
}
}
void CCScheduler::purgeSharedScheduler(void)

136
cocos2dx/CCTimer.cpp Normal file
View File

@ -0,0 +1,136 @@
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
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 "CCTimer.h"
#include "LuaEngine.h"
namespace cocos2d
{
CCTimer::CCTimer()
: m_pTarget(NULL)
, m_refID(0)
, m_fInterval(0.0f)
, m_fElapsed(0.0f)
, m_pfnSelector(NULL)
{
}
CCTimer::~CCTimer()
{
if (m_refID)
{
LuaEngine::sharedEngine()->releaseRefID(m_refID);
}
}
CCTimer* CCTimer::timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector)
{
CCTimer *pTimer = new CCTimer();
pTimer->initWithTarget(pTarget, pfnSelector);
pTimer->autorelease();
return pTimer;
}
CCTimer* CCTimer::timerWithScriptFunc(int refid, ccTime fSeconds)
{
CCTimer *pTimer = new CCTimer();
pTimer->initWithScriptFunc(refid, fSeconds);
pTimer->autorelease();
return pTimer;
}
CCTimer* CCTimer::timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds)
{
CCTimer *pTimer = new CCTimer();
pTimer->initWithTarget(pTarget, pfnSelector, fSeconds);
pTimer->autorelease();
return pTimer;
}
bool CCTimer::initWithScriptFunc(int newRefID, ccTime fSeconds)
{
LuaEngine::sharedEngine()->retainRefID(newRefID);
if (m_refID)
{
LuaEngine::sharedEngine()->releaseRefID(m_refID);
}
m_refID = newRefID;
m_fInterval = fSeconds;
m_fElapsed = -1;
return true;
}
bool CCTimer::initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector)
{
return initWithTarget(pTarget, pfnSelector, 0);
}
bool CCTimer::initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds)
{
m_pTarget = pTarget;
m_pfnSelector = pfnSelector;
m_fElapsed = -1;
m_fInterval = fSeconds;
return true;
}
void CCTimer::update(ccTime dt)
{
if (m_fElapsed == -1)
{
m_fElapsed = 0;
}
else
{
m_fElapsed += dt;
}
if (m_fElapsed >= m_fInterval)
{
if (m_pfnSelector)
{
(m_pTarget->*m_pfnSelector)(m_fElapsed);
m_fElapsed = 0;
}
if (m_refID)
{
LuaEngine::sharedEngine()->executeSchedule(m_refID, m_fElapsed);
m_fElapsed = 0;
}
}
}
} // namespace cocos2d

View File

@ -0,0 +1,54 @@
#ifndef __CC_LUA_SUPPORT_
#define __CC_LUA_SUPPORT_
#include "CCObject.h"
#include "ccTypes.h"
namespace cocos2d
{
class CCTimer;
class CCSchedulerFuncEntry : public CCObject
{
public:
static CCSchedulerFuncEntry* entryWithRefID(int refID, ccTime fInterval, bool bPaused);
~CCSchedulerFuncEntry(void);
inline CCTimer* getTimer(void) {
return m_timer;
}
inline bool isPaused(void) {
return m_paused;
}
inline int getRefID(void) {
return m_refID;
}
inline int getHandle(void) {
return m_handle;
}
void markDeleted(void) {
m_isMarkDeleted = true;
}
bool isMarkDeleted(void) {
return m_isMarkDeleted;
}
private:
CCSchedulerFuncEntry(void);
bool initWithRefID(int refID, ccTime fInterval, bool bPaused);
CCTimer* m_timer;
bool m_paused;
int m_refID;
int m_handle;
bool m_isMarkDeleted;
};
}
#endif // __CC_LUA_SUPPORT_

View File

@ -27,67 +27,13 @@ THE SOFTWARE.
#ifndef __CCSCHEDULER_H__
#define __CCSCHEDULER_H__
#include <string>
#include "CCObject.h"
#include "selector_protocol.h"
#include "support/data_support/uthash.h"
#include "LuaEngine.h"
namespace cocos2d
{
struct CCSchedulerFuncEntry;
//
// CCTimer
//
/** @brief Light weight timer */
class CC_DLL CCTimer : public CCObject
{
public:
CCTimer(void);
~CCTimer();
/** get interval in seconds */
inline ccTime getInterval(void) {
return m_fInterval;
}
/** set interval in seconds */
inline void setInterval(ccTime fInterval) {
m_fInterval = fInterval;
}
/** Initializes a timer with a target and a selector. */
bool initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector);
/** Initializes a timer with a target, a selector and an interval in seconds. */
bool initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds);
bool initWithScriptFunc(int refid, ccTime fSeconds);
/** triggers the timer */
void update(ccTime dt);
public:
/** Allocates a timer with a target and a selector. */
static CCTimer* timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector);
/** Allocates a timer with a script function. */
static CCTimer* timerWithScriptFunc(int refid, ccTime fSeconds);
/** Allocates a timer with a target, a selector and an interval in seconds. */
static CCTimer* timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds);
public:
SEL_SCHEDULE m_pfnSelector;
ccTime m_fInterval;
int m_refID;
protected:
SelectorProtocol *m_pTarget;
ccTime m_fElapsed;
};
class CCArray;
//
// CCScheduler
@ -147,7 +93,7 @@ public:
/** Unschedule the script function
*/
void unscheduleScriptFunc(int handle);
void unscheduleScriptFunctions();
void scheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol *pTarget, ccTime fInterval, bool bPaused);
@ -219,7 +165,6 @@ private:
void priorityIn(struct _listEntry **ppList, SelectorProtocol *pTarget, int nPriority, bool bPaused);
void appendIn(struct _listEntry **ppList, SelectorProtocol *pTarget, bool bPaused);
void clearScriptFunctions();
protected:
ccTime m_fTimeScale;
@ -240,8 +185,7 @@ protected:
bool m_bUpdateHashLocked;
// Used for "script function call back with interval"
int m_scriptHandleCount;
std::map<int, CCSchedulerFuncEntry*> m_scriptFunctions;
CCArray* m_scriptFunctions;
};
} // namespace cocos2d

View File

@ -0,0 +1,87 @@
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
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.
****************************************************************************/
#ifndef __CCTIMER_H_
#define __CCTIMER_H_
#include "CCObject.h"
#include "selector_protocol.h"
namespace cocos2d
{
//
// CCTimer
//
/** @brief Light weight timer */
class CC_DLL CCTimer : public CCObject
{
public:
CCTimer(void);
~CCTimer();
/** get interval in seconds */
inline ccTime getInterval(void) {
return m_fInterval;
}
/** set interval in seconds */
inline void setInterval(ccTime fInterval) {
m_fInterval = fInterval;
}
/** Initializes a timer with a target and a selector. */
bool initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector);
/** Initializes a timer with a target, a selector and an interval in seconds. */
bool initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds);
bool initWithScriptFunc(int refid, ccTime fSeconds);
/** triggers the timer */
void update(ccTime dt);
public:
/** Allocates a timer with a target and a selector. */
static CCTimer* timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector);
/** Allocates a timer with a script function. */
static CCTimer* timerWithScriptFunc(int refid, ccTime fSeconds);
/** Allocates a timer with a target, a selector and an interval in seconds. */
static CCTimer* timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds);
public:
SEL_SCHEDULE m_pfnSelector;
ccTime m_fInterval;
int m_refID;
protected:
SelectorProtocol *m_pTarget;
ccTime m_fElapsed;
};
} // namespace cocos2d
#endif // __CCTIMER_H_

View File

@ -88,6 +88,7 @@ THE SOFTWARE.
#include "CCTouchDispatcher.h"
#include "CCDrawingPrimitives.h"
#include "CCScheduler.h"
#include "CCTimer.h"
// havn't implement on wophone and airplay
#if (CC_TARGET_PLATFORM != CC_PLATFORM_AIRPLAY)