git repos / blockattack-game

blame: source/code/common.h

normal view · raw

89c4a3a6 sago007 2008-08-29 14:32 1
/*
c53e6443 sago007 2012-04-17 11:04 2
===========================================================================
c53e6443 sago007 2012-04-17 11:04 3
blockattack - Block Attack - Rise of the Blocks
c53e6443 sago007 2012-04-17 11:04 4
Copyright (C) 2005-2012 Poul Sander
c53e6443 sago007 2012-04-17 11:04 5
c53e6443 sago007 2012-04-17 11:04 6
This program is free software: you can redistribute it and/or modify
c53e6443 sago007 2012-04-17 11:04 7
it under the terms of the GNU General Public License as published by
c53e6443 sago007 2012-04-17 11:04 8
the Free Software Foundation, either version 2 of the License, or
c53e6443 sago007 2012-04-17 11:04 9
(at your option) any later version.
c53e6443 sago007 2012-04-17 11:04 10
c53e6443 sago007 2012-04-17 11:04 11
This program is distributed in the hope that it will be useful,
c53e6443 sago007 2012-04-17 11:04 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
c53e6443 sago007 2012-04-17 11:04 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c53e6443 sago007 2012-04-17 11:04 14
GNU General Public License for more details.
c53e6443 sago007 2012-04-17 11:04 15
c53e6443 sago007 2012-04-17 11:04 16
You should have received a copy of the GNU General Public License
c53e6443 sago007 2012-04-17 11:04 17
along with this program.  If not, see http://www.gnu.org/licenses/
c53e6443 sago007 2012-04-17 11:04 18
c53e6443 sago007 2012-04-17 11:04 19
Source information and contacts persons can be found at
c53e6443 sago007 2012-04-17 11:04 20
http://blockattack.sf.net
c53e6443 sago007 2012-04-17 11:04 21
===========================================================================
89c4a3a6 sago007 2008-08-29 14:32 22
*/
89c4a3a6 sago007 2008-08-29 14:32 23
fcd5a134 sago007 2008-12-19 01:11 24
/*
fcd5a134 sago007 2008-12-19 01:11 25
 *This is the common.h
fcd5a134 sago007 2008-12-19 01:11 26
 *It contains some basic functions that nearly all multiplatform games are going
fcd5a134 sago007 2008-12-19 01:11 27
 *to need.
fcd5a134 sago007 2008-12-19 01:11 28
 */
fcd5a134 sago007 2008-12-19 01:11 29
89c4a3a6 sago007 2008-08-29 14:32 30
#ifndef _COMMON_H
89c4a3a6 sago007 2008-08-29 14:32 31
#define	_COMMON_H
89c4a3a6 sago007 2008-08-29 14:32 32
fcd5a134 sago007 2008-12-19 01:11 33
/*
fcd5a134 sago007 2008-12-19 01:11 34
 *Files will be saved in:
fcd5a134 sago007 2008-12-19 01:11 35
 * HOME/.gamesaves/"+GAMENAME (unix)
fcd5a134 sago007 2008-12-19 01:11 36
 *or DOCUMENTS/My Games/GAMENAME (Windows)
fcd5a134 sago007 2008-12-19 01:11 37
 */
89c4a3a6 sago007 2008-08-29 14:32 38
#define GAMENAME "blockattack"
89c4a3a6 sago007 2008-08-29 14:32 39
89c4a3a6 sago007 2008-08-29 14:32 40
#include <string>
89c4a3a6 sago007 2008-08-29 14:32 41
#include <iostream>
89c4a3a6 sago007 2008-08-29 14:32 42
#include <fstream>
769a41a0 sago007 2008-09-24 12:54 43
#include <map>
89c4a3a6 sago007 2008-08-29 14:32 44
#include <stdlib.h>
c9dd376f sago007 2012-01-20 17:13 45
#include <libintl.h>
c9dd376f sago007 2012-01-20 17:13 46
#include <boost/format.hpp>
eec83b7d sago007 2009-03-29 15:46 47
#ifdef WIN32
eec83b7d sago007 2009-03-29 15:46 48
#include "windows.h"
eec83b7d sago007 2009-03-29 15:46 49
#include "shlobj.h"
eec83b7d sago007 2009-03-29 15:46 50
#endif
89c4a3a6 sago007 2008-08-29 14:32 51
c9dd376f sago007 2012-01-20 17:13 52
#define _(String) gettext (String)
c9dd376f sago007 2012-01-20 17:13 53
89c4a3a6 sago007 2008-08-29 14:32 54
using namespace std;
c9dd376f sago007 2012-01-20 17:13 55
using boost::format;
89c4a3a6 sago007 2008-08-29 14:32 56
c53e6443 sago007 2012-04-17 11:04 57
struct commonTime
c53e6443 sago007 2012-04-17 11:04 58
{
c53e6443 sago007 2012-04-17 11:04 59
	unsigned int days;
c53e6443 sago007 2012-04-17 11:04 60
	unsigned int hours;
c53e6443 sago007 2012-04-17 11:04 61
	unsigned int minutes;
c53e6443 sago007 2012-04-17 11:04 62
	unsigned int seconds;
89c4a3a6 sago007 2008-08-29 14:32 63
};
89c4a3a6 sago007 2008-08-29 14:32 64
cc3ef158 sago007 2011-07-03 16:13 65
string itoa(int num) __attribute__((const));
769a41a0 sago007 2008-09-24 12:54 66
cc3ef158 sago007 2011-07-03 16:13 67
string getPathToSaveFiles() __attribute__((pure));
89c4a3a6 sago007 2008-08-29 14:32 68
c9dd376f sago007 2012-01-20 17:13 69
c65f7c8d sago007 2010-02-22 19:27 70
/**
c65f7c8d sago007 2010-02-22 19:27 71
 * str2int parses a string and returns an int with the value of the string.
c65f7c8d sago007 2010-02-22 19:27 72
 * if the string is not an int then 0 is returned instead of throing an error
c65f7c8d sago007 2010-02-22 19:27 73
 * in that way this function will always return a useable value.
c65f7c8d sago007 2010-02-22 19:27 74
 */
af35c39d sago007 2013-11-16 16:37 75
int str2int(const string &str2parse) __attribute__((const));
cc3ef158 sago007 2011-07-03 16:13 76
cc3ef158 sago007 2011-07-03 16:13 77
string double2str(double num) __attribute__((const));
cc3ef158 sago007 2011-07-03 16:13 78
cc3ef158 sago007 2011-07-03 16:13 79
/**
cc3ef158 sago007 2011-07-03 16:13 80
 * str2double parses a string and returns a double with the value of the string.
cc3ef158 sago007 2011-07-03 16:13 81
 * if the string is not a double then 0.0 is returned instead of throing an error
cc3ef158 sago007 2011-07-03 16:13 82
 * in that way this function will always return a useable value.
cc3ef158 sago007 2011-07-03 16:13 83
 */
af35c39d sago007 2013-11-16 16:37 84
double str2double(const string &str2parse) __attribute__((const));
c65f7c8d sago007 2010-02-22 19:27 85
eec83b7d sago007 2009-03-29 15:46 86
#ifdef WIN32
eec83b7d sago007 2009-03-29 15:46 87
string getMyDocumentsPath();
eec83b7d sago007 2009-03-29 15:46 88
#endif
eec83b7d sago007 2009-03-29 15:46 89
cd12e0fe sago007 2009-01-29 08:47 90
class TimeHandler
cd12e0fe sago007 2009-01-29 08:47 91
{
cd12e0fe sago007 2009-01-29 08:47 92
public:
cd12e0fe sago007 2009-01-29 08:47 93
	static commonTime ms2ct(unsigned int milliseconds);
c53e6443 sago007 2012-04-17 11:04 94
af35c39d sago007 2013-11-16 16:37 95
	static commonTime getTime(const string &name);
c53e6443 sago007 2012-04-17 11:04 96
af35c39d sago007 2013-11-16 16:37 97
	static commonTime peekTime(const string &name, const commonTime &toAdd);
c53e6443 sago007 2012-04-17 11:04 98
af35c39d sago007 2013-11-16 16:37 99
	static commonTime addTime(const string &name, const commonTime &toAdd);
cd12e0fe sago007 2009-01-29 08:47 100
};
89c4a3a6 sago007 2008-08-29 14:32 101
769a41a0 sago007 2008-09-24 12:54 102
#define MAX_VAR_LENGTH 1024
769a41a0 sago007 2008-09-24 12:54 103
fcd5a134 sago007 2008-12-19 01:11 104
/*This is the Config class it is a map to hold config variables.
fcd5a134 sago007 2008-12-19 01:11 105
 *It is inspired by Quake 3's CVAR system although a lot simpler.
fcd5a134 sago007 2008-12-19 01:11 106
 *All variables have keys "varName" that is used to access a variable.
fcd5a134 sago007 2008-12-19 01:11 107
 *
fcd5a134 sago007 2008-12-19 01:11 108
 *This class is a singleton
fcd5a134 sago007 2008-12-19 01:11 109
 */
769a41a0 sago007 2008-09-24 12:54 110
class Config
769a41a0 sago007 2008-09-24 12:54 111
{
c53e6443 sago007 2012-04-17 11:04 112
private:
c53e6443 sago007 2012-04-17 11:04 113
	map<string,string> configMap;
c53e6443 sago007 2012-04-17 11:04 114
c53e6443 sago007 2012-04-17 11:04 115
	static Config *instance;
c53e6443 sago007 2012-04-17 11:04 116
c53e6443 sago007 2012-04-17 11:04 117
	void load();
c53e6443 sago007 2012-04-17 11:04 118
c53e6443 sago007 2012-04-17 11:04 119
	/* tells if the user has requested a shutdown */
c53e6443 sago007 2012-04-17 11:04 120
	long shuttingDown;
769a41a0 sago007 2008-09-24 12:54 121
protected:
c53e6443 sago007 2012-04-17 11:04 122
c53e6443 sago007 2012-04-17 11:04 123
	Config();
c53e6443 sago007 2012-04-17 11:04 124
c53e6443 sago007 2012-04-17 11:04 125
769a41a0 sago007 2008-09-24 12:54 126
public:
c53e6443 sago007 2012-04-17 11:04 127
	/*Config is a singleton.
c53e6443 sago007 2012-04-17 11:04 128
	 *It is accessed like this:
c53e6443 sago007 2012-04-17 11:04 129
	 *Config::getInstance()->method2call(paramters);
c53e6443 sago007 2012-04-17 11:04 130
	 */
c53e6443 sago007 2012-04-17 11:04 131
	static Config* getInstance();
c53e6443 sago007 2012-04-17 11:04 132
c53e6443 sago007 2012-04-17 11:04 133
	/*save()
c53e6443 sago007 2012-04-17 11:04 134
	 *forces the config to be written to disk. This will also happened if the
c53e6443 sago007 2012-04-17 11:04 135
	 *program terminates normally.
c53e6443 sago007 2012-04-17 11:04 136
	 */
c53e6443 sago007 2012-04-17 11:04 137
	void save();
c53e6443 sago007 2012-04-17 11:04 138
c53e6443 sago007 2012-04-17 11:04 139
	/*getString(varName)
c53e6443 sago007 2012-04-17 11:04 140
	 *Looks in the config file and returns the string that matches the key "varName"
c53e6443 sago007 2012-04-17 11:04 141
	 *Returns an empty string if varName does not exist.
c53e6443 sago007 2012-04-17 11:04 142
	 */
af35c39d sago007 2013-11-16 16:37 143
	string getString(const string &varName);
c53e6443 sago007 2012-04-17 11:04 144
c53e6443 sago007 2012-04-17 11:04 145
	/*getInt(varName)
c53e6443 sago007 2012-04-17 11:04 146
	 *Looks in the config file and returns the int that matches the key "varName"
c53e6443 sago007 2012-04-17 11:04 147
	 *Returns "0" if varName does not exist or cannot be parsed.
c53e6443 sago007 2012-04-17 11:04 148
	 */
af35c39d sago007 2013-11-16 16:37 149
	int getInt(const string &varName);
c53e6443 sago007 2012-04-17 11:04 150
c53e6443 sago007 2012-04-17 11:04 151
	/*getValue(varName)
c53e6443 sago007 2012-04-17 11:04 152
	 *Looks in the config file and returns the double that matches the key "varName"
c53e6443 sago007 2012-04-17 11:04 153
	 *Returns "0.0" if varName does not exist or cannot be parsed.
c53e6443 sago007 2012-04-17 11:04 154
	 */
af35c39d sago007 2013-11-16 16:37 155
	double getValue(const string &varName);
c53e6443 sago007 2012-04-17 11:04 156
c53e6443 sago007 2012-04-17 11:04 157
	/*setString(varName,content)
c53e6443 sago007 2012-04-17 11:04 158
	 *Sets the config variable with key "varName" to the value of "content"
c53e6443 sago007 2012-04-17 11:04 159
	 */
af35c39d sago007 2013-11-16 16:37 160
	void setString(const string &varName,const string &content);
c53e6443 sago007 2012-04-17 11:04 161
c53e6443 sago007 2012-04-17 11:04 162
	/*setInt(varName,content)
c53e6443 sago007 2012-04-17 11:04 163
	 *Sets the config variable with key "varName" to the value of "content"
c53e6443 sago007 2012-04-17 11:04 164
	 */
af35c39d sago007 2013-11-16 16:37 165
	void setInt(const string &varName,int content);
c53e6443 sago007 2012-04-17 11:04 166
c53e6443 sago007 2012-04-17 11:04 167
	/**
c53e6443 sago007 2012-04-17 11:04 168
	 * Sets a config variable to a given (double) value
c53e6443 sago007 2012-04-17 11:04 169
	 * @param varName Name of the variable to set
c53e6443 sago007 2012-04-17 11:04 170
	 * @param content Value to give the variable
c53e6443 sago007 2012-04-17 11:04 171
	 */
af35c39d sago007 2013-11-16 16:37 172
	void setValue(const string &varName,double content);
c53e6443 sago007 2012-04-17 11:04 173
c53e6443 sago007 2012-04-17 11:04 174
	/**
c53e6443 sago007 2012-04-17 11:04 175
	 * returns true if the key varName exists. This is used the first time 1.4.0
c53e6443 sago007 2012-04-17 11:04 176
	 * starts so that it can see that it has to import configs from an earlier
c53e6443 sago007 2012-04-17 11:04 177
	 * version.
c53e6443 sago007 2012-04-17 11:04 178
	 * @param varName Name of the variable
c53e6443 sago007 2012-04-17 11:04 179
	 * @return true if the varaible exists
c53e6443 sago007 2012-04-17 11:04 180
	 */
af35c39d sago007 2013-11-16 16:37 181
	bool exists(const string &varName) const;
c53e6443 sago007 2012-04-17 11:04 182
c53e6443 sago007 2012-04-17 11:04 183
	/*setDefault(varName,value)
c53e6443 sago007 2012-04-17 11:04 184
	 *if the variable "varName" does not exist it will be created with value "value"
c53e6443 sago007 2012-04-17 11:04 185
	 *if varName exists then this will have no effect
c53e6443 sago007 2012-04-17 11:04 186
	 */
c53e6443 sago007 2012-04-17 11:04 187
	/**
c53e6443 sago007 2012-04-17 11:04 188
	 * Set default value for a variable. If the variable "varName" does not exist it will be created with value "value"
c53e6443 sago007 2012-04-17 11:04 189
	 * if varName exists then this will have no effect
c53e6443 sago007 2012-04-17 11:04 190
	 * @param varName Name of the variable
c53e6443 sago007 2012-04-17 11:04 191
	 * @param content The default value
c53e6443 sago007 2012-04-17 11:04 192
	 */
af35c39d sago007 2013-11-16 16:37 193
	void setDefault(const string &varName, const string &content);
c53e6443 sago007 2012-04-17 11:04 194
c53e6443 sago007 2012-04-17 11:04 195
	/**
c53e6443 sago007 2012-04-17 11:04 196
	 * Should be set if the user has requested the program to shutdown.
c53e6443 sago007 2012-04-17 11:04 197
	 * @param shuttingDown value of shutdown command. 5 = default = shutdown but allow saving
c53e6443 sago007 2012-04-17 11:04 198
	 */
c53e6443 sago007 2012-04-17 11:04 199
	void setShuttingDown(long shuttingDown = 5);
c53e6443 sago007 2012-04-17 11:04 200
c53e6443 sago007 2012-04-17 11:04 201
	/**
c53e6443 sago007 2012-04-17 11:04 202
	 * tells if the user wants to shutdown. This can be used if the exit button is pressed deaply in the program.
c53e6443 sago007 2012-04-17 11:04 203
	 * @return
c53e6443 sago007 2012-04-17 11:04 204
	 */
c53e6443 sago007 2012-04-17 11:04 205
	long isShuttingDown() const;
769a41a0 sago007 2008-09-24 12:54 206
};
769a41a0 sago007 2008-09-24 12:54 207
89c4a3a6 sago007 2008-08-29 14:32 208
#endif	/* _COMMON_H */
89c4a3a6 sago007 2008-08-29 14:32 209
1970-01-01 00:00 210