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
021de090 sago007 2015-12-30 18:56 20
http://blockattack.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
89c4a3a6 sago007 2008-08-29 14:32 33
#include <string>
89c4a3a6 sago007 2008-08-29 14:32 34
#include <iostream>
769a41a0 sago007 2008-09-24 12:54 35
#include <map>
89c4a3a6 sago007 2008-08-29 14:32 36
#include <stdlib.h>
c9dd376f sago007 2012-01-20 17:13 37
#include <libintl.h>
c9dd376f sago007 2012-01-20 17:13 38
#include <boost/format.hpp>
1899da11 sago007 2015-12-30 19:04 39
89c4a3a6 sago007 2008-08-29 14:32 40
c9dd376f sago007 2012-01-20 17:13 41
#define _(String) gettext (String)
c9dd376f sago007 2012-01-20 17:13 42
c53e6443 sago007 2012-04-17 11:04 43
struct commonTime
c53e6443 sago007 2012-04-17 11:04 44
{
c53e6443 sago007 2012-04-17 11:04 45
	unsigned int days;
c53e6443 sago007 2012-04-17 11:04 46
	unsigned int hours;
c53e6443 sago007 2012-04-17 11:04 47
	unsigned int minutes;
c53e6443 sago007 2012-04-17 11:04 48
	unsigned int seconds;
89c4a3a6 sago007 2008-08-29 14:32 49
};
89c4a3a6 sago007 2008-08-29 14:32 50
53001fa2 sago007 2015-11-08 15:13 51
std::string itoa(int num) __attribute__((const));
769a41a0 sago007 2008-09-24 12:54 52
f306e3ca sago007 2015-12-04 21:16 53
bool strequals(const char* a, const char* b);
c9dd376f sago007 2012-01-20 17:13 54
c65f7c8d sago007 2010-02-22 19:27 55
/**
c65f7c8d sago007 2010-02-22 19:27 56
 * str2int parses a string and returns an int with the value of the string.
c65f7c8d sago007 2010-02-22 19:27 57
 * if the string is not an int then 0 is returned instead of throing an error
c65f7c8d sago007 2010-02-22 19:27 58
 * in that way this function will always return a useable value.
c65f7c8d sago007 2010-02-22 19:27 59
 */
53001fa2 sago007 2015-11-08 15:13 60
int str2int(const std::string &str2parse) __attribute__((const));
cc3ef158 sago007 2011-07-03 16:13 61
53001fa2 sago007 2015-11-08 15:13 62
std::string double2str(double num) __attribute__((const));
cc3ef158 sago007 2011-07-03 16:13 63
078900fe sago007 2015-12-05 15:31 64
void dieOnNullptr(bool, const char* msg);
078900fe sago007 2015-12-05 15:31 65
cc3ef158 sago007 2011-07-03 16:13 66
/**
cc3ef158 sago007 2011-07-03 16:13 67
 * str2double parses a string and returns a double with the value of the string.
cc3ef158 sago007 2011-07-03 16:13 68
 * if the string is not a double then 0.0 is returned instead of throing an error
cc3ef158 sago007 2011-07-03 16:13 69
 * in that way this function will always return a useable value.
cc3ef158 sago007 2011-07-03 16:13 70
 */
53001fa2 sago007 2015-11-08 15:13 71
double str2double(const std::string &str2parse) __attribute__((const));
c65f7c8d sago007 2010-02-22 19:27 72
15b9c93b sago007 2016-02-03 20:03 73
/**
15b9c93b sago007 2016-02-03 20:03 74
 * Does the eqivilent to snprintf but returns a C++ string
15b9c93b sago007 2016-02-03 20:03 75
 * @param fmt The format string
15b9c93b sago007 2016-02-03 20:03 76
 * @param ... Additional paremeters for the place holders
15b9c93b sago007 2016-02-03 20:03 77
 * @return A string with the result
15b9c93b sago007 2016-02-03 20:03 78
 */
15b9c93b sago007 2016-02-03 20:03 79
std::string SPrintStringF(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
15b9c93b sago007 2016-02-03 20:03 80
15b9c93b sago007 2016-02-03 20:03 81
/**
15b9c93b sago007 2016-02-03 20:03 82
 * Prints to an internal C-buffer
15b9c93b sago007 2016-02-03 20:03 83
 * Because it uses an internal buffer the returned buffer is only valid until the next call
15b9c93b sago007 2016-02-03 20:03 84
 * The String is cut at 1024 chars (including the 0 terminator)
15b9c93b sago007 2016-02-03 20:03 85
 * This is the larges string that can safely be parsed to NFont
15b9c93b sago007 2016-02-03 20:03 86
 * @param fmt The format string
15b9c93b sago007 2016-02-03 20:03 87
 * @param ... Additional paremeters for the place holders
15b9c93b sago007 2016-02-03 20:03 88
 * @return Pointer to an internal buffer
15b9c93b sago007 2016-02-03 20:03 89
 */
15b9c93b sago007 2016-02-03 20:03 90
const char* SPrintCF(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
eec83b7d sago007 2009-03-29 15:46 91
cd12e0fe sago007 2009-01-29 08:47 92
class TimeHandler
cd12e0fe sago007 2009-01-29 08:47 93
{
cd12e0fe sago007 2009-01-29 08:47 94
public:
cd12e0fe sago007 2009-01-29 08:47 95
	static commonTime ms2ct(unsigned int milliseconds);
c53e6443 sago007 2012-04-17 11:04 96
53001fa2 sago007 2015-11-08 15:13 97
	static commonTime getTime(const std::string &name);
c53e6443 sago007 2012-04-17 11:04 98
53001fa2 sago007 2015-11-08 15:13 99
	static commonTime peekTime(const std::string &name, const commonTime &toAdd);
c53e6443 sago007 2012-04-17 11:04 100
53001fa2 sago007 2015-11-08 15:13 101
	static commonTime addTime(const std::string &name, const commonTime &toAdd);
cd12e0fe sago007 2009-01-29 08:47 102
};
89c4a3a6 sago007 2008-08-29 14:32 103
769a41a0 sago007 2008-09-24 12:54 104
#define MAX_VAR_LENGTH 1024
769a41a0 sago007 2008-09-24 12:54 105
fcd5a134 sago007 2008-12-19 01:11 106
/*This is the Config class it is a map to hold config variables.
fcd5a134 sago007 2008-12-19 01:11 107
 *It is inspired by Quake 3's CVAR system although a lot simpler.
fcd5a134 sago007 2008-12-19 01:11 108
 *All variables have keys "varName" that is used to access a variable.
fcd5a134 sago007 2008-12-19 01:11 109
 *
fcd5a134 sago007 2008-12-19 01:11 110
 *This class is a singleton
fcd5a134 sago007 2008-12-19 01:11 111
 */
769a41a0 sago007 2008-09-24 12:54 112
class Config
769a41a0 sago007 2008-09-24 12:54 113
{
c53e6443 sago007 2012-04-17 11:04 114
private:
53001fa2 sago007 2015-11-08 15:13 115
	std::map<std::string, std::string> configMap;
c53e6443 sago007 2012-04-17 11:04 116
c53e6443 sago007 2012-04-17 11:04 117
	static Config *instance;
c53e6443 sago007 2012-04-17 11:04 118
c53e6443 sago007 2012-04-17 11:04 119
	void load();
c53e6443 sago007 2012-04-17 11:04 120
c53e6443 sago007 2012-04-17 11:04 121
	/* tells if the user has requested a shutdown */
c53e6443 sago007 2012-04-17 11:04 122
	long shuttingDown;
769a41a0 sago007 2008-09-24 12:54 123
protected:
c53e6443 sago007 2012-04-17 11:04 124
c53e6443 sago007 2012-04-17 11:04 125
	Config();
c53e6443 sago007 2012-04-17 11:04 126
c53e6443 sago007 2012-04-17 11:04 127
769a41a0 sago007 2008-09-24 12:54 128
public:
c53e6443 sago007 2012-04-17 11:04 129
	/*Config is a singleton.
c53e6443 sago007 2012-04-17 11:04 130
	 *It is accessed like this:
c53e6443 sago007 2012-04-17 11:04 131
	 *Config::getInstance()->method2call(paramters);
c53e6443 sago007 2012-04-17 11:04 132
	 */
c53e6443 sago007 2012-04-17 11:04 133
	static Config* getInstance();
c53e6443 sago007 2012-04-17 11:04 134
c53e6443 sago007 2012-04-17 11:04 135
	/*save()
c53e6443 sago007 2012-04-17 11:04 136
	 *forces the config to be written to disk. This will also happened if the
c53e6443 sago007 2012-04-17 11:04 137
	 *program terminates normally.
c53e6443 sago007 2012-04-17 11:04 138
	 */
c53e6443 sago007 2012-04-17 11:04 139
	void save();
c53e6443 sago007 2012-04-17 11:04 140
c53e6443 sago007 2012-04-17 11:04 141
	/*getString(varName)
c53e6443 sago007 2012-04-17 11:04 142
	 *Looks in the config file and returns the string that matches the key "varName"
c53e6443 sago007 2012-04-17 11:04 143
	 *Returns an empty string if varName does not exist.
c53e6443 sago007 2012-04-17 11:04 144
	 */
53001fa2 sago007 2015-11-08 15:13 145
	std::string getString(const std::string &varName);
c53e6443 sago007 2012-04-17 11:04 146
c53e6443 sago007 2012-04-17 11:04 147
	/*getInt(varName)
c53e6443 sago007 2012-04-17 11:04 148
	 *Looks in the config file and returns the int that matches the key "varName"
c53e6443 sago007 2012-04-17 11:04 149
	 *Returns "0" if varName does not exist or cannot be parsed.
c53e6443 sago007 2012-04-17 11:04 150
	 */
53001fa2 sago007 2015-11-08 15:13 151
	int getInt(const std::string &varName);
c53e6443 sago007 2012-04-17 11:04 152
c53e6443 sago007 2012-04-17 11:04 153
	/*getValue(varName)
c53e6443 sago007 2012-04-17 11:04 154
	 *Looks in the config file and returns the double that matches the key "varName"
c53e6443 sago007 2012-04-17 11:04 155
	 *Returns "0.0" if varName does not exist or cannot be parsed.
c53e6443 sago007 2012-04-17 11:04 156
	 */
53001fa2 sago007 2015-11-08 15:13 157
	double getValue(const std::string &varName);
c53e6443 sago007 2012-04-17 11:04 158
c53e6443 sago007 2012-04-17 11:04 159
	/*setString(varName,content)
c53e6443 sago007 2012-04-17 11:04 160
	 *Sets the config variable with key "varName" to the value of "content"
c53e6443 sago007 2012-04-17 11:04 161
	 */
53001fa2 sago007 2015-11-08 15:13 162
	void setString(const std::string &varName, const std::string &content);
c53e6443 sago007 2012-04-17 11:04 163
c53e6443 sago007 2012-04-17 11:04 164
	/*setInt(varName,content)
c53e6443 sago007 2012-04-17 11:04 165
	 *Sets the config variable with key "varName" to the value of "content"
c53e6443 sago007 2012-04-17 11:04 166
	 */
53001fa2 sago007 2015-11-08 15:13 167
	void setInt(const std::string &varName, int content);
c53e6443 sago007 2012-04-17 11:04 168
c53e6443 sago007 2012-04-17 11:04 169
	/**
c53e6443 sago007 2012-04-17 11:04 170
	 * Sets a config variable to a given (double) value
c53e6443 sago007 2012-04-17 11:04 171
	 * @param varName Name of the variable to set
c53e6443 sago007 2012-04-17 11:04 172
	 * @param content Value to give the variable
c53e6443 sago007 2012-04-17 11:04 173
	 */
53001fa2 sago007 2015-11-08 15:13 174
	void setValue(const std::string &varName,double content);
c53e6443 sago007 2012-04-17 11:04 175
c53e6443 sago007 2012-04-17 11:04 176
	/**
c53e6443 sago007 2012-04-17 11:04 177
	 * returns true if the key varName exists. This is used the first time 1.4.0
c53e6443 sago007 2012-04-17 11:04 178
	 * starts so that it can see that it has to import configs from an earlier
c53e6443 sago007 2012-04-17 11:04 179
	 * version.
c53e6443 sago007 2012-04-17 11:04 180
	 * @param varName Name of the variable
c53e6443 sago007 2012-04-17 11:04 181
	 * @return true if the varaible exists
c53e6443 sago007 2012-04-17 11:04 182
	 */
53001fa2 sago007 2015-11-08 15:13 183
	bool exists(const std::string &varName) const;
c53e6443 sago007 2012-04-17 11:04 184
c53e6443 sago007 2012-04-17 11:04 185
	/*setDefault(varName,value)
c53e6443 sago007 2012-04-17 11:04 186
	 *if the variable "varName" does not exist it will be created with value "value"
c53e6443 sago007 2012-04-17 11:04 187
	 *if varName exists then this will have no effect
c53e6443 sago007 2012-04-17 11:04 188
	 */
c53e6443 sago007 2012-04-17 11:04 189
	/**
c53e6443 sago007 2012-04-17 11:04 190
	 * 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 191
	 * if varName exists then this will have no effect
c53e6443 sago007 2012-04-17 11:04 192
	 * @param varName Name of the variable
c53e6443 sago007 2012-04-17 11:04 193
	 * @param content The default value
c53e6443 sago007 2012-04-17 11:04 194
	 */
53001fa2 sago007 2015-11-08 15:13 195
	void setDefault(const std::string &varName, const std::string &content);
c53e6443 sago007 2012-04-17 11:04 196
c53e6443 sago007 2012-04-17 11:04 197
	/**
c53e6443 sago007 2012-04-17 11:04 198
	 * Should be set if the user has requested the program to shutdown.
c53e6443 sago007 2012-04-17 11:04 199
	 * @param shuttingDown value of shutdown command. 5 = default = shutdown but allow saving
c53e6443 sago007 2012-04-17 11:04 200
	 */
c53e6443 sago007 2012-04-17 11:04 201
	void setShuttingDown(long shuttingDown = 5);
c53e6443 sago007 2012-04-17 11:04 202
c53e6443 sago007 2012-04-17 11:04 203
	/**
c53e6443 sago007 2012-04-17 11:04 204
	 * 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 205
	 * @return
c53e6443 sago007 2012-04-17 11:04 206
	 */
c53e6443 sago007 2012-04-17 11:04 207
	long isShuttingDown() const;
769a41a0 sago007 2008-09-24 12:54 208
};
769a41a0 sago007 2008-09-24 12:54 209
89c4a3a6 sago007 2008-08-29 14:32 210
#endif	/* _COMMON_H */
89c4a3a6 sago007 2008-08-29 14:32 211
1970-01-01 00:00 212