*******************************************************************************/
+#include <algorithm>
#include <cstdlib> // exit
-#include <iostream>
+#include <list>
#include <string>
#include <SDL/SDL.h>
#include "fastevents.h"
+#include <AL/alut.h>
#include "Dispatcher.hh"
#include "Engine.hh"
+#include "Event.hh"
+#include "Log.hh"
#include "Random.hh"
#include "Settings.hh"
#include "Timer.hh"
namespace Mf {
-class Engine::EngineImpl
+class Engine::Impl
{
public:
- EngineImpl(int argc, char* argv[], const std::string& configFile,
- const std::string& name, const std::string& iconFile,
- Engine* outer) :
- interface(outer),
- settings(argc, argv)
+ Impl(int argc, char* argv[], const std::string& name,
+ const std::string& iconFile, const std::string& configFile,
+ Engine& engine) :
+ interface(engine),
+ timestep(0.01),
+ printFps(false)
{
- if (SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_EVENTTHREAD) != 0)
+#if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__)
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
+#else
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) != 0)
+#endif
{
- throw Exception(SDL_GetError());
+ logError("sdl is complaining: %s", SDL_GetError());
+ throw Exception(Exception::SDL_ERROR);
}
if (FE_Init() != 0)
{
- throw Exception(FE_GetError());
+ logError("fast events error: %s", FE_GetError());
+ throw Exception(Exception::SDL_ERROR);
}
+ alutInit(&argc, argv);
+ Settings& settings = Settings::getInstance();
settings.loadFromFile(configFile);
+ settings.parseArgs(argc, argv);
long randomSeed;
- if (settings.get("engine.rngseed", randomSeed))
- {
- setSeed(randomSeed);
- }
- else
- {
- setSeed();
- }
+ 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);
+ timestep = 1.0 / timeStep;
- long maxFps = 40;
- settings.getNumber("video.maxfps", maxFps);
- drawRate = 1.0 / Scalar(maxFps);
+ Scalar maxFps = 40.0;
+ settings.get("maxfps", maxFps);
+ drawRate = 1.0 / maxFps;
- printFps = false;
- settings.get("video.printfps", printFps);
+ settings.get("printfps", printFps);
- video = VideoPtr(new Video(name, iconFile));
+ video = Video::alloc(name, iconFile);
video->makeActive();
}
- ~EngineImpl()
+ ~Impl()
{
// the video object must be destroyed before we can shutdown SDL
video.reset();
+ alutExit();
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
- * with a value of 0 if the member variable running becomes true.
+ * the exit code used to stop the loop.
*/
- int run()
+ void run()
{
- Scalar ticksNow = getTicks();
+ Scalar ticksNow = Timer::getTicks();
Scalar nextStep = ticksNow;
Scalar nextDraw = ticksNow;
fps = 0;
int frameAccum = 0;
- running = true;
do
{
- Scalar newTicks = getTicks();
+ Scalar newTicks = Timer::getTicks();
deltaTime = newTicks - ticksNow;
ticksNow = newTicks;
if (deltaTime >= 0.25) deltaTime = 0.25;
accumulator += deltaTime;
+ Timer::fireIfExpired(ticksNow);
+
while (accumulator >= timestep)
{
dispatchEvents();
- interface->update(totalTime, timestep);
+ update(totalTime, timestep);
totalTime += timestep;
accumulator -= timestep;
nextStep += timestep;
}
+ if (ticksNow >= nextStep)
+ {
+ nextStep = ticksNow + timestep;
+ }
if (ticksNow >= nextDraw)
{
if (printFps)
{
- std::cout << "FPS: " << fps << std::endl;
+ logInfo("%d fps", fps);
}
}
- interface->draw(accumulator / timestep);
+ draw(accumulator / timestep);
video->swap();
nextDraw += drawRate;
}
// be a good citizen and give back what you don't need
- sleep(std::min(nextStep, nextDraw), true);
+ Timer::sleep(std::min(std::min(nextStep, nextDraw),
+ Timer::getNextFire()), true);
}
- while (running);
-
- return 0;
+ while (!stack.empty());
}
-
void dispatchEvents()
{
SDL_Event event;
if (event.key.keysym.sym == SDLK_ESCAPE &&
(SDL_GetModState() & KMOD_CTRL) )
{
- exit(0);
+ // emergency escape
+ exit(0);
}
break;
break;
}
- interface->handleEvent(event);
+ handleEvent(event);
+ }
+ }
+
+
+ void update(Scalar t, Scalar dt)
+ {
+ for (stackIt = stack.begin(); stackIt != stack.end(); ++stackIt)
+ {
+ (*stackIt)->update(t, dt);
+ }
+ }
+
+ void draw(Scalar alpha)
+ {
+ // FIXME - this will crash if the layer being drawn pops itself
+ std::list<LayerP>::reverse_iterator it;
+ for (it = stack.rbegin(); it != stack.rend(); ++it)
+ {
+ (*it)->draw(alpha);
}
}
+ void handleEvent(const Event& event)
+ {
+ for (stackIt = stack.begin(); stackIt != stack.end(); ++stackIt)
+ {
+ if ((*stackIt)->handleEvent(event)) break;
+ }
+ }
+
+
+ void push(LayerP layer)
+ {
+ ASSERT(layer && "cannot push null layer");
+ stack.push_front(layer);
+ logInfo(" push: %d", stack.size());
+ layer->pushed(interface);
+ }
+
+ LayerP pop()
+ {
+ bool fixIt = false;
+ if (stack.begin() == stackIt) fixIt = true;
- Engine* interface;
+ LayerP popped = stack.front();
+ stack.pop_front();
+ logInfo(" pop: %d", stack.size());
+ popped->popped(interface);
- Settings settings;
- Dispatcher dispatcher;
- VideoPtr video;
+ if (fixIt) stackIt = --stack.begin();
- bool running;
+ return popped;
+ }
- Scalar timestep;
- Scalar drawRate;
+ LayerP pop(Layer* layer)
+ {
+ bool fixIt = false;
- long fps;
- bool printFps;
-};
+ std::list<LayerP> popped;
+
+ std::list<LayerP>::iterator it;
+ for (it = stack.begin(); it != stack.end(); ++it)
+ {
+ popped.push_back(*it);
+
+ if (it == stackIt) fixIt = true;
+ if ((*it).get() == layer)
+ {
+ ++it;
+ stack.erase(stack.begin(), it);
+
+ for (it = popped.begin(); it != popped.end(); ++it)
+ {
+ (*it)->popped(interface);
+ }
+
+ if (fixIt) stackIt = --stack.begin();
+
+ return popped.back();
+ }
+ }
+
+ return LayerP();
+ }
+
+ void clear()
+ {
+ stack.clear();
+ stackIt = stack.begin();
+ logInfo("clear: %d", stack.size());
+ }
+
+
+ Engine& interface;
+
+ VideoP video;
+
+ std::list<LayerP> stack;
+ std::list<LayerP>::iterator stackIt;
+
+ Scalar timestep;
+ Scalar drawRate;
-Engine::Engine(int argc, char* argv[], const std::string& configFile,
- const std::string& name, const std::string& iconFile) :
- impl_(new Engine::EngineImpl(argc, argv, configFile, name, iconFile, this))
-{}
+ long fps;
+ bool printFps;
+};
-Engine::~Engine() {}
+static Engine* instance = 0;
-int Engine::run()
+Engine::Engine(int argc, char* argv[], const std::string& name,
+ const std::string& iconFile, const std::string& configFile) :
+ impl_(new Engine::Impl(argc, argv, name, iconFile, configFile, *this))
{
- return impl_->run();
+ instance = this;
}
-void Engine::stop()
+
+Engine& Engine::getInstance()
{
- impl_->running = false;
+ ASSERT(instance && "dereferencing null pointer");
+ return *instance;
+ //static Engine engine;
+ //return engine;
}
+void Engine::run()
+{
+ return impl_->run();
+}
+
void Engine::setTimestep(Scalar ts)
{
impl_->timestep = ts;
}
-Scalar Engine::getTimestep()
+Scalar Engine::getTimestep() const
{
return impl_->timestep;
}
impl_->drawRate = 1.0 / Scalar(maxFps);
}
-long Engine::getMaxFrameRate()
+long Engine::getMaxFrameRate() const
{
return long(1.0 / impl_->drawRate);
}
-Video& Engine::getVideo()
+Video& Engine::getVideo() const
{
return *impl_->video;
}
-long Engine::getFrameRate()
+long Engine::getFrameRate() const
{
return impl_->fps;
}
-void Engine::update(Scalar t, Scalar dt) {}
-void Engine::draw(Scalar alpha) {}
-void Engine::handleEvent(const Event& event) {}
+void Engine::push(LayerP layer)
+{
+ // pass through
+ impl_->push(layer);
+}
+
+LayerP Engine::pop()
+{
+ // pass through
+ return impl_->pop();
+}
+
+LayerP Engine::pop(Layer* layer)
+{
+ // pass through
+ return impl_->pop(layer);
+}
+
+void Engine::clear()
+{
+ // pass through
+ impl_->clear();
+}
} // namespace Mf