Rally X
ELEN3009 Project by Justin Wernick and David Schneider
|
Object for handling user settings loaded from a file. More...
#include <Config.h>
Public Member Functions | |
Config (const string &filename) | |
Constructs a Config object from a file with the given path. | |
unsigned int | screenWidth () const |
Function for accessing the screen width setting in pixels. | |
unsigned int | screenHeight () const |
Function for accessing the screen height setting in pixels. | |
bool | fullscreen () const |
Function for accessing whether the game should be displayed in fullscreen or windowed mode. | |
Private Member Functions | |
void | readFile (ifstream &file, map< string, string > &map) |
Reads all of the settings defined in a file into a map. | |
void | fillValues (const map< string, string > &readValues, map< string, string > &unfoundValues) |
Initialises the Config option's parameters to those in the readValues map. | |
string | extractValue (const map< string, string > &readValues, map< string, string > &unfoundValues, const string &key, const string &defaultValue) |
Helper function for fillValues. Finds the value for a single key. | |
void | writeUnfoundValues (ofstream &file, const map< string, string > &unfoundValues) |
Writes settings that were not found in the file to the file with default values. | |
void | setScreenWidth (const string &screenWidthStr) |
Initializes the screen width in pixels from a given string. | |
void | setScreenHeight (const string &screenHeightStr) |
Initializes the screen height in pixels from a given string. | |
void | setFullscreen (const string &fullscreenStr) |
Initializes the fullscreen setting from a given string. | |
Private Attributes | |
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 Private Attributes | |
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. |
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.
Config::Config | ( | const string & | filename | ) |
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.
[in] | filename | The path of the file in which the settings are stored. |
Definition at line 12 of file Config.cpp.
string Config::extractValue | ( | const map< string, string > & | readValues, |
map< string, string > & | unfoundValues, | ||
const string & | key, | ||
const string & | defaultValue | ||
) | [private] |
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
[in] | readValues | A map containing all of the settings read in from a config file. |
[out] | unfoundValues | A map that is populated with and settings not found in readValues. |
[in] | key | The key of the setting to be found in readValues. |
[in] | defaultValue | The value to return and add to unfoundValues if the setting is not found in readValues. |
Definition at line 72 of file Config.cpp.
void Config::fillValues | ( | const map< string, string > & | readValues, |
map< string, string > & | unfoundValues | ||
) | [private] |
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.
[in] | readValues | A map containing all of the settings read in from a config file. |
[out] | unfoundValues | A map that is populated with and settings not found in readValues. |
Definition at line 65 of file Config.cpp.
bool Config::fullscreen | ( | ) | const |
Function for accessing whether the game should be displayed in fullscreen or windowed mode.
Definition at line 40 of file Config.cpp.
void Config::readFile | ( | ifstream & | file, |
map< string, string > & | map | ||
) | [private] |
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.
[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. |
[out] | map | The map that is populated with settings. |
Definition at line 45 of file Config.cpp.
unsigned int Config::screenHeight | ( | ) | const |
Function for accessing the screen height setting in pixels.
Definition at line 36 of file Config.cpp.
unsigned int Config::screenWidth | ( | ) | const |
Function for accessing the screen width setting in pixels.
Definition at line 32 of file Config.cpp.
void Config::setFullscreen | ( | const string & | fullscreenStr | ) | [private] |
Initializes the fullscreen setting from a given string.
[in] | fullscreenStr | A string representing whether the screen should be in fullscreen mode ("true") or windowed mode (anything else). |
Definition at line 102 of file Config.cpp.
void Config::setScreenHeight | ( | const string & | screenHeightStr | ) | [private] |
Initializes the screen height in pixels from a given string.
[in] | screenHeightStr | A string representing the desired screen height, read from a file. |
Definition at line 98 of file Config.cpp.
void Config::setScreenWidth | ( | const string & | screenWidthStr | ) | [private] |
Initializes the screen width in pixels from a given string.
[in] | screenWidthStr | A string representing the desired screen width, read from a file. |
Definition at line 94 of file Config.cpp.
void Config::writeUnfoundValues | ( | ofstream & | file, |
const map< string, string > & | unfoundValues | ||
) | [private] |
Writes settings that were not found in the file to the file with default values.
[in] | file | The opened filestream to which the key=value pairs are written. |
[in] | unfoundValues | The map of key value pairs to be written to the file. |
Definition at line 86 of file Config.cpp.
bool Config::_fullscreen [private] |
unsigned int Config::_screenHeight [private] |
unsigned int Config::_screenWidth [private] |
const string Config::FULLSCREEN_DEFAULT [static, private] |
const string Config::FULLSCREEN_KEY [static, private] |
const string Config::SCREEN_HEIGHT_DEFAULT [static, private] |
const string Config::SCREEN_HEIGHT_KEY [static, private] |
const string Config::SCREEN_WIDTH_DEFAULT [static, private] |
const string Config::SCREEN_WIDTH_KEY [static, private] |