[Orxonox-commit 1403] r6121 - code/branches/presentation2/src/libraries/core
rgrieder at orxonox.net
rgrieder at orxonox.net
Mon Nov 23 18:19:58 CET 2009
Author: rgrieder
Date: 2009-11-23 18:19:58 +0100 (Mon, 23 Nov 2009)
New Revision: 6121
Modified:
code/branches/presentation2/src/libraries/core/Core.cc
code/branches/presentation2/src/libraries/core/Core.h
code/branches/presentation2/src/libraries/core/Game.cc
code/branches/presentation2/src/libraries/core/Game.h
code/branches/presentation2/src/libraries/core/Language.cc
code/branches/presentation2/src/libraries/core/Language.h
code/branches/presentation2/src/libraries/core/OrxonoxClass.cc
code/branches/presentation2/src/libraries/core/OrxonoxClass.h
Log:
Removed CoreConfiguration and GameConfiguration workaround. I have found an easy solution that doesn't need this.
Config values for these classes can again be found under "Game" and "Core".
Modified: code/branches/presentation2/src/libraries/core/Core.cc
===================================================================
--- code/branches/presentation2/src/libraries/core/Core.cc 2009-11-22 15:25:02 UTC (rev 6120)
+++ code/branches/presentation2/src/libraries/core/Core.cc 2009-11-23 17:19:58 UTC (rev 6121)
@@ -50,6 +50,7 @@
#include "util/Clock.h"
#include "util/Debug.h"
#include "util/Exception.h"
+#include "util/Scope.h"
#include "util/SignalHandler.h"
#include "PathConfig.h"
#include "CommandExecutor.h"
@@ -80,87 +81,11 @@
SetCommandLineArgument(limitToCPU, 0).information("Limits the program to one cpu/core (1, 2, 3, etc.). 0 turns it off (default)");
#endif
- /**
- @brief
- Helper class for the Core singleton: we cannot derive
- Core from OrxonoxClass because we need to handle the Identifier
- destruction in the Core destructor.
- */
- class CoreConfiguration : public OrxonoxClass
- {
- public:
- CoreConfiguration()
- {
- }
-
- void initialise()
- {
- RegisterRootObject(CoreConfiguration);
- this->setConfigValues();
- }
-
- /**
- @brief Function to collect the SetConfigValue-macro calls.
- */
- void setConfigValues()
- {
-#ifdef ORXONOX_RELEASE
- const unsigned int defaultLevelLogFile = 3;
-#else
- const unsigned int defaultLevelLogFile = 4;
-#endif
- SetConfigValueGeneric(ConfigFileType::Settings, softDebugLevelLogFile_, "softDebugLevelLogFile", "OutputHandler", defaultLevelLogFile)
- .description("The maximum level of debug output shown in the log file");
- OutputHandler::getInstance().setSoftDebugLevel(OutputHandler::logFileOutputListenerName_s, this->softDebugLevelLogFile_);
-
- SetConfigValue(language_, Language::getInstance().defaultLanguage_)
- .description("The language of the in game text")
- .callback(this, &CoreConfiguration::languageChanged);
- SetConfigValue(bInitializeRandomNumberGenerator_, true)
- .description("If true, all random actions are different each time you start the game")
- .callback(this, &CoreConfiguration::initializeRandomNumberGenerator);
- }
-
- /**
- @brief Callback function if the language has changed.
- */
- void languageChanged()
- {
- // Read the translation file after the language was configured
- Language::getInstance().readTranslatedLanguageFile();
- }
-
- /**
- @brief Sets the language in the config-file back to the default.
- */
- void resetLanguage()
- {
- ResetConfigValue(language_);
- }
-
- void initializeRandomNumberGenerator()
- {
- static bool bInitialized = false;
- if (!bInitialized && this->bInitializeRandomNumberGenerator_)
- {
- srand(static_cast<unsigned int>(time(0)));
- rand();
- bInitialized = true;
- }
- }
-
- int softDebugLevelLogFile_; //!< The debug level for the log file (belongs to OutputHandler)
- std::string language_; //!< The language
- bool bInitializeRandomNumberGenerator_; //!< If true, srand(time(0)) is called
- };
-
-
Core::Core(const std::string& cmdLine)
// Cleanup guard for identifier destruction (incl. XMLPort, configValues, consoleCommands)
: identifierDestroyer_(Identifier::destroyAllIdentifiers)
// Cleanup guard for external console commands that don't belong to an Identifier
, consoleCommandDestroyer_(CommandExecutor::destroyExternalCommands)
- , configuration_(new CoreConfiguration()) // Don't yet create config values!
, bGraphicsLoaded_(false)
{
// Set the hard coded fixed paths
@@ -217,16 +142,19 @@
// Required as well for the config values
this->languageInstance_.reset(new Language());
+ // Do this soon after the ConfigFileManager has been created to open up the
+ // possibility to configure everything below here
+ ClassIdentifier<Core>::getIdentifier("Core")->initialiseObject(this, "Core", true);
+ // Remove us from the object lists again to avoid problems when destroying the Core
+ this->unregisterObject();
+ this->setConfigValues();
+
// create persistent io console
this->ioConsole_.reset(new IOConsole());
// creates the class hierarchy for all classes with factories
Identifier::createClassHierarchy();
- // Do this soon after the ConfigFileManager has been created to open up the
- // possibility to configure everything below here
- this->configuration_->initialise();
-
// Load OGRE excluding the renderer and the render window
this->graphicsManager_.reset(new GraphicsManager(false));
@@ -246,6 +174,44 @@
{
}
+ //! Function to collect the SetConfigValue-macro calls.
+ void Core::setConfigValues()
+ {
+#ifdef ORXONOX_RELEASE
+ const unsigned int defaultLevelLogFile = 3;
+#else
+ const unsigned int defaultLevelLogFile = 4;
+#endif
+ SetConfigValueGeneric(ConfigFileType::Settings, softDebugLevelLogFile_, "softDebugLevelLogFile", "OutputHandler", defaultLevelLogFile)
+ .description("The maximum level of debug output shown in the log file");
+ OutputHandler::getInstance().setSoftDebugLevel(OutputHandler::logFileOutputListenerName_s, this->softDebugLevelLogFile_);
+
+ SetConfigValue(language_, Language::getInstance().defaultLanguage_)
+ .description("The language of the in game text")
+ .callback(this, &Core::languageChanged);
+ SetConfigValue(bInitRandomNumberGenerator_, true)
+ .description("If true, all random actions are different each time you start the game")
+ .callback(this, &Core::initRandomNumberGenerator);
+ }
+
+ //! Callback function if the language has changed.
+ void Core::languageChanged()
+ {
+ // Read the translation file after the language was configured
+ Language::getInstance().readTranslatedLanguageFile();
+ }
+
+ void Core::initRandomNumberGenerator()
+ {
+ static bool bInitialized = false;
+ if (!bInitialized && this->bInitRandomNumberGenerator_)
+ {
+ srand(static_cast<unsigned int>(time(0)));
+ rand();
+ bInitialized = true;
+ }
+ }
+
void Core::loadGraphics()
{
// Any exception should trigger this, even in upgradeToGraphics (see its remarks)
@@ -295,23 +261,13 @@
GameMode::bShowsGraphics_s = false;
}
- /**
- @brief Returns the configured language.
- */
- /*static*/ const std::string& Core::getLanguage()
+ //! Sets the language in the config-file back to the default.
+ void Core::resetLanguage()
{
- return Core::getInstance().configuration_->language_;
+ ResetConfigValue(language_);
}
/**
- @brief Sets the language in the config-file back to the default.
- */
- /*static*/ void Core::resetLanguage()
- {
- Core::getInstance().configuration_->resetLanguage();
- }
-
- /**
@note
The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
(Object-oriented Graphics Rendering Engine)
Modified: code/branches/presentation2/src/libraries/core/Core.h
===================================================================
--- code/branches/presentation2/src/libraries/core/Core.h 2009-11-22 15:25:02 UTC (rev 6120)
+++ code/branches/presentation2/src/libraries/core/Core.h 2009-11-23 17:19:58 UTC (rev 6121)
@@ -34,22 +34,19 @@
#include <cassert>
#include <boost/scoped_ptr.hpp>
-#include "util/OutputHandler.h"
-#include "util/Scope.h"
#include "util/ScopeGuard.h"
#include "util/Singleton.h"
+#include "core/OrxonoxClass.h"
namespace orxonox
{
- class CoreConfiguration;
-
/**
@brief
The Core class is a singleton used to configure the program basics.
@remark
You should only create this singleton once because it destroys the identifiers!
*/
- class _CoreExport Core : public Singleton<Core>
+ class _CoreExport Core : public Singleton<Core>, public OrxonoxClass
{
typedef Loki::ScopeGuardImpl0<void (*)()> SimpleScopeGuard;
friend class Singleton<Core>;
@@ -68,12 +65,17 @@
void setConfigValues();
- static const std::string& getLanguage();
- static void resetLanguage();
+ //! Returns the configured language.
+ const std::string& getLanguage()
+ { return this->language_; }
+ void resetLanguage();
private:
Core(const Core&); //!< Don't use (undefined symbol)
+ void languageChanged();
+ void initRandomNumberGenerator();
+
void preUpdate(const Clock& time);
void postUpdate(const Clock& time);
@@ -81,7 +83,7 @@
void unloadGraphics();
void setThreadAffinity(int limitToCPU);
-
+ // MANAGED SINGLETONS/OBJECTS
// Mind the order for the destruction!
scoped_ptr<PathConfig> pathConfig_;
scoped_ptr<DynLibManager> dynLibManager_;
@@ -91,18 +93,21 @@
scoped_ptr<ConfigFileManager> configFileManager_;
scoped_ptr<Language> languageInstance_;
scoped_ptr<IOConsole> ioConsole_;
- scoped_ptr<CoreConfiguration> configuration_;
scoped_ptr<TclBind> tclBind_;
scoped_ptr<TclThreadManager> tclThreadManager_;
+ scoped_ptr<Scope<ScopeID::Root> > rootScope_;
// graphical
scoped_ptr<GraphicsManager> graphicsManager_; //!< Interface to OGRE
scoped_ptr<InputManager> inputManager_; //!< Interface to OIS
scoped_ptr<GUIManager> guiManager_; //!< Interface to GUI
- scoped_ptr<Scope<ScopeID::Root> > rootScope_;
scoped_ptr<Scope<ScopeID::Graphics> > graphicsScope_;
bool bGraphicsLoaded_;
- static Core* singletonPtr_s;
+ int softDebugLevelLogFile_; //!< The debug level for the log file (belongs to OutputHandler)
+ std::string language_; //!< The language
+ bool bInitRandomNumberGenerator_; //!< If true, srand(time(0)) is called
+
+ static Core* singletonPtr_s;
};
}
Modified: code/branches/presentation2/src/libraries/core/Game.cc
===================================================================
--- code/branches/presentation2/src/libraries/core/Game.cc 2009-11-22 15:25:02 UTC (rev 6120)
+++ code/branches/presentation2/src/libraries/core/Game.cc 2009-11-23 17:19:58 UTC (rev 6121)
@@ -66,11 +66,7 @@
std::map<std::string, GameStateInfo> Game::gameStateDeclarations_s;
Game* Game::singletonPtr_s = 0;
-
- /**
- @brief
- Represents one node of the game state tree.
- */
+ //! Represents one node of the game state tree.
struct GameStateTreeNode
{
std::string name_;
@@ -78,42 +74,6 @@
std::vector<shared_ptr<GameStateTreeNode> > children_;
};
-
- /**
- @brief
- Another helper class for the Game singleton: we cannot derive
- Game from OrxonoxClass because we need to handle the Identifier
- destruction in the Core destructor.
- */
- class GameConfiguration : public OrxonoxClass
- {
- public:
- GameConfiguration()
- {
- RegisterRootObject(GameConfiguration);
- this->setConfigValues();
- }
-
- void setConfigValues()
- {
- SetConfigValue(statisticsRefreshCycle_, 250000)
- .description("Sets the time in microseconds interval at which average fps, etc. get updated.");
- SetConfigValue(statisticsAvgLength_, 1000000)
- .description("Sets the time in microseconds interval at which average fps, etc. gets calculated.");
- SetConfigValue(fpsLimit_, 50)
- .description("Sets the desired framerate (0 for no limit).");
- }
-
- unsigned int statisticsRefreshCycle_;
- unsigned int statisticsAvgLength_;
- unsigned int fpsLimit_;
- };
-
-
- /**
- @brief
- Non-initialising constructor.
- */
Game::Game(const std::string& cmdLine)
// Destroy factories before the Core!
: gsFactoryDestroyer_(Game::GameStateFactory::getFactories(), &std::map<std::string, shared_ptr<GameStateFactory> >::clear)
@@ -136,6 +96,12 @@
// Create the Core
this->core_.reset(new Core(cmdLine));
+ // Do this after the Core creation!
+ ClassIdentifier<Game>::getIdentifier("Game")->initialiseObject(this, "Game", true);
+ // Remove us from the object lists again to avoid problems when destroying the Game
+ this->unregisterObject();
+ this->setConfigValues();
+
// After the core has been created, we can safely instantiate the GameStates that don't require graphics
for (std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.begin();
it != gameStateDeclarations_s.end(); ++it)
@@ -149,9 +115,6 @@
this->rootStateNode_->name_ = "emptyRootGameState";
this->loadedTopStateNode_ = this->rootStateNode_;
this->loadedStates_.push_back(this->getState(rootStateNode_->name_));
-
- // Do this after the Core creation!
- this->configuration_.reset(new GameConfiguration());
}
/**
@@ -162,6 +125,16 @@
{
}
+ void Game::setConfigValues()
+ {
+ SetConfigValue(statisticsRefreshCycle_, 250000)
+ .description("Sets the time in microseconds interval at which average fps, etc. get updated.");
+ SetConfigValue(statisticsAvgLength_, 1000000)
+ .description("Sets the time in microseconds interval at which average fps, etc. gets calculated.");
+ SetConfigValue(fpsLimit_, 50)
+ .description("Sets the desired framerate (0 for no limit).");
+ }
+
/**
@brief
Main loop of the orxonox game.
@@ -311,11 +284,11 @@
uint64_t currentRealTime = gameClock_->getRealMicroseconds();
this->statisticsTickTimes_.back().tickLength += currentRealTime - currentTime;
this->periodTickTime_ += currentRealTime - currentTime;
- if (this->periodTime_ > this->configuration_->statisticsRefreshCycle_)
+ if (this->periodTime_ > this->statisticsRefreshCycle_)
{
std::list<StatisticsTickInfo>::iterator it = this->statisticsTickTimes_.begin();
assert(it != this->statisticsTickTimes_.end());
- int64_t lastTime = currentTime - this->configuration_->statisticsAvgLength_;
+ int64_t lastTime = currentTime - this->statisticsAvgLength_;
if (static_cast<int64_t>(it->tickTime) < lastTime)
{
do
@@ -332,14 +305,14 @@
this->avgFPS_ = static_cast<float>(framesPerPeriod) / (currentTime - this->statisticsTickTimes_.front().tickTime) * 1000000.0f;
this->avgTickTime_ = static_cast<float>(this->periodTickTime_) / framesPerPeriod / 1000.0f;
- this->periodTime_ -= this->configuration_->statisticsRefreshCycle_;
+ this->periodTime_ -= this->statisticsRefreshCycle_;
}
}
void Game::updateFPSLimiter()
{
- // Why configuration_->fpsLimit_ - 1? No idea, but otherwise the fps rate is always (from 10 to 200!) one frame too high
- uint32_t nextTime = gameClock_->getMicroseconds() - excessSleepTime_ + static_cast<uint32_t>(1000000.0f / (configuration_->fpsLimit_ - 1));
+ // Why fpsLimit_ - 1? No idea, but otherwise the fps rate is always (from 10 to 200!) one frame too high
+ uint32_t nextTime = gameClock_->getMicroseconds() - excessSleepTime_ + static_cast<uint32_t>(1000000.0f / (fpsLimit_ - 1));
uint64_t currentRealTime = gameClock_->getRealMicroseconds();
while (currentRealTime < nextTime - minimumSleepTime_)
{
Modified: code/branches/presentation2/src/libraries/core/Game.h
===================================================================
--- code/branches/presentation2/src/libraries/core/Game.h 2009-11-22 15:25:02 UTC (rev 6120)
+++ code/branches/presentation2/src/libraries/core/Game.h 2009-11-23 17:19:58 UTC (rev 6121)
@@ -49,6 +49,7 @@
#include "util/Debug.h"
#include "util/ScopeGuard.h"
#include "util/Singleton.h"
+#include "core/OrxonoxClass.h"
/**
@def
@@ -60,8 +61,6 @@
namespace orxonox
{
- class GameConfiguration;
-
//! Helper object required before GameStates are being constructed
struct GameStateInfo
{
@@ -77,7 +76,7 @@
@remark
You should only create this singleton once because it owns the Core class! (see remark there)
*/
- class _CoreExport Game : public Singleton<Game>
+ class _CoreExport Game : public Singleton<Game>, public OrxonoxClass
{
friend class Singleton<Game>;
typedef std::vector<shared_ptr<GameState> > GameStateVector;
@@ -88,6 +87,8 @@
Game(const std::string& cmdLine);
~Game();
+ void setConfigValues();
+
void setStateHierarchy(const std::string& str);
shared_ptr<GameState> getState(const std::string& name);
@@ -159,7 +160,6 @@
scoped_ptr<Clock> gameClock_;
scoped_ptr<Core> core_;
ObjScopeGuard gsFactoryDestroyer_;
- scoped_ptr<GameConfiguration> configuration_;
GameStateMap constructedStates_;
GameStateVector loadedStates_;
@@ -180,6 +180,11 @@
int excessSleepTime_;
unsigned int minimumSleepTime_;
+ // config values
+ unsigned int statisticsRefreshCycle_;
+ unsigned int statisticsAvgLength_;
+ unsigned int fpsLimit_;
+
static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
static Game* singletonPtr_s; //!< Pointer to the Singleton
};
Modified: code/branches/presentation2/src/libraries/core/Language.cc
===================================================================
--- code/branches/presentation2/src/libraries/core/Language.cc 2009-11-22 15:25:02 UTC (rev 6120)
+++ code/branches/presentation2/src/libraries/core/Language.cc 2009-11-23 17:19:58 UTC (rev 6121)
@@ -245,9 +245,9 @@
*/
void Language::readTranslatedLanguageFile()
{
- COUT(4) << "Read translated language file (" << Core::getLanguage() << ")." << std::endl;
+ COUT(4) << "Read translated language file (" << Core::getInstance().getLanguage() << ")." << std::endl;
- std::string filepath = PathConfig::getConfigPathString() + getFilename(Core::getLanguage());
+ std::string filepath = PathConfig::getConfigPathString() + getFilename(Core::getInstance().getLanguage());
// Open the file
std::ifstream file;
@@ -256,8 +256,8 @@
if (!file.is_open())
{
COUT(1) << "An error occurred in Language.cc:" << std::endl;
- COUT(1) << "Error: Couldn't open file " << getFilename(Core::getLanguage()) << " to read the translated language entries!" << std::endl;
- Core::resetLanguage();
+ COUT(1) << "Error: Couldn't open file " << getFilename(Core::getInstance().getLanguage()) << " to read the translated language entries!" << std::endl;
+ Core::getInstance().resetLanguage();
COUT(3) << "Info: Reset language to " << this->defaultLanguage_ << "." << std::endl;
return;
}
@@ -286,7 +286,7 @@
}
else
{
- COUT(2) << "Warning: Invalid language entry \"" << lineString << "\" in " << getFilename(Core::getLanguage()) << std::endl;
+ COUT(2) << "Warning: Invalid language entry \"" << lineString << "\" in " << getFilename(Core::getInstance().getLanguage()) << std::endl;
}
}
}
Modified: code/branches/presentation2/src/libraries/core/Language.h
===================================================================
--- code/branches/presentation2/src/libraries/core/Language.h 2009-11-22 15:25:02 UTC (rev 6120)
+++ code/branches/presentation2/src/libraries/core/Language.h 2009-11-23 17:19:58 UTC (rev 6121)
@@ -115,7 +115,7 @@
class _CoreExport Language : public Singleton<Language>
{
friend class Singleton<Language>;
- friend class CoreConfiguration;
+ friend class Core;
public:
Language();
Modified: code/branches/presentation2/src/libraries/core/OrxonoxClass.cc
===================================================================
--- code/branches/presentation2/src/libraries/core/OrxonoxClass.cc 2009-11-22 15:25:02 UTC (rev 6120)
+++ code/branches/presentation2/src/libraries/core/OrxonoxClass.cc 2009-11-23 17:19:58 UTC (rev 6121)
@@ -58,12 +58,12 @@
assert(this->referenceCount_ <= 0);
- delete this->metaList_;
+ this->unregisterObject();
// parents_ exists only if isCreatingHierarchy() of the associated Identifier returned true while creating the class
if (this->parents_)
delete this->parents_;
-
+
// reset all weak pointers pointing to this object
for (std::set<WeakPtr<OrxonoxClass>*>::iterator it = this->weakPointers_.begin(); it != this->weakPointers_.end(); )
(*(it++))->objectDeleted();
@@ -78,6 +78,13 @@
delete this;
}
+ void OrxonoxClass::unregisterObject()
+ {
+ if (this->metaList_)
+ delete this->metaList_;
+ this->metaList_ = 0;
+ }
+
/** @brief Returns true if the objects class is of the given type or a derivative. */
bool OrxonoxClass::isA(const Identifier* identifier)
{ return this->getIdentifier()->isA(identifier); }
Modified: code/branches/presentation2/src/libraries/core/OrxonoxClass.h
===================================================================
--- code/branches/presentation2/src/libraries/core/OrxonoxClass.h 2009-11-22 15:25:02 UTC (rev 6120)
+++ code/branches/presentation2/src/libraries/core/OrxonoxClass.h 2009-11-23 17:19:58 UTC (rev 6121)
@@ -72,6 +72,7 @@
virtual ~OrxonoxClass();
void destroy();
+ void unregisterObject();
/** @brief Function to collect the SetConfigValue-macro calls. */
void setConfigValues() {};
More information about the Orxonox-commit
mailing list