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