/** 让当前帧停留一定时间 */ void FrameControl_Remain( FrameCtrlCtx *ctx ) { unsigned int n_ms, lost_ms; int64_t current_time; if( ctx->state == FRAME_CTRL_STATE_QUIT ) { return; } current_time = LCUI_GetTickCount(); n_ms = (int)(current_time - ctx->prev_frame_start_time); if( n_ms > ctx->one_frame_remain_time ) { goto normal_exit; } n_ms = ctx->one_frame_remain_time - n_ms; if( n_ms < 1 ) { goto normal_exit; } /** 进行睡眠,直到需要暂停为止 */ LCUISleeper_StartSleep( &ctx->wait_pause, n_ms ); /** 睡眠结束后,如果当前状态不为PAUSE,则说明睡眠不是因为要暂停而终止的 */ if( ctx->state != FRAME_CTRL_STATE_PAUSE ) { goto normal_exit; } /** 需要暂停,进行睡眠,直到需要继续为止 */ lost_ms = LCUISleeper_StartSleep( &ctx->wait_continue, INT_MAX ); ctx->pause_time = lost_ms; ctx->prev_frame_start_time += lost_ms; return; normal_exit:; current_time = LCUI_GetTickCount(); if( current_time - ctx->prev_fps_update_time >= 1000 ) { ctx->current_fps = ctx->temp_fps; ctx->prev_fps_update_time = current_time; ctx->temp_fps = 0; } ctx->prev_frame_start_time = current_time; ++ctx->temp_fps; }
/* 从消息队列中取出一个消息 */ int Game_GetMsg( GameMsg *msg_buff ) { GameMsg *p_msg; Game_RecvMsg(); while( Queue_GetTotal(&game_msg_queue)<=0 ) { LCUISleeper_StartSleep( &msg_sleeper, 1000 ); } Queue_Lock( &game_msg_queue ); p_msg = (GameMsg*)Queue_Get( &game_msg_queue, 0 ); if( p_msg == NULL ) { Queue_Unlock( &game_msg_queue ); return -1; } *msg_buff = *p_msg; Queue_Delete( &game_msg_queue, 0 ); Queue_Unlock( &game_msg_queue ); return 0; }
/** 定时器线程,用于处理列表中各个定时器 */ static void TimerThread( void *arg ) { int i, n; long int n_ms; LCUI_Func func_data; LCUI_Queue *timer_list; timer_data *timer = NULL; int64_t lost_ms; timer_list = (LCUI_Queue*)arg; func_data.arg[0] = NULL; func_data.arg[1] = NULL; while( !LCUI_Active() ) { LCUI_MSleep(10); } while( timer_thread_active ) { Queue_Lock( timer_list ); n = Queue_GetTotal( timer_list ); for(i=0; i<n; ++i) { timer = (timer_data*)Queue_Get( timer_list , i); if( !timer ) { continue; } if( timer->state == STATE_RUN ) { break; } } Queue_Unlock( timer_list ); /* 没有要处理的定时器,停留一段时间再进行下次循环 */ if(i >= n || !timer ) { LCUI_MSleep(10); continue; } lost_ms = LCUI_GetTicks( timer->start_time ); /* 减去处于暂停状态的时长 */ lost_ms -= timer->pause_ms; /* 若流失的时间未达到总定时时长 */ if( lost_ms < timer->total_ms ) { Queue_Lock( timer_list ); n_ms = timer->total_ms - lost_ms; /* 开始睡眠 */ LCUISleeper_StartSleep( &timer_sleeper, n_ms ); Queue_Unlock( timer_list ); lost_ms = LCUI_GetTicks( timer->start_time ); lost_ms -= timer->pause_ms; if( lost_ms < timer->total_ms ) { continue; } } DEBUG_MSG("timer: %d, start_time: %I64dms, cur_time: %I64dms, cur_ms: %I64d, total_ms: %ld\n", timer->id, timer->start_time, LCUI_GetTickCount(), timer->total_ms-lost_ms, timer->total_ms); /* 准备任务数据 */ func_data.id = timer->app_id; func_data.func = (CallBackFunc)timer->callback_func; func_data.arg[0] = timer->arg; func_data.destroy_arg[0] = FALSE; /* 添加该任务至指定程序的任务队列,添加模式是覆盖 */ AppTasks_CustomAdd( ADD_MODE_REPLACE | AND_ARG_F, &func_data ); Queue_Lock( timer_list ); /* 若需要重复使用,则重置剩余等待时间 */ if( timer->reuse ) { timer->start_time = LCUI_GetTickCount(); timer->pause_ms = 0; TimerList_UpdateTimerPos( timer_list, timer ); } else { /* 否则,释放该定时器 */ LCUITimer_Free( timer->id ); } Queue_Unlock( timer_list ); } LCUIThread_Exit(NULL); }