00001 00005 /* Copyright © 2009, 2010 James Legg. 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 */ 00011 #include "MainLoop.h" 00012 00013 #include <SDL.h> 00014 #include "Engine/InputHandler.h" 00015 #include "Debug.h" 00016 #include "Graphics/Window.h" 00017 00018 #include <climits> 00019 00020 MainLoop::MainLoop() 00021 : done(false) 00022 , quit(false) 00023 { 00024 } 00025 00026 MainLoop::~MainLoop() 00027 { 00028 } 00029 00030 void MainLoop::run(Engine::Scene & scene) 00031 { 00032 while (!(done || quit)) 00033 { 00034 Engine::InputHandler::get_instance().set_scene(scene); 00035 redraw(scene); 00036 update_sound(scene); 00037 process_events(); 00038 do_logic(scene); 00039 } 00040 done = false; 00041 } 00042 00043 void MainLoop::pop_scene() 00044 { 00045 done = true; 00046 } 00047 00048 void MainLoop::push_scene(Engine::Scene & scene_in) 00049 { 00050 scene_in.attach_main_loop(*this); 00051 // don't let the time spent loading the scene cause a large amount of time 00052 // to pass at the start. 00053 milliseconds = SDL_GetTicks(); 00054 run(scene_in); 00055 } 00056 00057 void MainLoop::process_events() 00058 { 00059 // Process events. We handle all quit and resize events. 00060 SDL_Event event; 00061 SDL_PumpEvents(); 00062 while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, 00063 SDL_EVENTMASK(SDL_QUIT) | SDL_EVENTMASK(SDL_VIDEORESIZE)) > 0) 00064 { 00065 switch (event.type) 00066 { 00067 case SDL_QUIT: 00068 // exit 00069 quit = true; 00070 break; 00071 case SDL_VIDEORESIZE: 00072 // tell the window. 00073 Graphics::Window::get_instance().set_size(event.resize.w, event.resize.h); 00074 break; 00075 } 00076 } 00077 // Get the input handler to poll to process the other events. 00078 Engine::InputHandler::get_instance().poll(); 00079 // Any remaining events can be ignored. We can't pump events, as this will 00080 // cause us to potentially ignore good events. SDLPollEvents implicitly 00081 // pumps the event queue, so we have to peep at all events. 00082 // If we don't remove all the ignored events from the queue, they eventually 00083 // clog up the event queue, and we cannot get to the events we want. 00084 while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS)); 00085 00086 } 00087 00088 void MainLoop::do_logic(Engine::Scene & scene) 00089 { 00090 // calculate elapsed time 00091 unsigned int new_milliseconds = SDL_GetTicks(); 00092 unsigned int elapsed_milliseconds = new_milliseconds - milliseconds; 00093 if (elapsed_milliseconds > UINT_MAX / 2) 00094 { 00095 // assume it has wrapped. 00096 elapsed_milliseconds = UINT_MAX - elapsed_milliseconds; 00097 } 00098 scene.update_logic(elapsed_milliseconds); 00099 milliseconds = new_milliseconds; 00100 } 00101 00102 void MainLoop::redraw(Engine::Scene & scene) 00103 { 00104 scene.draw(); 00105 Graphics::Window::get_instance().swap_buffers(); 00106 } 00107 00108 void MainLoop::update_sound(Engine::Scene & scene) 00109 { 00110 scene.do_sound(); 00111 }
Generated at Mon Sep 6 00:41:13 2010 by Doxygen version 1.4.7 for Racer version svn335.