Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: sandbox_qt/src/libraries/core/Core.cc @ 7421

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

Basic stuff up and running for the Qt sandbox.
No GUI support yet.

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