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