[Orxonox-commit 110] r2805 - in branches/gui/src: core orxonox orxonox/gamestates

rgrieder at orxonox.net rgrieder at orxonox.net
Thu Mar 19 16:59:31 CET 2009


Author: rgrieder
Date: 2009-03-19 15:59:30 +0000 (Thu, 19 Mar 2009)
New Revision: 2805

Added:
   branches/gui/src/orxonox/Game.cc
   branches/gui/src/orxonox/Game.h
Removed:
   branches/gui/src/orxonox/Main.cc
Modified:
   branches/gui/src/core/Clock.h
   branches/gui/src/core/GameState.h
   branches/gui/src/core/RootGameState.cc
   branches/gui/src/core/RootGameState.h
   branches/gui/src/orxonox/CMakeLists.txt
   branches/gui/src/orxonox/GraphicsManager.h
   branches/gui/src/orxonox/gamestates/GSGraphics.cc
   branches/gui/src/orxonox/gamestates/GSRoot.cc
   branches/gui/src/orxonox/gamestates/GSRoot.h
Log:
Added new class: Game
It represents basic operation related to the running game like start and stop or managing the new GameStates.
And since only three lines were left in Main.cc I thought I could move that to the very beginning of 'Game'.

Modified: branches/gui/src/core/Clock.h
===================================================================
--- branches/gui/src/core/Clock.h	2009-03-19 15:55:08 UTC (rev 2804)
+++ branches/gui/src/core/Clock.h	2009-03-19 15:59:30 UTC (rev 2805)
@@ -44,12 +44,12 @@
 {
     class _CoreExport Clock
     {
-        friend class RootGameState;
-
     public:
         Clock();
         ~Clock();
 
+        void capture();
+
         unsigned long long getMicroseconds()   const { return tickTime_; }
         unsigned long long getMilliseconds()   const { return tickTime_ / 1000; }
         int                getSeconds()        const { return tickTime_ / 1000000; }
@@ -62,7 +62,6 @@
 
     private:
         Clock(const Clock& instance);
-        void capture();
 
         Ogre::Timer*       timer_;
         unsigned long long storedTime_;

Modified: branches/gui/src/core/GameState.h
===================================================================
--- branches/gui/src/core/GameState.h	2009-03-19 15:55:08 UTC (rev 2804)
+++ branches/gui/src/core/GameState.h	2009-03-19 15:59:30 UTC (rev 2805)
@@ -64,6 +64,8 @@
         friend class RootGameState;
         template <class ParentType>
         friend class GameState;
+        // Hack
+        friend class Game;
 
     public:
         /**

Modified: branches/gui/src/core/RootGameState.cc
===================================================================
--- branches/gui/src/core/RootGameState.cc	2009-03-19 15:55:08 UTC (rev 2804)
+++ branches/gui/src/core/RootGameState.cc	2009-03-19 15:59:30 UTC (rev 2805)
@@ -31,12 +31,9 @@
 #include "util/Debug.h"
 #include "util/Exception.h"
 #include "Clock.h"
-#include "CommandLine.h"
 
 namespace orxonox
 {
-    SetCommandLineArgument(state, "gui").shortcut("s");
-
     RootGameState::RootGameState(const std::string& name)
         : GameState<GameStateBase>(name)
         , stateRequest_("")
@@ -118,61 +115,4 @@
     {
         this->stateRequest_ = name;
     }
-
-    /**
-    @brief
-        Main loop of the orxonox game.
-        Starts the game. The little 'while' denotes the main loop.
-        Whenever the root state is selected, the game ends.
-    @param name
-        State to start with (usually main menu or specified by command line)
-    @note
-        We use the Ogre::Timer to measure time since it uses the most precise
-        method an a platform (however the windows timer lacks time when under
-        heavy kernel load!).
-    */
-    void RootGameState::start()
-    {
-        // Don't catch errors when having a debugger in msvc
-#if !defined(ORXONOX_COMPILER_MSVC) || defined(NDEBUG)
-        try
-        {
-#endif
-            // start global orxonox time
-            Clock clock;
-
-            this->activate();
-
-            // get initial state from command line
-            gotoState(CommandLine::getValue("state"));
-
-            while (this->activeChild_)
-            {
-                clock.capture();
-
-                this->tick(clock);
-
-                if (this->stateRequest_ != "")
-                    gotoState(stateRequest_);
-            }
-
-            this->deactivate();
-#if !defined(ORXONOX_COMPILER_MSVC) || defined(NDEBUG)
-        }
-        // Note: These are all unhandled exceptions that should not have made its way here!
-        // almost complete game catch block to display the messages appropriately.
-        catch (std::exception& ex)
-        {
-            COUT(0) << ex.what() << std::endl;
-            COUT(0) << "Program aborted." << std::endl;
-            abort();
-        }
-        // anything that doesn't inherit from std::exception
-        catch (...)
-        {
-            COUT(0) << "An unidentifiable exception has occured. Program aborted." << std::endl;
-            abort();
-        }
-#endif
-    }
 }

Modified: branches/gui/src/core/RootGameState.h
===================================================================
--- branches/gui/src/core/RootGameState.h	2009-03-19 15:55:08 UTC (rev 2804)
+++ branches/gui/src/core/RootGameState.h	2009-03-19 15:59:30 UTC (rev 2805)
@@ -36,12 +36,14 @@
 {
     class _CoreExport RootGameState : public GameState<GameStateBase>
     {
+        // Hack!
+        friend class Game;
+
     public:
         RootGameState(const std::string& name);
         ~RootGameState();
 
         void requestState(const std::string& name);
-        void start();
 
     private:
         void makeTransition(GameStateBase* source, GameStateBase* destination);

Modified: branches/gui/src/orxonox/CMakeLists.txt
===================================================================
--- branches/gui/src/orxonox/CMakeLists.txt	2009-03-19 15:55:08 UTC (rev 2804)
+++ branches/gui/src/orxonox/CMakeLists.txt	2009-03-19 15:59:30 UTC (rev 2805)
@@ -19,9 +19,9 @@
 
 SET_SOURCE_FILES(ORXONOX_SRC_FILES
   CameraManager.cc
+  Game.cc
   GraphicsManager.cc
   LevelManager.cc
-  Main.cc
   PawnManager.cc
   PlayerManager.cc
 )

Added: branches/gui/src/orxonox/Game.cc
===================================================================
--- branches/gui/src/orxonox/Game.cc	                        (rev 0)
+++ branches/gui/src/orxonox/Game.cc	2009-03-19 15:59:30 UTC (rev 2805)
@@ -0,0 +1,171 @@
+/*
+ *   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:
+ *      ...
+ *
+ */
+
+/**
+ at file
+ at brief
+    Implementation of the Game class.
+*/
+
+#include "OrxonoxStableHeaders.h"
+#include "Game.h"
+
+#include <exception>
+#include <cassert>
+
+#include "util/Debug.h"
+#include "util/Exception.h"
+#include "core/CommandLine.h"
+#include "core/ConsoleCommand.h"
+#include "core/Core.h"
+#include "core/Identifier.h"
+
+#include "gamestates/GSRoot.h"
+#include "gamestates/GSGraphics.h"
+#include "gamestates/GSStandalone.h"
+#include "gamestates/GSServer.h"
+#include "gamestates/GSClient.h"
+#include "gamestates/GSDedicated.h"
+#include "gamestates/GSGUI.h"
+#include "gamestates/GSIOConsole.h"
+
+/*
+ at brief
+    Main method. Game starts here (except for static initialisations).
+*/
+int main(int argc, char** argv)
+{
+    orxonox::Game orxonox(argc, argv);
+    orxonox.run();
+    return 0;
+}
+
+namespace orxonox
+{
+    void stop_game()
+    {
+        Game::getInstance().stop();
+    }
+
+    SetCommandLineArgument(state, "gui").shortcut("s");
+    SetConsoleCommandShortcutExternAlias(stop_game, "exit");
+
+    Game* Game::singletonRef_s = 0;
+
+    /**
+    @brief
+        Non-initialising constructor.
+    */
+    Game::Game(int argc, char** argv)
+    {
+        assert(singletonRef_s == 0);
+        singletonRef_s = this;
+
+        this->abort_ = false;
+
+        this->core_ = new orxonox::Core(argc, argv);
+        if (!this->core_->isLoaded())
+        {
+            COUT(0) << "Core was not fully loaded, probably an exception occurred during consruction. Aborting" << std::endl;
+            abort();
+        }
+    }
+
+    /**
+    @brief
+    */
+    Game::~Game()
+    {
+        // Destroy pretty much everyhting left
+        delete this->core_;
+
+        // Clean up class hierarchy stuff (identifiers, xmlport, configvalue, consolecommand)
+        // Needs to be done after 'delete core' because of ~OrxonoxClass
+        orxonox::Identifier::destroyAllIdentifiers();
+
+        assert(singletonRef_s);
+        singletonRef_s = 0;
+    }
+
+    /**
+    @brief
+        Main loop of the orxonox game.
+    @note
+        We use the Ogre::Timer to measure time since it uses the most precise
+        method an any platform (however the windows timer lacks time when under
+        heavy kernel load!).
+    */
+    void Game::run()
+    {
+        // create the gamestates
+        GSRoot root;
+        GSGraphics graphics;
+        GSStandalone standalone;
+        GSServer server;
+        GSClient client;
+        GSDedicated dedicated;
+        GSGUI gui;
+        GSIOConsole ioConsole;
+
+        // make the hierarchy
+        root.addChild(&graphics);
+        graphics.addChild(&standalone);
+        graphics.addChild(&server);
+        graphics.addChild(&client);
+        graphics.addChild(&gui);
+        root.addChild(&ioConsole);
+        root.addChild(&dedicated);
+
+
+        // start global orxonox time
+        Clock clock;
+
+        root.activate();
+
+        // get initial state from command line
+        root.gotoState(CommandLine::getValue("state"));
+
+        while (!this->abort_)
+        {
+            clock.capture();
+
+            root.tick(clock);
+
+            if (root.stateRequest_ != "")
+                root.gotoState(root.stateRequest_);
+        }
+
+        root.gotoState("root");
+        root.deactivate();
+    }
+
+    void Game::stop()
+    {
+        this->abort_ = true;
+    }
+}


Property changes on: branches/gui/src/orxonox/Game.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: branches/gui/src/orxonox/Game.h
===================================================================
--- branches/gui/src/orxonox/Game.h	                        (rev 0)
+++ branches/gui/src/orxonox/Game.h	2009-03-19 15:59:30 UTC (rev 2805)
@@ -0,0 +1,70 @@
+/*
+ *   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:
+ *      ...
+ *
+ */
+
+/**
+ at file
+ at brief
+    Declaration of Game Singleton.
+ */
+
+#ifndef _Game_H__
+#define _Game_H__
+
+#include "OrxonoxPrereqs.h"
+#include <cassert>
+#include "core/CorePrereqs.h"
+
+namespace orxonox
+{
+    /**
+    @brief
+        Main class responsible for running the game.
+    */
+    class _OrxonoxExport Game
+    {
+    public:
+        Game(int argc, char** argv);
+        ~Game();
+
+        void run();
+        void stop();
+
+        static Game& getInstance() { assert(singletonRef_s); return *singletonRef_s; }
+
+    private:
+        Game(Game&); // don't mess with singletons
+
+        Core* core_;
+
+        bool abort_;
+
+        static Game* singletonRef_s;        //!< Pointer to the Singleton
+    };
+}
+
+#endif /* _Game_H__ */


Property changes on: branches/gui/src/orxonox/Game.h
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: branches/gui/src/orxonox/GraphicsManager.h
===================================================================
--- branches/gui/src/orxonox/GraphicsManager.h	2009-03-19 15:55:08 UTC (rev 2804)
+++ branches/gui/src/orxonox/GraphicsManager.h	2009-03-19 15:59:30 UTC (rev 2805)
@@ -33,8 +33,8 @@
     Declaration of GraphicsManager Singleton.
  */
 
-#ifndef _GraphicsEngine_H__
-#define _GraphicsEngine_H__
+#ifndef _GraphicsManager_H__
+#define _GraphicsManager_H__
 
 #include "OrxonoxPrereqs.h"
 
@@ -130,4 +130,4 @@
     };
 }
 
-#endif /* _GraphicsEngine_H__ */
+#endif /* _GraphicsManager_H__ */

Deleted: branches/gui/src/orxonox/Main.cc
===================================================================
--- branches/gui/src/orxonox/Main.cc	2009-03-19 15:55:08 UTC (rev 2804)
+++ branches/gui/src/orxonox/Main.cc	2009-03-19 15:59:30 UTC (rev 2805)
@@ -1,126 +0,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:
- *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
- *      Reto Grieder
- *   Co-authors:
- *      ...
- *
- */
-
- /**
- @file
- @brief Entry point of the program.
-  */
-
-#include "OrxonoxStableHeaders.h"
-#include "OrxonoxConfig.h"
-
-#include <exception>
-#include <cassert>
-
-#include "util/Debug.h"
-#include "core/Core.h"
-#include "core/Identifier.h"
-
-#include "gamestates/GSRoot.h"
-#include "gamestates/GSGraphics.h"
-#include "gamestates/GSStandalone.h"
-#include "gamestates/GSServer.h"
-#include "gamestates/GSClient.h"
-#include "gamestates/GSDedicated.h"
-#include "gamestates/GSGUI.h"
-#include "gamestates/GSIOConsole.h"
-
-#ifdef ORXONOX_PLATFORM_APPLE
-#include <CoreFoundation/CoreFoundation.h>
-
-// This function will locate the path to our application on OS X,
-// unlike windows you can not rely on the curent working directory
-// for locating your configuration files and resources.
-             std::string macBundlePath()
-{
-    char path[1024];
-    CFBundleRef mainBundle = CFBundleGetMainBundle();
-    assert(mainBundle);
-
-    CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
-    assert(mainBundleURL);
-
-    CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
-    assert(cfStringRef);
-
-    CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
-
-    CFRelease(mainBundleURL);
-    CFRelease(cfStringRef);
-
-    return std::string(path);
-}
-#endif
-
-
-
-int main(int argc, char** argv)
-{
-    orxonox::Core* core = new orxonox::Core(argc, argv);
-    if (!core->isLoaded())
-    {
-        COUT(0) << "Core was not fully loaded, probably an exception occurred during consruction. Aborting" << std::endl;
-        abort();
-    }
-
-    // put GameStates in its own scope so we can destroy the identifiers at the end of main().
-    {
-        using namespace orxonox;
-        // create the gamestates
-        GSRoot root;
-        GSGraphics graphics;
-        GSStandalone standalone;
-        GSServer server;
-        GSClient client;
-        GSDedicated dedicated;
-        GSGUI gui;
-        GSIOConsole ioConsole;
-
-        // make the hierarchy
-        root.addChild(&graphics);
-        graphics.addChild(&standalone);
-        graphics.addChild(&server);
-        graphics.addChild(&client);
-        graphics.addChild(&gui);
-        root.addChild(&ioConsole);
-        root.addChild(&dedicated);
-
-        // Here happens the game
-        root.start();
-    }
-
-    // Destroy pretty much everyhting left
-    delete core;
-
-    // Clean up class hierarchy stuff (identifiers, xmlport, configvalue, consolecommand)
-    // Needs to be done after 'delete core' because of ~OrxonoxClass
-    orxonox::Identifier::destroyAllIdentifiers();
-
-    return 0;
-}

Modified: branches/gui/src/orxonox/gamestates/GSGraphics.cc
===================================================================
--- branches/gui/src/orxonox/gamestates/GSGraphics.cc	2009-03-19 15:55:08 UTC (rev 2804)
+++ branches/gui/src/orxonox/gamestates/GSGraphics.cc	2009-03-19 15:59:30 UTC (rev 2805)
@@ -74,7 +74,7 @@
     {
         Core::setShowsGraphics(true);
 
-        // initialise graphics engine. Doesn't load the render window yet!
+        // initialise graphics manager. Doesn't load the render window yet!
         this->graphicsManager_ = new GraphicsManager();
         this->graphicsManager_->initialise();
 

Modified: branches/gui/src/orxonox/gamestates/GSRoot.cc
===================================================================
--- branches/gui/src/orxonox/gamestates/GSRoot.cc	2009-03-19 15:55:08 UTC (rev 2804)
+++ branches/gui/src/orxonox/gamestates/GSRoot.cc	2009-03-19 15:59:30 UTC (rev 2805)
@@ -79,14 +79,6 @@
 
         {
             // add console commands
-            FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::exitGame);
-            functor->setObject(this);
-            this->ccExit_ = createConsoleCommand(functor, "exit");
-            CommandExecutor::addConsoleCommandShortcut(this->ccExit_);
-        }
-
-        {
-            // add console commands
             FunctorMember01<GameStateBase, const std::string&>* functor = createFunctor(&GameStateBase::requestState);
             functor->setObject(this);
             this->ccSelectGameState_ = createConsoleCommand(functor, "selectGameState");
@@ -113,7 +105,6 @@
     void GSRoot::leave()
     {
         // destroy console commands
-        delete this->ccExit_;
         delete this->ccSelectGameState_;
 
         if (this->ccSetTimeFactor_)

Modified: branches/gui/src/orxonox/gamestates/GSRoot.h
===================================================================
--- branches/gui/src/orxonox/gamestates/GSRoot.h	2009-03-19 15:55:08 UTC (rev 2804)
+++ branches/gui/src/orxonox/gamestates/GSRoot.h	2009-03-19 15:59:30 UTC (rev 2805)
@@ -52,9 +52,6 @@
         GSRoot();
         ~GSRoot();
 
-        void exitGame()
-        { requestState("root"); }
-
         // this has to be public because proteced triggers a bug in msvc
         // when taking the function address.
         void setTimeFactor(float factor);
@@ -92,7 +89,6 @@
         unsigned int          statisticsAvgLength_;
 
         // console commands
-        ConsoleCommand*       ccExit_;
         ConsoleCommand*       ccSelectGameState_;
         ConsoleCommand*       ccSetTimeFactor_;
         ConsoleCommand*       ccPause_;




More information about the Orxonox-commit mailing list