ScreenManager Class Reference

Manages, updates and draws all Screens, Controllers, Menus and the Console. More...

#include <screen_manager.hpp>

List of all members.

Public Member Functions

 ScreenManager ()
 ~ScreenManager ()
void run (DrawingContext &context)
void exit_screen (ScreenFade *fade=NULL)
void quit (ScreenFade *fade=NULL)
void set_speed (float speed)
float get_speed () const
bool has_no_pending_fadeout () const
void take_screenshot ()
 requests that a screenshot be taken after the next frame has been rendered
void push_screen (Screen *screen, ScreenFade *fade=NULL)
void set_screen_fade (ScreenFade *fade)

Public Attributes

scripting::ThreadQueue waiting_threads
 threads that wait for a screenswitch

Private Member Functions

void draw_fps (DrawingContext &context, float fps)
void draw (DrawingContext &context)
void update_gamelogic (float elapsed_time)
void process_events ()
void handle_screen_switch ()

Private Attributes

bool running
float speed
bool nextpop
bool nextpush
float fps
 measured fps
std::auto_ptr< Screennext_screen
std::auto_ptr< Screencurrent_screen
std::auto_ptr< Consoleconsole
std::auto_ptr< ScreenFadescreen_fade
std::vector< Screen * > screen_stack
bool screenshot_requested
 true if a screenshot should be taken after the next frame has been rendered


Detailed Description

Manages, updates and draws all Screens, Controllers, Menus and the Console.

Definition at line 33 of file screen_manager.hpp.


Constructor & Destructor Documentation

ScreenManager::ScreenManager (  ) 

Definition at line 43 of file screen_manager.cpp.

00043                              :
00044   waiting_threads(),
00045   running(),
00046   speed(1.0), 
00047   nextpop(false), 
00048   nextpush(false), 
00049   fps(0), 
00050   next_screen(),
00051   current_screen(),
00052   console(),
00053   screen_fade(),
00054   screen_stack(),
00055   screenshot_requested(false)
00056 {
00057   using namespace scripting;
00058   TimeScheduler::instance = new TimeScheduler();
00059 }

ScreenManager::~ScreenManager (  ) 

Definition at line 61 of file screen_manager.cpp.

References screen_stack.

00062 {
00063   using namespace scripting;
00064   delete TimeScheduler::instance;
00065   TimeScheduler::instance = NULL;
00066 
00067   for(std::vector<Screen*>::iterator i = screen_stack.begin();
00068       i != screen_stack.end(); ++i) {
00069     delete *i;
00070   }
00071 }


Member Function Documentation

void ScreenManager::run ( DrawingContext context  ) 

Definition at line 278 of file screen_manager.cpp.

References current_screen, draw(), g_game_speed, game_time, handle_screen_switch(), LOGICAL_FPS, process_events(), real_time, running, sound_manager, speed, TICKS_PER_FRAME, SoundManager::update(), and update_gamelogic().

Referenced by Main::run().

00279 {
00280   Uint32 last_ticks = 0;
00281   Uint32 elapsed_ticks = 0;
00282 
00283   running = true;
00284   while(running) {
00285 
00286     handle_screen_switch();
00287     if(!running || current_screen.get() == NULL)
00288       break;
00289 
00290     Uint32 ticks = SDL_GetTicks();
00291     elapsed_ticks += ticks - last_ticks;
00292     last_ticks = ticks;
00293 
00294     Uint32 ticks_per_frame = (Uint32) (TICKS_PER_FRAME * g_game_speed);
00295 
00296     if (elapsed_ticks > ticks_per_frame*4) {
00297       // when the game loads up or levels are switched the
00298       // elapsed_ticks grows extremely large, so we just ignore those
00299       // large time jumps
00300       elapsed_ticks = 0;
00301     }
00302 
00303     if(elapsed_ticks < ticks_per_frame)
00304     {
00305       Uint32 delay_ticks = ticks_per_frame - elapsed_ticks;
00306       SDL_Delay(delay_ticks);
00307       last_ticks += delay_ticks;
00308       elapsed_ticks += delay_ticks;
00309     }
00310 
00311     int frames = 0;
00312 
00313     while(elapsed_ticks >= ticks_per_frame && frames < MAX_FRAME_SKIP) 
00314     {
00315       elapsed_ticks -= ticks_per_frame;
00316       float timestep = 1.0 / LOGICAL_FPS;
00317       real_time += timestep;
00318       timestep *= speed;
00319       game_time += timestep;
00320 
00321       process_events();
00322       update_gamelogic(timestep);
00323       frames += 1;
00324     }
00325 
00326     draw(context);
00327 
00328     sound_manager->update();
00329   }
00330 }

void ScreenManager::exit_screen ( ScreenFade fade = NULL  ) 

Definition at line 84 of file screen_manager.cpp.

References next_screen, nextpop, nextpush, and screen_fade.

Referenced by worldmap::WorldMap::change(), TextScroller::draw(), scripting::exit_screen(), GameSession::finish(), GameSession::process_menu(), quit(), GameSession::restart_level(), worldmap::WorldMap::update(), TextScroller::update(), and LevelIntro::update().

00085 {
00086   next_screen.reset(NULL);
00087   this->screen_fade.reset(screen_fade);
00088   nextpop = true;
00089   nextpush = false;
00090 }

void ScreenManager::quit ( ScreenFade fade = NULL  ) 

Definition at line 99 of file screen_manager.cpp.

References exit_screen(), screen_fade, and screen_stack.

Referenced by MainMenu::check_menu(), process_events(), scripting::quit(), and Main::wait_for_event().

00100 {
00101   for(std::vector<Screen*>::iterator i = screen_stack.begin();
00102       i != screen_stack.end(); ++i)
00103     delete *i;
00104   screen_stack.clear();
00105 
00106   exit_screen(screen_fade);
00107 }

void ScreenManager::set_speed ( float  speed  ) 

Definition at line 110 of file screen_manager.cpp.

Referenced by GameSession::start_sequence(), GameSession::toggle_pause(), TitleScreen::update(), and GameSession::update().

00111 {
00112   this->speed = speed;
00113 }

float ScreenManager::get_speed (  )  const

Definition at line 116 of file screen_manager.cpp.

References speed.

Referenced by GameSession::GameSession(), EndSequenceWalkRight::starting(), EndSequenceWalkLeft::starting(), EndSequenceFireworks::starting(), and GameSession::toggle_pause().

00117 {
00118   return speed;
00119 }

bool ScreenManager::has_no_pending_fadeout (  )  const

Definition at line 122 of file screen_manager.cpp.

References screen_fade.

Referenced by handle_screen_switch(), and TitleScreen::update().

00123 {
00124   return screen_fade.get() == NULL || screen_fade->done();
00125 }

void ScreenManager::take_screenshot (  ) 

requests that a screenshot be taken after the next frame has been rendered

Definition at line 333 of file screen_manager.cpp.

References screenshot_requested.

Referenced by process_events().

00334 {
00335   screenshot_requested = true;
00336 }

void ScreenManager::push_screen ( Screen screen,
ScreenFade fade = NULL 
)

Definition at line 74 of file screen_manager.cpp.

References next_screen, nextpop, nextpush, screen_fade, and speed.

Referenced by worldmap::WorldMap::change(), MainMenu::check_menu(), ContribWorldMenu::check_menu(), scripting::display_text_file(), scripting::load_level(), scripting::load_worldmap(), World::run(), Main::run(), GameSession::setup(), and worldmap::WorldMap::update().

00075 {
00076   this->next_screen.reset(screen);
00077   this->screen_fade.reset(screen_fade);
00078   nextpush = !nextpop;
00079   nextpop = false;
00080   speed = 1.0f;
00081 }

void ScreenManager::set_screen_fade ( ScreenFade fade  ) 

Definition at line 93 of file screen_manager.cpp.

References screen_fade.

Referenced by scripting::abort_screenfade(), scripting::fadeout_screen(), and scripting::shrink_screen().

00094 {
00095   this->screen_fade.reset(screen_fade);
00096 }

void ScreenManager::draw_fps ( DrawingContext context,
float  fps 
) [private]

Definition at line 128 of file screen_manager.cpp.

References ALIGN_LEFT, ALIGN_RIGHT, BORDER_X, BORDER_Y, DrawingContext::draw_text(), LAYER_HUD, SCREEN_WIDTH, and Resources::small_font.

Referenced by draw().

00129 {
00130   char str[60];
00131   snprintf(str, sizeof(str), "%3.1f", fps_fps);
00132   const char* fpstext = "FPS";
00133   context.draw_text(Resources::small_font, fpstext, 
00134                     Vector(SCREEN_WIDTH - Resources::small_font->get_text_width(fpstext) - Resources::small_font->get_text_width(" 99999") - BORDER_X, 
00135                            BORDER_Y + 20), ALIGN_LEFT, LAYER_HUD);
00136   context.draw_text(Resources::small_font, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), ALIGN_RIGHT, LAYER_HUD);
00137 }

void ScreenManager::draw ( DrawingContext context  )  [private]

Definition at line 140 of file screen_manager.cpp.

References MenuManager::current(), current_screen, DrawingContext::do_drawing(), Console::draw(), Menu::draw(), draw_fps(), fps, g_config, Console::instance, screen_fade, screenshot_requested, Config::show_fps, and DrawingContext::take_screenshot().

Referenced by run().

00141 {
00142   static Uint32 fps_ticks = SDL_GetTicks();
00143   static int frame_count = 0;
00144 
00145   current_screen->draw(context);
00146   if(MenuManager::current() != NULL)
00147     MenuManager::current()->draw(context);
00148   if(screen_fade.get() != NULL)
00149     screen_fade->draw(context);
00150   Console::instance->draw(context);
00151 
00152   if(g_config->show_fps)
00153     draw_fps(context, fps);
00154 
00155   // if a screenshot was requested, pass request on to drawing_context
00156   if (screenshot_requested) {
00157     context.take_screenshot();
00158     screenshot_requested = false;
00159   }
00160   context.do_drawing();
00161 
00162   /* Calculate frames per second */
00163   if(g_config->show_fps)
00164   {
00165     ++frame_count;
00166 
00167     if(SDL_GetTicks() - fps_ticks >= 500)
00168     {
00169       fps = (float) frame_count / .5;
00170       frame_count = 0;
00171       fps_ticks = SDL_GetTicks();
00172     }
00173   }
00174 }

void ScreenManager::update_gamelogic ( float  elapsed_time  )  [private]

Definition at line 177 of file screen_manager.cpp.

References MenuManager::current(), current_screen, game_time, Console::instance, scripting::TimeScheduler::instance, screen_fade, Console::update(), Menu::update(), and scripting::update_debugger().

Referenced by run().

00178 {
00179   scripting::update_debugger();
00180   scripting::TimeScheduler::instance->update(game_time);
00181   current_screen->update(elapsed_time);
00182   if (MenuManager::current() != NULL)
00183     MenuManager::current()->update();
00184   if(screen_fade.get() != NULL)
00185     screen_fade->update(elapsed_time);
00186   Console::instance->update(elapsed_time);
00187 }

void ScreenManager::process_events (  )  [private]

Definition at line 190 of file screen_manager.cpp.

References Renderer::apply_config(), Config::console_enabled, MenuManager::current(), Menu::event(), g_config, g_jk_controller, Console::instance, Renderer::instance(), JoystickKeyboardController::process_event(), quit(), MenuManager::recalc_pos(), Renderer::resize(), Config::save(), Config::show_fps, take_screenshot(), Console::toggle(), JoystickKeyboardController::update(), and Config::use_fullscreen.

Referenced by run().

00191 {
00192   g_jk_controller->update();
00193   Uint8* keystate = SDL_GetKeyState(NULL);
00194   SDL_Event event;
00195   while(SDL_PollEvent(&event)) 
00196   {
00197     g_jk_controller->process_event(event);
00198 
00199     if(MenuManager::current() != NULL)
00200       MenuManager::current()->event(event);
00201 
00202     switch(event.type)
00203     {
00204       case SDL_QUIT:
00205         quit();
00206         break;
00207               
00208       case SDL_VIDEORESIZE:
00209         Renderer::instance()->resize(event.resize.w, event.resize.h);
00210         MenuManager::recalc_pos();
00211         break;
00212             
00213       case SDL_KEYDOWN:
00214         if (event.key.keysym.sym == SDLK_F10)
00215         {
00216           g_config->show_fps = !g_config->show_fps;
00217         }
00218         if (event.key.keysym.sym == SDLK_F11) 
00219         {
00220           g_config->use_fullscreen = !g_config->use_fullscreen;
00221           Renderer::instance()->apply_config();
00222           MenuManager::recalc_pos();
00223         }
00224         else if (event.key.keysym.sym == SDLK_PRINT ||
00225                  event.key.keysym.sym == SDLK_F12)
00226         {
00227           take_screenshot();
00228         }
00229         else if (event.key.keysym.sym == SDLK_F1 &&
00230                  (keystate[SDLK_LCTRL] || keystate[SDLK_RCTRL]) &&
00231                  keystate[SDLK_c])
00232         {
00233           Console::instance->toggle();
00234           g_config->console_enabled = true;
00235           g_config->save();
00236         }
00237         break;
00238     }
00239   }
00240 }

void ScreenManager::handle_screen_switch (  )  [private]

Definition at line 243 of file screen_manager.cpp.

References current_screen, has_no_pending_fadeout(), next_screen, nextpop, nextpush, running, screen_fade, screen_stack, Screen::setup(), speed, waiting_threads, and scripting::ThreadQueue::wakeup().

Referenced by run().

00244 {
00245   while( (next_screen.get() != NULL || nextpop) &&
00246          has_no_pending_fadeout()) {
00247     if(current_screen.get() != NULL) {
00248       current_screen->leave();
00249     }
00250 
00251     if(nextpop) {
00252       if(screen_stack.empty()) {
00253         running = false;
00254         break;
00255       }
00256       next_screen.reset(screen_stack.back());
00257       screen_stack.pop_back();
00258     }
00259     if(nextpush && current_screen.get() != NULL) {
00260       screen_stack.push_back(current_screen.release());
00261     }
00262 
00263     nextpush = false;
00264     nextpop = false;
00265     speed = 1.0;
00266     Screen* next_screen_ptr = next_screen.release();
00267     next_screen.reset(0);
00268     if(next_screen_ptr)
00269       next_screen_ptr->setup();
00270     current_screen.reset(next_screen_ptr);
00271     screen_fade.reset(NULL);
00272 
00273     waiting_threads.wakeup();
00274   }
00275 }


Member Data Documentation

scripting::ThreadQueue ScreenManager::waiting_threads

threads that wait for a screenswitch

Definition at line 56 of file screen_manager.hpp.

Referenced by handle_screen_switch(), and scripting::wait_for_screenswitch().

bool ScreenManager::running [private]

Definition at line 66 of file screen_manager.hpp.

Referenced by handle_screen_switch(), and run().

float ScreenManager::speed [private]

Definition at line 67 of file screen_manager.hpp.

Referenced by get_speed(), handle_screen_switch(), push_screen(), and run().

bool ScreenManager::nextpop [private]

Definition at line 68 of file screen_manager.hpp.

Referenced by exit_screen(), handle_screen_switch(), and push_screen().

bool ScreenManager::nextpush [private]

Definition at line 69 of file screen_manager.hpp.

Referenced by exit_screen(), handle_screen_switch(), and push_screen().

float ScreenManager::fps [private]

measured fps

Definition at line 71 of file screen_manager.hpp.

Referenced by draw().

std::auto_ptr<Screen> ScreenManager::next_screen [private]

Definition at line 72 of file screen_manager.hpp.

Referenced by exit_screen(), handle_screen_switch(), and push_screen().

std::auto_ptr<Screen> ScreenManager::current_screen [private]

Definition at line 73 of file screen_manager.hpp.

Referenced by draw(), handle_screen_switch(), run(), and update_gamelogic().

std::auto_ptr<Console> ScreenManager::console [private]

Definition at line 74 of file screen_manager.hpp.

std::auto_ptr<ScreenFade> ScreenManager::screen_fade [private]

Definition at line 75 of file screen_manager.hpp.

Referenced by draw(), exit_screen(), handle_screen_switch(), has_no_pending_fadeout(), push_screen(), quit(), set_screen_fade(), and update_gamelogic().

std::vector<Screen*> ScreenManager::screen_stack [private]

Definition at line 76 of file screen_manager.hpp.

Referenced by handle_screen_switch(), quit(), and ~ScreenManager().

bool ScreenManager::screenshot_requested [private]

true if a screenshot should be taken after the next frame has been rendered

Definition at line 77 of file screen_manager.hpp.

Referenced by draw(), and take_screenshot().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:34 2014 for SuperTux by  doxygen 1.5.1