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
f6d8a699 sago007 2018-10-07 12:16 20
https://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
463f5ffe Poul Sander 2018-10-20 16:17 26
 *It contains some basic functions that nearly all multi platform 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>
d3f1e001 Poul Sander 2017-03-29 18:17 38
#include <ctime>
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
f306e3ca sago007 2015-12-04 21:16 51
bool strequals(const char* a, const char* b);
c9dd376f sago007 2012-01-20 17:13 52
c65f7c8d sago007 2010-02-22 19:27 53
/**
c65f7c8d sago007 2010-02-22 19:27 54
 * str2int parses a string and returns an int with the value of the string.
463f5ffe Poul Sander 2018-10-20 16:17 55
 * if the string is not an int then 0 is returned instead of throwing an error
463f5ffe Poul Sander 2018-10-20 16:17 56
 * in that way this function will always return a usable value.
c65f7c8d sago007 2010-02-22 19:27 57
 */
53001fa2 sago007 2015-11-08 15:13 58
int str2int(const std::string &str2parse) __attribute__((const));
cc3ef158 sago007 2011-07-03 16:13 59
078900fe sago007 2015-12-05 15:31 60
void dieOnNullptr(bool, const char* msg);
078900fe sago007 2015-12-05 15:31 61
cc3ef158 sago007 2011-07-03 16:13 62
/**
cc3ef158 sago007 2011-07-03 16:13 63
 * str2double parses a string and returns a double with the value of the string.
463f5ffe Poul Sander 2018-10-20 16:17 64
 * if the string is not a double then 0.0 is returned instead of throwing an error
463f5ffe Poul Sander 2018-10-20 16:17 65
 * in that way this function will always return a usable value.
cc3ef158 sago007 2011-07-03 16:13 66
 */
53001fa2 sago007 2015-11-08 15:13 67
double str2double(const std::string &str2parse) __attribute__((const));
c65f7c8d sago007 2010-02-22 19:27 68
cd12e0fe sago007 2009-01-29 08:47 69
class TimeHandler
cd12e0fe sago007 2009-01-29 08:47 70
{
cd12e0fe sago007 2009-01-29 08:47 71
public:
cd12e0fe sago007 2009-01-29 08:47 72
	static commonTime ms2ct(unsigned int milliseconds);
c53e6443 sago007 2012-04-17 11:04 73
53001fa2 sago007 2015-11-08 15:13 74
	static commonTime getTime(const std::string &name);
c53e6443 sago007 2012-04-17 11:04 75
53001fa2 sago007 2015-11-08 15:13 76
	static commonTime peekTime(const std::string &name, const commonTime &toAdd);
c53e6443 sago007 2012-04-17 11:04 77
53001fa2 sago007 2015-11-08 15:13 78
	static commonTime addTime(const std::string &name, const commonTime &toAdd);
cd12e0fe sago007 2009-01-29 08:47 79
};
89c4a3a6 sago007 2008-08-29 14:32 80
769a41a0 sago007 2008-09-24 12:54 81
#define MAX_VAR_LENGTH 1024
769a41a0 sago007 2008-09-24 12:54 82
fcd5a134 sago007 2008-12-19 01:11 83
/*This is the Config class it is a map to hold config variables.
fcd5a134 sago007 2008-12-19 01:11 84
 *It is inspired by Quake 3's CVAR system although a lot simpler.
fcd5a134 sago007 2008-12-19 01:11 85
 *All variables have keys "varName" that is used to access a variable.
fcd5a134 sago007 2008-12-19 01:11 86
 *
fcd5a134 sago007 2008-12-19 01:11 87
 *This class is a singleton
fcd5a134 sago007 2008-12-19 01:11 88
 */
769a41a0 sago007 2008-09-24 12:54 89
class Config
769a41a0 sago007 2008-09-24 12:54 90
{
c53e6443 sago007 2012-04-17 11:04 91
private:
53001fa2 sago007 2015-11-08 15:13 92
	std::map<std::string, std::string> configMap;
c53e6443 sago007 2012-04-17 11:04 93
c53e6443 sago007 2012-04-17 11:04 94
	static Config *instance;
c53e6443 sago007 2012-04-17 11:04 95
c53e6443 sago007 2012-04-17 11:04 96
	void load();
c53e6443 sago007 2012-04-17 11:04 97
c53e6443 sago007 2012-04-17 11:04 98
	/* tells if the user has requested a shutdown */
8ff26872 sago007 2016-03-25 09:46 99
	long shuttingDown = 0;
c53e6443 sago007 2012-04-17 11:04 100
c53e6443 sago007 2012-04-17 11:04 101
	Config();
c53e6443 sago007 2012-04-17 11:04 102
c53e6443 sago007 2012-04-17 11:04 103
769a41a0 sago007 2008-09-24 12:54 104
public:
c53e6443 sago007 2012-04-17 11:04 105
	/*Config is a singleton.
c53e6443 sago007 2012-04-17 11:04 106
	 *It is accessed like this:
463f5ffe Poul Sander 2018-10-20 16:17 107
	 *Config::getInstance()->method2call(parameters);
c53e6443 sago007 2012-04-17 11:04 108
	 */
c53e6443 sago007 2012-04-17 11:04 109
	static Config* getInstance();
c53e6443 sago007 2012-04-17 11:04 110
c53e6443 sago007 2012-04-17 11:04 111
	/*save()
c53e6443 sago007 2012-04-17 11:04 112
	 *forces the config to be written to disk. This will also happened if the
c53e6443 sago007 2012-04-17 11:04 113
	 *program terminates normally.
c53e6443 sago007 2012-04-17 11:04 114
	 */
c53e6443 sago007 2012-04-17 11:04 115
	void save();
c53e6443 sago007 2012-04-17 11:04 116
c53e6443 sago007 2012-04-17 11:04 117
	/*getString(varName)
c53e6443 sago007 2012-04-17 11:04 118
	 *Looks in the config file and returns the string that matches the key "varName"
c53e6443 sago007 2012-04-17 11:04 119
	 *Returns an empty string if varName does not exist.
c53e6443 sago007 2012-04-17 11:04 120
	 */
53001fa2 sago007 2015-11-08 15:13 121
	std::string getString(const std::string &varName);
c53e6443 sago007 2012-04-17 11:04 122
c53e6443 sago007 2012-04-17 11:04 123
	/*getInt(varName)
c53e6443 sago007 2012-04-17 11:04 124
	 *Looks in the config file and returns the int that matches the key "varName"
c53e6443 sago007 2012-04-17 11:04 125
	 *Returns "0" if varName does not exist or cannot be parsed.
c53e6443 sago007 2012-04-17 11:04 126
	 */
53001fa2 sago007 2015-11-08 15:13 127
	int getInt(const std::string &varName);
c53e6443 sago007 2012-04-17 11:04 128
c53e6443 sago007 2012-04-17 11:04 129
	/*getValue(varName)
c53e6443 sago007 2012-04-17 11:04 130
	 *Looks in the config file and returns the double that matches the key "varName"
c53e6443 sago007 2012-04-17 11:04 131
	 *Returns "0.0" if varName does not exist or cannot be parsed.
c53e6443 sago007 2012-04-17 11:04 132
	 */
53001fa2 sago007 2015-11-08 15:13 133
	double getValue(const std::string &varName);
c53e6443 sago007 2012-04-17 11:04 134
c53e6443 sago007 2012-04-17 11:04 135
	/*setString(varName,content)
c53e6443 sago007 2012-04-17 11:04 136
	 *Sets the config variable with key "varName" to the value of "content"
c53e6443 sago007 2012-04-17 11:04 137
	 */
53001fa2 sago007 2015-11-08 15:13 138
	void setString(const std::string &varName, const std::string &content);
c53e6443 sago007 2012-04-17 11:04 139
c53e6443 sago007 2012-04-17 11:04 140
	/*setInt(varName,content)
c53e6443 sago007 2012-04-17 11:04 141
	 *Sets the config variable with key "varName" to the value of "content"
c53e6443 sago007 2012-04-17 11:04 142
	 */
53001fa2 sago007 2015-11-08 15:13 143
	void setInt(const std::string &varName, int content);
c53e6443 sago007 2012-04-17 11:04 144
c53e6443 sago007 2012-04-17 11:04 145
	/**
c53e6443 sago007 2012-04-17 11:04 146
	 * Sets a config variable to a given (double) value
c53e6443 sago007 2012-04-17 11:04 147
	 * @param varName Name of the variable to set
c53e6443 sago007 2012-04-17 11:04 148
	 * @param content Value to give the variable
c53e6443 sago007 2012-04-17 11:04 149
	 */
53001fa2 sago007 2015-11-08 15:13 150
	void setValue(const std::string &varName,double content);
c53e6443 sago007 2012-04-17 11:04 151
c53e6443 sago007 2012-04-17 11:04 152
	/**
c53e6443 sago007 2012-04-17 11:04 153
	 * returns true if the key varName exists. This is used the first time 1.4.0
c53e6443 sago007 2012-04-17 11:04 154
	 * starts so that it can see that it has to import configs from an earlier
c53e6443 sago007 2012-04-17 11:04 155
	 * version.
c53e6443 sago007 2012-04-17 11:04 156
	 * @param varName Name of the variable
463f5ffe Poul Sander 2018-10-20 16:17 157
	 * @return true if the variable exists
c53e6443 sago007 2012-04-17 11:04 158
	 */
53001fa2 sago007 2015-11-08 15:13 159
	bool exists(const std::string &varName) const;
c53e6443 sago007 2012-04-17 11:04 160
c53e6443 sago007 2012-04-17 11:04 161
	/*setDefault(varName,value)
c53e6443 sago007 2012-04-17 11:04 162
	 *if the variable "varName" does not exist it will be created with value "value"
c53e6443 sago007 2012-04-17 11:04 163
	 *if varName exists then this will have no effect
c53e6443 sago007 2012-04-17 11:04 164
	 */
c53e6443 sago007 2012-04-17 11:04 165
	/**
c53e6443 sago007 2012-04-17 11:04 166
	 * 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 167
	 * if varName exists then this will have no effect
c53e6443 sago007 2012-04-17 11:04 168
	 * @param varName Name of the variable
c53e6443 sago007 2012-04-17 11:04 169
	 * @param content The default value
c53e6443 sago007 2012-04-17 11:04 170
	 */
53001fa2 sago007 2015-11-08 15:13 171
	void setDefault(const std::string &varName, const std::string &content);
c53e6443 sago007 2012-04-17 11:04 172
c53e6443 sago007 2012-04-17 11:04 173
	/**
c53e6443 sago007 2012-04-17 11:04 174
	 * Should be set if the user has requested the program to shutdown.
c53e6443 sago007 2012-04-17 11:04 175
	 * @param shuttingDown value of shutdown command. 5 = default = shutdown but allow saving
c53e6443 sago007 2012-04-17 11:04 176
	 */
c53e6443 sago007 2012-04-17 11:04 177
	void setShuttingDown(long shuttingDown = 5);
c53e6443 sago007 2012-04-17 11:04 178
c53e6443 sago007 2012-04-17 11:04 179
	/**
463f5ffe Poul Sander 2018-10-20 16:17 180
	 * tells if the user wants to shutdown. This can be used if the exit button is pressed deeply in the program.
c53e6443 sago007 2012-04-17 11:04 181
	 * @return
c53e6443 sago007 2012-04-17 11:04 182
	 */
c53e6443 sago007 2012-04-17 11:04 183
	long isShuttingDown() const;
769a41a0 sago007 2008-09-24 12:54 184
};
769a41a0 sago007 2008-09-24 12:54 185
89c4a3a6 sago007 2008-08-29 14:32 186
#endif	/* _COMMON_H */
89c4a3a6 sago007 2008-08-29 14:32 187
1970-01-01 00:00 188