Index: code/trunk/src/libraries/util/CMakeLists.txt
===================================================================
--- code/trunk/src/libraries/util/CMakeLists.txt	(revision 5781)
+++ code/trunk/src/libraries/util/CMakeLists.txt	(revision 5929)
@@ -19,17 +19,20 @@
 
 SET_SOURCE_FILES(UTIL_SRC_FILES
-  Clipboard.cc
-  CRC32.cc
   Exception.cc
-  ExprParser.cc
   Math.cc
   MultiType.cc
+  Scope.cc
+  StringUtils.cc
+COMPILATION_BEGIN StableCompilation.cc
+  Clipboard.cc
+  Clock.cc
+  CRC32.cc
+  ExprParser.cc
   OutputBuffer.cc
   OutputHandler.cc
-  Scope.cc
   SignalHandler.cc
   Sleep.cc
-  StringUtils.cc
   SubString.cc
+COMPILATION_END
 )
 
Index: code/trunk/src/libraries/util/Clock.cc
===================================================================
--- code/trunk/src/libraries/util/Clock.cc	(revision 5929)
+++ code/trunk/src/libraries/util/Clock.cc	(revision 5929)
@@ -0,0 +1,73 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Reto Grieder
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "Clock.h"
+#include <OgreTimer.h>
+
+namespace orxonox
+{
+    Clock::Clock()
+        : timer_(new Ogre::Timer())
+        , storedTime_(0)
+        , tickTime_(0)
+        , tickDt_(0)
+        , tickDtFloat_(0.0f)
+        , lastTimersTime_(0)
+    {
+    }
+
+    Clock::~Clock()
+    {
+        delete timer_;
+    }
+    
+    void Clock::capture()
+    {
+        unsigned long timersTime = timer_->getMicroseconds();
+        tickTime_ = storedTime_ + timersTime;
+        tickDt_ = timersTime - lastTimersTime_;
+        tickDtFloat_ = static_cast<float>(tickDt_) / 1000000.0f;
+
+        if (timersTime > 0xFFFFFFFF/4)
+        {
+            // Ogre timer will overflow at 2^32 microseconds if unsigned long is 32 bit
+            storedTime_ += timersTime;
+            lastTimersTime_ = 0;
+            timer_->reset();
+        }
+        else
+        {
+            lastTimersTime_ = timersTime;
+        }
+    }
+
+    unsigned long long Clock::getRealMicroseconds() const
+    {
+        return this->timer_->getMicroseconds() + this->storedTime_;
+    }
+}
Index: code/trunk/src/libraries/util/Clock.h
===================================================================
--- code/trunk/src/libraries/util/Clock.h	(revision 5929)
+++ code/trunk/src/libraries/util/Clock.h	(revision 5929)
@@ -0,0 +1,67 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Reto Grieder
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _Clock_H__
+#define _Clock_H__
+
+#include "UtilPrereqs.h"
+#include "OgreForwardRefs.h"
+
+namespace orxonox
+{
+    class _UtilExport Clock
+    {
+    public:
+        Clock();
+        ~Clock();
+
+        void capture();
+
+        unsigned long long getMicroseconds()   const { return tickTime_; }
+        unsigned long long getMilliseconds()   const { return tickTime_ / 1000; }
+        unsigned long      getSeconds()        const { return static_cast<long> (tickTime_ / 1000000); }
+        float              getSecondsPrecise() const { return static_cast<float>(tickTime_ / 1000000.0f); }
+
+        float              getDeltaTime()      const { return tickDtFloat_; }
+        long               getDeltaTimeMicroseconds() const { return tickDt_; }
+
+        unsigned long long getRealMicroseconds() const;
+
+    private:
+        Clock(const Clock& instance);
+
+        Ogre::Timer*       timer_;
+        unsigned long long storedTime_;
+        unsigned long long tickTime_;
+        long               tickDt_;
+        float              tickDtFloat_;
+        unsigned long      lastTimersTime_;
+    };
+}
+
+#endif /* _Clock_H__ */
Index: code/trunk/src/libraries/util/Scope.h
===================================================================
--- code/trunk/src/libraries/util/Scope.h	(revision 5781)
+++ code/trunk/src/libraries/util/Scope.h	(revision 5929)
@@ -31,26 +31,14 @@
 
 #include "UtilPrereqs.h"
+
 #include <cassert>
+#include <map>
 #include <set>
-#include <map>
+
 #include "Debug.h"
+#include "ScopeGuard.h"
 
 namespace orxonox
 {
-    namespace ScopeID
-    {
-        /**
-            @brief A list of available scopes for the Scope template.
-        */
-        enum Value
-        {
-            GSRoot,
-            GSGraphics,
-            GSLevel
-        };
-    }
-
-    class ScopeListener; // Forward declaration
-
     /**
         @brief The ScopeManager stores the variables of the scope templates in a statically linked context.
@@ -77,5 +65,5 @@
         protected:
             //! Constructor: Registers the instance.
-            ScopeListener(ScopeID::Value scope) : scope_(scope)
+            ScopeListener(ScopeID::Value scope) : scope_(scope), bActivated_(false)
                 { ScopeManager::listeners_s[this->scope_].insert(this); }
             //! Destructor: Unregisters the instance.
@@ -90,4 +78,5 @@
         private:
             ScopeID::Value scope_; //!< Store the scope to unregister on destruction
+            bool bActivated_;
     };
 
@@ -105,10 +94,23 @@
             Scope()
             {
-                ScopeManager::instanceCounts_s[scope]++;
-                assert(ScopeManager::instanceCounts_s[scope] > 0);
-                if (ScopeManager::instanceCounts_s[scope] == 1)
+                try
                 {
-                    for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); )
-                        (*(it++))->activated();
+                    ScopeManager::instanceCounts_s[scope]++;
+                    assert(ScopeManager::instanceCounts_s[scope] > 0);
+                    if (ScopeManager::instanceCounts_s[scope] == 1)
+                    {
+                        Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateListeners);
+                        for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); )
+                        {
+                            (*it)->activated();
+                            (*(it++))->bActivated_ = true;
+                        }
+                        deactivator.Dismiss();
+                    }
+                }
+                catch (...)
+                {
+                    ScopeManager::instanceCounts_s[scope]--;
+                    throw;
                 }
             }
@@ -125,7 +127,21 @@
 
                 if (ScopeManager::instanceCounts_s[scope] == 0)
+                    this->deactivateListeners();
+            }
+
+            void deactivateListeners()
+            {
+                for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); )
                 {
-                    for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); )
-                        (*(it++))->deactivated();
+                    if ((*it)->bActivated_)
+                    {
+                        try
+                            { (*it)->deactivated(); }
+                        catch (...)
+                            { COUT(0) << "ScopeListener::deactivated() failed! This MUST NOT happen, fix it!" << std::endl; }
+                        (*(it++))->bActivated_ = false;
+                    }
+                    else
+                        ++it;
                 }
             }
Index: code/trunk/src/libraries/util/ScopedSingleton.h
===================================================================
--- code/trunk/src/libraries/util/ScopedSingleton.h	(revision 5781)
+++ 	(revision )
@@ -1,102 +1,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Fabian 'x3n' Landau
- *   Co-authors:
- *      ...
- *
- */
-
-#ifndef __Util_ScopedSingleton_H__
-#define __Util_ScopedSingleton_H__
-
-#include "UtilPrereqs.h"
-#include <cassert>
-
-#include "Scope.h"
-
-namespace orxonox
-{
-    /**
-    @brief
-        Base for scoped singleton classes.
-        A Scoped singleton creates itself if the scope is active and getInstance() is called.
-        Destroys itself if the scope is deactivated.
-
-        Usage:
-        Inherit publicly from ScopedSingleton<MyClass, scope> and provide access to
-        MyClass::singletonPtr_s.
-        This can easily be done with a friend declaration.
-
-        See @ref UtilPrereqs.h for a list of scopes (ScopeID::Value).
-    */
-    template <class T, ScopeID::Value scope>
-    class ScopedSingleton : public ScopeListener
-    {
-        public:
-            //! Returns a reference to the singleton instance
-            static T& getInstance()
-            {
-                assert(Scope<scope>::isActive());
-
-                if (!T::singletonPtr_s && Scope<scope>::isActive())
-                    T::singletonPtr_s = new T();
-
-                return *T::singletonPtr_s;
-            }
-
-        protected:
-            //! Constructor sets the singleton instance pointer
-            ScopedSingleton() : ScopeListener(scope)
-            {
-                assert(T::singletonPtr_s == 0);
-                T::singletonPtr_s = static_cast<T*>(this);
-            }
-
-            //! Constructor resets the singleton instance pointer
-            ~ScopedSingleton()
-            {
-                assert(T::singletonPtr_s != 0);
-                T::singletonPtr_s = 0;
-            }
-
-        private:
-            //! Called if the Scope of this Singleton gets active (no instance should be active then)
-            void activated()
-            {
-                // The ScopedSingleton shouldn't be active bevor the scope is activated -> always assertion failed
-                assert(T::singletonPtr_s == 0 && false);
-            }
-
-            //! Called if the Scope of this Singleton gets deactivated (destroys the instance)
-            void deactivated()
-            {
-                if (T::singletonPtr_s)
-                {
-                    delete T::singletonPtr_s;
-                    T::singletonPtr_s = 0;
-                }
-            }
-    };
-}
-
-#endif /* __Util_ScopedSingleton_H__ */
Index: code/trunk/src/libraries/util/Singleton.h
===================================================================
--- code/trunk/src/libraries/util/Singleton.h	(revision 5781)
+++ code/trunk/src/libraries/util/Singleton.h	(revision 5929)
@@ -55,4 +55,9 @@
         }
 
+        //! Update method called by ClassSingletonManager (if used)
+        void updateSingleton(const Clock& time) { static_cast<T*>(T::singletonPtr_s)->update(time); }
+        //! Empty update method for the static polymorphism
+        void update(const Clock& time) { }
+
     protected:
         //! Constructor sets the singleton instance pointer
Index: code/trunk/src/libraries/util/UtilPrereqs.h
===================================================================
--- code/trunk/src/libraries/util/UtilPrereqs.h	(revision 5781)
+++ code/trunk/src/libraries/util/UtilPrereqs.h	(revision 5929)
@@ -28,6 +28,7 @@
 
 /**
-  @file
-  @brief Contains all the necessary forward declarations for all classes and structs.
+@file
+@brief
+    Shared library macros, enums, constants and forward declarations for the util library
 */
 
@@ -40,4 +41,5 @@
 // Shared library settings
 //-----------------------------------------------------------------------
+
 #if defined(ORXONOX_PLATFORM_WINDOWS) && !defined( UTIL_STATIC_BUILD )
 #  ifdef UTIL_SHARED_BUILD
@@ -56,4 +58,20 @@
 #endif
 
+//-----------------------------------------------------------------------
+// Enums
+//-----------------------------------------------------------------------
+
+namespace orxonox
+{
+    namespace ScopeID
+    {
+        //!A list of available scopes for the Scope template.
+        enum Value
+        {
+            Root,
+            Graphics
+        };
+    }
+}
 
 //-----------------------------------------------------------------------
@@ -63,4 +81,5 @@
 namespace orxonox
 {
+    class Clock;
     class Exception;
     class ExprParser;
@@ -71,5 +90,12 @@
     class OutputBufferListener;
     class OutputHandler;
+    template <ScopeID::Value>
+    class Scope;
+    template <class, ScopeID::Value>
+    class ScopedSingleton;
+    class ScopeListener;
     class SignalHandler;
+    template <class T>
+    class Singleton;
     class SubString;
 }
