Changeset 1663
- Timestamp:
- Aug 20, 2008, 10:30:28 PM (16 years ago)
- Location:
- code/branches/gui
- Files:
-
- 2 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gui/src/core/CorePrereqs.h
r1638 r1663 102 102 class CommandEvaluation; 103 103 class CommandExecutor; 104 class CommandLine; 105 class BaseCommandLineArgument; 106 template <class T> 107 class CommandLineArgument; 104 108 class ConfigFile; 105 109 class ConfigFileEntry; -
code/branches/gui/src/core/Exception.h
r1660 r1663 59 59 General, 60 60 FileNotFound, 61 Argument, 61 62 PluginsNotFound, 62 63 InitialisationFailed, … … 119 120 RETURN_EXCEPTION_CODE(General) 120 121 RETURN_EXCEPTION_CODE(FileNotFound); 122 RETURN_EXCEPTION_CODE(Argument); 121 123 RETURN_EXCEPTION_CODE(PluginsNotFound); 122 124 RETURN_EXCEPTION_CODE(InitialisationFailed); … … 132 134 CREATE_ORXONOX_EXCEPTION(General); 133 135 CREATE_ORXONOX_EXCEPTION(FileNotFound); 136 CREATE_ORXONOX_EXCEPTION(Argument); 134 137 CREATE_ORXONOX_EXCEPTION(PluginsNotFound); 135 138 CREATE_ORXONOX_EXCEPTION(InitialisationFailed); … … 142 145 // define an assert macro that can display a message 143 146 #ifndef NDEBUG 144 #define OrxAssert( condition, errorMessage) \145 condition ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << errorMessage << std::endl); \146 assert( condition)147 #define OrxAssert(assertion, errorMessage) \ 148 assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << errorMessage << std::endl); \ 149 assert(assertion) 147 150 #else 148 151 #define OrxAssert(condition, errorMessage) ((void)0) -
code/branches/gui/src/orxonox/Main.cc
r1661 r1663 39 39 40 40 #include "util/OrxonoxPlatform.h" 41 #include "util/ArgReader.h"42 41 #include "core/SignalHandler.h" 43 42 #include "core/Debug.h" 44 #include "network/ClientConnection.h" 45 #include "Settings.h" 46 #include "Orxonox.h" 43 #include "core/CommandLine.h" 44 //#include "Orxonox.h" 47 45 48 46 #include "gamestates/GSRoot.h" … … 80 78 #endif 81 79 82 bool parseCommandLine(int argc, char** argv)83 {84 ArgReader args;85 std::string errorStr = args.parse(argc, argv);86 if (errorStr != "")87 {88 COUT(1) << "Command Line: Parsing failed.\n" << errorStr << std::endl;89 return false;90 }91 92 // Argument reader parses the command line to check syntax.93 // Settings Singleton then stores the arguments. It always94 // expects a default value.95 bool success = true;96 success &= Settings::addCommandLineArgument<std::string>97 ("mode", args.getArgument("mode"), "standalone");98 success &= Settings::addCommandLineArgument<std::string>99 ("dataPath", args.getArgument("dataPath"), "./");100 success &= Settings::addCommandLineArgument<std::string>101 ("ip", args.getArgument("ip"), "127.0.0.0");102 success &= Settings::addCommandLineArgument<int>103 ("port", args.getArgument("port"), NETWORK_PORT);104 105 if (!success)106 return false;107 108 if (!args.allChecked())109 {110 COUT(1) << "Command Line: Parsing failed.\nNot all arguments were matched." << std::endl;111 return false;112 }113 114 return true;115 }116 117 #include "core/GameState.h"118 80 119 81 #ifdef __cplusplus … … 123 85 int main(int argc, char** argv) 124 86 { 87 try 88 { 89 orxonox::CommandLine::parse(argc, argv); 90 } 91 catch (orxonox::ArgumentException& ex) 92 { 93 COUT(1) << ex.what() << std::endl; 94 COUT(0) << "Usage:" << std::endl << "orxonox [--mode client|server|dedicated|standalone] " 95 << "[--data PATH] [--ip IP] [--port PORT]" << std::endl; 96 } 97 98 125 99 // create a signal handler (only works for linux) 126 100 SignalHandler::getInstance()->doCatch(argv[0], "orxonox.log"); 127 101 128 if (!parseCommandLine(argc, argv))129 {130 COUT(0) << "Usage:" << std::endl << "orxonox [--mode client|server|dedicated|standalone] "131 << "[--data PATH] [--ip IP] [--port PORT]" << std::endl;132 return 0;133 }134 102 135 103 … … 181 149 182 150 183 Orxonox orxonoxInstance;151 //Orxonox orxonoxInstance; 184 152 185 153 try -
code/branches/gui/src/orxonox/Orxonox.cc
r1662 r1663 108 108 , bAbort_(false) 109 109 , timefactor_(1.0f) 110 , mode_(GameMode::GM_Unspecified)111 110 , debugRefreshTime_(0.0f) 112 111 , graphicsEngine_(0) … … 119 118 120 119 //assert(singletonRef_s == 0); 121 OrxAssert(singletonRef_s == 0, " asdfasdfasdfasdfblahblah");120 OrxAssert(singletonRef_s == 0, "Cannot instantiate Orxonox class twice"); 122 121 singletonRef_s = this; 123 122 } … … 229 228 setConfigValues(); 230 229 231 const Settings::CommandLineArgument* mode = Settings::getCommandLineArgument("mode"); 232 assert(mode); 233 if (!mode->bHasDefaultValue_) 234 { 235 Settings::setGameMode(mode->value_); 236 this->mode_ = Settings::getGameMode(); 237 } 238 COUT(3) << "Orxonox: Game mode is " << mode_.name << "." << std::endl; 239 240 const Settings::CommandLineArgument* dataPath = Settings::getCommandLineArgument("dataPath"); 241 assert(dataPath); 242 if (!dataPath->bHasDefaultValue_) 243 { 244 if (*dataPath->value_.getString().end() != '/' && *dataPath->value_.getString().end() != '\\') 245 Settings::tsetDataPath(dataPath->value_.getString() + "/"); 246 else 247 Settings::tsetDataPath(dataPath->value_.getString()); 248 } 230 //const Settings::CommandLineArgument* dataPath = Settings::getCommandLineArgument("dataPath"); 231 //assert(dataPath); 232 //if (!dataPath->bHasDefaultValue_) 233 //{ 234 // if (*dataPath->value_.getString().end() != '/' && *dataPath->value_.getString().end() != '\\') 235 // Settings::tsetDataPath(dataPath->value_.getString() + "/"); 236 // else 237 // Settings::tsetDataPath(dataPath->value_.getString()); 238 //} 249 239 250 240 try … … 256 246 graphicsEngine_->setup(); // creates ogre root and other essentials 257 247 258 if ( mode_.showsGraphics)248 if (Settings::showsGraphics()) 259 249 { 260 250 graphicsEngine_->loadRenderer(); // creates the render window … … 286 276 // We probably want to use std::cin to catch input (OIS uses DirectX or X server) 287 277 } 288 289 bool showGUI = true;290 if (mode_.mode != GameMode::Unspecified)291 {292 showGUI = false;293 // a game mode was specified with the command line294 // we therefore load the game and level directly295 296 if (!loadLevel(this->mode_))297 {298 COUT(1) << "Loading with predefined mode failed. Showing main menu." << std::endl;299 showGUI = true;300 mode_ = GameMode::GM_Unspecified;301 }302 }303 304 if (showGUI)305 {306 // show main menu307 GUIManager::getInstance().showGUI("MainMenu", 0);308 GraphicsEngine::getInstance().getViewport()->setCamera(GUIManager::getInstance().getCamera());309 }310 278 } 311 279 catch (std::exception& ex) … … 316 284 } 317 285 318 modeRequest_ = mode_;319 286 // here happens the game 320 287 startRenderLoop(); 321 288 322 if (mode_.mode == GameMode::Client) 323 network::Client::getSingleton()->closeConnection(); 324 325 if (mode_.hasServer) 326 server_g->close(); 327 } 328 329 /*static*/ void Orxonox::loadGame(const std::string& name) 330 { 331 const GameMode& mode = Settings::getGameMode(name); 332 if (mode.mode == GameMode::None) 333 return; 334 335 getInstance().modeRequest_ = mode; 336 } 337 338 bool Orxonox::loadLevel(const GameMode& mode) 339 { 340 bool success = true; 341 342 if (mode.showsGraphics) 343 { 344 // create Ogre SceneManager for the level 345 graphicsEngine_->createNewScene(); 346 347 if (!loadPlayground()) 348 return false; 349 } 350 351 switch (mode.mode) 352 { 353 case GameMode::Server: 354 success &= serverLoad(); 355 break; 356 case GameMode::Client: 357 success &= clientLoad(); 358 break; 359 case GameMode::Dedicated: 360 success &= serverLoad(); 361 break; 362 case GameMode::Standalone: 363 success &= standaloneLoad(); 364 break; 365 default: // never happens 366 assert(false); 367 } 368 369 if (success) 370 { 371 InputManager::getInstance().requestEnterState("game"); 372 this->mode_ = mode; 373 } 374 375 return success; 289 //if (mode_.mode == GameMode::Client) 290 // network::Client::getSingleton()->closeConnection(); 291 292 //if (mode_.hasServer) 293 // server_g->close(); 376 294 } 377 295 … … 408 326 COUT(0) << "Loading level in server mode" << std::endl; 409 327 410 assert(Settings::getCommandLineArgument("port"));411 int serverPort = Settings::getCommandLineArgument("port")->value_;412 // server_g = new network::Server(serverPort_);413 server_g = network::Server::createSingleton(serverPort);328 //assert(Settings::getCommandLineArgument("port")); 329 //int serverPort = Settings::getCommandLineArgument("port")->value_; 330 ////server_g = new network::Server(serverPort_); 331 //server_g = network::Server::createSingleton(serverPort); 414 332 415 333 if (!loadScene()) … … 428 346 COUT(0) << "Loading level in client mode" << std::endl;\ 429 347 430 assert(Settings::getCommandLineArgument("port"));431 assert(Settings::getCommandLineArgument("ip"));432 int serverPort = Settings::getCommandLineArgument("port")->value_;433 std::string serverIP = Settings::getCommandLineArgument("ip")->value_;434 435 if (serverIP.compare("") == 0)436 client_g = network::Client::createSingleton();437 else438 client_g = network::Client::createSingleton(serverIP, serverPort);348 //assert(Settings::getCommandLineArgument("port")); 349 //assert(Settings::getCommandLineArgument("ip")); 350 //int serverPort = Settings::getCommandLineArgument("port")->value_; 351 //std::string serverIP = Settings::getCommandLineArgument("ip")->value_; 352 353 //if (serverIP.compare("") == 0) 354 // client_g = network::Client::createSingleton(); 355 //else 356 // client_g = network::Client::createSingleton(serverIP, serverPort); 439 357 440 358 if(!client_g->establishConnection()) … … 504 422 float dt = (timeBeforeTick - timeBeforeTickOld) / 1000000.0; 505 423 506 // check whether we have to load a game507 if (mode_.mode != modeRequest_.mode && mode_.mode == GameMode::Unspecified)508 {509 this->loadLevel(modeRequest_);510 this->modeRequest_ = GameMode::GM_None;511 }512 513 424 514 425 // tick the core (needs real time for input and tcl thread management) … … 552 463 ogreRoot._fireFrameStarted(evt); 553 464 554 if ( mode_.showsGraphics)465 if (Settings::showsGraphics()) 555 466 { 556 467 // Pump messages in all registered RenderWindows -
code/branches/gui/src/orxonox/Orxonox.h
r1654 r1663 74 74 Orxonox(Orxonox& instance); 75 75 76 bool loadLevel(const GameMode& mode);77 76 bool serverLoad(); 78 77 bool clientLoad(); … … 91 90 float timefactor_; //!< A factor to change the gamespeed 92 91 93 // this is used to identify the mode (server/client/...) we're in94 GameMode mode_;95 GameMode modeRequest_;96 97 92 // config values 98 93 float debugRefreshTime_; -
code/branches/gui/src/orxonox/OrxonoxPrereqs.h
r1653 r1663 75 75 class GraphicsEngine; 76 76 class Orxonox; 77 class Settings; 77 78 78 79 class RadarViewable; -
code/branches/gui/src/orxonox/Settings.cc
r1651 r1663 42 42 namespace orxonox 43 43 { 44 45 // Using a macro makes the above list much more readable. 46 // Settings::addGameMode adds the mode in a map, so we can access game modes by string. 47 #define CreateGameMode(name, showsGraphics, isMaster, hasServer) \ 48 const GameMode GameMode::GM_##name = { GameMode::name, showsGraphics, isMaster, hasServer, #name }; \ 49 bool temporaryVariable##name = Settings::addGameMode(&GameMode::GM_##name) 50 51 // showsGraphics isMaster hasServer 52 CreateGameMode(None, false, false, false); 53 CreateGameMode(Unspecified, true, false, false); 54 CreateGameMode(Server, true, true, true ); 55 CreateGameMode(Client, true, false, false); 56 CreateGameMode(Standalone, true, true, false); 57 CreateGameMode(Dedicated, false, true, true ); 44 Settings* Settings::singletonRef_s = 0; 58 45 59 46 /** … … 62 49 */ 63 50 Settings::Settings() 51 : bShowsGraphics_(false) 52 , bHasServer_(false) 64 53 { 65 54 RegisterRootObject(Settings); 66 gameMode_ = GameMode::GM_None; 55 assert(singletonRef_s == 0); 56 singletonRef_s = this; 67 57 setConfigValues(); 68 }69 70 /**71 @brief72 Returns a unique instance of Core.73 @return74 The instance75 */76 Settings& Settings::getInstance()77 {78 static Settings instance;79 return instance;80 58 } 81 59 … … 110 88 } 111 89 112 /**113 @brief114 Sets the game mode.115 */116 /*static*/ void Settings::setGameMode(const std::string& mode)117 {118 std::string modeL = getLowercase(mode);119 std::map<std::string, const GameMode*>::const_iterator it = getInstance().gameModes_.find(modeL);120 if (it != getInstance().gameModes_.end())121 getInstance().gameMode_ = *(*it).second;122 else123 {124 COUT(2) << "Warning: mode \"" << mode << "\" doesn't exist. "125 << "Defaulting to 'Standalone'" << std::endl;126 getInstance().gameMode_ = GameMode::GM_Standalone;127 }128 }129 130 /*static*/ bool Settings::addGameMode(const GameMode* mode)131 {132 getInstance().gameModes_[getLowercase(mode->name)] = mode;133 return true;134 }135 136 137 /**138 @brief139 Gets an argument from the command line by name.140 @return141 Is 0 if name was not found.142 */143 /*static*/ const Settings::CommandLineArgument* Settings::getCommandLineArgument(const std::string &name)144 {145 std::map<std::string, CommandLineArgument>::const_iterator it = getInstance().commandArguments_.find(name);146 if (it != getInstance().commandArguments_.end())147 {148 return &((*it).second);149 }150 else151 return 0;152 }153 154 90 } -
code/branches/gui/src/orxonox/Settings.h
r1638 r1663 47 47 namespace orxonox 48 48 { 49 /** 50 @brief 51 Defines a bit field structure that holds the mode as enum plus 52 the attributes as single named bits. 53 Every different GameMode is stored as static const, but if you wish to 54 compare two modes, you will have to use the 'mode' member variable. 55 */ 56 struct GameMode 49 class _OrxonoxExport Settings : public OrxonoxClass 57 50 { 58 enum Mode 59 { 60 None, 61 Unspecified, 62 Server, 63 Client, 64 Standalone, 65 Dedicated, 66 }; 51 friend class ClassIdentifier<Settings>; 52 friend class GSRoot; 67 53 68 Mode mode;69 bool showsGraphics;70 bool isMaster;71 bool hasServer;72 std::string name;54 public: 55 static const std::string& getDataPath() 56 { assert(singletonRef_s); return singletonRef_s->dataPath_; } 57 static void tsetDataPath(const std::string& path) 58 { assert(singletonRef_s); singletonRef_s->_tsetDataPath(path); } 73 59 74 static const GameMode GM_None; 75 static const GameMode GM_Unspecified; 76 static const GameMode GM_Server; 77 static const GameMode GM_Client; 78 static const GameMode GM_Standalone; 79 static const GameMode GM_Dedicated; 80 }; 60 // an alternative to a global game mode variable 61 static bool showsGraphics() { assert(singletonRef_s); return singletonRef_s->bShowsGraphics_; } 62 static bool hasServer() { assert(singletonRef_s); return singletonRef_s->bHasServer_; } 81 63 82 64 83 class _OrxonoxExport Settings : public OrxonoxClass 84 { 85 public: 86 struct CommandLineArgument 87 { 88 std::string name_; 89 MultiTypeMath value_; 90 bool bHasDefaultValue_; 91 }; 65 private: 66 // GSRoot has access to these 67 static void setShowsGraphics(bool val) { assert(singletonRef_s); singletonRef_s->bShowsGraphics_ = val; } 68 static void setHasServer (bool val) { assert(singletonRef_s); singletonRef_s->bHasServer_ = val; } 69 70 Settings(); 71 Settings(const Settings& instance); 72 ~Settings() { singletonRef_s = 0; } 73 74 static Settings& _getInstance() { assert(singletonRef_s); return *singletonRef_s; } 75 void _tsetDataPath(const std::string& path); 92 76 93 77 void setConfigValues(); 94 78 95 static const std::string& getDataPath();96 static void tsetDataPath(const std::string& path);79 bool bShowsGraphics_; //!< global variable that tells whether to show graphics 80 bool bHasServer_; //!< global variable that tells whether this is a server 97 81 98 static const GameMode& getGameMode(); 99 static const GameMode& getGameMode(const std::string& name); 100 static void setGameMode(const GameMode& mode); 101 static void setGameMode(const std::string& mode); 102 static bool addGameMode(const GameMode* mode); 82 std::string dataPath_; //!< Path to the game data 103 83 104 static const CommandLineArgument* getCommandLineArgument(const std::string& name); 105 template <class T> 106 static bool addCommandLineArgument(const std::string &name, const std::string& valueStr, const T& defaultValue); 107 108 private: 109 Settings(); 110 Settings(const Settings& instance); 111 ~Settings() { } 112 static Settings& getInstance(); 113 114 void _tsetDataPath(const std::string& path); 115 116 std::string dataPath_; //!< Path to the game data 117 GameMode gameMode_; //!< Current game mode 118 std::map<std::string, const GameMode*> gameModes_; //!< Holds all game modes for easy string access 119 //! holds all command line arguments (even if not given!) 120 std::map<std::string, CommandLineArgument> commandArguments_; 84 static Settings* singletonRef_s; //!< Static pointer to the only instance. 121 85 }; 122 86 123 /**124 @brief125 Returns the relative path to the game data.126 */127 inline const std::string& Settings::getDataPath()128 {129 return getInstance().dataPath_;130 }131 132 inline void Settings::tsetDataPath(const std::string& path)133 {134 getInstance()._tsetDataPath(path);135 }136 137 inline const GameMode& Settings::getGameMode()138 {139 return getInstance().gameMode_;140 }141 142 inline const GameMode& Settings::getGameMode(const std::string& name)143 {144 if (getInstance().gameModes_.find(name) != getInstance().gameModes_.end())145 return *getInstance().gameModes_[name];146 else147 {148 COUT(2) << "Warning: GameMode '" << name << "' doesn't exist." << std::endl;149 return GameMode::GM_None;150 }151 }152 153 inline void Settings::setGameMode(const GameMode& mode)154 {155 getInstance().gameMode_ = mode;156 }157 158 /**159 @brief160 Adds one argument of the command line to the map of command line arguments.161 @param name162 Name of the command line option.163 @param valueStr164 The value of the command line option as string165 @param defaultValue166 Default value for the option (marked when used).167 @return168 Dummy return value to enable code execution before main().169 */170 template <class T>171 bool Settings::addCommandLineArgument(const std::string &name, const std::string& valueStr, const T& defaultValue)172 {173 T value;174 bool useDefault = false;175 if (valueStr == "")176 {177 // note: ArgReader only returns "" for not found arguments, " " otherwise for empty ones.178 value = defaultValue;179 useDefault = true;180 }181 else if (!convertValue(&value, valueStr))182 {183 COUT(1) << "Command Line: Couldn't read option '" << name << "'." << std::endl;184 return false;185 }186 CommandLineArgument arg = { name, MultiTypeMath(value), useDefault };187 getInstance().commandArguments_[name] = arg;188 return true;189 }190 87 } 191 88 -
code/branches/gui/src/orxonox/gamestates/GSLevel.cc
r1662 r1663 35 35 #include "core/input/KeyBinder.h" 36 36 #include "core/Loader.h" 37 #include "core/CommandLine.h" 37 38 #include "overlays/console/InGameConsole.h" 38 39 #include "gui/GUIManager.h" … … 45 46 namespace orxonox 46 47 { 48 SetCommandLineArgument(port, 55556).setShortcut("p"); 49 SetCommandLineArgument(ip, std::string("127.0.0.0")); 50 47 51 GSLevel::GSLevel() 48 52 : GameState("level") -
code/branches/gui/src/orxonox/gamestates/GSRoot.cc
r1662 r1663 38 38 #include "core/TclBind.h" 39 39 #include "core/Core.h" 40 #include "core/CommandLine.h" 40 41 #include "GraphicsEngine.h" 41 42 #include "Settings.h" … … 43 44 namespace orxonox 44 45 { 46 SetCommandLineArgument(dataPath, std::string("./")); 47 45 48 GSRoot::GSRoot() 46 49 : GameState("root") 50 , settings_(0) 47 51 , graphicsEngine_(0) 48 52 { … … 64 68 Factory::createClassHierarchy(); 65 69 66 const Settings::CommandLineArgument* dataPath = Settings::getCommandLineArgument("dataPath"); 67 assert(dataPath); 68 if (!dataPath->bHasDefaultValue_) 70 // instantiate Settings class 71 this->settings_ = new Settings(); 72 73 const CommandLineArgument<std::string>* dataPath = CommandLine::getCommandLineArgument<std::string>("dataPath"); 74 if (!dataPath->hasDefaultValue()) 69 75 { 70 if (*dataPath-> value_.getString().end() != '/' && *dataPath->value_.getString().end() != '\\')71 Settings::tsetDataPath(dataPath-> value_.getString() + "/");76 if (*dataPath->getValue().end() != '/' && *dataPath->getValue().end() != '\\') 77 Settings::tsetDataPath(dataPath->getValue() + "/"); 72 78 else 73 Settings::tsetDataPath(dataPath-> value_.getString());79 Settings::tsetDataPath(dataPath->getValue()); 74 80 } 75 81 … … 89 95 void GSRoot::leave() 90 96 { 91 if (this->graphicsEngine_) 92 delete graphicsEngine_; 97 delete graphicsEngine_; 98 99 delete settings_; 93 100 } 94 101 -
code/branches/gui/src/orxonox/gamestates/GSRoot.h
r1662 r1663 48 48 void leave(); 49 49 50 Settings* settings_; 50 51 GraphicsEngine* graphicsEngine_; //!< our dearest graphics engine <3 51 52 }; -
code/branches/gui/visual_studio/vc8/core.vcproj
r1660 r1663 245 245 </File> 246 246 <File 247 RelativePath="..\..\src\core\CommandLine.cc" 248 > 249 </File> 250 <File 247 251 RelativePath="..\..\src\core\ConsoleCommand.cc" 248 252 > … … 507 511 </File> 508 512 <File 513 RelativePath="..\..\src\core\CommandLine.h" 514 > 515 </File> 516 <File 509 517 RelativePath="..\..\src\core\ConsoleCommand.h" 510 518 >
Note: See TracChangeset
for help on using the changeset viewer.