void mainloop(game_manager* manager) { event_queue_init(); game_manager_start(manager); unsigned long next_game_tick = black_sdl_get_tick(); unsigned long fps_counter = 0; unsigned long game_tick = 0; unsigned long fps_start_time = black_sdl_get_tick(); while (manager->is_running) { game_manager_handle_events(manager); int loops = 0; unsigned int tick = black_sdl_get_tick(); while(tick > next_game_tick && loops < max_frame_skip) { game_manager_update(manager, game_tick++); next_game_tick += time_per_tick; loops++; tick = black_sdl_get_tick(); } float interpolation = (tick + time_per_tick - next_game_tick)/time_per_tick; game_manager_render(manager, interpolation); ++fps_counter; if(black_sdl_get_tick() - fps_start_time > 1000) { manager->fps = fps_counter; fps_counter = 0; fps_start_time = black_sdl_get_tick(); } } event_queue_destroy(); }
/* 工作者线程 */ static void * event_worker(void * arg) { event_queue_t * peq = arg; struct cmd_event * pe; struct cmd_event * events[EVENTS_SIZ]; int cnt; peq->sql_db = malloc(sizeof(*(peq->sql_db))); //在当前线程中打开数据库连接 if (peq->sql_db == NULL) { log_error(LOG_ERROR, "NO MEM"); exit(1); } if (sqlite_db_open(peq->sql_db, SQL_DB) < 0) { //在当前线程中打开数据库连接 log_error(LOG_ERROR, "event_queue_init"); //!!!出错 } //sleep(1); do { // printf("\nevent_worker\n"); cnt = 0; while ((pe = event_queue_pop(peq)) != NULL) { pe->cb(peq->sql_db, pe->psock, pe->buffer, pe->buf_siz); events[cnt++] = pe; cnt -= del_events(cnt, events, peq); } del_all_events(cnt, events, peq); event_queue_wait(peq); //sleep(1); } while (1); sqlite_db_close(peq->sql_db); free(peq->sql_db); event_queue_destroy(peq); return NULL; }