- elapsed_time *= speed;
-
- game_time += elapsed_time;
- ScriptManager::instance->update();
- current_screen->update(elapsed_time);
- if(screen_fade.get() != NULL)
- screen_fade->update(elapsed_time);
- Console::instance->update(elapsed_time);
-
- main_controller->update();
- SDL_Event event;
- while(SDL_PollEvent(&event)) {
- main_controller->process_event(event);
- if(Menu::current() != NULL)
- Menu::current()->event(event);
- if(event.type == SDL_QUIT)
- quit();
+ nextpush = false;
+ nextpop = false;
+ speed = 1.0;
+ if(next_screen.get() != NULL)
+ next_screen->setup();
+ current_screen.reset(next_screen.release());
+ screen_fade.reset(NULL);
+
+ waiting_threads.wakeup();
+ }
+}
+
+void
+MainLoop::run(DrawingContext &context)
+{
+ Uint32 last_ticks = 0;
+ Uint32 elapsed_ticks = 0;
+
+ running = true;
+ while(running) {
+
+ handle_screen_switch();
+ if(!running || current_screen.get() == NULL)
+ break;
+
+ Uint32 ticks = SDL_GetTicks();
+ elapsed_ticks += ticks - last_ticks;
+ last_ticks = ticks;
+
+ if (elapsed_ticks > TICKS_PER_FRAME*4) {
+ // when the game loads up or levels are switched the
+ // elapsed_ticks grows extremly large, so we just ignore those
+ // large time jumps
+ elapsed_ticks = 0;
+ }
+
+ int frames = 0;
+
+ if (elapsed_ticks > TICKS_PER_FRAME) {
+ while(elapsed_ticks > TICKS_PER_FRAME && frames < MAX_FRAME_SKIP) {
+ elapsed_ticks -= TICKS_PER_FRAME;
+ float timestep = 1.0 / LOGICAL_FPS;
+ real_time += timestep;
+ timestep *= speed;
+ game_time += timestep;
+
+ process_events();
+ update_gamelogic(timestep);
+ frames += 1;
+ }
+
+ draw(context);