| 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 <ctime> |
|---|
| 40 | #include <fstream> |
|---|
| 41 | #include <vector> |
|---|
| 42 | |
|---|
| 43 | #ifdef ORXONOX_PLATFORM_WINDOWS |
|---|
| 44 | # ifndef WIN32_LEAN_AND_MEAN |
|---|
| 45 | # define WIN32_LEAN_AND_MEAN |
|---|
| 46 | # endif |
|---|
| 47 | # include <windows.h> |
|---|
| 48 | # undef min |
|---|
| 49 | # undef max |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | #include "util/Debug.h" |
|---|
| 53 | #include "util/Exception.h" |
|---|
| 54 | #include "PathConfig.h" |
|---|
| 55 | |
|---|
| 56 | namespace orxonox |
|---|
| 57 | { |
|---|
| 58 | //! Static pointer to the singleton |
|---|
| 59 | Core* Core::singletonPtr_s = 0; |
|---|
| 60 | |
|---|
| 61 | Core::Core(const std::string& cmdLine) |
|---|
| 62 | { |
|---|
| 63 | // Set the hard coded fixed paths |
|---|
| 64 | this->pathConfig_.reset(new PathConfig()); |
|---|
| 65 | |
|---|
| 66 | // Set configurable paths like log, config and media |
|---|
| 67 | this->pathConfig_->setConfigurablePaths(); |
|---|
| 68 | |
|---|
| 69 | // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% (Windows) was used |
|---|
| 70 | OutputHandler::getInstance().setLogPath(PathConfig::getLogPathString()); |
|---|
| 71 | |
|---|
| 72 | #ifdef ORXONOX_PLATFORM_WINDOWS |
|---|
| 73 | // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump |
|---|
| 74 | int limitToCPU = 0;//CommandLineParser::getValue("limitToCPU"); |
|---|
| 75 | if (limitToCPU > 0) |
|---|
| 76 | setThreadAffinity(static_cast<unsigned int>(limitToCPU)); |
|---|
| 77 | #endif |
|---|
| 78 | |
|---|
| 79 | // Generate documentation instead of normal run? |
|---|
| 80 | std::string docFilename; |
|---|
| 81 | //CommandLineParser::getValue("generateDoc", &docFilename); |
|---|
| 82 | if (!docFilename.empty()) |
|---|
| 83 | { |
|---|
| 84 | std::ofstream docFile(docFilename.c_str()); |
|---|
| 85 | if (docFile.is_open()) |
|---|
| 86 | { |
|---|
| 87 | //CommandLineParser::generateDoc(docFile); |
|---|
| 88 | docFile.close(); |
|---|
| 89 | } |
|---|
| 90 | else |
|---|
| 91 | COUT(0) << "Error: Could not open file for documentation writing" << endl; |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | @brief |
|---|
| 97 | All destruction code is handled by QScopedPointers |
|---|
| 98 | */ |
|---|
| 99 | Core::~Core() |
|---|
| 100 | { |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 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 |
|---|
| 111 | /* |
|---|
| 112 | SetConfigValueExternal(softDebugLevelLogFile_, "OutputHandler", "softDebugLevelLogFile", defaultLevelLogFile) |
|---|
| 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); |
|---|
| 122 | SetConfigValue(bStartIOConsole_, true) |
|---|
| 123 | .description("Set to false if you don't want to use the IOConsole (for Lua debugging for instance)"); |
|---|
| 124 | */ |
|---|
| 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 | |
|---|
| 138 | /** |
|---|
| 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. |
|---|
| 147 | */ |
|---|
| 148 | void Core::setThreadAffinity(int limitToCPU) |
|---|
| 149 | { |
|---|
| 150 | #ifdef ORXONOX_PLATFORM_WINDOWS |
|---|
| 151 | |
|---|
| 152 | if (limitToCPU <= 0) |
|---|
| 153 | return; |
|---|
| 154 | |
|---|
| 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 |
|---|
| 164 | |
|---|
| 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 |
|---|
| 182 | } |
|---|
| 183 | } |
|---|