zoukankan      html  css  js  c++  java
  • 写的一个简单定时器(非独立线程)

    //Callback.h
    #ifndef __CALLBACK_H__
    #define __CALLBACK_H__
    
    typedef void (*T_CallBack)(void *);
    typedef struct  
    {
        T_CallBack cb;
        void *obj;
    }ST_CallBack;
    
    int __NewTimer(void* obj, int interval, bool isloop, T_CallBack cb);
    void __DeleteTimer(int handle);
    
    
    #define NewTimer(handle,obj,delay,isloop,CLASS,MEMBER) do{ 
    union {       
        T_CallBack cb; 
        void (CLASS::*___M)(void); 
    }___T; 
        ___T.___M = &CLASS::MEMBER;    
        handle = __NewTimer(obj, delay, isloop, ___T.cb); 
    }while(0)
    
    #define DeleteTimer(handle) do{ 
        if ( handle) { 
            __DeleteTimer(handle); 
            handle = 0; 
        } 
    }while(0)
    
    //#define NewTimer
    
    int TimerGetDelay(int handle);
    void TimerSetDelay(int handle, int delay);
    void TimerStart(int handle);
    void TimerStop(int handle);
    
    
    #endif

    //Callback.cpp
    
    
    typedef struct  
    {    
        Uint32 lastticks;    
        bool isloop;
        int delay;
        ST_CallBack cb;    
        bool isdel;
        bool ispause;
    }ST_TimerTask;
    
    static std::list<ST_TimerTask*> timerTasks; 
    
    int __NewTimer(void* obj, int delay, bool isloop, T_CallBack cb)
    {  
        ST_TimerTask *task;
    
        NEW_OBJ(task, ST_TimerTask);
        task->isloop = isloop;
        task->delay = delay;
        task->cb.obj = obj;
        task->cb.cb = (T_CallBack)cb;
        task->lastticks = SDL_GetTicks();
        task->isdel = false;    
        task->ispause = false;
        timerTasks.push_back(task);
        return (int)task;
    }
    
    void __DeleteTimer(int handle)
    {
        std::list<ST_TimerTask*>::iterator iter;
        ST_TimerTask *task;
        for (iter = timerTasks.begin(); iter != timerTasks.end(); iter++)    {
            task = (*iter);
            if ( (int)task == handle){
                task->isdel = true;
                return;
            }
        }
    }
    
    void DeleteAllTimer(void)
    {
        std::list<ST_TimerTask*>::iterator iter;
        ST_TimerTask *task;
        for (iter = timerTasks.begin(); iter != timerTasks.end();)    {
            task = (*iter);
            if ( task){
                iter = timerTasks.erase(iter);        
                DELETE_OBJ(task);
            }else
                iter++;
        }
        timerTasks.clear();
    }
    
    void TimerRunning(Uint32 curticks)
    {
        std::list<ST_TimerTask*>::iterator iter;
        ST_TimerTask *task;
        for (iter = timerTasks.begin(); iter != timerTasks.end(); )    {
            task = (*iter);
            if ( task) { 
                if(curticks - task->lastticks >= task->delay){
                    if ( task->isdel == false && task->ispause == false)
                        task->cb.cb(task->cb.obj);                
                    if (task->isdel || task->isloop == false){
                        iter = timerTasks.erase(iter);
                        DELETE_OBJ(task);
                        continue;
                    }
                    task->lastticks = curticks;            
                }            
            }
            iter++;
        }
    }
    
    int TimerGetDelay(int handle)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        return task->delay;
    }
    
    void TimerSetDelay(int handle, int delay)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        task->delay = delay;
    }
    void TimerStart(int handle)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        task->ispause = false;
    }
    void TimerStop(int handle)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        task->ispause = true;
    }
    
    
    
     
  • 相关阅读:
    [NOI 2011][BZOJ 2434] 阿狸的打字机
    列出cocos2dx运行场景所有节点信息
    png 转 eps (电脑已安装latex环境前提下)
    latex小记
    第二篇
    博客第一篇
    百度动态规划
    百度约瑟夫算环
    转~最大连续子序列求和
    Oracle学习笔记——一对多的关系关联查询时只关联查找最新的第一条数据
  • 原文地址:https://www.cnblogs.com/colife/p/3779210.html
Copyright © 2011-2022 走看看