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>
89c4a3a6 sago007 2008-08-29 14:32 48
89c4a3a6 sago007 2008-08-29 14:32 49
using namespace std;
89c4a3a6 sago007 2008-08-29 14:32 50
89c4a3a6 sago007 2008-08-29 14:32 51
struct commonTime{
89c4a3a6 sago007 2008-08-29 14:32 52
    unsigned int days;
89c4a3a6 sago007 2008-08-29 14:32 53
    unsigned int hours;
89c4a3a6 sago007 2008-08-29 14:32 54
    unsigned int minutes;
89c4a3a6 sago007 2008-08-29 14:32 55
    unsigned int seconds;
89c4a3a6 sago007 2008-08-29 14:32 56
};
89c4a3a6 sago007 2008-08-29 14:32 57
769a41a0 sago007 2008-09-24 12:54 58
string itoa(int num);
769a41a0 sago007 2008-09-24 12:54 59
89c4a3a6 sago007 2008-08-29 14:32 60
string getPathToSaveFiles();
89c4a3a6 sago007 2008-08-29 14:32 61
cd12e0fe sago007 2009-01-29 08:47 62
class TimeHandler
cd12e0fe sago007 2009-01-29 08:47 63
{
cd12e0fe sago007 2009-01-29 08:47 64
public:
cd12e0fe sago007 2009-01-29 08:47 65
	static commonTime ms2ct(unsigned int milliseconds);
cd12e0fe sago007 2009-01-29 08:47 66
	
cd12e0fe sago007 2009-01-29 08:47 67
	static commonTime getTime(string name);
cd12e0fe sago007 2009-01-29 08:47 68
	
cd12e0fe sago007 2009-01-29 08:47 69
	static commonTime peekTime(string name, commonTime toAdd);
cd12e0fe sago007 2009-01-29 08:47 70
	
cd12e0fe sago007 2009-01-29 08:47 71
	static commonTime addTime(string name, commonTime toAdd);
cd12e0fe sago007 2009-01-29 08:47 72
};
89c4a3a6 sago007 2008-08-29 14:32 73
769a41a0 sago007 2008-09-24 12:54 74
#define MAX_VAR_LENGTH 1024
769a41a0 sago007 2008-09-24 12:54 75
fcd5a134 sago007 2008-12-19 01:11 76
/*This is the Config class it is a map to hold config variables.
fcd5a134 sago007 2008-12-19 01:11 77
 *It is inspired by Quake 3's CVAR system although a lot simpler.
fcd5a134 sago007 2008-12-19 01:11 78
 *All variables have keys "varName" that is used to access a variable.
fcd5a134 sago007 2008-12-19 01:11 79
 *
fcd5a134 sago007 2008-12-19 01:11 80
 *This class is a singleton
fcd5a134 sago007 2008-12-19 01:11 81
 */
769a41a0 sago007 2008-09-24 12:54 82
class Config
769a41a0 sago007 2008-09-24 12:54 83
{
769a41a0 sago007 2008-09-24 12:54 84
private: 
769a41a0 sago007 2008-09-24 12:54 85
    map<string,string> configMap;
769a41a0 sago007 2008-09-24 12:54 86
769a41a0 sago007 2008-09-24 12:54 87
    static Config *instance;
769a41a0 sago007 2008-09-24 12:54 88
    
769a41a0 sago007 2008-09-24 12:54 89
    void load();
769a41a0 sago007 2008-09-24 12:54 90
protected:
769a41a0 sago007 2008-09-24 12:54 91
    
769a41a0 sago007 2008-09-24 12:54 92
    Config();
769a41a0 sago007 2008-09-24 12:54 93
    
769a41a0 sago007 2008-09-24 12:54 94
    
769a41a0 sago007 2008-09-24 12:54 95
public:
fcd5a134 sago007 2008-12-19 01:11 96
    /*Config is a singleton.
fcd5a134 sago007 2008-12-19 01:11 97
     *It is accessed like this:
fcd5a134 sago007 2008-12-19 01:11 98
     *Config::getInstance()->method2call(paramters);
fcd5a134 sago007 2008-12-19 01:11 99
     */
769a41a0 sago007 2008-09-24 12:54 100
    static Config* getInstance();
769a41a0 sago007 2008-09-24 12:54 101
    
fcd5a134 sago007 2008-12-19 01:11 102
    /*save()
fcd5a134 sago007 2008-12-19 01:11 103
     *forces the config to be written to disk. This will also happened if the
fcd5a134 sago007 2008-12-19 01:11 104
     *program terminates normally.
fcd5a134 sago007 2008-12-19 01:11 105
     */
769a41a0 sago007 2008-09-24 12:54 106
    void save();
769a41a0 sago007 2008-09-24 12:54 107
    
fcd5a134 sago007 2008-12-19 01:11 108
    /*getString(varName)
fcd5a134 sago007 2008-12-19 01:11 109
     *Looks in the config file and returns the string that matches the key "varName"
fcd5a134 sago007 2008-12-19 01:11 110
     *Returns an empty string if varName does not exist.
fcd5a134 sago007 2008-12-19 01:11 111
     */
769a41a0 sago007 2008-09-24 12:54 112
    string getString(string varName);
769a41a0 sago007 2008-09-24 12:54 113
    
fcd5a134 sago007 2008-12-19 01:11 114
    /*getInt(varName)
fcd5a134 sago007 2008-12-19 01:11 115
     *Looks in the config file and returns the int that matches the key "varName"
fcd5a134 sago007 2008-12-19 01:11 116
     *Returns "0" if varName does not exist or cannot be parsed.
fcd5a134 sago007 2008-12-19 01:11 117
     */
769a41a0 sago007 2008-09-24 12:54 118
    int getInt(string varName);
769a41a0 sago007 2008-09-24 12:54 119
    
fcd5a134 sago007 2008-12-19 01:11 120
    /*getValue(varName)
fcd5a134 sago007 2008-12-19 01:11 121
     *Looks in the config file and returns the double that matches the key "varName"
fcd5a134 sago007 2008-12-19 01:11 122
     *Returns "0.0" if varName does not exist or cannot be parsed.
fcd5a134 sago007 2008-12-19 01:11 123
     */
769a41a0 sago007 2008-09-24 12:54 124
    double getValue(string varName);
769a41a0 sago007 2008-09-24 12:54 125
    
fcd5a134 sago007 2008-12-19 01:11 126
    /*setString(varName,content)
fcd5a134 sago007 2008-12-19 01:11 127
     *Sets the config variable with key "varName" to the value of "content"
fcd5a134 sago007 2008-12-19 01:11 128
     */
769a41a0 sago007 2008-09-24 12:54 129
    void setString(string varName,string content);
769a41a0 sago007 2008-09-24 12:54 130
    
fcd5a134 sago007 2008-12-19 01:11 131
    /*setInt(varName,content)
fcd5a134 sago007 2008-12-19 01:11 132
     *Sets the config variable with key "varName" to the value of "content"
fcd5a134 sago007 2008-12-19 01:11 133
     */
769a41a0 sago007 2008-09-24 12:54 134
    void setInt(string varName,int content);
769a41a0 sago007 2008-09-24 12:54 135
    
fcd5a134 sago007 2008-12-19 01:11 136
    /*setValue(varName,content)
fcd5a134 sago007 2008-12-19 01:11 137
     *Sets the config variable with key "varName" to the value of "content"
fcd5a134 sago007 2008-12-19 01:11 138
     */
769a41a0 sago007 2008-09-24 12:54 139
    void setValue(string varName,double content);
769a41a0 sago007 2008-09-24 12:54 140
    
fcd5a134 sago007 2008-12-19 01:11 141
    /*exists(varName)
fcd5a134 sago007 2008-12-19 01:11 142
     *returns true if the key varName exists. This is used the first time 1.4.0
fcd5a134 sago007 2008-12-19 01:11 143
     *starts so that it can see that it has to import configs from an earlier 
fcd5a134 sago007 2008-12-19 01:11 144
     *version.
fcd5a134 sago007 2008-12-19 01:11 145
     */
769a41a0 sago007 2008-09-24 12:54 146
    bool exists(string varName);
769a41a0 sago007 2008-09-24 12:54 147
};
769a41a0 sago007 2008-09-24 12:54 148
89c4a3a6 sago007 2008-08-29 14:32 149
#endif	/* _COMMON_H */
89c4a3a6 sago007 2008-08-29 14:32 150
1970-01-01 00:00 151