Index: code/branches/gui/src/orxonox/gamestates/GSGUI.cc
===================================================================
--- code/branches/gui/src/orxonox/gamestates/GSGUI.cc	(revision 1674)
+++ code/branches/gui/src/orxonox/gamestates/GSGUI.cc	(revision 1686)
@@ -30,4 +30,5 @@
 #include "GSGUI.h"
 
+#include <OgreViewport.h>
 #include "GraphicsEngine.h"
 #include "core/input/InputManager.h"
Index: code/branches/gui/src/orxonox/gamestates/GSGraphics.cc
===================================================================
--- code/branches/gui/src/orxonox/gamestates/GSGraphics.cc	(revision 1674)
+++ code/branches/gui/src/orxonox/gamestates/GSGraphics.cc	(revision 1686)
@@ -30,14 +30,26 @@
 #include "GSGraphics.h"
 
+#include <fstream>
+#include <OgreConfigFile.h>
 #include <OgreFrameListener.h>
 #include <OgreRoot.h>
+#include <OgreException.h>
+#include <OgreRenderWindow.h>
+#include <OgreTextureManager.h>
+#include <OgreViewport.h>
 #include <OgreWindowEventUtilities.h>
-#include <OgreRenderWindow.h>
-
+
+#include "core/Debug.h"
 #include "core/ConsoleCommand.h"
 #include "core/ConfigValueIncludes.h"
+#include "core/CoreIncludes.h"
 #include "core/input/InputManager.h"
+#include "core/Exception.h"
 #include "overlays/console/InGameConsole.h"
 #include "gui/GUIManager.h"
+#include "tools/WindowEventListener.h"
+#include "Settings.h"
+
+// for compatibility
 #include "GraphicsEngine.h"
 
@@ -47,5 +59,4 @@
         : GameState("graphics")
         , ogreRoot_(0)
-        , graphicsEngine_(0)
         , inputManager_(0)
         , console_(0)
@@ -57,4 +68,5 @@
         , tickTime_(0)
     {
+        RegisterRootObject(GSGraphics);
     }
 
@@ -65,4 +77,5 @@
     void GSGraphics::setConfigValues()
     {
+        SetConfigValue(resourceFile_,    "resources.cfg").description("Location of the resources file in the data path.");
         SetConfigValue(statisticsRefreshCycle_, 200000).description("Sets the time in microseconds interval at which average fps, etc. get updated.");
     }
@@ -73,16 +86,24 @@
 
         this->ogreRoot_ = Ogre::Root::getSingletonPtr();
-        this->graphicsEngine_ = GraphicsEngine::getInstancePtr();
-
-        graphicsEngine_->loadRenderer();    // creates the render window
-
+
+        this->declareResources();
+        this->loadRenderer();    // creates the render window
         // TODO: Spread this so that this call only initialises things needed for the Console and GUI
-        graphicsEngine_->initialiseResources();
+        this->initialiseResources();
+
+
+        // HACK: temporary:
+        GraphicsEngine& graphicsEngine = GraphicsEngine::getInstance();
+        graphicsEngine.renderWindow_ = this->renderWindow_;
+        graphicsEngine.root_ = this->ogreRoot_;
+        graphicsEngine.viewport_ = this->viewport_;
+
 
         // Calls the InputManager which sets up the input devices.
         // The render window width and height are used to set up the mouse movement.
         inputManager_ = new InputManager();
-        inputManager_->initialise(graphicsEngine_->getWindowHandle(),
-            graphicsEngine_->getWindowWidth(), graphicsEngine_->getWindowHeight(), true);
+        size_t windowHnd = 0;
+        this->renderWindow_->getCustomAttribute("WINDOW", &windowHnd);
+        inputManager_->initialise(windowHnd, renderWindow_->getWidth(), renderWindow_->getHeight(), true);
 
         // Load the InGameConsole
@@ -92,5 +113,5 @@
         // load the CEGUI interface
         guiManager_ = new GUIManager();
-        guiManager_->initialise();
+        guiManager_->initialise(this->renderWindow_);
 
         // reset frame counter
@@ -99,4 +120,9 @@
         statisticsStartTime_ = 0;
         statisticsStartCount_ = 0;
+
+        // add console commands
+        FunctorMember<GSGraphics>* functor1 = createFunctor(&GSGraphics::printScreen);
+        functor1->setObject(this);
+        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "printScreen"));
     }
 
@@ -109,6 +135,6 @@
         delete this->inputManager_;
 
-        this->ogreRoot_->detachRenderTarget(GraphicsEngine::getInstance().getRenderWindow());
-        delete GraphicsEngine::getInstance().getRenderWindow();
+        this->ogreRoot_->detachRenderTarget(this->renderWindow_);
+        delete this->renderWindow_;
         //this->ogreRoot_->shutdown
         // TODO: destroy render window
@@ -166,5 +192,5 @@
         // make sure the window stays active even when not focused
         // (probably only necessary on windows)
-        GraphicsEngine::getInstance().setWindowActivity(true);
+        this->renderWindow_->setActive(true);
 
         // render
@@ -176,3 +202,156 @@
         ++frameCount_;
     }
+
+    void GSGraphics::declareResources()
+    {
+        CCOUT(4) << "Declaring Resources" << std::endl;
+        //TODO: Specify layout of data file and maybe use xml-loader
+        //TODO: Work with ressource groups (should be generated by a special loader)
+
+        if (resourceFile_ == "")
+        {
+            COUT(2) << "Warning: Ogre resource file set to \"\". Defaulting to resources.cfg" << std::endl;
+            ModifyConfigValue(resourceFile_, tset, "resources.cfg");
+        }
+
+        // Load resource paths from data file using configfile ressource type
+        Ogre::ConfigFile cf;
+        try
+        {
+            cf.load(Settings::getDataPath() + resourceFile_);
+        }
+        catch (...)
+        {
+            //COUT(1) << ex.getFullDescription() << std::endl;
+            COUT(0) << "Have you forgotten to set the data path in orxnox.ini?" << std::endl;
+            throw;
+        }
+
+        // Go through all sections & settings in the file
+        Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
+
+        std::string secName, typeName, archName;
+        while (seci.hasMoreElements())
+        {
+            try
+            {
+                secName = seci.peekNextKey();
+                Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
+                Ogre::ConfigFile::SettingsMultiMap::iterator i;
+                for (i = settings->begin(); i != settings->end(); ++i)
+                {
+                    typeName = i->first; // for instance "FileSystem" or "Zip"
+                    archName = i->second; // name (and location) of archive
+
+                    Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
+                        std::string(Settings::getDataPath() + archName), typeName, secName);
+                }
+            }
+            catch (Ogre::Exception& ex)
+            {
+                COUT(1) << ex.getFullDescription() << std::endl;
+            }
+        }
+    }
+
+    void GSGraphics::loadRenderer()
+    {
+        CCOUT(4) << "Configuring Renderer" << std::endl;
+
+        if (!ogreRoot_->restoreConfig())
+            if (!ogreRoot_->showConfigDialog())
+                ThrowException(InitialisationFailed, "Could not show Ogre configuration dialogue.");
+
+        CCOUT(4) << "Creating render window" << std::endl;
+
+        this->renderWindow_ = ogreRoot_->initialise(true, "OrxonoxV2");
+
+        Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, this);
+
+        //Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
+
+        // create a full screen default viewport
+        this->viewport_ = this->renderWindow_->addViewport(0, 0);
+    }
+
+    void GSGraphics::initialiseResources()
+    {
+        CCOUT(4) << "Initialising resources" << std::endl;
+        //TODO: Do NOT load all the groups, why are we doing that? And do we really do that? initialise != load...
+        try
+        {
+            Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
+            /*Ogre::StringVector str = Ogre::ResourceGroupManager::getSingleton().getResourceGroups();
+            for (unsigned int i = 0; i < str.size(); i++)
+            {
+            Ogre::ResourceGroupManager::getSingleton().loadResourceGroup(str[i]);
+            }*/
+        }
+        catch (...)
+        {
+            CCOUT(2) << "Error: There was a serious error when initialising the resources." << std::endl;
+            throw;
+        }
+    }
+
+
+    /**
+    @brief
+        Window has moved.
+    @param rw
+        The render window it occured in
+    */
+    void GSGraphics::windowMoved(Ogre::RenderWindow *rw)
+    {
+        for (Iterator<orxonox::WindowEventListener> it = ObjectList<orxonox::WindowEventListener>::start(); it; ++it)
+            it->windowMoved();
+    }
+
+    /**
+    @brief
+        Window has resized.
+    @param rw
+        The render window it occured in
+    @note
+        GraphicsEngine has a render window stored itself. This is the same
+        as rw. But we have to be careful when using multiple render windows!
+    */
+    void GSGraphics::windowResized(Ogre::RenderWindow *rw)
+    {
+        for (Iterator<orxonox::WindowEventListener> it = ObjectList<orxonox::WindowEventListener>::start(); it; ++it)
+            it->windowResized(this->renderWindow_->getWidth(), this->renderWindow_->getHeight());
+    }
+
+    /**
+    @brief
+        Window focus has changed.
+    @param rw
+        The render window it occured in
+    */
+    void GSGraphics::windowFocusChanged(Ogre::RenderWindow *rw)
+    {
+        for (Iterator<orxonox::WindowEventListener> it = ObjectList<orxonox::WindowEventListener>::start(); it; ++it)
+            it->windowFocusChanged();
+    }
+
+    /**
+    @brief
+        Window was closed.
+    @param rw
+        The render window it occured in
+    */
+    void GSGraphics::windowClosed(Ogre::RenderWindow *rw)
+    {
+        // using CommandExecutor in order to avoid depending on Orxonox.h.
+        //CommandExecutor::execute("exit", false);
+        this->requestState("root");
+    }
+
+    void GSGraphics::printScreen()
+    {
+        if (this->renderWindow_)
+        {
+            this->renderWindow_->writeContentsToTimestampedFile("shot_", ".jpg");
+        }
+    }
 }
Index: code/branches/gui/src/orxonox/gamestates/GSGraphics.h
===================================================================
--- code/branches/gui/src/orxonox/gamestates/GSGraphics.h	(revision 1674)
+++ code/branches/gui/src/orxonox/gamestates/GSGraphics.h	(revision 1686)
@@ -32,4 +32,5 @@
 #include "OrxonoxPrereqs.h"
 #include <OgrePrerequisites.h>
+#include <OgreWindowEventUtilities.h>
 #include "core/GameState.h"
 #include "core/OrxonoxClass.h"
@@ -37,11 +38,10 @@
 namespace orxonox
 {
-    class _OrxonoxExport GSGraphics : public GameState, public OrxonoxClass
+    class _OrxonoxExport GSGraphics : public GameState, public OrxonoxClass, public Ogre::WindowEventListener
     {
+        friend class ClassIdentifier<GSGraphics>;
     public:
         GSGraphics();
         ~GSGraphics();
-
-        void setConfigValues();
 
     private:
@@ -50,6 +50,23 @@
         void ticked(const Clock& time);
 
+        void setConfigValues();
+
+        void declareResources();
+        void loadRenderer();
+        void initialiseResources();
+
+        void printScreen();
+
+        // window events from Ogre::WindowEventListener
+        void windowMoved       (Ogre::RenderWindow* rw);
+        void windowResized     (Ogre::RenderWindow* rw);
+        void windowFocusChanged(Ogre::RenderWindow* rw);
+        void windowClosed      (Ogre::RenderWindow* rw);
+
         Ogre::Root*           ogreRoot_;
-        GraphicsEngine*       graphicsEngine_;   //!< pointer to GraphicsEngine instance
+        Ogre::RenderWindow*   renderWindow_;          //!< the current render window
+        Ogre::Viewport*       viewport_;              //!< default full size viewport
+
+        // managed singletons
         InputManager*         inputManager_;
         InGameConsole*        console_;
@@ -62,4 +79,8 @@
         unsigned long         statisticsStartCount_;
         unsigned int          tickTime_;
+
+        // config values
+        std::string           resourceFile_;          //!< resources file name
+        unsigned int          detailLevelParticle_;   //!< Detail level of particle effects (0: off, 1: low, 2: normal, 3: high)
     };
 }
Index: code/branches/gui/src/orxonox/gamestates/GSLevel.cc
===================================================================
--- code/branches/gui/src/orxonox/gamestates/GSLevel.cc	(revision 1674)
+++ code/branches/gui/src/orxonox/gamestates/GSLevel.cc	(revision 1686)
@@ -30,4 +30,6 @@
 #include "GSLevel.h"
 
+#include <OgreSceneManager.h>
+#include <OgreRoot.h>
 #include "core/input/InputManager.h"
 #include "core/input/SimpleInputState.h"
@@ -45,4 +47,5 @@
         : GameState(name)
         , timeFactor_(1.0f)
+        , sceneManager_(0)
         , keyBinder_(0)
         , inputState_(0)
@@ -65,5 +68,8 @@
 
         // create Ogre SceneManager for the level
-        GraphicsEngine::getInstance().createNewScene();
+        this->sceneManager_ = GraphicsEngine::getInstance().getOgreRoot()->
+            createSceneManager(Ogre::ST_GENERIC, "LevelSceneManager");
+        COUT(4) << "Created SceneManager: " << sceneManager_->getName() << std::endl;
+        GraphicsEngine::getInstance().setLevelSceneManager(this->sceneManager_);
 
         // Start the Radar
@@ -92,5 +98,5 @@
         delete this->radar_;
 
-        // TODO: delete SceneManager
+        GraphicsEngine::getInstance().getOgreRoot()->destroySceneManager(this->sceneManager_);
 
         inputState_->setHandler(0);
Index: code/branches/gui/src/orxonox/gamestates/GSLevel.h
===================================================================
--- code/branches/gui/src/orxonox/gamestates/GSLevel.h	(revision 1674)
+++ code/branches/gui/src/orxonox/gamestates/GSLevel.h	(revision 1686)
@@ -31,4 +31,5 @@
 
 #include "OrxonoxPrereqs.h"
+#include <OgrePrerequisites.h>
 #include "core/GameState.h"
 
@@ -58,4 +59,5 @@
         float timeFactor_;       //!< A factor to change the gamespeed
 
+        Ogre::SceneManager*   sceneManager_;
         KeyBinder*            keyBinder_;        //!< tool that loads and manages the input bindings
         SimpleInputState*     inputState_;
Index: code/branches/gui/src/orxonox/gamestates/GSRoot.cc
===================================================================
--- code/branches/gui/src/orxonox/gamestates/GSRoot.cc	(revision 1674)
+++ code/branches/gui/src/orxonox/gamestates/GSRoot.cc	(revision 1686)
@@ -30,8 +30,12 @@
 #include "GSRoot.h"
 
+#include <OgreLogManager.h>
+#include <OgreRoot.h>
+
 //#include "util/SubString.h"
 #include "core/Factory.h"
 #include "core/ConfigFileManager.h"
 #include "core/ConfigValueIncludes.h"
+#include "core/CoreIncludes.h"
 #include "core/ConsoleCommand.h"
 #include "core/CommandLine.h"
@@ -65,10 +69,24 @@
         : RootGameState("root")
         , settings_(0)
+        , ogreRoot_(0)
+        , ogreLogger_(0)
         , graphicsEngine_(0)
     {
+        RegisterRootObject(GSRoot);
     }
 
     GSRoot::~GSRoot()
     {
+    }
+
+    void GSRoot::setConfigValues()
+    {
+        SetConfigValue(ogreConfigFile_,  "ogre.cfg").description("Location of the Ogre config file");
+        SetConfigValue(ogrePluginsFile_, "plugins.cfg").description("Location of the Ogre plugins file");
+        SetConfigValue(ogreLogFile_,     "ogre.log").description("Logfile for messages from Ogre. \
+                                                                 Use \"\" to suppress log file creation.");
+        SetConfigValue(ogreLogLevelTrivial_ , 5).description("Corresponding orxonox debug level for ogre Trivial");
+        SetConfigValue(ogreLogLevelNormal_  , 4).description("Corresponding orxonox debug level for ogre Normal");
+        SetConfigValue(ogreLogLevelCritical_, 2).description("Corresponding orxonox debug level for ogre Critical");
     }
 
@@ -108,4 +126,7 @@
 #endif
 
+        // do this after the previous call..
+        setConfigValues();
+
         // creates the class hierarchy for all classes with factories
         Factory::createClassHierarchy();
@@ -128,7 +149,8 @@
         TclThreadManager::getInstance();
 
+        setupOgre();
+
         // initialise graphics engine. Doesn't load the render window yet!
         graphicsEngine_ = new GraphicsEngine();
-        graphicsEngine_->setup();       // creates ogre root and other essentials
 
         // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
@@ -151,4 +173,14 @@
     {
         delete graphicsEngine_;
+
+        delete this->ogreRoot_;
+
+#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
+        // delete the ogre log and the logManager (since we have created it).
+        this->ogreLogger_->getDefaultLog()->removeListener(this);
+        this->ogreLogger_->destroyLog(Ogre::LogManager::getSingleton().getDefaultLog());
+        delete this->ogreLogger_;
+#endif
+
         delete settings_;
 
@@ -199,3 +231,112 @@
 #endif
     }
+
+    /**
+    @brief
+        Creates the Ogre Root object and sets up the ogre log.
+    */
+    void GSRoot::setupOgre()
+    {
+        COUT(3) << "Setting up Ogre..." << std::endl;
+
+        // TODO: LogManager doesn't work on oli platform. The why is yet unknown.
+#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
+        // create a new logManager
+        ogreLogger_ = new Ogre::LogManager();
+        COUT(4) << "Ogre LogManager created" << std::endl;
+
+        // create our own log that we can listen to
+        Ogre::Log *myLog;
+        if (this->ogreLogFile_ == "")
+            myLog = ogreLogger_->createLog("ogre.log", true, false, true);
+        else
+            myLog = ogreLogger_->createLog(this->ogreLogFile_, true, false, false);
+        COUT(4) << "Ogre Log created" << std::endl;
+
+        myLog->setLogDetail(Ogre::LL_BOREME);
+        myLog->addListener(this);
+#endif
+
+        // Root will detect that we've already created a Log
+        COUT(4) << "Creating Ogre Root..." << std::endl;
+
+        if (ogrePluginsFile_ == "")
+        {
+            COUT(2) << "Warning: Ogre plugins file set to \"\". Defaulting to plugins.cfg" << std::endl;
+            ModifyConfigValue(ogrePluginsFile_, tset, "plugins.cfg");
+        }
+        if (ogreConfigFile_ == "")
+        {
+            COUT(2) << "Warning: Ogre config file set to \"\". Defaulting to config.cfg" << std::endl;
+            ModifyConfigValue(ogreConfigFile_, tset, "config.cfg");
+        }
+        if (ogreLogFile_ == "")
+        {
+            COUT(2) << "Warning: Ogre log file set to \"\". Defaulting to ogre.log" << std::endl;
+            ModifyConfigValue(ogreLogFile_, tset, "ogre.log");
+        }
+
+        // check for config file existence because Ogre displays (caught) exceptions if not
+        std::ifstream probe;
+        probe.open(ogreConfigFile_.c_str());
+        if (!probe)
+        {
+            // create a zero sized file
+            std::ofstream creator;
+            creator.open(ogreConfigFile_.c_str());
+            creator.close();
+        }
+        else
+            probe.close();
+
+        ogreRoot_ = new Ogre::Root(ogrePluginsFile_, ogreConfigFile_, ogreLogFile_);
+
+#if 0 // Ogre 1.4.3 doesn't yet support setDebugOutputEnabled(.)
+#if ORXONOX_PLATFORM != ORXONOX_PLATFORM_WIN32
+        // tame the ogre ouput so we don't get all the mess in the console
+        Ogre::Log* defaultLog = Ogre::LogManager::getSingleton().getDefaultLog();
+        defaultLog->setDebugOutputEnabled(false);
+        defaultLog->setLogDetail(Ogre::LL_BOREME);
+        defaultLog->addListener(this);
+#endif
+#endif
+
+        COUT(3) << "Ogre set up done." << std::endl;
+    }
+
+    /**
+    @brief
+        Method called by the LogListener interface from Ogre.
+        We use it to capture Ogre log messages and handle it ourselves.
+    @param message
+        The message to be logged
+    @param lml
+        The message level the log is using
+    @param maskDebug
+        If we are printing to the console or not
+    @param logName
+        The name of this log (so you can have several listeners
+        for different logs, and identify them)
+    */
+    void GSRoot::messageLogged(const std::string& message,
+        Ogre::LogMessageLevel lml, bool maskDebug, const std::string& logName)
+    {
+        int orxonoxLevel;
+        switch (lml)
+        {
+        case Ogre::LML_TRIVIAL:
+            orxonoxLevel = this->ogreLogLevelTrivial_;
+            break;
+        case Ogre::LML_NORMAL:
+            orxonoxLevel = this->ogreLogLevelNormal_;
+            break;
+        case Ogre::LML_CRITICAL:
+            orxonoxLevel = this->ogreLogLevelCritical_;
+            break;
+        default:
+            orxonoxLevel = 0;
+        }
+        OutputHandler::getOutStream().setOutputLevel(orxonoxLevel)
+            << "Ogre: " << message << std::endl;
+    }
 }
Index: code/branches/gui/src/orxonox/gamestates/GSRoot.h
===================================================================
--- code/branches/gui/src/orxonox/gamestates/GSRoot.h	(revision 1674)
+++ code/branches/gui/src/orxonox/gamestates/GSRoot.h	(revision 1686)
@@ -31,10 +31,13 @@
 
 #include "OrxonoxPrereqs.h"
+#include <OgreLog.h>
 #include "core/RootGameState.h"
+#include "core/OrxonoxClass.h"
 
 namespace orxonox
 {
-    class _OrxonoxExport GSRoot : public RootGameState
+    class _OrxonoxExport GSRoot : public RootGameState, public Ogre::LogListener, public OrxonoxClass
     {
+        friend class ClassIdentifier<GSRoot>;
     public:
         GSRoot();
@@ -51,8 +54,21 @@
         void ticked(const Clock& time);
 
+        void setConfigValues();
+        void messageLogged(const std::string& message, Ogre::LogMessageLevel lml,
+            bool maskDebug, const std::string& logName);
         void setThreadAffinity();
+        void setupOgre();
 
         Settings*             settings_;
+        Ogre::Root*           ogreRoot_;                  //!< Ogre's root
+        Ogre::LogManager*     ogreLogger_;
         GraphicsEngine*       graphicsEngine_;   //!< Interface to Ogre
+
+        std::string           ogreConfigFile_;        //!< ogre config file name
+        std::string           ogrePluginsFile_;       //!< ogre plugins file name
+        std::string           ogreLogFile_;           //!< log file name for Ogre log messages
+        int                   ogreLogLevelTrivial_;   //!< Corresponding Orxonx debug level for LL_TRIVIAL
+        int                   ogreLogLevelNormal_;    //!< Corresponding Orxonx debug level for LL_NORMAL
+        int                   ogreLogLevelCritical_;  //!< Corresponding Orxonx debug level for LL_CRITICAL
     };
 }
