Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/orxonox/Settings.h @ 1638

Last change on this file since 1638 was 1638, checked in by rgrieder, 16 years ago

merged input branch into gui test branch (was about time)
svn save (it's still a mess and CMLs haven't been updated)
I'll have to create a special project to create the tolua_bind files for tolua itself anyway..

  • Property svn:eol-style set to native
File size: 5.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file Core.h
31    @brief Declaration of the Settings class.
32
33    The static Settings class is only used to configure some variables
34    through the config-file.
35*/
36
37#ifndef _Settings_H__
38#define _Settings_H__
39
40#include "OrxonoxPrereqs.h"
41#include <string>
42#include "core/OrxonoxClass.h"
43#include "core/Debug.h"
44#include "util/MultiTypeMath.h"
45#include "util/Convert.h"
46
47namespace orxonox
48{
49    /**
50    @brief
51        Defines a bit field structure that holds the mode as enum plus
52        the attributes as single named bits.
53        Every different GameMode is stored as static const, but if you wish to
54        compare two modes, you will have to use the 'mode' member variable.
55    */
56    struct GameMode
57    {
58        enum Mode
59        {
60            None,
61            Unspecified,
62            Server,
63            Client,
64            Standalone,
65            Dedicated,
66        };
67
68        Mode mode;
69        bool showsGraphics;
70        bool isMaster;
71        bool hasServer;
72        std::string name;
73
74        static const GameMode GM_None;
75        static const GameMode GM_Unspecified;
76        static const GameMode GM_Server;
77        static const GameMode GM_Client;
78        static const GameMode GM_Standalone;
79        static const GameMode GM_Dedicated;
80    };
81
82
83    class _OrxonoxExport Settings : public OrxonoxClass
84    {
85    public:
86        struct CommandLineArgument
87        {
88            std::string name_;
89            MultiTypeMath value_;
90            bool bHasDefaultValue_;
91        };
92
93        void setConfigValues();
94
95        static const std::string& getDataPath();
96        static void tsetDataPath(const std::string& path);
97
98        static const GameMode& getGameMode();
99        static const GameMode& getGameMode(const std::string& name);
100        static void setGameMode(const GameMode& mode);
101        static void setGameMode(const std::string& mode);
102        static bool addGameMode(const GameMode* mode);
103
104        static const CommandLineArgument* getCommandLineArgument(const std::string& name);
105        template <class T>
106        static bool addCommandLineArgument(const std::string &name, const std::string& valueStr, const T& defaultValue);
107
108    private:
109        Settings();
110        Settings(const Settings& instance);
111        ~Settings() { }
112        static Settings& getInstance();
113
114        void _tsetDataPath(const std::string& path);
115
116        std::string dataPath_;                                        //!< Path to the game data
117        GameMode gameMode_;                                           //!< Current game mode
118        std::map<std::string, const GameMode*> gameModes_;            //!< Holds all game modes for easy string access
119        //! holds all command line arguments (even if not given!)
120        std::map<std::string, CommandLineArgument> commandArguments_;
121    };
122
123    /**
124    @brief
125        Returns the relative path to the game data.
126    */
127    inline const std::string& Settings::getDataPath()
128    {
129        return getInstance().dataPath_;
130    }
131
132    inline void Settings::tsetDataPath(const std::string& path)
133    {
134        getInstance()._tsetDataPath(path);
135    }
136
137    inline const GameMode& Settings::getGameMode()
138    {
139        return getInstance().gameMode_;
140    }
141
142    inline const GameMode& Settings::getGameMode(const std::string& name)
143    {
144        if (getInstance().gameModes_.find(name) != getInstance().gameModes_.end())
145            return *getInstance().gameModes_[name];
146        else
147        {
148            COUT(2) << "Warning: GameMode '" << name << "' doesn't exist." << std::endl;
149            return GameMode::GM_None;
150        }
151    }
152
153    inline void Settings::setGameMode(const GameMode& mode)
154    {
155        getInstance().gameMode_ = mode;
156    }
157
158    /**
159    @brief
160        Adds one argument of the command line to the map of command line arguments.
161    @param name
162        Name of the command line option.
163    @param valueStr
164        The value of the command line option as string
165    @param defaultValue
166        Default value for the option (marked when used).
167    @return
168        Dummy return value to enable code execution before main().
169    */
170    template <class T>
171    bool Settings::addCommandLineArgument(const std::string &name, const std::string& valueStr, const T& defaultValue)
172    {
173        T value;
174        bool useDefault = false;
175        if (valueStr == "")
176        {
177            // note: ArgReader only returns "" for not found arguments, " " otherwise for empty ones.
178            value = defaultValue;
179            useDefault = true;
180        }
181        else if (!convertValue(&value, valueStr))
182        {
183            COUT(1) << "Command Line: Couldn't read option '" << name << "'." << std::endl;
184            return false;
185        }
186        CommandLineArgument arg = { name, MultiTypeMath(value), useDefault };
187        getInstance().commandArguments_[name] = arg;
188        return true;
189    }
190}
191
192#endif /* _Settings_H__ */
Note: See TracBrowser for help on using the repository browser.