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