void Framework::run_loop() { al_start_timer(primary_timer); while(!shutdown_program || Display::displays.empty()) { ALLEGRO_EVENT this_event, next_event; al_wait_for_event(event_queue, &this_event); current_event = &this_event; time_now = this_event.any.timestamp; get_instance()->motions.update(time_now); Screen::on_events(current_event); switch(this_event.type) { case ALLEGRO_EVENT_TIMER: if (this_event.timer.source == primary_timer) Screen::primary_timer_funcs(); else Screen::timer_funcs(); while (al_peek_next_event(event_queue, &next_event) && next_event.type == ALLEGRO_EVENT_TIMER && next_event.timer.source == this_event.timer.source) al_drop_next_event(event_queue); break; case ALLEGRO_EVENT_KEY_DOWN: if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_LSHIFT || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_RSHIFT) Framework::key_shift++; if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_ALT || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_ALTGR) Framework::key_alt++; if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_RCTRL || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_LCTRL) Framework::key_ctrl++; if (current_event->keyboard.keycode == ALLEGRO_KEY_F1) drawing_profiler_graph = !drawing_profiler_graph; // toggle the profiler graph with F1 Screen::key_down_funcs(); break; case ALLEGRO_EVENT_KEY_UP: if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_LSHIFT || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_RSHIFT) Framework::key_shift--; if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_ALT || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_ALTGR) Framework::key_alt--; if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_RCTRL || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_LCTRL) Framework::key_ctrl--; Screen::key_up_funcs(); break; case ALLEGRO_EVENT_KEY_CHAR: Screen::key_char_funcs(); break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: Screen::mouse_up_funcs(); break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: Screen::mouse_down_funcs(); break; case ALLEGRO_EVENT_MOUSE_WARPED: Screen::mouse_warp_funcs(); break; case ALLEGRO_EVENT_MOUSE_AXES: Screen::mouse_axes_funcs(); break; case ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN: Screen::joy_button_down_funcs(); break; case ALLEGRO_EVENT_JOYSTICK_BUTTON_UP: Screen::joy_button_up_funcs(); break; case ALLEGRO_EVENT_JOYSTICK_AXIS: Screen::joy_axis_funcs(); break; case ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY: case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY: // currently ignored break; case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT: Screen::display_switch_out_funcs(); break; case ALLEGRO_EVENT_DISPLAY_SWITCH_IN: Screen::display_switch_in_funcs(); break; case ALLEGRO_EVENT_NATIVE_DIALOG_CLOSE: //Screen::display_switch_in_funcs(); if (textlog) close_log_window(); break; case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION: std::cout << "a joystick was added/removed" << std::endl; al_reconfigure_joysticks(); // note: a bug in allegro causes a crash when al_get_joystick(0) if there // are 0 joysticks. So this extra check has been added to prevent // the crash from occuring, though it should be corrected in future // versions when this bug in allegro is fixed. joystick = (al_get_num_joysticks() == 0) ? NULL : al_get_joystick(0); Screen::joy_config_funcs(); break; case ALLEGRO_EVENT_MENU_CLICK: Screen::native_menu_click_funcs(); break; case ALLEGRO_EVENT_DISPLAY_CLOSE: { Display *this_display = Display::find_display(this_event.display.source); if (this_display) this_display->display_close_func(); } break; default: if (ALLEGRO_EVENT_TYPE_IS_USER(this_event.type)) Screen::user_event_funcs(); else std::cout << "uncaught event [" << this_event.type << "]" << std::endl; break; } } }
static int allua_Event_queue_drop_next_event(lua_State * L) { ALLUA_event_queue event_queue = allua_check_event_queue(L, 1); lua_pushboolean(L, al_drop_next_event(event_queue)); return 1; }
void SceneManager::run() { m_devices->setSceneMessenger(this); #ifdef CGE_MOBILE //ALLEGRO_JOYSTICK *joystick = NULL; // al_reconfigure_joysticks(); //joystick=al_get_joystick(al_get_num_joysticks()-1); #endif al_start_timer(m_gameTimer); //is the event handled? bool handled = false; ALLEGRO_EVENT next; bool skip = false; double numFrames = 1; double totalDelta = 0.0f; int frames = 0; double starttime = 0; bool first = true; float fps = 0.0f; //main loop while(m_gameIsRunning) { handled = false; al_wait_for_event(queue,&evt); bool hasNext = al_peek_next_event(queue,&next); if(hasNext && next.type == ALLEGRO_EVENT_TIMER) { al_drop_next_event(queue); } //render the scene if(m_canDraw && m_needsRedraw && al_is_event_queue_empty(queue)) { if(skip) skip = false; else { double a = al_get_time(); if(m_currentScene->canRender()) { m_g.begin(); m_currentScene->render(); /* m_g.drawText(StringUtil::toString(fps),m_devices->getFontManager()->getDefaultFont(),agui::Color(0,0,0),-2,-2,0); m_g.drawText(StringUtil::toString(fps),m_devices->getFontManager()->getDefaultFont(),agui::Color(0,0,0),2,2,0); m_g.drawText(StringUtil::toString(fps),m_devices->getFontManager()->getDefaultFont(),agui::Color(255,255,255),0,0,0); */ if(m_needsTransition) { m_needsTransition = false; m_transitioning = true; } if(m_transitioning) m_g.drawTintedSprite(m_g.getBuffer(), Color(m_transitionOpacity,m_transitionOpacity,m_transitionOpacity,m_transitionOpacity),0,0,0); m_g.end(); } double t = al_get_time(); if (first) { frames = 0; starttime = t; first = false; continue; } frames++; if (t - starttime > 0.25 && frames > 10) { fps = (double) frames / (t - starttime); starttime = t; frames = 0; } m_needsRedraw = false; if(!m_transitioning && m_needFirstRender) { m_needFirstRender = false; m_callOnNextLogic = true; m_nextLogicFrames = 0; } double delta = al_get_time() - a; totalDelta += delta; numFrames++; double avgDelta = totalDelta / numFrames; if(numFrames > 100) { numFrames = 0; totalDelta = 0; } if(avgDelta > (1.0f / 40.0f) || Platf::isMini()) skip = true; } } defaultBeginEventHandler(&evt); m_currentScene->processEvent(&evt,handled); //do default behavior if event was not handled by the scene if (!handled) { defaultEndEventHandler(&evt); } processMessages(); } }