*******************************************************************************/
#include <algorithm>
-#include <cstdlib> // exit
+#include <cstdlib> // exit, srand
+#include <ctime> // time
#include <list>
#include <string>
+#include <AL/alc.h>
#include <SDL/SDL.h>
#include "fastevents.h"
-#include <AL/alut.h>
+
#include "Engine.hh"
#include "Event.hh"
-#include "Exception.hh"
#include "Log.hh"
#include "Math.hh"
-#include "Random.hh"
#include "Settings.hh"
#include "Timer.hh"
{
public:
- Impl(Engine& engine) :
- mInterface(engine),
+ Impl() :
+ mError(Error::NONE),
mTimestep(0.01),
mPrintFps(false)
{
#endif
{
const char* error = SDL_GetError();
- throw Exception(ErrorCode::SDL_INIT, error);
+ mError.init(Error::SDL_INIT, error);
+ //throw Exception(Error::SDL_INIT, error);
+ }
+ else
+ {
+ char vdName[128];
+ SDL_VideoDriverName(vdName, sizeof(vdName));
+ logInfo << "initialized SDL; using video driver `"
+ << vdName << "'" << std::endl;
}
+
if (FE_Init() != 0)
{
const char* error = FE_GetError();
- throw Exception(ErrorCode::FASTEVENTS_INIT, error);
+ mError.init(Error::FASTEVENTS_INIT, error);
+ //throw Exception(Error::FASTEVENTS_INIT, error);
+ }
+
+ mAlDevice = alcOpenDevice(0);
+ mAlContext = alcCreateContext(mAlDevice, 0);
+ if (!mAlDevice || !mAlContext)
+ {
+ const char* error = alcGetString(mAlDevice,alcGetError(mAlDevice));
+ logError << "error while creating audio context: "
+ << error << std::endl;
+ }
+ else
+ {
+ alcMakeContextCurrent(mAlContext);
+ logInfo << "opened sound device `"
+ << alcGetString(mAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER)
+ << "'" << std::endl;
}
- int argc = 1;
- char name[] = "hello";
- alutInit(&argc, (char**)&name);
-
+
// now load the settings the engine needs
Settings& settings = Settings::getInstance();
unsigned randomSeed;
- if (settings.get("rngseed", randomSeed)) setSeed(randomSeed);
- else setSeed();
+ if (settings.get("rngseed", randomSeed)) srand(randomSeed);
+ else srand(time(0));
Scalar timestep = 80.0;
settings.get("timestep", timestep);
// the video object must be destroyed before we can shutdown SDL
mVideo.reset();
- alutExit();
+ alcMakeContextCurrent(0);
+ alcDestroyContext(mAlContext);
+ alcCloseDevice(mAlDevice);
+
FE_Quit();
SDL_Quit();
}
if (mPrintFps)
{
- logInfo("%d fps", mFps);
+ logInfo << mFps << " fps" << std::endl;
}
}
Timer::getNextFire()), Timer::ACTUAL);
}
while (!mStack.empty());
+
+ mDispatch.dispatch("engine.stopping");
}
void dispatchEvents()
{
for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
{
- (*mStackIt)->update(mInterface, t, dt);
+ (*mStackIt)->update(t, dt);
}
}
std::list<LayerP>::reverse_iterator it;
for (it = mStack.rbegin(); it != mStack.rend(); ++it)
{
- (*it)->draw(mInterface, alpha);
+ (*it)->draw(alpha);
}
}
{
for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
{
- if ((*mStackIt)->handleEvent(mInterface, event)) break;
+ if ((*mStackIt)->handleEvent(event)) break;
}
}
{
ASSERT(layer && "cannot push null layer");
mStack.push_front(layer);
- logDebug("stack: %d [pushed %X]", mStack.size(), layer.get());
- layer->pushed(mInterface);
+ logInfo << "stack: " << mStack.size()
+ << " [pushed " << layer.get() << "]" << std::endl;
+ layer->pushedOntoEngine();
}
LayerP pop()
LayerP layer = mStack.front();
mStack.pop_front();
- logDebug("stack: %d [popped %X]", mStack.size(), layer.get());
- layer->popped(mInterface);
+ logInfo << "stack: " << mStack.size()
+ << " [popped " << layer.get() << "]" << std::endl;
+ layer->poppedFromEngine();
if (fixIt) mStackIt = --mStack.begin();
for (it = layers.begin(); it != layers.end(); ++it)
{
- (*it)->popped(mInterface);
- logDebug("stack: %d [popped %X]", mStack.size(), (*it).get());
+ (*it)->poppedFromEngine();
+ logInfo << "stack: " << mStack.size()
+ << " [popped " << (*it).get() << "]" << std::endl;
}
if (fixIt) mStackIt = --mStack.begin();
{
mStack.clear();
mStackIt = mStack.begin();
- logDebug("stack: 0 [cleared]");
+ logInfo("stack: 0 [cleared]");
}
{
if (mMaxFps < mTimestep)
{
- logWarning("capping maximum fps to timestep (%f)", mTimestep);
+ logWarning << "capping maximum fps to timestep ("
+ << mTimestep << ")" << std::endl;
mMaxFps = mTimestep;
}
}
- Engine& mInterface;
+ Error mError;
+
VideoP mVideo;
Dispatch mDispatch;
+ ALCdevice* mAlDevice;
+ ALCcontext* mAlContext;
+
std::list<LayerP> mStack;
std::list<LayerP>::iterator mStackIt;
Engine::Engine() :
// pass through
- mImpl(new Engine::Impl(*this)) {}
+ mImpl(new Engine::Impl) {}
-Engine& Engine::getInstance()
+
+const Error& Engine::getError() const
{
- static Engine engine;
- return engine;
+ // pass through
+ return mImpl->mError;
}
}
+Engine engine;
+
+
} // namespace Mf
/** vim: set ts=4 sw=4 tw=80: *************************************************/