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