Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/forks/sandbox_qt/src/libraries/core/Core.cc

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

Fixed various problems revealed on tardis

  • Property svn:eol-style set to native
File size: 6.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 *      Reto Grieder
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30/**
31@file
32@brief
33    Implementation of the Core singleton
34*/
35
36#include "Core.h"
37
38#include <cassert>
39#include <cstdlib>
40#include <ctime>
41#include <fstream>
42#include <vector>
43
44#ifdef ORXONOX_PLATFORM_WINDOWS
45#  ifndef WIN32_LEAN_AND_MEAN
46#    define WIN32_LEAN_AND_MEAN
47#  endif
48#  include <windows.h>
49#  undef min
50#  undef max
51#endif
52
53#include "util/Debug.h"
54#include "util/Exception.h"
55#include "PathConfig.h"
56#include "CommandLineParser.h"
57
58namespace orxonox
59{
60    //! Static pointer to the singleton
61    Core* Core::singletonPtr_s  = 0;
62
63    SetCommandLineArgument(settingsFile, "orxonox.ini").information("THE configuration file");
64
65#ifdef ORXONOX_PLATFORM_WINDOWS
66    SetCommandLineArgument(limitToCPU, 1).information("Limits the program to one CPU/core (1, 2, 3, etc.). Default is the first core (faster than off)");
67#endif
68
69    Core::Core(const std::string& cmdLine)
70    {
71        // Set the hard coded fixed paths
72        this->pathConfig_.reset(new PathConfig());
73
74        // Parse command line arguments
75        CommandLineParser::parseCommandLine(cmdLine);
76
77        // Set configurable paths like log, config and media
78        this->pathConfig_->setConfigurablePaths();
79
80        // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% (Windows) was used
81        OutputHandler::getInstance().setLogPath(PathConfig::getLogPathString());
82
83        // Parse additional options file now that we know its path
84        CommandLineParser::parseFile();
85
86#ifdef ORXONOX_PLATFORM_WINDOWS
87        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
88        int limitToCPU = CommandLineParser::getValue("limitToCPU").toInt();
89        if (limitToCPU > 0)
90            setThreadAffinity(static_cast<unsigned int>(limitToCPU));
91#endif
92
93        // Generate documentation instead of normal run?
94        std::string docFilename = CommandLineParser::getValue("generateDoc").toString().toStdString();
95        if (!docFilename.empty())
96        {
97            std::ofstream docFile(docFilename.c_str());
98            if (docFile.is_open())
99            {
100                CommandLineParser::generateDoc(docFile);
101                docFile.close();
102            }
103            else
104                COUT(0) << "Error: Could not open file for documentation writing" << endl;
105        }
106    }
107
108    /**
109    @brief
110        All destruction code is handled by std::auto_ptr
111    */
112    Core::~Core()
113    {
114    }
115
116    //! Function to collect the SetConfigValue-macro calls.
117    void Core::setConfigValues()
118    {
119#ifdef ORXONOX_RELEASE
120        const unsigned int defaultLevelLogFile = 3;
121#else
122        const unsigned int defaultLevelLogFile = 4;
123#endif
124        /*
125        SetConfigValueExternal(softDebugLevelLogFile_, "OutputHandler", "softDebugLevelLogFile", defaultLevelLogFile)
126            .description("The maximum level of debug output shown in the log file");
127        OutputHandler::getInstance().setSoftDebugLevel(OutputHandler::logFileOutputListenerName_s, this->softDebugLevelLogFile_);
128
129        SetConfigValue(language_, Language::getInstance().defaultLanguage_)
130            .description("The language of the in game text")
131            .callback(this, &Core::languageChanged);
132        SetConfigValue(bInitRandomNumberGenerator_, true)
133            .description("If true, all random actions are different each time you start the game")
134            .callback(this, &Core::initRandomNumberGenerator);
135        SetConfigValue(bStartIOConsole_, true)
136            .description("Set to false if you don't want to use the IOConsole (for Lua debugging for instance)");
137        */
138    }
139
140    void Core::initRandomNumberGenerator()
141    {
142        static bool bInitialized = false;
143        if (!bInitialized && this->bInitRandomNumberGenerator_)
144        {
145            srand(static_cast<unsigned int>(time(0)));
146            rand();
147            bInitialized = true;
148        }
149    }
150
151    /**
152    @note
153        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
154            (Object-oriented Graphics Rendering Engine)
155        For the latest info, see http://www.ogre3d.org/
156
157        Copyright (c) 2000-2008 Torus Knot Software Ltd
158
159        OGRE is licensed under the LGPL. For more info, see OGRE license.
160    */
161    void Core::setThreadAffinity(int limitToCPU)
162    {
163#ifdef ORXONOX_PLATFORM_WINDOWS
164
165        if (limitToCPU <= 0)
166            return;
167
168        unsigned int coreNr = limitToCPU - 1;
169        // Get the current process core mask
170        DWORD procMask;
171        DWORD sysMask;
172#  if _MSC_VER >= 1400 && defined (_M_X64)
173        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
174#  else
175        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
176#  endif
177
178        // If procMask is 0, consider there is only one core available
179        // (using 0 as procMask will cause an infinite loop below)
180        if (procMask == 0)
181            procMask = 1;
182
183        // if the core specified with coreNr is not available, take the lowest one
184        if (!(procMask & (1 << coreNr)))
185            coreNr = 0;
186
187        // Find the lowest core that this process uses and coreNr suggests
188        DWORD threadMask = 1;
189        while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr)))
190            threadMask <<= 1;
191
192        // Set affinity to the first core
193        SetThreadAffinityMask(GetCurrentThread(), threadMask);
194#endif
195    }
196}
Note: See TracBrowser for help on using the repository browser.