*******************************************************************************/
+#include <algorithm>
#include <cstdlib> // exit
+#include <list>
#include <string>
#include <SDL/SDL.h>
#include "Dispatcher.hh"
#include "Engine.hh"
+#include "Event.hh"
#include "Log.hh"
#include "Random.hh"
#include "Settings.hh"
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)
+
+ Impl(int argc, char* argv[], const std::string& name,
+ const std::string& iconFile, const std::string& configFile,
+ Engine& engine) :
+ mInterface(engine),
+ mTimestep(0.01),
+ mPrintFps(false)
{
#if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
alutInit(&argc, argv);
Settings& settings = Settings::getInstance();
- settings.parseArgs(argc, argv);
settings.loadFromFile(configFile);
+ settings.parseArgs(argc, argv);
long randomSeed;
- if (settings.get("engine.rngseed", randomSeed)) setSeed(randomSeed);
+ if (settings.get("rngseed", randomSeed)) setSeed(randomSeed);
else setSeed();
- double ts = 0.01;
- settings.get("engine.timestep", ts);
- timestep = Scalar(ts);
+ Scalar timestep = 80.0;
+ settings.get("timestep", timestep);
+ mTimestep = 1.0 / timestep;
- long maxFps = 40;
- settings.getNumber("video.maxfps", maxFps);
- drawRate = 1.0 / Scalar(maxFps);
+ Scalar maxFps = 40.0;
+ settings.get("maxfps", maxFps);
+ mDrawRate = 1.0 / maxFps;
- printFps = false;
- settings.get("video.printfps", printFps);
+ settings.get("printfps", mPrintFps);
- video = Video::alloc(name, iconFile);
- video->makeActive();
+ mVideo = Video::alloc(name, iconFile);
+ mVideo->makeActive();
}
~Impl()
{
// the video object must be destroyed before we can shutdown SDL
- video.reset();
+ mVideo.reset();
alutExit();
FE_Quit();
* the exit code used to stop the loop.
*/
- int run()
+ void run()
{
Scalar ticksNow = Timer::getTicks();
Scalar totalTime = 0.0;
Scalar deltaTime = 0.0;
- Scalar accumulator = timestep;
+ Scalar accumulator = mTimestep;
- fps = 0;
+ mFps = 0;
int frameAccum = 0;
- running = true;
do
{
Scalar newTicks = Timer::getTicks();
Timer::fireIfExpired(ticksNow);
- while (accumulator >= timestep)
+ while (accumulator >= mTimestep)
{
dispatchEvents();
- interface->update(totalTime, timestep);
+ update(totalTime, mTimestep);
- totalTime += timestep;
- accumulator -= timestep;
+ totalTime += mTimestep;
+ accumulator -= mTimestep;
- nextStep += timestep;
+ nextStep += mTimestep;
}
if (ticksNow >= nextStep)
{
- nextStep = ticksNow + timestep;
+ nextStep = ticksNow + mTimestep;
}
if (ticksNow >= nextDraw)
if (ticksNow >= nextFpsUpdate) // determine the actual fps
{
- fps = frameAccum;
+ mFps = frameAccum;
frameAccum = 0;
nextFpsUpdate += 1.0;
nextFpsUpdate = ticksNow + 1.0;
}
- if (printFps)
+ if (mPrintFps)
{
- logInfo("framerate: %d fps", fps);
+ logInfo("%d fps", mFps);
}
}
- interface->draw(accumulator / timestep);
- video->swap();
+ draw(accumulator / mTimestep);
+ mVideo->swap();
- nextDraw += drawRate;
+ nextDraw += mDrawRate;
if (ticksNow >= nextDraw)
{
// we missed some scheduled draws, so reset the schedule
- nextDraw = ticksNow + drawRate;
+ nextDraw = ticksNow + mDrawRate;
}
}
Timer::sleep(std::min(std::min(nextStep, nextDraw),
Timer::getNextFire()), true);
}
- while (running);
-
- return exitCode;
+ while (!mStack.empty());
}
-
void dispatchEvents()
{
SDL_Event event;
if (event.key.keysym.sym == SDLK_ESCAPE &&
(SDL_GetModState() & KMOD_CTRL) )
{
- exit(0);
+ // emergency escape
+ exit(0);
}
break;
case SDL_VIDEORESIZE:
- video->resize(event.resize.w, event.resize.h);
+ mVideo->resize(event.resize.w, event.resize.h);
break;
}
- interface->handleEvent(event);
+ handleEvent(event);
}
}
- Engine* interface;
+ void update(Scalar t, Scalar dt)
+ {
+ for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
+ {
+ (*mStackIt)->update(t, dt);
+ }
+ }
- VideoP video;
+ void draw(Scalar alpha)
+ {
+ // FIXME - this will crash if the layer being drawn pops itself
+ std::list<LayerP>::reverse_iterator it;
+ for (it = mStack.rbegin(); it != mStack.rend(); ++it)
+ {
+ (*it)->draw(alpha);
+ }
+ }
- bool running;
- int exitCode;
+ void handleEvent(const Event& event)
+ {
+ for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
+ {
+ if ((*mStackIt)->handleEvent(event)) break;
+ }
+ }
- Scalar timestep;
- Scalar drawRate;
- long fps;
- bool printFps;
-};
+ void push(LayerP layer)
+ {
+ ASSERT(layer && "cannot push null layer");
+ mStack.push_front(layer);
+ logInfo(" push: %d", mStack.size());
+ layer->pushed(mInterface);
+ }
+
+ LayerP pop()
+ {
+ bool fixIt = false;
+ if (mStack.begin() == mStackIt) fixIt = true;
+
+ LayerP popped = mStack.front();
+ mStack.pop_front();
+ logInfo(" pop: %d", mStack.size());
+ popped->popped(mInterface);
+
+ if (fixIt) mStackIt = --mStack.begin();
+
+ return popped;
+ }
+
+ LayerP pop(Layer* layer)
+ {
+ bool fixIt = false;
+
+ std::list<LayerP> popped;
+
+ std::list<LayerP>::iterator it;
+ for (it = mStack.begin(); it != mStack.end(); ++it)
+ {
+ popped.push_back(*it);
+
+ if (it == mStackIt) fixIt = true;
+
+ if ((*it).get() == layer)
+ {
+ ++it;
+ mStack.erase(mStack.begin(), it);
+ for (it = popped.begin(); it != popped.end(); ++it)
+ {
+ (*it)->popped(mInterface);
+ }
-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)) {}
+ if (fixIt) mStackIt = --mStack.begin();
-Engine::~Engine() {}
+ return popped.back();
+ }
+ }
+
+ return LayerP();
+ }
+
+ void clear()
+ {
+ mStack.clear();
+ mStackIt = mStack.begin();
+ logInfo("clear: %d", mStack.size());
+ }
+
+
+ Engine& mInterface;
+
+ VideoP mVideo;
+
+ std::list<LayerP> mStack;
+ std::list<LayerP>::iterator mStackIt;
+
+ Scalar mTimestep;
+ Scalar mDrawRate;
+
+ long mFps;
+ bool mPrintFps;
+};
-int Engine::run()
+static Engine* instance = 0;
+
+Engine::Engine(int argc, char* argv[], const std::string& name,
+ const std::string& iconFile, const std::string& configFile) :
+ mImpl(new Engine::Impl(argc, argv, name, iconFile, configFile, *this))
{
- return impl_->run();
+ instance = this;
}
-void Engine::stop(int exitCode)
+
+Engine& Engine::getInstance()
{
- impl_->running = false;
- impl_->exitCode = exitCode;
+ ASSERT(instance && "dereferencing null pointer");
+ return *instance;
+ // TODO this has not been completely thought out
+ //static Engine engine;
+ //return engine;
}
+void Engine::run()
+{
+ return mImpl->run();
+}
+
void Engine::setTimestep(Scalar ts)
{
- impl_->timestep = ts;
+ mImpl->mTimestep = ts;
}
-Scalar Engine::getTimestep()
+Scalar Engine::getTimestep() const
{
- return impl_->timestep;
+ return mImpl->mTimestep;
}
void Engine::setMaxFrameRate(long maxFps)
{
- impl_->drawRate = 1.0 / Scalar(maxFps);
+ mImpl->mDrawRate = 1.0 / Scalar(maxFps);
+}
+
+long Engine::getMaxFrameRate() const
+{
+ return long(1.0 / mImpl->mDrawRate);
}
-long Engine::getMaxFrameRate()
+
+Video& Engine::getVideo() const
+{
+ return *mImpl->mVideo;
+}
+
+long Engine::getFrameRate() const
{
- return long(1.0 / impl_->drawRate);
+ return mImpl->mFps;
}
-Video& Engine::getVideo()
+void Engine::push(LayerP layer)
{
- return *impl_->video;
+ // pass through
+ mImpl->push(layer);
}
-long Engine::getFrameRate()
+LayerP Engine::pop()
{
- return impl_->fps;
+ // pass through
+ return mImpl->pop();
}
+LayerP Engine::pop(Layer* layer)
+{
+ // pass through
+ return mImpl->pop(layer);
+}
-void Engine::update(Scalar t, Scalar dt) {}
-void Engine::draw(Scalar alpha) {}
-void Engine::handleEvent(const Event& event) {}
+void Engine::clear()
+{
+ // pass through
+ mImpl->clear();
+}
} // namespace Mf