cocos2dx 2.x定时器分析(2)

1、分析下update类型,即每帧都调用的定时器,如何实现:

//CCScheduler中的成员变量
struct _listEntry *m_pUpdates0List;            // list priority == 0

-->>存放update类型定时器的结构
// A list double-linked list used for "updates with priority"
typedef struct _listEntry
{
    struct _listEntry   *prev, *next;
    CCObject            *target;        // not retained (retained by hashUpdateEntry)
    int                 priority;
    bool                paused;
    bool                markedForDeletion; // selector will no longer be called and entry will be removed at end of the next tick
} tListEntry;
2、添加update类型的定时器
    /** 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
     @lua NA
     */
    void scheduleUpdateForTarget(CCObject *pTarget, int nPriority, bool bPaused);

    -->>
    void CCScheduler::scheduleUpdateForTarget(CCObject *pTarget, int nPriority, bool bPaused)
{

    //先判断是否已经加入
    tHashUpdateEntry *pHashElement = NULL;
    HASH_FIND_INT(m_pHashForUpdates, &pTarget, pHashElement);
    if (pHashElement)
    {
#if COCOS2D_DEBUG >= 1
        CCAssert(pHashElement->entry->markedForDeletion,"");
#endif
        // TODO: check if priority has changed!
        pHashElement->entry->paused = bPaused;
        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)
    {
        appendIn(&m_pUpdates0List, pTarget, bPaused);
    }
    else if (nPriority < 0)
    {
        priorityIn(&m_pUpdatesNegList, pTarget, nPriority, bPaused);
    }
    else
    {
        // priority > 0
        priorityIn(&m_pUpdatesPosList, pTarget, nPriority, bPaused);
    }
}

-->>分析下appendIn(&m_pUpdates0List, pTarget, bPaused)这个,其他连个类似,只不过
加入了优先级的概念,需要根据优先级确定插入位置,但是都为加入到快速查找哈希链表中,
而且同样会retain。
void CCScheduler::appendIn(_listEntry **ppList, CCObject *pTarget, bool bPaused)
{
    //分配tListEntry
    tListEntry *pListElement = (tListEntry *)malloc(sizeof(*pListElement));

    pListElement->target = pTarget;
    pListElement->paused = bPaused;
    pListElement->markedForDeletion = false;
    //添加进列表
    DL_APPEND(*ppList, pListElement);
    
    //添加到快速查找哈希链表中
    // update hash entry for quicker access
    tHashUpdateEntry *pHashElement = (tHashUpdateEntry *)calloc(sizeof(*pHashElement), 1);
    pHashElement->target = pTarget;
    pTarget->retain();  //有一次retain对象的操作,会影响对象的释放
    pHashElement->list = ppList;
    pHashElement->entry = pListElement;
    HASH_ADD_INT(m_pHashForUpdates, target, pHashElement);
}

3、移除update类型的定时器
   /** Unschedules the update selector for a given target
     @since v0.99.3
     @lua NA
     */
    void unscheduleUpdateForTarget(const CCObject *pTarget);

    -->>
    void CCScheduler::unscheduleUpdateForTarget(const CCObject *pTarget)
{
    if (pTarget == NULL)
    {
        return;
    }
    //如果哈希表中有
    tHashUpdateEntry *pElement = NULL;
    HASH_FIND_INT(m_pHashForUpdates, &pTarget, pElement);
    if (pElement)
    {
        if (m_bUpdateHashLocked)
        {
            pElement->entry->markedForDeletion = true; //标记为移除
        }
        else
        {
            this->removeUpdateFromHash(pElement->entry);//真正的移除
        }
    }
}

-->>
void CCScheduler::removeUpdateFromHash(struct _listEntry *entry)
{
    tHashUpdateEntry *element = NULL;

    HASH_FIND_INT(m_pHashForUpdates, &entry->target, element);
    if (element)
    {
        // list entry,从链表移除
        DL_DELETE(*element->list, element->entry);
        free(element->entry);

        // hash entry,从哈希表移除
        CCObject* pTarget = element->target;
        HASH_DEL(m_pHashForUpdates, element);
        free(element);

        // target#release should be the last one to prevent
        // a possible double-free. eg: If the [target dealloc] might want to remove it itself from there
        pTarget->release(); //对对象执行一次release操作
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值