Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fix for Core::getInstance didn't work on linux.

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