Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/Core.cc @ 6417

Last change on this file since 6417 was 6417, checked in by rgrieder, 14 years ago

Merged presentation2 branch back to trunk.
Major new features:

  • Actual GUI with settings, etc.
  • Improved space ship steering (human interaction)
  • Rocket fire and more particle effects
  • Advanced sound framework
  • Property svn:eol-style set to native
File size: 12.2 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
[2896]24 *      Reto Grieder
[1505]25 *   Co-authors:
[2896]26 *      ...
[1505]27 *
28 */
29
30/**
[3196]31@file
32@brief
33    Implementation of the Core singleton with its global variables (avoids boost include)
[1505]34*/
35
[1524]36#include "Core.h"
[2710]37
[1756]38#include <cassert>
[5929]39#include <vector>
[2710]40
41#ifdef ORXONOX_PLATFORM_WINDOWS
[2896]42#  ifndef WIN32_LEAN_AND_MEAN
43#    define WIN32_LEAN_AND_MEAN
44#  endif
[2710]45#  include <windows.h>
[3214]46#  undef min
47#  undef max
[2710]48#endif
49
[5929]50#include "util/Clock.h"
[2896]51#include "util/Debug.h"
[2710]52#include "util/Exception.h"
[6417]53#include "util/Scope.h"
[2896]54#include "util/SignalHandler.h"
[5929]55#include "PathConfig.h"
[5781]56#include "CommandExecutor.h"
[6021]57#include "CommandLineParser.h"
[2896]58#include "ConfigFileManager.h"
59#include "ConfigValueIncludes.h"
60#include "CoreIncludes.h"
[5693]61#include "DynLibManager.h"
[5781]62#include "GameMode.h"
63#include "GraphicsManager.h"
64#include "GUIManager.h"
[2896]65#include "Identifier.h"
[1505]66#include "Language.h"
[6105]67#include "IOConsole.h"
[5695]68#include "LuaState.h"
[5929]69#include "ScopedSingletonManager.h"
[5781]70#include "TclBind.h"
71#include "TclThreadManager.h"
72#include "input/InputManager.h"
[1505]73
74namespace orxonox
75{
[3196]76    //! Static pointer to the singleton
[3370]77    Core* Core::singletonPtr_s  = 0;
[2662]78
[3280]79    SetCommandLineArgument(settingsFile, "orxonox.ini").information("THE configuration file");
80#ifdef ORXONOX_PLATFORM_WINDOWS
[6417]81    SetCommandLineArgument(limitToCPU, 1).information("Limits the program to one CPU/core (1, 2, 3, etc.). Default is the first core (faster than off)");
[3280]82#endif
[2710]83
[3323]84    Core::Core(const std::string& cmdLine)
[3370]85        // Cleanup guard for identifier destruction (incl. XMLPort, configValues, consoleCommands)
86        : identifierDestroyer_(Identifier::destroyAllIdentifiers)
[5781]87        // Cleanup guard for external console commands that don't belong to an Identifier
88        , consoleCommandDestroyer_(CommandExecutor::destroyExternalCommands)
89        , bGraphicsLoaded_(false)
[3280]90    {
[5693]91        // Set the hard coded fixed paths
[5929]92        this->pathConfig_.reset(new PathConfig());
[3280]93
[5693]94        // Create a new dynamic library manager
95        this->dynLibManager_.reset(new DynLibManager());
[2896]96
[5693]97        // Load modules
[5929]98        const std::vector<std::string>& modulePaths = this->pathConfig_->getModulePaths();
99        for (std::vector<std::string>::const_iterator it = modulePaths.begin(); it != modulePaths.end(); ++it)
[5693]100        {
[5929]101            try
[5693]102            {
[5929]103                this->dynLibManager_->load(*it);
[5693]104            }
[5929]105            catch (...)
106            {
107                COUT(1) << "Couldn't load module \"" << *it << "\": " << Exception::handleMessage() << std::endl;
108            }
[5693]109        }
110
111        // Parse command line arguments AFTER the modules have been loaded (static code!)
[6021]112        CommandLineParser::parseCommandLine(cmdLine);
[5693]113
114        // Set configurable paths like log, config and media
[5929]115        this->pathConfig_->setConfigurablePaths();
[5693]116
[6105]117        // create a signal handler (only active for Linux)
[2896]118        // This call is placed as soon as possible, but after the directories are set
[3370]119        this->signalHandler_.reset(new SignalHandler());
[5929]120        this->signalHandler_->doCatch(PathConfig::getExecutablePathString(), PathConfig::getLogPathString() + "orxonox_crash.log");
[2896]121
[6105]122        // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% (Windows) was used
123        OutputHandler::getInstance().setLogPath(PathConfig::getLogPathString());
[2710]124
[3280]125        // Parse additional options file now that we know its path
[6021]126        CommandLineParser::parseFile();
[3280]127
128#ifdef ORXONOX_PLATFORM_WINDOWS
129        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
130        // do this after ogre has initialised. Somehow Ogre changes the settings again (not through
131        // the timer though).
[6021]132        int limitToCPU = CommandLineParser::getValue("limitToCPU");
[3280]133        if (limitToCPU > 0)
134            setThreadAffinity(static_cast<unsigned int>(limitToCPU));
135#endif
136
[2896]137        // Manage ini files and set the default settings file (usually orxonox.ini)
[3370]138        this->configFileManager_.reset(new ConfigFileManager());
[2896]139        this->configFileManager_->setFilename(ConfigFileType::Settings,
[6021]140            CommandLineParser::getValue("settingsFile").getString());
[2896]141
[3280]142        // Required as well for the config values
[3370]143        this->languageInstance_.reset(new Language());
[2896]144
[6417]145        // Do this soon after the ConfigFileManager has been created to open up the
146        // possibility to configure everything below here
147        ClassIdentifier<Core>::getIdentifier("Core")->initialiseObject(this, "Core", true);
148        this->setConfigValues();
149
[6105]150        // create persistent io console
151        this->ioConsole_.reset(new IOConsole());
152
[5695]153        // creates the class hierarchy for all classes with factories
[5929]154        Identifier::createClassHierarchy();
[5695]155
[5781]156        // Load OGRE excluding the renderer and the render window
157        this->graphicsManager_.reset(new GraphicsManager(false));
158
159        // initialise Tcl
[5929]160        this->tclBind_.reset(new TclBind(PathConfig::getDataPathString()));
[5781]161        this->tclThreadManager_.reset(new TclThreadManager(tclBind_->getTclInterpreter()));
162
[5929]163        // Create singletons that always exist (in other libraries)
164        this->rootScope_.reset(new Scope<ScopeID::Root>());
[1505]165    }
166
167    /**
[3370]168    @brief
[5695]169        All destruction code is handled by scoped_ptrs and ScopeGuards.
[1505]170    */
[1524]171    Core::~Core()
[1505]172    {
[6417]173        // Remove us from the object lists again to avoid problems when destroying them
174        this->unregisterObject();
[3370]175    }
[2896]176
[6417]177    //! Function to collect the SetConfigValue-macro calls.
178    void Core::setConfigValues()
179    {
180#ifdef ORXONOX_RELEASE
181        const unsigned int defaultLevelLogFile = 3;
182#else
183        const unsigned int defaultLevelLogFile = 4;
184#endif
185        setConfigValueGeneric(this, &this->softDebugLevelLogFile_, ConfigFileType::Settings, "OutputHandler", "softDebugLevelLogFile", defaultLevelLogFile)
186            .description("The maximum level of debug output shown in the log file");
187        OutputHandler::getInstance().setSoftDebugLevel(OutputHandler::logFileOutputListenerName_s, this->softDebugLevelLogFile_);
188
189        SetConfigValue(language_, Language::getInstance().defaultLanguage_)
190            .description("The language of the in game text")
191            .callback(this, &Core::languageChanged);
192        SetConfigValue(bInitRandomNumberGenerator_, true)
193            .description("If true, all random actions are different each time you start the game")
194            .callback(this, &Core::initRandomNumberGenerator);
195    }
196
197    //! Callback function if the language has changed.
198    void Core::languageChanged()
199    {
200        // Read the translation file after the language was configured
201        Language::getInstance().readTranslatedLanguageFile();
202    }
203
204    void Core::initRandomNumberGenerator()
205    {
206        static bool bInitialized = false;
207        if (!bInitialized && this->bInitRandomNumberGenerator_)
208        {
209            srand(static_cast<unsigned int>(time(0)));
210            rand();
211            bInitialized = true;
212        }
213    }
214
[5781]215    void Core::loadGraphics()
216    {
217        // Any exception should trigger this, even in upgradeToGraphics (see its remarks)
218        Loki::ScopeGuard unloader = Loki::MakeObjGuard(*this, &Core::unloadGraphics);
219
220        // Upgrade OGRE to receive a render window
221        graphicsManager_->upgradeToGraphics();
222
223        // Calls the InputManager which sets up the input devices.
224        inputManager_.reset(new InputManager());
225
[5929]226        // Load the CEGUI interface
[5781]227        guiManager_.reset(new GUIManager(graphicsManager_->getRenderWindow(),
228            inputManager_->getMousePosition(), graphicsManager_->isFullScreen()));
229
[5929]230        bGraphicsLoaded_ = true;
231        GameMode::bShowsGraphics_s = true;
232
233        // Load some sort of a debug overlay (only denoted by its name, "debug.oxo")
234        graphicsManager_->loadDebugOverlay();
235
236        // Create singletons associated with graphics (in other libraries)
237        graphicsScope_.reset(new Scope<ScopeID::Graphics>());
238
[5781]239        unloader.Dismiss();
240    }
241
242    void Core::unloadGraphics()
243    {
[5929]244        this->graphicsScope_.reset();
245        this->guiManager_.reset();
246        this->inputManager_.reset();
[5781]247        this->graphicsManager_.reset();
248
249        // Load Ogre::Root again, but without the render system
250        try
251            { this->graphicsManager_.reset(new GraphicsManager(false)); }
252        catch (...)
253        {
254            COUT(0) << "An exception occurred during 'unloadGraphics':" << Exception::handleMessage() << std::endl
255                    << "Another exception might be being handled which may lead to undefined behaviour!" << std::endl
256                    << "Terminating the program." << std::endl;
257            abort();
258        }
259
260        bGraphicsLoaded_ = false;
[5929]261        GameMode::bShowsGraphics_s = false;
[5781]262    }
263
[6417]264    //! Sets the language in the config-file back to the default.
265    void Core::resetLanguage()
[1505]266    {
[6417]267        ResetConfigValue(language_);
[1505]268    }
269
270    /**
[2896]271    @note
272        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
273            (Object-oriented Graphics Rendering Engine)
274        For the latest info, see http://www.ogre3d.org/
275
276        Copyright (c) 2000-2008 Torus Knot Software Ltd
277
278        OGRE is licensed under the LGPL. For more info, see OGRE license.
[2710]279    */
[2896]280    void Core::setThreadAffinity(int limitToCPU)
[2710]281    {
[3280]282#ifdef ORXONOX_PLATFORM_WINDOWS
283
[2896]284        if (limitToCPU <= 0)
285            return;
[2710]286
[2896]287        unsigned int coreNr = limitToCPU - 1;
288        // Get the current process core mask
289        DWORD procMask;
290        DWORD sysMask;
291#  if _MSC_VER >= 1400 && defined (_M_X64)
292        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
293#  else
294        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
295#  endif
[2710]296
[2896]297        // If procMask is 0, consider there is only one core available
298        // (using 0 as procMask will cause an infinite loop below)
299        if (procMask == 0)
300            procMask = 1;
301
302        // if the core specified with coreNr is not available, take the lowest one
303        if (!(procMask & (1 << coreNr)))
304            coreNr = 0;
305
306        // Find the lowest core that this process uses and coreNr suggests
307        DWORD threadMask = 1;
308        while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr)))
309            threadMask <<= 1;
310
311        // Set affinity to the first core
312        SetThreadAffinityMask(GetCurrentThread(), threadMask);
313#endif
[2710]314    }
315
[5695]316    void Core::preUpdate(const Clock& time)
[2896]317    {
[6417]318        // Update singletons before general ticking
319        ScopedSingletonManager::preUpdate<ScopeID::Root>(time);
[5781]320        if (this->bGraphicsLoaded_)
321        {
[6417]322            // Process input events
323            this->inputManager_->preUpdate(time);
324            // Update GUI
325            this->guiManager_->preUpdate(time);
326            // Update singletons before general ticking
327            ScopedSingletonManager::preUpdate<ScopeID::Graphics>(time);
[5781]328        }
[6417]329        // Process console events and status line
330        this->ioConsole_->preUpdate(time);
331        // Process thread commands
332        this->tclThreadManager_->preUpdate(time);
[2896]333    }
[3370]334
[5695]335    void Core::postUpdate(const Clock& time)
[3370]336    {
[6417]337        // Update singletons just before rendering
338        ScopedSingletonManager::postUpdate<ScopeID::Root>(time);
[5781]339        if (this->bGraphicsLoaded_)
340        {
[6417]341            // Update singletons just before rendering
342            ScopedSingletonManager::postUpdate<ScopeID::Graphics>(time);
[5781]343            // Render (doesn't throw)
[6417]344            this->graphicsManager_->postUpdate(time);
[5781]345        }
[3370]346    }
[1505]347}
Note: See TracBrowser for help on using the repository browser.