forked from Milez213/CityTrial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameSettings.h
114 lines (85 loc) · 1.96 KB
/
GameSettings.h
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
/*
* GameSettings.h
* Author: Mustafa khafateh
*
* Date: 2/6/2013
*
* Stores game options/settings, such as if playing music, screen resolution,
* etc.
*
*
* load: settings.load("settings file.ini")
*
*
* settings file has the format:
*
* property = value
*
* property is a string, value is an int now.
*
*
* to get properties
* settings["property"]
*
* if it doesn't exist, it returns -1
*
*
*/
#ifndef GAME_SETTINGS_H
#define GAME_SETTINGS_H
#include <map>
#include <string>
#include <stdio.h>
#include <iostream>
#include <fstream>
using std::map;
using std::string;
typedef int propertyType;
class GameSettings {
public:
// empty settings
GameSettings() {
m_settings["one"] = 1; // for sanity checking
m_isLoaded = false;
}
// load game settings
GameSettings(const char* fname) {
m_isLoaded = load(fname);
}
bool save(const char* fname);
bool load(const char* fname) {
ifstream in(fname, ios::in);
if (!in) {
fprintf(stderr, "Couldn't open %s for reading the map\n", fname);
return false;
} else {
string key;
propertyType value;
string equals;
while (!in.eof()) {
in >> key >> equals >> value;
m_settings[key] = value;
}
}
in.close();
return true;
}
// returns element. -1 if it doesn't exist
propertyType get(string property) {
map<string, propertyType>::iterator it;
it = m_settings.find(property);
if (it == m_settings.end()) {
fprintf(stderr, "GameSettings.get(): couldn't find property '%s'\n",
property.c_str());
return -1;
}
return it->second;
}
propertyType operator[] (const string& key) {
return get(key);
}
private:
bool m_isLoaded;
map<string, propertyType> m_settings;
};
#endif