]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Core.cc
arch linux prefers pkgconfig for finding lua
[chaz/yoink] / src / Moof / Core.cc
index 90027307c081f4050411713d47e24e2bb57a5c87..b659f9890784c2c66cdc867b91671914d0f5260c 100644 (file)
@@ -1,30 +1,13 @@
 
-/*******************************************************************************
-
- 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.
-
-*******************************************************************************/
+/*]  Copyright (c) 2009-2010, Charles McGarvey  [**************************
+**]  All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
 
 #include <algorithm>
 #include <cstdlib>                     // exit, srand
@@ -32,7 +15,6 @@
 #include <list>
 #include <string>
 
-#include <AL/alc.h>
 #include <SDL/SDL.h>
 #include "fastevents.h"
 
@@ -79,15 +61,16 @@ public:
 
 
        /**
-        * 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.
+        * 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.
         */
 
        void run()
        {
                init();
+               ASSERT(video && "cannot run core without a current video context");
 
                Scalar totalTime = 0.0;
                Scalar ticks = Timer::getTicks();
@@ -97,49 +80,50 @@ public:
                Scalar nextSecond = ticks + SCALAR(1.0);
 
                mFps = 0;
-               int frames = 0;
+               int frameCount = 0;
 
-               const int MAX_FRAMESKIP = 15;
-               const Scalar inverseTimestep = SCALAR(1.0) / mTimestep;
+               const Scalar timestep = mTimestep;
+               const Scalar framerate = mFramerate;
 
-               ASSERT(video && "cannot run core without a current video context");
+               const int MAX_FRAMESKIP = 15;
+               const Scalar inverseTimestep = SCALAR(1.0) / timestep;
 
                do
                {
-                       Timer::fireIfExpired();
-                       dispatchEvents();
+                       Timer::fireIfExpired();                         // 1. fire timers
+                       dispatchEvents();                                       // 2. dispatch events
 
                        int i = 0;
                        while (nextUpdate < Timer::getTicks() && i < MAX_FRAMESKIP)
                        {
-                               totalTime += mTimestep;
-                               update(totalTime, mTimestep);
+                               totalTime += timestep;
+                               update(totalTime, timestep);    // 3. update state
 
-                               nextUpdate += mTimestep;
+                               nextUpdate += timestep;
                                ++i;
                        }
 
                        if (nextDraw < (ticks = Timer::getTicks()))
                        {
-                               ++frames;
-                               draw((ticks + mTimestep - nextUpdate) * inverseTimestep);
-                               video->swap();
+                               draw((ticks + timestep - nextUpdate) * inverseTimestep);
+                               video->swap();                                  // 4. draw state
 
-                               nextDraw += mFramerate;
+                               nextDraw += framerate;
+                               ++frameCount;
 
-                               if (mShowFps && nextSecond < ticks)
+                               if (nextSecond < Timer::getTicks())
                                {
-                                       mFps = frames;
-                                       frames = 0;
+                                       mFps = frameCount;
+                                       frameCount = 0;
 
-                                       logInfo << mFps << " fps" << std::endl;
+                                       if (mShowFps) logInfo << mFps << " fps" << std::endl;
 
                                        nextSecond += SCALAR(1.0);
                                }
                        }
 
-                       // be a good citizen and give back what you don't need
-                       Timer::sleep(0.0);
+                       ticks = Timer::getTicks();                      // 5. yield timeslice
+                       if (ticks < nextUpdate && ticks < nextDraw) Timer::sleep(0.0);
                }
                while (!mStack.empty());
 
@@ -177,7 +161,8 @@ public:
 
        void update(Scalar t, Scalar dt)
        {
-               for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
+               for (mStackIt = mStack.begin(); mStackIt != mStack.end();
+                               ++mStackIt)
                {
                        (*mStackIt)->update(t, dt);
                }
@@ -195,7 +180,8 @@ public:
 
        void handleEvent(const Event& event)
        {
-               for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
+               for (mStackIt = mStack.begin(); mStackIt != mStack.end();
+                               ++mStackIt)
                {
                        if ((*mStackIt)->handleEvent(event)) break;
                }
@@ -207,7 +193,7 @@ public:
                ASSERT(layer && "cannot push null layer");
                mStack.push_front(layer);
                logInfo << "stack: " << mStack.size()
-                                << " [pushed " << layer.get() << "]" << std::endl;
+                               << " [pushed " << layer.get() << "]" << std::endl;
                layer->addedToCore();
        }
 
@@ -219,7 +205,7 @@ public:
                LayerP layer = mStack.front();
                mStack.pop_front();
                logInfo << "stack: " << mStack.size()
-                                << " [popped " << layer.get() << "]" << std::endl;
+                               << " [popped " << layer.get() << "]" << std::endl;
                layer->removedFromCore();
 
                if (fixIt) mStackIt = --mStack.begin();
@@ -249,7 +235,8 @@ public:
                                {
                                        (*it)->removedFromCore();
                                        logInfo << "stack: " << mStack.size()
-                                                        << " [popped " << (*it).get() << "]" << std::endl;
+                                                       << " [popped " << (*it).get() << "]"
+                                                       << std::endl;
                                }
 
                                if (fixIt) mStackIt = --mStack.begin();
@@ -340,19 +327,20 @@ void Core::run()
 
 
 Dispatch::Handler Core::addHandler(const std::string& event,
-               const Dispatch::Function& callback)
+                                                                  const Dispatch::Function& callback)
 {
        return mImpl->mDispatch.addHandler(event, callback);
 }
 
 Dispatch::Handler Core::addHandler(const std::string& event,
-               const Dispatch::Function& callback, Dispatch::Handler handler)
+                                                                  const Dispatch::Function& callback,
+                                                                  Dispatch::Handler handler)
 {
        return mImpl->mDispatch.addHandler(event, callback, handler);
 }
 
 void Core::dispatch(const std::string& event,
-               const Dispatch::Message* message)
+                                       const Dispatch::Message* message)
 {
        mImpl->mDispatch.dispatch(event, message);
 }
@@ -368,9 +356,7 @@ class Backend_
 {
 public:
 
-       Backend_() :
-               mAlDevice(0),
-               mAlContext(0)
+       Backend_()
        {
 #if defined(_WIN32) || defined(__WIN32__)
                if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
@@ -387,7 +373,7 @@ public:
                        char name[128];
                        SDL_VideoDriverName(name, sizeof(name));
                        logInfo << "initialized SDL; using video driver `"
-                                        << name << "'" << std::endl;
+                                       << name << "'" << std::endl;
                }
 
                if (FE_Init() != 0)
@@ -397,31 +383,11 @@ public:
                        return; // fatal
                }
 
-               mAlDevice = alcOpenDevice(0);
-               mAlContext = alcCreateContext(mAlDevice, 0);
-               if (!mAlDevice || !mAlContext)
-               {
-                       const char* error = alcGetString(mAlDevice,alcGetError(mAlDevice));
-                       gError.init(Error::OPENAL_INIT, error);
-                       return;
-               }
-               else
-               {
-                       alcMakeContextCurrent(mAlContext);
-                       logInfo << "opened sound device `"
-                                        << alcGetString(mAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER)
-                                        << "'" << std::endl;
-               }
-
                gError.init(Error::NONE);
        }
 
        ~Backend_()
        {
-               alcMakeContextCurrent(0);
-               alcDestroyContext(mAlContext);
-               alcCloseDevice(mAlDevice);
-
                FE_Quit();
                SDL_Quit();
        }
@@ -443,25 +409,21 @@ public:
                }
        }
 
-       static bool check(Error& error)
+       static const Error& getError()
        {
-               error = gError;
-               return error.code() == Error::NONE;
+               return gError;
        }
 
 private:
 
-       ALCdevice*              mAlDevice;
-       ALCcontext*             mAlContext;
-
        static Error    gError;
        static int              gRetainCount;
        static BackendP gInstance;
 };
 
-Error Backend_::gError(Error::UNINITIALIZED);
-int Backend_::gRetainCount = 0;
-BackendP Backend_::gInstance;
+Error          Backend_::gError(Error::UNINITIALIZED);
+int                    Backend_::gRetainCount = 0;
+BackendP       Backend_::gInstance;
 
 
 Backend::Backend()
@@ -474,13 +436,16 @@ Backend::~Backend()
        Backend_::release();
 }
 
-bool Backend::check(Error& error)
+bool Backend::isInitialized()
 {
-       return Backend_::check(error);
+       return getError().code() == Error::NONE;
 }
 
+const Error& Backend::getError()
+{
+       return Backend_::getError();
+}
 
-} // namespace Mf
 
-/** vim: set ts=4 sw=4 tw=80: *************************************************/
+} // namespace Mf
 
This page took 0.026229 seconds and 4 git commands to generate.