//stop timer void CTimer::Stop() { //stop timer if it exists if(GetTimerID()) { //kill the timer SDL_RemoveTimer(GetTimerID()); //set timerid to 0 m_TimerID=0; } }
int CGameFlyControl::OnGameTimer(UINT_PTR nIDEvent) { int nTimer1=GetTimerID(BIRDMOVETIMER); int nTimer2=GetTimerID(BIRDFLYLEVELTIMER); if (nIDEvent == nTimer1) { DrawBirdBody(RGB(40,150,40)); birdBody->MoveBirdPos(); DrawBirdBody(dwBirdColor); if(birdBody->HasHitWall()) { KillAllTimer(); return -2; } if (birdBody->HasHitBird(gunControl->GetGunArry(),gunControl->GetGunWidth(),gunControl->GetGunHight())) { KillAllTimer(); return -2; } ClearLast(); DrawGunBody(RGB(40,150,40)); gunControl->MoveGun(); DrawGunBody(dwGunColor); if (gunControl->HasHitBird(birdBody->GetBirdRect())) { KillAllTimer(); return -2; } IncreaseScore(1); gunControl->HasHitWall(); gunControl->ReSetGunPos(); } if (nIDEvent == nTimer2) { if (nHardLevel<21) { nHardLevel++; gunControl->SetMaxGun(nHardLevel); } } if (nIDEvent==GetTimerID(GUNMOVETIMER)) { birdBody->ChangeBirdSpeed(bLButtonDown,0.6); } return 2; }
/* * fn: 添加指定ID号的定时器 * fun: 定时器回调函数 * arg: 定时器回调函数参数 * second: 函数每隔多少秒执行一次 * timerId: out,定时器节点ID,根据他可以找到已经add 的定时器节点 * !!!注意: 1. 回调函数fun中不能再次调用定时器中的接口函数, 否则会造成死锁 !!! ( Init(), Destroy(), Add(...), Delete(id) ) 2. 建议回调函数尽量简单,不要阻塞 */ int ClTimer::Add( TIMER_CMD fun, void *arg, unsigned second, unsigned int *timerId ) { int nRet = -1; if ( m_TimerThread == 0 ) { TIMER_NODE_T *pTimer = ( TIMER_NODE_T * )Malloc( sizeof(TIMER_NODE_T) ); if ( pTimer != NULL ) { pTimer->id = GetTimerID(); pTimer->interval = GetInterval( second ); pTimer->elapse = 0; pTimer->fun = fun; pTimer->arg = arg; pTimer->next = NULL; if( NULL != timerId ) { *timerId = pTimer->id; } InsertTimerList( pTimer ); nRet = 0; } } return nRet; }
//start timer void CTimer::Start() { //stop timer if already exists if(GetTimerID()) Stop(); //start the timer m_TimerID=SDL_AddTimer(GetInterval(),CTimer::TimerProc,this); }
bool CDuiTimer::SetDuiTimer( HWND hWnd,LPARAM lParam,WPARAM wParam,int iInterval,int iTotalTimer /*= NULL*/,bool bAutoRun /*= true*/,bool bLoop /*= false*/,bool bRevers /*= false*/ ) { if(GetTimerID()) KillDuiTimer(); SetTimerParam(hWnd,lParam,wParam,iInterval,iTotalTimer,bAutoRun,bLoop,bRevers); return InnerSetTimer(); }
//功 能: 设置新定时器执行操作 //描 叙: 设置新定时器,按时间间隔执行操作 //参 数: // timeInterval: 间隔时间,以毫秒为单位 // pFunc:执行动作的函数指针 // parameter: 函数参数的指针 //返回值: 错误则小于0, 正确则返回id: 定时器唯一标识号,KillTimer函数的参数 int SetTimer(long timeInterval, pFuncTimer pFunc, void* parameter) { if (0 >= timeInterval || NULL == pFunc) { LOG_WRITE_POS(LOG_ERR, "Parameter error . timeInterval=%ld, pFunc=%x\n", timeInterval, pFunc); return TIMER_ERROR; } timerInit(); sem_wait(&g_sem); int id = GetTimerID(); if (id == TIMER_ERROR) { sem_post(&g_sem); return TIMER_ERROR; } struct timeval tv={0, 0}; gettimeofday (&tv , NULL); g_timerArray[id].state = 1; g_timerArray[id].id = id; g_timerArray[id].interval.tv_sec = timeInterval / 1000; g_timerArray[id].interval.tv_usec = timeInterval % 1000 * 1000; g_timerArray[id].tv.tv_sec = (tv.tv_sec + g_timerArray[id].interval.tv_sec) + (tv.tv_usec + g_timerArray[id].interval.tv_usec) / 1000000; g_timerArray[id].tv.tv_usec = (tv.tv_usec + g_timerArray[id].interval.tv_usec) % 1000000; g_timerArray[id].func = pFunc; g_timerArray[id].parameter = parameter; g_timerArray[id].msgid = -1; //bool b = list_empty(&g_listTimerWork); insertTimer(id); //if (b) { sem_post(&g_sem_new); } printf_debug3("SetTimer id=%d\n", id); sem_post(&g_sem); return id; }
void CTimer::Update(float fTimeElaps) { if ( eTimerState_Pause == m_eState || eTimerState_None == m_eState ) return ; // process delay if ( (m_fDelayKeeper += fTimeElaps) < m_fDelay ) { return ; } // prcess interval ; if ( (m_fIntervalKeeper += fTimeElaps) < m_fInterval ) { return ; } // invoke funcion ; m_fIntervalKeeper -= m_fInterval ; (m_pDelegate->*m_pTimerFunc)(fTimeElaps,GetTimerID()); }
//功 能: 设置新定时器执行操作 //描 叙: 设置新定时器,按时间间隔发送消息到消息队列 //参 数: // timeInterval: 间隔时间,以毫秒为单位 // msgid:消息队列ID // times: 定时器执行次数,<=0为无限次, >0为执行times次数后自动删除定时器 //返回值: 错误则小于0, 正确则返回id: 定时器唯一标识号,KillTimer函数的参数 int SetMsgTimer(long timeInterval, int msgid, int times) { if (0 >= timeInterval || 0 > msgid) { LOG_WRITE_POS(LOG_ERR, "Parameter error. timeInterval=%ld, msgid=%d\n", timeInterval, msgid); return TIMER_ERROR; } timerInit(); sem_wait(&g_sem); int id = GetTimerID(); if (id == TIMER_ERROR) { sem_post(&g_sem); return TIMER_ERROR; } struct timeval tv={0, 0}; gettimeofday (&tv , NULL); g_timerArray[id].state = 1; g_timerArray[id].id = id; g_timerArray[id].interval.tv_sec = timeInterval / 1000; g_timerArray[id].interval.tv_usec = timeInterval % 1000 * 1000; g_timerArray[id].tv.tv_sec = (tv.tv_sec + g_timerArray[id].interval.tv_sec) + (tv.tv_usec + g_timerArray[id].interval.tv_usec) / 1000000; g_timerArray[id].tv.tv_usec = (tv.tv_usec + g_timerArray[id].interval.tv_usec) % 1000000; g_timerArray[id].msgid = msgid; g_timerArray[id].times = times;// > 0 ? 1 : 0; //bool b = list_empty(&g_listTimerWork); insertTimer(id); //if (b) { sem_post(&g_sem_new); } sem_post(&g_sem); return id; }