summaryrefslogtreecommitdiff
path: root/source/data/Config.h
blob: 8fdf5aa058da8839fd28d834bf0d32e5b8d1723f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifndef CONFIG_H
#define CONFIG_H

#include <cstdlib>
#include <string>
#include <fstream>
#include <map>
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<string,string>& 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<string, string>& readValues, map<string, string>& 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<string, string>& readValues, map<string, string>& 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<string, string>& 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