Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/Core.cc @ 1759

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

Fixed a bug in Core.cc when creating it via getInstance directly for the first time.

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