Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/core/Core.cc @ 1961

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

Moved all showsGraphics, hasServer, etc. functions from Settings class to Core class.
This was necessary to avoid circular dependencies.

  • Property svn:eol-style set to native
File size: 7.1 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Reto Grieder
26 *
27 */
28
29/**
30    @file Core.cc
31    @brief Implementation of the Core class.
32*/
33
34#include "Core.h"
35#include <cassert>
36#include "Language.h"
37#include "CoreIncludes.h"
38#include "ConfigValueIncludes.h"
39
40namespace orxonox
41{
42    bool Core::bShowsGraphics_s = false;
43    bool Core::bHasServer_s     = false;
44    bool Core::bIsClient_s      = false;
45    bool Core::bIsStandalone_s  = false;
46    bool Core::bIsMaster_s      = false;
47
48    /**
49        @brief Constructor: Registers the object and sets the config-values.
50        @param A reference to a global variable, used to avoid an infinite recursion in getSoftDebugLevel()
51    */
52    Core::Core()
53    {
54        RegisterRootObject(Core);
55        this->setConfigValues();
56        isCreatingCoreSettings() = false;
57    }
58
59    /**
60        @brief Sets the bool to true to avoid static functions accessing a deleted object.
61    */
62    Core::~Core()
63    {
64        isCreatingCoreSettings() = true;
65    }
66
67    /**
68        @brief Returns true if the Core instance is not yet ready and the static functions have to return a default value.
69    */
70    bool& Core::isCreatingCoreSettings()
71    {
72        static bool bCreatingCoreSettings = true;
73        return bCreatingCoreSettings;
74    }
75
76    /**
77        @brief Returns a unique instance of Core.
78        @return The instance
79    */
80    Core& Core::getInstance()
81    {
82        // If bCreatingSoftDebugLevelObject is true, we're just about to create an instance of the DebugLevel class
83        //if (Core::isCreatingCoreSettings())
84        //{
85        //    isCreatingCoreSettings() = false;
86        //    //instance.setConfigValues();
87        //}
88
89        static bool firstTime = true;
90        if (firstTime)
91            isCreatingCoreSettings() = true;
92
93        static Core instance;
94        return instance;
95    }
96
97    /**
98        @brief Function to collect the SetConfigValue-macro calls.
99    */
100    void Core::setConfigValues()
101    {
102        SetConfigValue(softDebugLevelConsole_, 3).description("The maximal level of debug output shown in the console").callback(this, &Core::debugLevelChanged);
103        SetConfigValue(softDebugLevelLogfile_, 3).description("The maximal level of debug output shown in the logfile").callback(this, &Core::debugLevelChanged);
104        SetConfigValue(softDebugLevelShell_, 1).description("The maximal level of debug output shown in the ingame shell").callback(this, &Core::debugLevelChanged);
105        SetConfigValue(language_, Language::getLanguage().defaultLanguage_).description("The language of the ingame text").callback(this, &Core::languageChanged);
106    }
107
108    /**
109        @brief Callback function if the debug level has changed.
110    */
111    void Core::debugLevelChanged()
112    {
113        // softDebugLevel_ is the maximum of the 3 variables
114        this->softDebugLevel_ = this->softDebugLevelConsole_;
115        if (this->softDebugLevelLogfile_ > this->softDebugLevel_)
116            this->softDebugLevel_ = this->softDebugLevelLogfile_;
117        if (this->softDebugLevelShell_ > this->softDebugLevel_)
118            this->softDebugLevel_ = this->softDebugLevelShell_;
119
120        OutputHandler::setSoftDebugLevel(OutputHandler::LD_All,     this->softDebugLevel_);
121        OutputHandler::setSoftDebugLevel(OutputHandler::LD_Console, this->softDebugLevelConsole_);
122        OutputHandler::setSoftDebugLevel(OutputHandler::LD_Logfile, this->softDebugLevelLogfile_);
123        OutputHandler::setSoftDebugLevel(OutputHandler::LD_Shell,   this->softDebugLevelShell_);
124    }
125
126    /**
127        @brief Callback function if the language has changed.
128    */
129    void Core::languageChanged()
130    {
131        // Read the translation file after the language was configured
132        Language::getLanguage().readTranslatedLanguageFile();
133    }
134
135    /**
136        @brief Returns the softDebugLevel for the given device (returns a default-value if the class ist right about to be created).
137        @param device The device
138        @return The softDebugLevel
139    */
140    int Core::getSoftDebugLevel(OutputHandler::OutputDevice device)
141    {
142        if (!Core::isCreatingCoreSettings())
143        {
144            switch (device)
145            {
146            case OutputHandler::LD_All:
147                return Core::getInstance().softDebugLevel_;
148            case OutputHandler::LD_Console:
149                return Core::getInstance().softDebugLevelConsole_;
150            case OutputHandler::LD_Logfile:
151                return Core::getInstance().softDebugLevelLogfile_;
152            case OutputHandler::LD_Shell:
153                return Core::getInstance().softDebugLevelShell_;
154            default:
155                assert(0);
156            }
157        }
158
159        // Return a constant value while we're creating the object
160        return 2;
161    }
162
163     /**
164        @brief Sets the softDebugLevel for the given device. Please use this only temporary and restore the value afterwards, as it overrides the configured value.
165        @param device The device
166        @param level The level
167    */
168     void Core::setSoftDebugLevel(OutputHandler::OutputDevice device, int level)
169     {
170        if (!Core::isCreatingCoreSettings())
171        {
172            if (device == OutputHandler::LD_All)
173                Core::getInstance().softDebugLevel_ = level;
174            else if (device == OutputHandler::LD_Console)
175                Core::getInstance().softDebugLevelConsole_ = level;
176            else if (device == OutputHandler::LD_Logfile)
177                Core::getInstance().softDebugLevelLogfile_ = level;
178            else if (device == OutputHandler::LD_Shell)
179                Core::getInstance().softDebugLevelShell_ = level;
180
181            OutputHandler::setSoftDebugLevel(device, level);
182        }
183     }
184
185    /**
186        @brief Returns the configured language.
187    */
188    const std::string& Core::getLanguage()
189    {
190        if (!Core::isCreatingCoreSettings())
191            return Core::getInstance().language_;
192
193        return Language::getLanguage().defaultLanguage_;
194    }
195
196    /**
197        @brief Sets the language in the config-file back to the default.
198    */
199    void Core::resetLanguage()
200    {
201        Core::getInstance().resetLanguageIntern();
202    }
203
204    /**
205        @brief Sets the language in the config-file back to the default.
206    */
207    void Core::resetLanguageIntern()
208    {
209        ResetConfigValue(language_);
210    }
211}
Note: See TracBrowser for help on using the repository browser.