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
826cf176 sago007 2016-03-12 10:53 20
http://www.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
{
8ff26872 sago007 2016-03-25 09:46 45
	unsigned int days = 0;
8ff26872 sago007 2016-03-25 09:46 46
	unsigned int hours = 0;
8ff26872 sago007 2016-03-25 09:46 47
	unsigned int minutes = 0;
8ff26872 sago007 2016-03-25 09:46 48
	unsigned int seconds = 0;
89c4a3a6 sago007 2008-08-29 14:32 49
};
89c4a3a6 sago007 2008-08-29 14:32 50
549ecfae sago007 2016-10-09 08:37 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
078900fe sago007 2015-12-05 15:31 62
void dieOnNullptr(bool, const char* msg);
078900fe sago007 2015-12-05 15:31 63
cc3ef158 sago007 2011-07-03 16:13 64
/**
cc3ef158 sago007 2011-07-03 16:13 65
 * str2double parses a string and returns a double with the value of the string.
cc3ef158 sago007 2011-07-03 16:13 66
 * if the string is not a double then 0.0 is returned instead of throing an error
cc3ef158 sago007 2011-07-03 16:13 67
 * in that way this function will always return a useable value.
cc3ef158 sago007 2011-07-03 16:13 68
 */
53001fa2 sago007 2015-11-08 15:13 69
double str2double(const std::string &str2parse) __attribute__((const));
c65f7c8d sago007 2010-02-22 19:27 70
15b9c93b sago007 2016-02-03 20:03 71
/**
15b9c93b sago007 2016-02-03 20:03 72
 * Does the eqivilent to snprintf but returns a C++ string
15b9c93b sago007 2016-02-03 20:03 73
 * @param fmt The format string
15b9c93b sago007 2016-02-03 20:03 74
 * @param ... Additional paremeters for the place holders
15b9c93b sago007 2016-02-03 20:03 75
 * @return A string with the result
15b9c93b sago007 2016-02-03 20:03 76
 */
15b9c93b sago007 2016-02-03 20:03 77
std::string SPrintStringF(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
15b9c93b sago007 2016-02-03 20:03 78
15b9c93b sago007 2016-02-03 20:03 79
/**
15b9c93b sago007 2016-02-03 20:03 80
 * Prints to an internal C-buffer
15b9c93b sago007 2016-02-03 20:03 81
 * Because it uses an internal buffer the returned buffer is only valid until the next call
15b9c93b sago007 2016-02-03 20:03 82
 * The String is cut at 1024 chars (including the 0 terminator)
15b9c93b sago007 2016-02-03 20:03 83
 * This is the larges string that can safely be parsed to NFont
15b9c93b sago007 2016-02-03 20:03 84
 * @param fmt The format string
15b9c93b sago007 2016-02-03 20:03 85
 * @param ... Additional paremeters for the place holders
15b9c93b sago007 2016-02-03 20:03 86
 * @return Pointer to an internal buffer
15b9c93b sago007 2016-02-03 20:03 87
 */
15b9c93b sago007 2016-02-03 20:03 88
const char* SPrintCF(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
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
53001fa2 sago007 2015-11-08 15:13 95
	static commonTime getTime(const std::string &name);
c53e6443 sago007 2012-04-17 11:04 96
53001fa2 sago007 2015-11-08 15:13 97
	static commonTime peekTime(const std::string &name, const commonTime &toAdd);
c53e6443 sago007 2012-04-17 11:04 98
53001fa2 sago007 2015-11-08 15:13 99
	static commonTime addTime(const std::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:
53001fa2 sago007 2015-11-08 15:13 113
	std::map<std::string, std::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 */
8ff26872 sago007 2016-03-25 09:46 120
	long shuttingDown = 0;
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
	 */
53001fa2 sago007 2015-11-08 15:13 143
	std::string getString(const std::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
	 */
53001fa2 sago007 2015-11-08 15:13 149
	int getInt(const std::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
	 */
53001fa2 sago007 2015-11-08 15:13 155
	double getValue(const std::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
	 */
53001fa2 sago007 2015-11-08 15:13 160
	void setString(const std::string &varName, const std::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
	 */
53001fa2 sago007 2015-11-08 15:13 165
	void setInt(const std::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
	 */
53001fa2 sago007 2015-11-08 15:13 172
	void setValue(const std::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
	 */
53001fa2 sago007 2015-11-08 15:13 181
	bool exists(const std::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
	 */
53001fa2 sago007 2015-11-08 15:13 193
	void setDefault(const std::string &varName, const std::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