Index: code/branches/ogre/src/orxonox/Orxonox.cc
===================================================================
--- code/branches/ogre/src/orxonox/Orxonox.cc	(revision 1241)
+++ code/branches/ogre/src/orxonox/Orxonox.cc	(revision 1243)
@@ -65,4 +65,5 @@
 #include "core/InputBuffer.h"
 #include "core/InputManager.h"
+#include "core/TclBind.h"
 
 // audio
@@ -166,6 +167,5 @@
     this->ogre_ = &GraphicsEngine::getSingleton();
     this->timer_ = 0;
-    this->dataPath_ = "";
-    this->auMan_ = 0;
+    //this->auMan_ = 0;
     this->orxonoxHUD_ = 0;
     this->inputHandler_ = 0;
@@ -186,6 +186,6 @@
     Loader::close();
     InputManager::destroy();
-    if (this->auMan_)
-      delete this->auMan_;
+    //if (this->auMan_)
+    //  delete this->auMan_;
     if (this->timer_)
       delete this->timer_;
@@ -196,14 +196,4 @@
     if (server_g)
       delete server_g;
-  }
-
-  /**
-    @brief Immediately deletes the orxonox object.
-    Never use if you can help it while rendering!
-  */
-  void Orxonox::abortImmediateForce()
-  {
-    COUT(1) << "*** Orxonox Error: Orxonox object was unexpectedly destroyed." << std::endl;
-    delete this;
   }
 
@@ -243,5 +233,5 @@
    * @param path path to config (in home dir or something)
    */
-  void Orxonox::init(int argc, char **argv, std::string path)
+  bool Orxonox::init(int argc, char **argv, std::string path)
   {
     //TODO: find config file (assuming executable directory)
@@ -249,57 +239,35 @@
     //TODO: give config file to Ogre
     std::string mode;
+    std::string dataPath;
 
     ArgReader ar(argc, argv);
     ar.checkArgument("mode", mode, false);
-    ar.checkArgument("data", this->dataPath_, false);
+    ar.checkArgument("data", dataPath, false);
     ar.checkArgument("ip", serverIp_, false);
-    if(ar.errorHandling()) abortImmediateForce();
-    if(mode == std::string("client"))
-    {
+    if(ar.errorHandling())
+      return false;
+
+    if (mode == "client")
       mode_ = CLIENT;
-      clientInit(path);
-    }
-    else if(mode== std::string("server")){
+    else if (mode == "server")
       mode_ = SERVER;
-      serverInit(path);
-    }
-    else{
+    else
       mode_ = STANDALONE;
-      standaloneInit(path);
-    }
-  }
-
-  void Orxonox::serverInit(std::string path)
-  {
-    COUT(2) << "initialising server" << std::endl;
-
-    ogre_->setConfigPath(path);
-    ogre_->setup();
-    //root_ = ogre_->getRoot();
-    if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */);
-
-    server_g = new network::Server();
-  }
-
-  void Orxonox::clientInit(std::string path)
-  {
-    COUT(2) << "initialising client" << std::endl;\
-
-    ogre_->setConfigPath(path);
-    ogre_->setup();
-    if(serverIp_.compare("")==0)
-      client_g = new network::Client();
-    else
-      client_g = new network::Client(serverIp_, NETWORK_PORT);
-    if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */);
-  }
-
-  void Orxonox::standaloneInit(std::string path)
-  {
-    COUT(2) << "initialising standalone mode" << std::endl;
-
-    ogre_->setConfigPath(path);
-    ogre_->setup();
-    if(!ogre_->load(this->dataPath_)) abortImmediateForce(/* unable to load */);
+
+    //if (mode_ == DEDICATED)
+      // TODO: decide what to do here
+    //else
+
+    // for playable server, client and standalone, the startup
+    // procedure until the GUI is the identical
+
+    TclBind::getInstance().setDataPath(dataPath);
+    ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
+    Factory::createClassHierarchy();
+
+    if (!ogre_->setup(path))       // creates ogre root and other essentials
+      return false;
+
+    return true;
   }
 
@@ -307,26 +275,67 @@
    * start modules
    */
-  void Orxonox::start()
-  {
-    switch(mode_){
+  bool Orxonox::start()
+  {
+    //if (mode == DEDICATED)
+    // do something else
+    //else
+
+    if (!ogre_->loadRenderer())    // creates the render window
+      return false;
+
+    // Calls the InputHandler which sets up the input devices.
+    // The render window width and height are used to set up the mouse movement.
+    if (!InputManager::initialise(ogre_->getWindowHandle(),
+          ogre_->getWindowWidth(), ogre_->getWindowHeight()))
+      return false;
+
+    // TODO: Spread this so that this call onyl initialises things needed for the GUI
+    ogre_->initialiseResources();
+
+    // TOOD: load the GUI here
+    // set InputManager to GUI mode
+    InputManager::setInputState(InputManager::IS_GUI);
+    // TODO: run GUI here
+
+    // The following lines depend very much on the GUI, so they're probably misplaced here..
+
+    InputManager::setInputState(InputManager::IS_NONE);
+
+    if (!loadPlayground())
+      return false;
+
+    switch (mode_)
+    {
+    case SERVER:
+      if (!serverLoad())
+        return false;
+      break;
     case CLIENT:
-      clientStart();
-      break;
-    case SERVER:
-      serverStart();
+      if (!clientLoad())
+        return false;
       break;
     default:
-      standaloneStart();
+      if (!standaloneLoad())
+        return false;
     }
-  }
-
-  void Orxonox::clientStart(){
-    ogre_->initialise();
-    ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
-    Factory::createClassHierarchy();
-
-
-    auMan_ = new audio::AudioManager();
-
+
+    InputManager::setInputState(InputManager::IS_NORMAL);
+
+    return startRenderLoop();
+  }
+
+  bool Orxonox::loadPlayground()
+  {
+    ogre_->createNewScene();
+
+	  // Init audio
+    //auMan_ = new audio::AudioManager();
+    //auMan_->ambientAdd("a1");
+    //auMan_->ambientAdd("a2");
+    //auMan_->ambientAdd("a3");
+    //auMan->ambientAdd("ambient1");
+    //auMan_->ambientStart();
+
+    // Load the HUD
     Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2");
     orxonoxHUD_ = new HUD();
@@ -335,91 +344,4 @@
     hudOverlay->show();
 
-    client_g->establishConnection();
-    client_g->tick(0);
-
-
-    //setupInputSystem();
-
-    startRenderLoop();
-  }
-
-  void Orxonox::serverStart(){
-    //TODO: start modules
-    ogre_->initialise();
-    //TODO: run engine
-    ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
-    Factory::createClassHierarchy();
-    createScene();
-    setupInputSystem();
-
-    server_g->open();
-
-    startRenderLoop();
-  }
-
-  void Orxonox::standaloneStart(){
-    //TODO: start modules
-    ogre_->initialise();
-    //TODO: run engine
-    ConfigFileManager::getSingleton()->setFile(CFT_Settings, "orxonox.ini");
-    Factory::createClassHierarchy();
-    createScene();
-    setupInputSystem();
-
-    startRenderLoop();
-  }
-
-  void Orxonox::createScene(void)
-  {
-	  // Init audio
-    auMan_ = new audio::AudioManager();
-
-    // load this file from config
-    Level* startlevel = new Level("levels/sample.oxw");
-    Loader::open(startlevel);
-
-    Ogre::Overlay* hudOverlay = Ogre::OverlayManager::getSingleton().getByName("Orxonox/HUD1.2");
-    orxonoxHUD_ = new HUD();
-    orxonoxHUD_->setEnergyValue(20);
-    orxonoxHUD_->setEnergyDistr(20,20,60);
-    hudOverlay->show();
-
-	/*
-    auMan_->ambientAdd("a1");
-    auMan_->ambientAdd("a2");
-    auMan_->ambientAdd("a3");
-    //auMan->ambientAdd("ambient1");
-    auMan_->ambientStart();
-  */
-  }
-
-  /**
-    @brief Calls the InputHandler which sets up the input devices.
-    The render window width and height are used to set up the mouse movement.
-  */
-  void Orxonox::setupInputSystem()
-  {
-    if (!InputManager::initialise(ogre_->getWindowHandle(),
-          ogre_->getWindowWidth(), ogre_->getWindowHeight()))
-      abortImmediateForce();
-    InputManager::setInputState(InputManager::IS_NORMAL);
-  }
-
-  /**
-    Main loop of the orxonox game.
-    This is a new solution, using the ogre engine instead of being used by it.
-    An alternative solution would be to simply use the timer of the Root object,
-    but that implies using Ogre in any case. There would be no way to test
-    our code without the help of the root object.
-    There's even a chance that we can dispose of the root object entirely
-    in server mode.
-    About the loop: The design is almost exactly like the one in ogre, so that
-    if any part of ogre registers a framelisteners, it will still behave
-    correctly. Furthermore the time smoothing feature from ogre has been
-    implemented too. If turned on (see orxonox constructor), it will calculate
-    the dt_n by means of the recent most dt_n-1, dt_n-2, etc.
-  */
-  void Orxonox::startRenderLoop()
-  {
     InputBuffer* ib = dynamic_cast<InputBuffer*>(InputManager::getKeyHandler("buffer"));
     /*
@@ -432,5 +354,4 @@
     ib->registerListener(console, &Testconsole::exit, (char)0x1B, true);
     */
-
     orxonoxConsole_ = new InGameConsole(ib);
     ib->registerListener(orxonoxConsole_, &InGameConsole::listen, true);
@@ -441,9 +362,70 @@
     ib->registerListener(orxonoxConsole_, &InGameConsole::exit, (char)0x1B, true);
 
+    return true;
+  }
+
+  bool Orxonox::serverLoad()
+  {
+    COUT(2) << "initialising server" << std::endl;
+
+    server_g = new network::Server();
+
+    if (!loadScene())
+      return false;
+
+    server_g->open();
+
+    return true;
+  }
+
+  bool Orxonox::clientLoad()
+  {
+    COUT(2) << "initialising client" << std::endl;\
+
+    if (serverIp_.compare("") == 0)
+      client_g = new network::Client();
+    else
+      client_g = new network::Client(serverIp_, NETWORK_PORT);
+
+    client_g->establishConnection();
+    client_g->tick(0);
+
+    return true;
+  }
+
+  bool Orxonox::standaloneLoad()
+  {
+    COUT(2) << "initialising standalone mode" << std::endl;
+
+    if (!loadScene())
+      return false;
+
+    return true;
+  }
+
+  bool Orxonox::loadScene()
+  {
+    Level* startlevel = new Level("levels/sample.oxw");
+    Loader::open(startlevel);
+
+    return true;
+  }
+
+
+  /**
+    Main loop of the orxonox game.
+    About the loop: The design is almost exactly like the one in ogre, so that
+    if any part of ogre registers a framelisteners, it will still behave
+    correctly. Furthermore the time smoothing feature from ogre has been
+    implemented too. If turned on (see orxonox constructor), it will calculate
+    the dt_n by means of the recent most dt_n-1, dt_n-2, etc.
+  */
+  bool Orxonox::startRenderLoop()
+  {
     // first check whether ogre root object has been created
     if (Ogre::Root::getSingletonPtr() == 0)
     {
       COUT(2) << "*** Orxonox Error: Could not start rendering. No Ogre root object found" << std::endl;
-      return;
+      return false;
     }
     Ogre::Root& ogreRoot = Ogre::Root::getSingleton();
@@ -510,4 +492,6 @@
       ogreRoot._fireFrameEnded(evt);
 	  }
+
+    return true;
   }
 
