File: common.h

package info (click to toggle)
blockattack 1.4.1%2Bds1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 6,260 kB
  • ctags: 1,038
  • sloc: cpp: 9,461; ansic: 142; makefile: 38
file content (164 lines) | stat: -rw-r--r-- 4,344 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Block Attack - Rise of the Blocks, SDL game, besed on Nintendo's Tetris Attack
Copyright (C) 2008 Poul Sander

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Poul Sander
    R�vehjvej 36, V. 1111
    2800 Kgs. Lyngby
    DENMARK
    blockattack@poulsander.com
    http://blockattack.sf.net
*/

/*
 *This is the common.h
 *It contains some basic functions that nearly all multiplatform games are going
 *to need.
 */

#ifndef _COMMON_H
#define	_COMMON_H

/*
 *Files will be saved in:
 * HOME/.gamesaves/"+GAMENAME (unix)
 *or DOCUMENTS/My Games/GAMENAME (Windows)
 */
#define GAMENAME "blockattack"

#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include <stdlib.h>
#ifdef WIN32
#include "windows.h"
#include "shlobj.h"
#endif

using namespace std;

struct commonTime{
    unsigned int days;
    unsigned int hours;
    unsigned int minutes;
    unsigned int seconds;
};

string itoa(int num);

string getPathToSaveFiles();

#ifdef WIN32
string getMyDocumentsPath();
#endif

class TimeHandler
{
public:
	static commonTime ms2ct(unsigned int milliseconds);
	
	static commonTime getTime(string name);
	
	static commonTime peekTime(string name, commonTime toAdd);
	
	static commonTime addTime(string name, commonTime toAdd);
};

#define MAX_VAR_LENGTH 1024

/*This is the Config class it is a map to hold config variables.
 *It is inspired by Quake 3's CVAR system although a lot simpler.
 *All variables have keys "varName" that is used to access a variable.
 *
 *This class is a singleton
 */
class Config
{
private: 
    map<string,string> configMap;

    static Config *instance;
    
    void load();
protected:
    
    Config();
    
    
public:
    /*Config is a singleton.
     *It is accessed like this:
     *Config::getInstance()->method2call(paramters);
     */
    static Config* getInstance();
    
    /*save()
     *forces the config to be written to disk. This will also happened if the
     *program terminates normally.
     */
    void save();
    
    /*getString(varName)
     *Looks in the config file and returns the string that matches the key "varName"
     *Returns an empty string if varName does not exist.
     */
    string getString(string varName);
    
    /*getInt(varName)
     *Looks in the config file and returns the int that matches the key "varName"
     *Returns "0" if varName does not exist or cannot be parsed.
     */
    int getInt(string varName);
    
    /*getValue(varName)
     *Looks in the config file and returns the double that matches the key "varName"
     *Returns "0.0" if varName does not exist or cannot be parsed.
     */
    double getValue(string varName);
    
    /*setString(varName,content)
     *Sets the config variable with key "varName" to the value of "content"
     */
    void setString(string varName,string content);
    
    /*setInt(varName,content)
     *Sets the config variable with key "varName" to the value of "content"
     */
    void setInt(string varName,int content);
    
    /*setValue(varName,content)
     *Sets the config variable with key "varName" to the value of "content"
     */
    void setValue(string varName,double content);
    
    /*exists(varName)
     *returns true if the key varName exists. This is used the first time 1.4.0
     *starts so that it can see that it has to import configs from an earlier 
     *version.
     */
    bool exists(string varName);
    
    /*setDefault(varName,value)
     *if the variable "varName" does not exist it will be created with value "value"
     *if varName exists then this will have no effect
     */
    void setDefault(string varName, string content);
};

#endif	/* _COMMON_H */