diff --git a/SDK/include/NDK/Application.hpp b/SDK/include/NDK/Application.hpp index 7f4af6679..84b331e95 100644 --- a/SDK/include/NDK/Application.hpp +++ b/SDK/include/NDK/Application.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -35,7 +36,7 @@ namespace Ndk #endif inline Application(); - inline Application(int argc, const char* argv[]); + Application(int argc, char* argv[]); Application(const Application&) = delete; Application(Application&&) = delete; inline ~Application(); @@ -53,8 +54,14 @@ namespace Ndk inline FPSCounterOverlay& GetFPSCounterOverlay(std::size_t windowIndex = 0U); #endif + inline const std::set& GetOptions() const; + inline const std::map& GetParameters() const; + inline float GetUpdateTime() const; + inline bool HasOption(const Nz::String& option) const; + inline bool HasParameter(const Nz::String& key, Nz::String* value) const; + #ifndef NDK_SERVER inline bool IsConsoleEnabled() const; inline bool IsFPSCounterEnabled() const; @@ -120,6 +127,8 @@ namespace Ndk template void SetupWindow(WindowInfo& info, T* renderTarget, std::true_type /*isRenderTarget*/); template void SetupWindow(WindowInfo& /*info*/, T* /*renderTarget*/, std::false_type /*isNotRenderTarget*/); + std::map m_parameters; + std::set m_options; std::vector m_windows; #endif std::list m_worlds; diff --git a/SDK/include/NDK/Application.inl b/SDK/include/NDK/Application.inl index a36ee3a13..7701160c5 100644 --- a/SDK/include/NDK/Application.inl +++ b/SDK/include/NDK/Application.inl @@ -33,22 +33,6 @@ namespace Ndk Sdk::Initialize(); } - /*! - * \brief Constructs an Application object with command-line arguments - * - * Pass the argc and argv arguments from the main function. - * - * Command-line arguments can be retrieved by application methods - * - * This calls Sdk::Initialize() - * - * \remark Only one Application instance can exist at a time - */ - inline Application::Application(int argc, const char* argv[]) : - Application() - { - } - /*! * \brief Destructs the application object * @@ -233,6 +217,30 @@ namespace Ndk } #endif + /*! + * \brief Gets the options used to start the application + * + * Options are defined as "-optionName" in command-line and are always lower-case + * + * \return Command-line options + */ + inline const std::set& Application::GetOptions() const + { + return m_options; + } + + /*! + * \brief Gets the parameters used to start the application + * + * Parameters are defined as "-key=value" in command-line, their key is lower-case but value capitals are kept. + * + * \return Command-line parameters + */ + inline const std::map& Application::GetParameters() const + { + return m_parameters; + } + /*! * \brief Gets the update time of the application * \return Update rate @@ -242,6 +250,46 @@ namespace Ndk return m_updateTime; } + /*! + * \brief Query for a command-line option + * + * \param option Option name + * + * \remark option must be lower-case + * + * \return True if option is present + * + * \see GetOptions + */ + inline bool Application::HasOption(const Nz::String& option) const + { + return m_options.count(option) != 0; + } + + /*! + * \brief Query for a command-line option + * + * \param key Parameter name + * \param value Optional string to receive the parameter value + * + * \remark key must be lower-case + * + * \return True if parameter is present + * + * \see GetParameters + */ + inline bool Application::HasParameter(const Nz::String& key, Nz::String* value) const + { + auto it = m_parameters.find(key); + if (it == m_parameters.end()) + return false; + + if (value) + *value = it->second; + + return true; + } + /*! * \brief Checks if the console overlay is enabled * diff --git a/SDK/src/NDK/Application.cpp b/SDK/src/NDK/Application.cpp index bce62093a..71301a13b 100644 --- a/SDK/src/NDK/Application.cpp +++ b/SDK/src/NDK/Application.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Prerequesites.hpp #include +#include #ifndef NDK_SERVER #include @@ -21,6 +22,54 @@ namespace Ndk * \brief NDK class that represents the application, it offers a set of tools to ease the development */ + /*! + * \brief Constructs an Application object with command-line arguments + * + * Pass the argc and argv arguments from the main function. + * + * Command-line arguments can be retrieved by application methods + * + * This calls Sdk::Initialize() + * + * \remark Only one Application instance can exist at a time + */ + inline Application::Application(int argc, char* argv[]) : + Application() + { + std::regex optionRegex(R"(-(\w+))"); + std::regex valueRegex(R"(-(\w+)\s*=\s*(.+))"); + + std::smatch results; + + for (int i = 1; i < argc; ++i) + { + std::string argument(argv[i]); + if (std::regex_match(argument, results, valueRegex)) + { + Nz::String key(results[1].str()); + Nz::String value(results[2].str()); + + m_parameters[key.ToLower()] = value; + NazaraDebug("Registred parameter from command-line: " + key.ToLower() + "=" + value); + } + else if (std::regex_match(argument, results, optionRegex)) + { + Nz::String option(results[1].str()); + + m_options.insert(option); + NazaraDebug("Registred option from command-line: " + option); + } + else + NazaraWarning("Ignored command-line argument #" + Nz::String::Number(i) + " \"" + argument + '"'); + } + + if (HasOption("console")) + EnableConsole(true); + + if (HasOption("fpscounter")) + EnableFPSCounter(true); + } + /*! * \brief Runs the application by updating worlds, taking care about windows, ... */ diff --git a/examples/Tut00/main.cpp b/examples/Tut00/main.cpp index 472db3148..cfffa4c59 100644 --- a/examples/Tut00/main.cpp +++ b/examples/Tut00/main.cpp @@ -9,16 +9,14 @@ #include #include #include -#include -#include -int main() +int main(int argc, char* argv[]) { - // This "example" has only one purpose: Giving an empty project for you to test whatever you want - // If you wish to have multiple test projects, you only have to copy/paste this directory and change the name in the build.lua - Ndk::Application app; + // This "example" has only one purpose: Giving an empty project for you to test whatever you want + // If you wish to have multiple test projects, you only have to copy/paste this directory and change the name in the build.lua + Ndk::Application application(argc, argv); - // Do what you want here - - return EXIT_SUCCESS; + // Do what you want here + + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/examples/Tut01/main.cpp b/examples/Tut01/main.cpp index f5fca94a0..9c33f6859 100644 --- a/examples/Tut01/main.cpp +++ b/examples/Tut01/main.cpp @@ -9,9 +9,9 @@ #include #include -int main() +int main(int argc, char* argv[]) { - Ndk::Application application; + Ndk::Application application(argc, argv); Nz::RenderWindow& mainWindow = application.AddWindow(); mainWindow.Create(Nz::VideoMode(800, 600, 32), "Test");