#include "Dispatcher.hh"
#include "Engine.hh"
#include "Event.hh"
+#include "Exception.hh"
#include "Log.hh"
#include "Random.hh"
#include "Settings.hh"
class Engine::Impl
{
public:
+
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)
+ mInterface(engine),
+ mTimestep(0.01),
+ mPrintFps(false)
{
-#if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__)
+#if defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) || defined(__WINDOWS__)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
#else
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) != 0)
#endif
{
- logError("sdl is complaining: %s", SDL_GetError());
- throw Exception(Exception::SDL_ERROR);
+ const char* error = SDL_GetError();
+ logError("sdl is complaining: %s", error);
+ throw Exception(ErrorCode::SDL_INIT, error);
}
if (FE_Init() != 0)
{
- logError("fast events error: %s", FE_GetError());
- throw Exception(Exception::SDL_ERROR);
+ const char* error = FE_GetError();
+ logError("fast events error: %s", error);
+ throw Exception(ErrorCode::FASTEVENTS_INIT, error);
}
alutInit(&argc, argv);
if (settings.get("rngseed", randomSeed)) setSeed(randomSeed);
else setSeed();
- Scalar timeStep = 80.0;
- settings.get("timestep", timeStep);
- timestep = 1.0 / timeStep;
+ Scalar timestep = 80.0;
+ settings.get("timestep", timestep);
+ mTimestep = 1.0 / timestep;
Scalar maxFps = 40.0;
settings.get("maxfps", maxFps);
- drawRate = 1.0 / maxFps;
+ mDrawRate = 1.0 / maxFps;
- settings.get("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();
Scalar totalTime = 0.0;
Scalar deltaTime = 0.0;
- Scalar accumulator = timestep;
+ Scalar accumulator = mTimestep;
- fps = 0;
+ mFps = 0;
int frameAccum = 0;
do
Timer::fireIfExpired(ticksNow);
- while (accumulator >= timestep)
+ while (accumulator >= mTimestep)
{
dispatchEvents();
- 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("%d fps", fps);
+ logInfo("%d fps", mFps);
}
}
- 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 (!stack.empty());
+ while (!mStack.empty());
}
void dispatchEvents()
break;
case SDL_VIDEORESIZE:
- video->resize(event.resize.w, event.resize.h);
+ mVideo->resize(event.resize.w, event.resize.h);
break;
}
void update(Scalar t, Scalar dt)
{
- for (stackIt = stack.begin(); stackIt != stack.end(); ++stackIt)
+ for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
{
- (*stackIt)->update(t, dt);
+ (*mStackIt)->update(t, dt);
}
}
{
// 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)
+ for (it = mStack.rbegin(); it != mStack.rend(); ++it)
{
(*it)->draw(alpha);
}
void handleEvent(const Event& event)
{
- for (stackIt = stack.begin(); stackIt != stack.end(); ++stackIt)
+ for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
{
- if ((*stackIt)->handleEvent(event)) break;
+ if ((*mStackIt)->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);
+ mStack.push_front(layer);
+ logInfo(" push: %d", mStack.size());
+ layer->pushed(mInterface);
}
LayerP pop()
{
bool fixIt = false;
- if (stack.begin() == stackIt) fixIt = true;
+ if (mStack.begin() == mStackIt) fixIt = true;
- LayerP popped = stack.front();
- stack.pop_front();
- logInfo(" pop: %d", stack.size());
- popped->popped(interface);
+ LayerP popped = mStack.front();
+ mStack.pop_front();
+ logInfo(" pop: %d", mStack.size());
+ popped->popped(mInterface);
- if (fixIt) stackIt = --stack.begin();
+ if (fixIt) mStackIt = --mStack.begin();
return popped;
}
std::list<LayerP> popped;
std::list<LayerP>::iterator it;
- for (it = stack.begin(); it != stack.end(); ++it)
+ for (it = mStack.begin(); it != mStack.end(); ++it)
{
popped.push_back(*it);
- if (it == stackIt) fixIt = true;
+ if (it == mStackIt) fixIt = true;
if ((*it).get() == layer)
{
++it;
- stack.erase(stack.begin(), it);
+ mStack.erase(mStack.begin(), it);
for (it = popped.begin(); it != popped.end(); ++it)
{
- (*it)->popped(interface);
+ (*it)->popped(mInterface);
}
- if (fixIt) stackIt = --stack.begin();
+ if (fixIt) mStackIt = --mStack.begin();
return popped.back();
}
void clear()
{
- stack.clear();
- stackIt = stack.begin();
- logInfo("clear: %d", stack.size());
+ mStack.clear();
+ mStackIt = mStack.begin();
+ logInfo("clear: %d", mStack.size());
}
- Engine& interface;
+ Engine& mInterface;
- VideoP video;
+ VideoP mVideo;
- std::list<LayerP> stack;
- std::list<LayerP>::iterator stackIt;
+ std::list<LayerP> mStack;
+ std::list<LayerP>::iterator mStackIt;
- Scalar timestep;
- Scalar drawRate;
+ Scalar mTimestep;
+ Scalar mDrawRate;
- long fps;
- bool printFps;
+ long mFps;
+ bool mPrintFps;
};
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))
+ mImpl(new Engine::Impl(argc, argv, name, iconFile, configFile, *this))
{
instance = this;
}
{
ASSERT(instance && "dereferencing null pointer");
return *instance;
+ // TODO this has not been completely thought out
//static Engine engine;
//return engine;
}
void Engine::run()
{
- return impl_->run();
+ return mImpl->run();
}
void Engine::setTimestep(Scalar ts)
{
- impl_->timestep = ts;
+ mImpl->mTimestep = ts;
}
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 / impl_->drawRate);
+ return long(1.0 / mImpl->mDrawRate);
}
Video& Engine::getVideo() const
{
- return *impl_->video;
+ return *mImpl->mVideo;
}
long Engine::getFrameRate() const
{
- return impl_->fps;
+ return mImpl->mFps;
}
void Engine::push(LayerP layer)
{
// pass through
- impl_->push(layer);
+ mImpl->push(layer);
}
LayerP Engine::pop()
{
// pass through
- return impl_->pop();
+ return mImpl->pop();
}
LayerP Engine::pop(Layer* layer)
{
// pass through
- return impl_->pop(layer);
+ return mImpl->pop(layer);
}
void Engine::clear()
{
// pass through
- impl_->clear();
+ mImpl->clear();
}