/******************************************************************************* Copyright (c) 2009, Charles McGarvey All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include // exit #include #include #include #include "fastevents.h" #include #include #include "Engine.hh" #include "Random.hh" #include "Settings.hh" #include "Timer.hh" #include "Video.hh" namespace Mf { class Engine::Impl { public: Impl(int argc, char* argv[], const std::string& configFile, const std::string& name, const std::string& iconFile, Engine* outer) : interface(outer) { #if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__) if (SDL_Init(SDL_INIT_EVERYTHING) != 0) #else if (SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_EVENTTHREAD) != 0) #endif { std::cerr << "sdl is complaining: " << SDL_GetError() << std::endl; throw Exception(Exception::SDL_ERROR); } if (FE_Init() != 0) { std::cerr << "fast events error: " << FE_GetError() << std::endl; throw Exception(Exception::SDL_ERROR); } if (Sound_Init() == 0) { std::cerr << "sound initialization failed: " << Sound_GetError() << std::endl; throw Exception(Exception::SDL_ERROR); } alutInit(&argc, argv); Settings& settings = Settings::getInstance(); settings.parseArgs(argc, argv); settings.loadFromFile(configFile); long randomSeed; if (settings.get("engine.rngseed", randomSeed)) setSeed(randomSeed); else setSeed(); double ts = 0.01; settings.get("engine.timestep", ts); timestep = Scalar(ts); long maxFps = 40; settings.getNumber("video.maxfps", maxFps); drawRate = 1.0 / Scalar(maxFps); printFps = false; settings.get("video.printfps", printFps); video = Video::alloc(name, iconFile); video->makeActive(); } ~Impl() { // the video object must be destroyed before we can shutdown SDL video.reset(); alutExit(); Sound_Quit(); FE_Quit(); SDL_Quit(); } /** * The main loop. This just calls dispatchEvents(), update(), and draw() * over and over again. The timing of the update and draw are decoupled. * The actual frame rate is also calculated here. This function will return * the exit code used to stop the loop. */ int run() { Scalar ticksNow = getTicks(); Scalar nextStep = ticksNow; Scalar nextDraw = ticksNow; Scalar nextFpsUpdate = ticksNow + 1.0; Scalar totalTime = 0.0; Scalar deltaTime = 0.0; Scalar accumulator = timestep; fps = 0; int frameAccum = 0; running = true; do { Scalar newTicks = getTicks(); deltaTime = newTicks - ticksNow; ticksNow = newTicks; if (deltaTime >= 0.25) deltaTime = 0.25; accumulator += deltaTime; while (accumulator >= timestep) { dispatchEvents(); interface->update(totalTime, timestep); totalTime += timestep; accumulator -= timestep; nextStep += timestep; } if (ticksNow >= nextStep) { nextStep = ticksNow + timestep; } if (ticksNow >= nextDraw) { frameAccum++; if (ticksNow >= nextFpsUpdate) // determine the actual fps { fps = frameAccum; frameAccum = 0; nextFpsUpdate += 1.0; if (ticksNow >= nextFpsUpdate) { nextFpsUpdate = ticksNow + 1.0; } if (printFps) { std::cout << "FPS: " << fps << std::endl; } } interface->draw(accumulator / timestep); video->swap(); nextDraw += drawRate; if (ticksNow >= nextDraw) { // we missed some scheduled draws, so reset the schedule nextDraw = ticksNow + drawRate; } } // be a good citizen and give back what you don't need sleep(std::min(nextStep, nextDraw), true); } while (running); return exitCode; } void dispatchEvents() { SDL_Event event; while (FE_PollEvent(&event) == 1) { switch (event.type) { case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE && (SDL_GetModState() & KMOD_CTRL) ) { exit(0); } break; case SDL_VIDEORESIZE: video->resize(event.resize.w, event.resize.h); break; } interface->handleEvent(event); } } Engine* interface; VideoP video; bool running; int exitCode; Scalar timestep; Scalar drawRate; long fps; bool printFps; }; Engine::Engine(int argc, char* argv[], const std::string& configFile, const std::string& name, const std::string& iconFile) : impl_(new Engine::Impl(argc, argv, configFile, name, iconFile, this)) {} Engine::~Engine() {} int Engine::run() { return impl_->run(); } void Engine::stop(int exitCode) { impl_->running = false; impl_->exitCode = exitCode; } void Engine::setTimestep(Scalar ts) { impl_->timestep = ts; } Scalar Engine::getTimestep() { return impl_->timestep; } void Engine::setMaxFrameRate(long maxFps) { impl_->drawRate = 1.0 / Scalar(maxFps); } long Engine::getMaxFrameRate() { return long(1.0 / impl_->drawRate); } Video& Engine::getVideo() { return *impl_->video; } long Engine::getFrameRate() { return impl_->fps; } void Engine::update(Scalar t, Scalar dt) {} void Engine::draw(Scalar alpha) {} void Engine::handleEvent(const Event& event) {} } // namespace Mf /** vim: set ts=4 sw=4 tw=80: *************************************************/