X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fmainloop.cpp;h=978d6bfede1b5360b06903ddcfceb84e9cbd59b2;hb=38105c22495d9439b30221732dd5d7b89f328a0c;hp=55a87d3bdecb4e7b6b966fdbb4a471c11c45c3fa;hpb=0be84025d0ece62acd59d2634ec1f0355b426fab;p=supertux.git diff --git a/src/mainloop.cpp b/src/mainloop.cpp index 55a87d3bd..978d6bfed 100644 --- a/src/mainloop.cpp +++ b/src/mainloop.cpp @@ -35,6 +35,7 @@ #include "screen_fade.hpp" #include "timer.hpp" #include "player_status.hpp" +#include "video/renderer.hpp" #include "random_generator.hpp" // the engine will be run with a logical framerate of 64fps. @@ -46,10 +47,12 @@ static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS); /** don't skip more than every 2nd frame */ static const int MAX_FRAME_SKIP = 2; +float game_speed = 1.0f; + MainLoop* main_loop = NULL; MainLoop::MainLoop() - : speed(1.0), nextpop(false), nextpush(false) + : speed(1.0), nextpop(false), nextpush(false), fps(0), screenshot_requested(false) { using namespace Scripting; TimeScheduler::instance = new TimeScheduler(); @@ -109,14 +112,20 @@ MainLoop::set_speed(float speed) this->speed = speed; } +float +MainLoop::get_speed() const +{ + return speed; +} + void MainLoop::draw_fps(DrawingContext& context, float fps_fps) { char str[60]; snprintf(str, sizeof(str), "%3.1f", fps_fps); const char* fpstext = "FPS"; - context.draw_text(white_text, fpstext, Vector(SCREEN_WIDTH - white_text->get_text_width(fpstext) - gold_text->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), LEFT_ALLIGN, LAYER_HUD); - context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_HUD); + context.draw_text(white_text, fpstext, Vector(SCREEN_WIDTH - white_text->get_text_width(fpstext) - gold_text->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD); + context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD); } void @@ -135,6 +144,11 @@ MainLoop::draw(DrawingContext& context) if(config->show_fps) draw_fps(context, fps); + // if a screenshot was requested, pass request on to drawing_context + if (screenshot_requested) { + context.take_screenshot(); + screenshot_requested = false; + } context.do_drawing(); /* Calculate frames per second */ @@ -166,14 +180,49 @@ void MainLoop::process_events() { main_controller->update(); + Uint8* keystate = SDL_GetKeyState(NULL); 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(); - } + while(SDL_PollEvent(&event)) + { + main_controller->process_event(event); + + if(Menu::current() != NULL) + Menu::current()->event(event); + + switch(event.type) + { + case SDL_QUIT: + quit(); + break; + + case SDL_VIDEORESIZE: + Renderer::instance()->resize(event.resize.w, event.resize.h); + Menu::recalc_pos(); + break; + + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_F11) + { + config->use_fullscreen = !config->use_fullscreen; + init_video(); + Menu::recalc_pos(); + } + else if (event.key.keysym.sym == SDLK_PRINT || + event.key.keysym.sym == SDLK_F12) + { + take_screenshot(); + } + else if (event.key.keysym.sym == SDLK_F1 && + (keystate[SDLK_LCTRL] || keystate[SDLK_RCTRL]) && + keystate[SDLK_c]) + { + Console::instance->toggle(); + config->console_enabled = true; + config->save(); + } + break; + } + } } void @@ -210,13 +259,10 @@ MainLoop::handle_screen_switch() } void -MainLoop::run() +MainLoop::run(DrawingContext &context) { - DrawingContext context; - Uint32 last_ticks = 0; Uint32 elapsed_ticks = 0; - int skipped_frames = 0; running = true; while(running) { @@ -229,34 +275,44 @@ MainLoop::run() elapsed_ticks += ticks - last_ticks; last_ticks = ticks; - /* time for a new logical frame? */ - if(elapsed_ticks > TICKS_PER_FRAME) { - elapsed_ticks -= TICKS_PER_FRAME; - float timestep = 1.0 / LOGICAL_FPS; - real_time += timestep; - timestep *= speed; - game_time += timestep; + Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * game_speed); - process_events(); - update_gamelogic(timestep); + if (elapsed_ticks > ticks_per_frame*4) { + // when the game loads up or levels are switched the + // elapsed_ticks grows extremely large, so we just ignore those + // large time jumps + elapsed_ticks = 0; } - if(elapsed_ticks > TICKS_PER_FRAME) { - // too much time passed since last frame, the computer doesn't seem to be - // able to draw 64fps on screen, so skip 1 frame for now - skipped_frames++; + 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; + } - // slow down the game if we skip too many frames - if(skipped_frames > MAX_FRAME_SKIP) { - elapsed_ticks = elapsed_ticks % TICKS_PER_FRAME; draw(context); - skipped_frames = 0; } - } else { - draw(context); - skipped_frames = 0; - } sound_manager->update(); + + SDL_Delay(0); } } + +void +MainLoop::take_screenshot() +{ + screenshot_requested = true; +} +