• 写的一个简单定时器(非独立线程)


    //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;
    }
    
    
    
     
  • 相关阅读:
    【转载】[教程]OpenSEES超简单易懂的入门第一课
    【转载】面向对象的非线性有限元方法
    与李文雄老师讨论有限元
    【转载】 Moving Beyond OpenGL 1.1 for Windows
    【转载】国外免费期刊全文数据库
    与李文雄老师讨论学术研究
    【转载】VS 2010和.NET 4.0之WPF 4改进全解析
    【转载】MFC中SDI、MDI框架各部分指针获取(网上找的,好东西大家一起分享,多谢原创作者!)
    【转载】一位院士——搞科研的几个条件
    Visual Studio 2010 step by step学习摘要
  • 原文地址:https://www.cnblogs.com/colife/p/3779210.html
Copyright © 2020-2023  润新知