From 04fd1d038ba340ef0404104368555df9d632dba0 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Tue, 12 Aug 2014 04:22:06 +0200 Subject: [PATCH] Minor cleanup in ScreenManager --- src/scripting/functions.cpp | 2 +- src/supertux/screen_manager.cpp | 159 +++++++++++++++++++++------------------- src/supertux/screen_manager.hpp | 23 +++--- 3 files changed, 97 insertions(+), 87 deletions(-) diff --git a/src/scripting/functions.cpp b/src/scripting/functions.cpp index f5a07a9b9..404516a7d 100644 --- a/src/scripting/functions.cpp +++ b/src/scripting/functions.cpp @@ -64,7 +64,7 @@ void wait(HSQUIRRELVM vm, float seconds) void wait_for_screenswitch(HSQUIRRELVM vm) { - g_screen_manager->waiting_threads.add(vm); + g_screen_manager->m_waiting_threads.add(vm); } void exit_screen() diff --git a/src/supertux/screen_manager.cpp b/src/supertux/screen_manager.cpp index a326d9b94..a7f810b6e 100644 --- a/src/supertux/screen_manager.cpp +++ b/src/supertux/screen_manager.cpp @@ -43,20 +43,19 @@ static const Uint32 TICKS_PER_FRAME = (Uint32) (1000.0 / LOGICAL_FPS); static const int MAX_FRAME_SKIP = 2; ScreenManager::ScreenManager() : - waiting_threads(), + m_waiting_threads(), m_menu_storage(new MenuStorage), m_menu_manager(new MenuManager), - running(), - speed(1.0), - nextpop(false), - nextpush(false), - fps(0), - next_screen(), - current_screen(), - console(), - screen_fade(), - screen_stack(), - screenshot_requested(false) + m_running(), + m_speed(1.0), + m_nextpop(false), + m_nextpush(false), + m_fps(0), + m_next_screen(), + m_current_screen(), + m_screen_fade(), + m_screen_stack(), + m_screenshot_requested(false) { using namespace scripting; TimeScheduler::instance = new TimeScheduler(); @@ -72,52 +71,52 @@ ScreenManager::~ScreenManager() void ScreenManager::push_screen(std::unique_ptr screen, std::unique_ptr screen_fade) { - assert(!this->next_screen); - this->next_screen = std::move(screen); - this->screen_fade = std::move(screen_fade); - nextpush = !nextpop; - nextpop = false; - speed = 1.0f; + assert(!m_next_screen); + m_next_screen = std::move(screen); + m_screen_fade = std::move(screen_fade); + m_nextpush = !m_nextpop; + m_nextpop = false; + m_speed = 1.0f; } void ScreenManager::exit_screen(std::unique_ptr screen_fade) { - next_screen.reset(); - this->screen_fade = std::move(screen_fade); - nextpop = true; - nextpush = false; + m_next_screen.reset(); + m_screen_fade = std::move(screen_fade); + m_nextpop = true; + m_nextpush = false; } void ScreenManager::set_screen_fade(std::unique_ptr screen_fade) { - this->screen_fade = std::move(screen_fade); + m_screen_fade = std::move(screen_fade); } void ScreenManager::quit(std::unique_ptr screen_fade) { - screen_stack.clear(); + m_screen_stack.clear(); exit_screen(std::move(screen_fade)); } void ScreenManager::set_speed(float speed) { - this->speed = speed; + m_speed = speed; } float ScreenManager::get_speed() const { - return speed; + return m_speed; } bool ScreenManager::has_no_pending_fadeout() const { - return screen_fade.get() == NULL || screen_fade->done(); + return !m_screen_fade || m_screen_fade->done(); } void @@ -126,8 +125,8 @@ ScreenManager::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(Resources::small_font, fpstext, - Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X, + context.draw_text(Resources::small_font, fpstext, + Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD); context.draw_text(Resources::small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD); } @@ -138,30 +137,35 @@ ScreenManager::draw(DrawingContext& context) static Uint32 fps_ticks = SDL_GetTicks(); static int frame_count = 0; - current_screen->draw(context); + m_current_screen->draw(context); m_menu_manager->draw(context); - if(screen_fade.get() != NULL) - screen_fade->draw(context); + if (m_screen_fade) + { + m_screen_fade->draw(context); + } Console::instance->draw(context); - if(g_config->show_fps) - draw_fps(context, fps); + if (g_config->show_fps) + { + draw_fps(context, m_fps); + } // if a screenshot was requested, pass request on to drawing_context - if (screenshot_requested) { + if (m_screenshot_requested) + { context.take_screenshot(); - screenshot_requested = false; + m_screenshot_requested = false; } context.do_drawing(); /* Calculate frames per second */ - if(g_config->show_fps) + if (g_config->show_fps) { ++frame_count; - if(SDL_GetTicks() - fps_ticks >= 500) + if (SDL_GetTicks() - fps_ticks >= 500) { - fps = (float) frame_count / .5; + m_fps = (float) frame_count / .5; frame_count = 0; fps_ticks = SDL_GetTicks(); } @@ -173,10 +177,12 @@ ScreenManager::update_gamelogic(float elapsed_time) { scripting::update_debugger(); scripting::TimeScheduler::instance->update(game_time); - current_screen->update(elapsed_time); + m_current_screen->update(elapsed_time); m_menu_manager->process_input(); - if(screen_fade.get() != NULL) - screen_fade->update(elapsed_time); + if (m_screen_fade) + { + m_screen_fade->update(elapsed_time); + } Console::instance->update(elapsed_time); } @@ -185,7 +191,7 @@ ScreenManager::process_events() { g_input_manager->update(); SDL_Event event; - while(SDL_PollEvent(&event)) + while (SDL_PollEvent(&event)) { g_input_manager->process_event(event); @@ -196,7 +202,7 @@ ScreenManager::process_events() case SDL_QUIT: quit(); break; - + case SDL_WINDOWEVENT: switch(event.window.event) { @@ -207,13 +213,13 @@ ScreenManager::process_events() break; } break; - + case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_F10) { g_config->show_fps = !g_config->show_fps; } - if (event.key.keysym.sym == SDLK_F11) + else if (event.key.keysym.sym == SDLK_F11) { g_config->use_fullscreen = !g_config->use_fullscreen; Renderer::instance()->apply_config(); @@ -239,34 +245,36 @@ ScreenManager::process_events() void ScreenManager::handle_screen_switch() { - while((next_screen || nextpop) && + while ((m_next_screen || m_nextpop) && has_no_pending_fadeout()) { - if(current_screen) { - current_screen->leave(); + if (m_current_screen) { + m_current_screen->leave(); } - if(nextpop) { - if(screen_stack.empty()) { - running = false; + if (m_nextpop) { + if (m_screen_stack.empty()) { + m_running = false; break; } - next_screen = std::move(screen_stack.back()); - screen_stack.pop_back(); + m_next_screen = std::move(m_screen_stack.back()); + m_screen_stack.pop_back(); } - if(nextpush && current_screen) { - screen_stack.push_back(std::move(current_screen)); + if (m_nextpush && m_current_screen) { + m_screen_stack.push_back(std::move(m_current_screen)); } - nextpush = false; - nextpop = false; - speed = 1.0; - current_screen = std::move(next_screen); - if(current_screen) - current_screen->setup(); - screen_fade.reset(); + m_nextpush = false; + m_nextpop = false; + m_speed = 1.0; + m_current_screen = std::move(m_next_screen); + if (m_current_screen) + { + m_current_screen->setup(); + } + m_screen_fade.reset(); - waiting_threads.wakeup(); + m_waiting_threads.wakeup(); } } @@ -276,12 +284,14 @@ ScreenManager::run(DrawingContext &context) Uint32 last_ticks = 0; Uint32 elapsed_ticks = 0; - running = true; - while(running) { - + m_running = true; + while (m_running) + { handle_screen_switch(); - if(!running || !current_screen) + if (!m_running || !m_current_screen) + { break; + } Uint32 ticks = SDL_GetTicks(); elapsed_ticks += ticks - last_ticks; @@ -289,14 +299,15 @@ ScreenManager::run(DrawingContext &context) Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed); - if (elapsed_ticks > ticks_per_frame*4) { + 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) + if (elapsed_ticks < ticks_per_frame) { Uint32 delay_ticks = ticks_per_frame - elapsed_ticks; SDL_Delay(delay_ticks); @@ -306,12 +317,12 @@ ScreenManager::run(DrawingContext &context) int frames = 0; - while(elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP) + 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; + timestep *= m_speed; game_time += timestep; process_events(); @@ -325,10 +336,10 @@ ScreenManager::run(DrawingContext &context) } } -void +void ScreenManager::take_screenshot() { - screenshot_requested = true; + m_screenshot_requested = true; } /* EOF */ diff --git a/src/supertux/screen_manager.hpp b/src/supertux/screen_manager.hpp index b0ae9d205..cdc79c74b 100644 --- a/src/supertux/screen_manager.hpp +++ b/src/supertux/screen_manager.hpp @@ -55,7 +55,7 @@ public: void set_screen_fade(std::unique_ptr fade); /// threads that wait for a screenswitch - scripting::ThreadQueue waiting_threads; + scripting::ThreadQueue m_waiting_threads; private: void draw_fps(DrawingContext& context, float fps); @@ -67,18 +67,17 @@ private: private: std::unique_ptr m_menu_storage; std::unique_ptr m_menu_manager; - bool running; - float speed; - bool nextpop; - bool nextpush; + bool m_running; + float m_speed; + bool m_nextpop; + bool m_nextpush; /// measured fps - float fps; - std::unique_ptr next_screen; - std::unique_ptr current_screen; - std::unique_ptr console; - std::unique_ptr screen_fade; - std::vector > screen_stack; - bool screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */ + float m_fps; + std::unique_ptr m_next_screen; + std::unique_ptr m_current_screen; + std::unique_ptr m_screen_fade; + std::vector > m_screen_stack; + bool m_screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */ }; #endif -- 2.11.0