git repos / blockattack-game

blame: source/code/common.h

normal view · raw

89c4a3a6 sago007 2008-08-29 14:32 1
/*
89c4a3a6 sago007 2008-08-29 14:32 2
Block Attack - Rise of the Blocks, SDL game, besed on Nintendo's Tetris Attack
89c4a3a6 sago007 2008-08-29 14:32 3
Copyright (C) 2008 Poul Sander
89c4a3a6 sago007 2008-08-29 14:32 4
89c4a3a6 sago007 2008-08-29 14:32 5
    This program is free software; you can redistribute it and/or modify
89c4a3a6 sago007 2008-08-29 14:32 6
    it under the terms of the GNU General Public License as published by
89c4a3a6 sago007 2008-08-29 14:32 7
    the Free Software Foundation; either version 2 of the License, or
89c4a3a6 sago007 2008-08-29 14:32 8
    (at your option) any later version.
89c4a3a6 sago007 2008-08-29 14:32 9
89c4a3a6 sago007 2008-08-29 14:32 10
    This program is distributed in the hope that it will be useful,
89c4a3a6 sago007 2008-08-29 14:32 11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
89c4a3a6 sago007 2008-08-29 14:32 12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
89c4a3a6 sago007 2008-08-29 14:32 13
    GNU General Public License for more details.
89c4a3a6 sago007 2008-08-29 14:32 14
89c4a3a6 sago007 2008-08-29 14:32 15
    You should have received a copy of the GNU General Public License
89c4a3a6 sago007 2008-08-29 14:32 16
    along with this program; if not, write to the Free Software
89c4a3a6 sago007 2008-08-29 14:32 17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
89c4a3a6 sago007 2008-08-29 14:32 18
89c4a3a6 sago007 2008-08-29 14:32 19
    Poul Sander
89c4a3a6 sago007 2008-08-29 14:32 20
    R�vehjvej 36, V. 1111
89c4a3a6 sago007 2008-08-29 14:32 21
    2800 Kgs. Lyngby
89c4a3a6 sago007 2008-08-29 14:32 22
    DENMARK
89c4a3a6 sago007 2008-08-29 14:32 23
    blockattack@poulsander.com
89c4a3a6 sago007 2008-08-29 14:32 24
    http://blockattack.sf.net
89c4a3a6 sago007 2008-08-29 14:32 25
*/
89c4a3a6 sago007 2008-08-29 14:32 26
fcd5a134 sago007 2008-12-19 01:11 27
/*
fcd5a134 sago007 2008-12-19 01:11 28
 *This is the common.h
fcd5a134 sago007 2008-12-19 01:11 29
 *It contains some basic functions that nearly all multiplatform games are going
fcd5a134 sago007 2008-12-19 01:11 30
 *to need.
fcd5a134 sago007 2008-12-19 01:11 31
 */
fcd5a134 sago007 2008-12-19 01:11 32
89c4a3a6 sago007 2008-08-29 14:32 33
#ifndef _COMMON_H
89c4a3a6 sago007 2008-08-29 14:32 34
#define	_COMMON_H
89c4a3a6 sago007 2008-08-29 14:32 35
fcd5a134 sago007 2008-12-19 01:11 36
/*
fcd5a134 sago007 2008-12-19 01:11 37
 *Files will be saved in:
fcd5a134 sago007 2008-12-19 01:11 38
 * HOME/.gamesaves/"+GAMENAME (unix)
fcd5a134 sago007 2008-12-19 01:11 39
 *or DOCUMENTS/My Games/GAMENAME (Windows)
fcd5a134 sago007 2008-12-19 01:11 40
 */
89c4a3a6 sago007 2008-08-29 14:32 41
#define GAMENAME "blockattack"
89c4a3a6 sago007 2008-08-29 14:32 42
89c4a3a6 sago007 2008-08-29 14:32 43
#include <string>
89c4a3a6 sago007 2008-08-29 14:32 44
#include <iostream>
89c4a3a6 sago007 2008-08-29 14:32 45
#include <fstream>
769a41a0 sago007 2008-09-24 12:54 46
#include <map>
89c4a3a6 sago007 2008-08-29 14:32 47
#include <stdlib.h>
c9dd376f sago007 2012-01-20 17:13 48
#include <libintl.h>
c9dd376f sago007 2012-01-20 17:13 49
#include <boost/format.hpp>
eec83b7d sago007 2009-03-29 15:46 50
#ifdef WIN32
eec83b7d sago007 2009-03-29 15:46 51
#include "windows.h"
eec83b7d sago007 2009-03-29 15:46 52
#include "shlobj.h"
eec83b7d sago007 2009-03-29 15:46 53
#endif
89c4a3a6 sago007 2008-08-29 14:32 54
c9dd376f sago007 2012-01-20 17:13 55
#define _(String) gettext (String)
c9dd376f sago007 2012-01-20 17:13 56
89c4a3a6 sago007 2008-08-29 14:32 57
using namespace std;
c9dd376f sago007 2012-01-20 17:13 58
using boost::format;
89c4a3a6 sago007 2008-08-29 14:32 59
89c4a3a6 sago007 2008-08-29 14:32 60
struct commonTime{
89c4a3a6 sago007 2008-08-29 14:32 61
    unsigned int days;
89c4a3a6 sago007 2008-08-29 14:32 62
    unsigned int hours;
89c4a3a6 sago007 2008-08-29 14:32 63
    unsigned int minutes;
89c4a3a6 sago007 2008-08-29 14:32 64
    unsigned int seconds;
89c4a3a6 sago007 2008-08-29 14:32 65
};
89c4a3a6 sago007 2008-08-29 14:32 66
cc3ef158 sago007 2011-07-03 16:13 67
string itoa(int num) __attribute__((const));
769a41a0 sago007 2008-09-24 12:54 68
cc3ef158 sago007 2011-07-03 16:13 69
string getPathToSaveFiles() __attribute__((pure));
89c4a3a6 sago007 2008-08-29 14:32 70
c9dd376f sago007 2012-01-20 17:13 71
c65f7c8d sago007 2010-02-22 19:27 72
/**
c65f7c8d sago007 2010-02-22 19:27 73
 * str2int parses a string and returns an int with the value of the string.
c65f7c8d sago007 2010-02-22 19:27 74
 * if the string is not an int then 0 is returned instead of throing an error
c65f7c8d sago007 2010-02-22 19:27 75
 * in that way this function will always return a useable value.
c65f7c8d sago007 2010-02-22 19:27 76
 */
cc3ef158 sago007 2011-07-03 16:13 77
int str2int(string str2parse) __attribute__((const));
cc3ef158 sago007 2011-07-03 16:13 78
cc3ef158 sago007 2011-07-03 16:13 79
string double2str(double num) __attribute__((const));
cc3ef158 sago007 2011-07-03 16:13 80
cc3ef158 sago007 2011-07-03 16:13 81
/**
cc3ef158 sago007 2011-07-03 16:13 82
 * str2double parses a string and returns a double with the value of the string.
cc3ef158 sago007 2011-07-03 16:13 83
 * if the string is not a double then 0.0 is returned instead of throing an error
cc3ef158 sago007 2011-07-03 16:13 84
 * in that way this function will always return a useable value.
cc3ef158 sago007 2011-07-03 16:13 85
 */
cc3ef158 sago007 2011-07-03 16:13 86
double str2double(string str2parse) __attribute__((const));
c65f7c8d sago007 2010-02-22 19:27 87
eec83b7d sago007 2009-03-29 15:46 88
#ifdef WIN32
eec83b7d sago007 2009-03-29 15:46 89
string getMyDocumentsPath();
eec83b7d sago007 2009-03-29 15:46 90
#endif
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);
cd12e0fe sago007 2009-01-29 08:47 96
	
cd12e0fe sago007 2009-01-29 08:47 97
	static commonTime getTime(string name);
cd12e0fe sago007 2009-01-29 08:47 98
	
cd12e0fe sago007 2009-01-29 08:47 99
	static commonTime peekTime(string name, commonTime toAdd);
cd12e0fe sago007 2009-01-29 08:47 100
	
cd12e0fe sago007 2009-01-29 08:47 101
	static commonTime addTime(string name, 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
{
769a41a0 sago007 2008-09-24 12:54 114
private: 
769a41a0 sago007 2008-09-24 12:54 115
    map<string,string> configMap;
769a41a0 sago007 2008-09-24 12:54 116
769a41a0 sago007 2008-09-24 12:54 117
    static Config *instance;
769a41a0 sago007 2008-09-24 12:54 118
    
769a41a0 sago007 2008-09-24 12:54 119
    void load();
cc3ef158 sago007 2011-07-03 16:13 120
    
cc3ef158 sago007 2011-07-03 16:13 121
    /* tells if the user has requested a shutdown */
cc3ef158 sago007 2011-07-03 16:13 122
    long shuttingDown;
769a41a0 sago007 2008-09-24 12:54 123
protected:
769a41a0 sago007 2008-09-24 12:54 124
    
769a41a0 sago007 2008-09-24 12:54 125
    Config();
769a41a0 sago007 2008-09-24 12:54 126
    
769a41a0 sago007 2008-09-24 12:54 127
    
769a41a0 sago007 2008-09-24 12:54 128
public:
fcd5a134 sago007 2008-12-19 01:11 129
    /*Config is a singleton.
fcd5a134 sago007 2008-12-19 01:11 130
     *It is accessed like this:
fcd5a134 sago007 2008-12-19 01:11 131
     *Config::getInstance()->method2call(paramters);
fcd5a134 sago007 2008-12-19 01:11 132
     */
769a41a0 sago007 2008-09-24 12:54 133
    static Config* getInstance();
769a41a0 sago007 2008-09-24 12:54 134
    
fcd5a134 sago007 2008-12-19 01:11 135
    /*save()
fcd5a134 sago007 2008-12-19 01:11 136
     *forces the config to be written to disk. This will also happened if the
fcd5a134 sago007 2008-12-19 01:11 137
     *program terminates normally.
fcd5a134 sago007 2008-12-19 01:11 138
     */
769a41a0 sago007 2008-09-24 12:54 139
    void save();
769a41a0 sago007 2008-09-24 12:54 140
    
fcd5a134 sago007 2008-12-19 01:11 141
    /*getString(varName)
fcd5a134 sago007 2008-12-19 01:11 142
     *Looks in the config file and returns the string that matches the key "varName"
fcd5a134 sago007 2008-12-19 01:11 143
     *Returns an empty string if varName does not exist.
fcd5a134 sago007 2008-12-19 01:11 144
     */
769a41a0 sago007 2008-09-24 12:54 145
    string getString(string varName);
769a41a0 sago007 2008-09-24 12:54 146
    
fcd5a134 sago007 2008-12-19 01:11 147
    /*getInt(varName)
fcd5a134 sago007 2008-12-19 01:11 148
     *Looks in the config file and returns the int that matches the key "varName"
fcd5a134 sago007 2008-12-19 01:11 149
     *Returns "0" if varName does not exist or cannot be parsed.
fcd5a134 sago007 2008-12-19 01:11 150
     */
769a41a0 sago007 2008-09-24 12:54 151
    int getInt(string varName);
769a41a0 sago007 2008-09-24 12:54 152
    
fcd5a134 sago007 2008-12-19 01:11 153
    /*getValue(varName)
fcd5a134 sago007 2008-12-19 01:11 154
     *Looks in the config file and returns the double that matches the key "varName"
fcd5a134 sago007 2008-12-19 01:11 155
     *Returns "0.0" if varName does not exist or cannot be parsed.
fcd5a134 sago007 2008-12-19 01:11 156
     */
769a41a0 sago007 2008-09-24 12:54 157
    double getValue(string varName);
769a41a0 sago007 2008-09-24 12:54 158
    
fcd5a134 sago007 2008-12-19 01:11 159
    /*setString(varName,content)
fcd5a134 sago007 2008-12-19 01:11 160
     *Sets the config variable with key "varName" to the value of "content"
fcd5a134 sago007 2008-12-19 01:11 161
     */
769a41a0 sago007 2008-09-24 12:54 162
    void setString(string varName,string content);
769a41a0 sago007 2008-09-24 12:54 163
    
fcd5a134 sago007 2008-12-19 01:11 164
    /*setInt(varName,content)
fcd5a134 sago007 2008-12-19 01:11 165
     *Sets the config variable with key "varName" to the value of "content"
fcd5a134 sago007 2008-12-19 01:11 166
     */
769a41a0 sago007 2008-09-24 12:54 167
    void setInt(string varName,int content);
769a41a0 sago007 2008-09-24 12:54 168
    
cc3ef158 sago007 2011-07-03 16:13 169
    /**
cc3ef158 sago007 2011-07-03 16:13 170
     * Sets a config variable to a given (double) value
cc3ef158 sago007 2011-07-03 16:13 171
     * @param varName Name of the variable to set 
cc3ef158 sago007 2011-07-03 16:13 172
     * @param content Value to give the variable
fcd5a134 sago007 2008-12-19 01:11 173
     */
769a41a0 sago007 2008-09-24 12:54 174
    void setValue(string varName,double content);
769a41a0 sago007 2008-09-24 12:54 175
    
cc3ef158 sago007 2011-07-03 16:13 176
    /**
cc3ef158 sago007 2011-07-03 16:13 177
     * returns true if the key varName exists. This is used the first time 1.4.0
cc3ef158 sago007 2011-07-03 16:13 178
     * starts so that it can see that it has to import configs from an earlier 
cc3ef158 sago007 2011-07-03 16:13 179
     * version.
cc3ef158 sago007 2011-07-03 16:13 180
     * @param varName Name of the variable
cc3ef158 sago007 2011-07-03 16:13 181
     * @return true if the varaible exists
fcd5a134 sago007 2008-12-19 01:11 182
     */
cc3ef158 sago007 2011-07-03 16:13 183
    bool exists(string varName) const;
42f8ed5f sago007 2009-03-05 11:47 184
    
42f8ed5f sago007 2009-03-05 11:47 185
    /*setDefault(varName,value)
42f8ed5f sago007 2009-03-05 11:47 186
     *if the variable "varName" does not exist it will be created with value "value"
42f8ed5f sago007 2009-03-05 11:47 187
     *if varName exists then this will have no effect
42f8ed5f sago007 2009-03-05 11:47 188
     */
cc3ef158 sago007 2011-07-03 16:13 189
    /**
cc3ef158 sago007 2011-07-03 16:13 190
     * Set default value for a variable. If the variable "varName" does not exist it will be created with value "value"
cc3ef158 sago007 2011-07-03 16:13 191
     * if varName exists then this will have no effect
cc3ef158 sago007 2011-07-03 16:13 192
     * @param varName Name of the variable
cc3ef158 sago007 2011-07-03 16:13 193
     * @param content The default value
cc3ef158 sago007 2011-07-03 16:13 194
     */
42f8ed5f sago007 2009-03-05 11:47 195
    void setDefault(string varName, string content);
cc3ef158 sago007 2011-07-03 16:13 196
    
cc3ef158 sago007 2011-07-03 16:13 197
    /**
cc3ef158 sago007 2011-07-03 16:13 198
     * Should be set if the user has requested the program to shutdown.
cc3ef158 sago007 2011-07-03 16:13 199
     * @param shuttingDown value of shutdown command. 5 = default = shutdown but allow saving
cc3ef158 sago007 2011-07-03 16:13 200
     */
cc3ef158 sago007 2011-07-03 16:13 201
    void setShuttingDown(long shuttingDown = 5);
cc3ef158 sago007 2011-07-03 16:13 202
    
cc3ef158 sago007 2011-07-03 16:13 203
    /**
cc3ef158 sago007 2011-07-03 16:13 204
     * tells if the user wants to shutdown. This can be used if the exit button is pressed deaply in the program.
cc3ef158 sago007 2011-07-03 16:13 205
     * @return 
cc3ef158 sago007 2011-07-03 16:13 206
     */
cc3ef158 sago007 2011-07-03 16:13 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