Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/forks/sandbox_light/src/libraries/core/Core.cc @ 7908

Last change on this file since 7908 was 7908, checked in by rgrieder, 13 years ago

Stripped down trunk to form a new light sandbox.

  • Property svn:eol-style set to native
File size: 5.6 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 with its global variables (avoids boost include)
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/SignalHandler.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#ifdef ORXONOX_PLATFORM_WINDOWS
64    SetCommandLineArgument(limitToCPU, 1).information("Limits the program to one CPU/core (1, 2, 3, etc.). Default is the first core (faster than off)");
65#endif
66
67    Core::Core(const std::string& cmdLine)
68    {
69        // Set the hard coded fixed paths
70        this->pathConfig_.reset(new PathConfig());
71
72        // Parse command line arguments AFTER the modules have been loaded (static code!)
73        CommandLineParser::parseCommandLine(cmdLine);
74
75        // Set configurable paths like log, config and media
76        this->pathConfig_->setConfigurablePaths();
77
78        // create a signal handler (only active for Linux)
79        // This call is placed as soon as possible, but after the directories are set
80        this->signalHandler_.reset(new SignalHandler());
81        this->signalHandler_->doCatch(PathConfig::getExecutablePathString(), PathConfig::getLogPathString() + "orxonox_crash.log");
82
83        // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% (Windows) was used
84        OutputHandler::getInstance().setLogPath(PathConfig::getLogPathString());
85
86        // Parse additional options file now that we know its path
87        CommandLineParser::parseFile();
88
89#ifdef ORXONOX_PLATFORM_WINDOWS
90        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
91        // do this after ogre has initialised. Somehow Ogre changes the settings again (not through
92        // the timer though).
93        int limitToCPU = CommandLineParser::getValue("limitToCPU");
94        if (limitToCPU > 0)
95            setThreadAffinity(static_cast<unsigned int>(limitToCPU));
96#endif
97
98        // Generate documentation instead of normal run?
99        std::string docFilename;
100        CommandLineParser::getValue("generateDoc", &docFilename);
101        if (!docFilename.empty())
102        {
103            std::ofstream docFile(docFilename.c_str());
104            if (docFile.is_open())
105            {
106                CommandLineParser::generateDoc(docFile);
107                docFile.close();
108            }
109            else
110                COUT(0) << "Error: Could not open file for documentation writing" << endl;
111        }
112    }
113
114    /**
115    @brief
116        All destruction code is handled by scoped_ptrs and ScopeGuards.
117    */
118    Core::~Core()
119    {
120    }
121
122    void Core::initRandomNumberGenerator()
123    {
124        static bool bInitialized = false;
125        if (!bInitialized && this->bInitRandomNumberGenerator_)
126        {
127            srand(static_cast<unsigned int>(time(0)));
128            rand();
129            bInitialized = true;
130        }
131    }
132
133    /**
134    @note
135        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
136            (Object-oriented Graphics Rendering Engine)
137        For the latest info, see http://www.ogre3d.org/
138
139        Copyright (c) 2000-2008 Torus Knot Software Ltd
140
141        OGRE is licensed under the LGPL. For more info, see OGRE license.
142    */
143    void Core::setThreadAffinity(int limitToCPU)
144    {
145#ifdef ORXONOX_PLATFORM_WINDOWS
146
147        if (limitToCPU <= 0)
148            return;
149
150        unsigned int coreNr = limitToCPU - 1;
151        // Get the current process core mask
152        DWORD procMask;
153        DWORD sysMask;
154#  if _MSC_VER >= 1400 && defined (_M_X64)
155        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
156#  else
157        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
158#  endif
159
160        // If procMask is 0, consider there is only one core available
161        // (using 0 as procMask will cause an infinite loop below)
162        if (procMask == 0)
163            procMask = 1;
164
165        // if the core specified with coreNr is not available, take the lowest one
166        if (!(procMask & (1 << coreNr)))
167            coreNr = 0;
168
169        // Find the lowest core that this process uses and coreNr suggests
170        DWORD threadMask = 1;
171        while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr)))
172            threadMask <<= 1;
173
174        // Set affinity to the first core
175        SetThreadAffinityMask(GetCurrentThread(), threadMask);
176#endif
177    }
178}
Note: See TracBrowser for help on using the repository browser.