From 98ba22e7064db57316dfff1ae127feb3dceeb73e Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Thu, 31 Jul 2014 13:58:22 +0200 Subject: Initial commit --- source/data/Config.h | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 source/data/Config.h (limited to 'source/data/Config.h') diff --git a/source/data/Config.h b/source/data/Config.h new file mode 100644 index 0000000..8fdf5aa --- /dev/null +++ b/source/data/Config.h @@ -0,0 +1,136 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include +#include +#include +#include +using namespace std; + +/** +* @brief Object for handling user settings loaded from a file. +* +* These settings are currently all related to the screen (resolution and windowed or fullscreen). +* If custom controls are implemented in a later release, they will be loaded here. If a setting exists +* but is not found in the config file, it is set to a default value and written to the file. +* +* @author Justin Wernick +* @author David Schneider +*/ +class Config +{ + public: + /** + * @brief Constructs a Config object from a file with the given path. + * + * Opens the file and reads all of the settings in the file. The read settings are + * bound to the settings that have keys defined in the class. And settings missing from + * the file are set to default values, and the default values are written to the file. + * + * @param [in] filename The path of the file in which the settings are stored. + */ + Config(const string& filename); + + //Assignment and copy operations are handled by the compiler generated versions + + /** + * @brief Function for accessing the screen width setting in pixels. + * + * @return The desired width of the screen in pixels. + */ + unsigned int screenWidth() const; + + /** + * @brief Function for accessing the screen height setting in pixels. + * + * @return The desired height of the screen in pixels. + */ + unsigned int screenHeight() const; + + /** + * @brief Function for accessing whether the game should be displayed in fullscreen or windowed mode. + * + * @return The desired fullscreen setting. A result of true means fullscreen mode, while a result of false means windowed mode. + */ + bool fullscreen() const; + + private: + /** + * @brief Reads all of the settings defined in a file into a map. + * + * Reads each line that is in the format "key=value" in a file into a map. + * Lines that do not contain a '=' are ignored. + * + * @param [in] file An opened filestream object at the beginning of the file to be read. After the function call, file will be at the end of the file. + * @param [out] map The map that is populated with settings. + */ + void readFile(ifstream& file, map& map); + + /** + * @brief Initialises the Config option's parameters to those in the readValues map. + * + * Parameters with a key that does not appear in readValues are initialised to a default value. + * The default value, and its key, are then added to the unfoundValues map. + * + * @param [in] readValues A map containing all of the settings read in from a config file. + * @param [out] unfoundValues A map that is populated with and settings not found in readValues. + */ + void fillValues(const map& readValues, map& unfoundValues); + + /** + * @brief Helper function for fillValues. Finds the value for a single key. + * + * If the given key does not appear, it is added to the unfoundValues map with the given default + * + * @param [in] readValues A map containing all of the settings read in from a config file. + * @param [out] unfoundValues A map that is populated with and settings not found in readValues. + * @param [in] key The key of the setting to be found in readValues. + * @param [in] defaultValue The value to return and add to unfoundValues if the setting is not found in readValues. + * + * @return The value corresponding to the requested key. + */ + string extractValue(const map& readValues, map& unfoundValues, const string& key, const string& defaultValue); + + /** + * @brief Writes settings that were not found in the file to the file with default values. + * + * @param [in] file The opened filestream to which the key=value pairs are written. + * @param [in] unfoundValues The map of key value pairs to be written to the file. + */ + void writeUnfoundValues(ofstream& file, const map& unfoundValues); + + /** + * @brief Initializes the screen width in pixels from a given string. + * + * @param [in] screenWidthStr A string representing the desired screen width, read from a file. + */ + void setScreenWidth(const string& screenWidthStr); + + /** + * @brief Initializes the screen height in pixels from a given string. + * + * @param [in] screenHeightStr A string representing the desired screen height, read from a file. + */ + void setScreenHeight(const string& screenHeightStr); + + /** + * @brief Initializes the fullscreen setting from a given string. + * + * @param [in] fullscreenStr A string representing whether the screen should be in fullscreen mode ("true") or windowed mode (anything else). + */ + void setFullscreen(const string& fullscreenStr); + + unsigned int _screenWidth; ///< The desired width of the screen in pixels. + unsigned int _screenHeight; ///< The desired height of the screen in pixels. + bool _fullscreen; ///< The desired fullscreen or windowed setting. + + static const string SCREEN_WIDTH_KEY; ///< The key for the screen width setting, initialized to "screen_width". + static const string SCREEN_HEIGHT_KEY; ///< The key for the screen height setting, initialized to "screen_height". + static const string FULLSCREEN_KEY; ///< The key for the fullscreen setting, initialized to "fullscreen". + + static const string SCREEN_WIDTH_DEFAULT; ///< The default value for the screen width setting, initialized to 800. + static const string SCREEN_HEIGHT_DEFAULT; ///< The default value for the screen height setting, initialized to 600. + static const string FULLSCREEN_DEFAULT; ///< The default value for the fullscreen setting, initialized to false. +}; + +#endif // CONFIG_H -- cgit v1.2.3