-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.hpp
152 lines (131 loc) · 3.92 KB
/
Game.hpp
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
#include "stdafx.h"
#include "ImageManager.hpp"
#include "CollisionManager.hpp"
#include "InputManager.hpp"
#include "entities\EntityManager.hpp"
#include "entities\Entity.hpp"
#include "entities\Player.hpp"
#include "entities\ProjectileManager.hpp"
/*!
* \brief The game itself
* \details Stores user settings, gaming loops and game steps
* \author Vincent Studer
* \date 2013
* \pre None
* \bug None
* \warning None
* \copyright GNU Public License.
*/
class Game
{
public:
Game();
~Game();
//! Starts the game (creating the window included). Used after loadConfig().
void StartGame();
//! Loads the configuration file "config.txt" and saves the parameters
bool loadConfig();
private:
//! Used to check the lines in the config file and to save the parameter in the proper variable
template<typename T>
void checkAndSave(std::string line, std::string reference, T &t);
//! The path to the config file
const std::string m_configFile;
//! The resolution of the window
sf::Vector2i resolution;
//! The configuration parameters list
std::vector<std::string> configList;
};
void tokenize(const std::string& str, std::vector< std::string>& tokens);
Game::Game():
m_configFile("config.txt")
{
}
Game::~Game()
{
}
void Game::StartGame()
{
sf::RenderWindow window(sf::VideoMode(resolution.x, resolution.y), "ShootEmUp");
ImageManager imageManager;
sf::Event event;
ProjectileManager ProjManager(imageManager, window);
sf::Texture text;
text.loadFromImage(imageManager.getImage("images/player.png"));
Player player(text, ProjManager);
EntityManager entityManager(imageManager, window, player);
CollisionManager collisionManager(entityManager, ProjManager, resolution);
InputManager inputManager(entityManager, window, event);
entityManager.createEnnemy(sf::Vector2f(100,100));
entityManager.createEnnemy(sf::Vector2f(800,500));
entityManager.createEnnemy(sf::Vector2f(800,00));
entityManager.createEnnemy(sf::Vector2f(0,500));
entityManager.createEnnemy(sf::Vector2f(000,000));
entityManager.createEnnemy(sf::Vector2f(800,500));
while (window.isOpen())
{
window.clear();
inputManager.update();
collisionManager.update();
entityManager.update();
ProjManager.update();
entityManager.draw();
ProjManager.draw();
window.display();
sf::sleep(sf::Time(sf::milliseconds(10)));
}
}
template<typename T>
void Game::checkAndSave(std::string line, std::string reference, T &t)
{
std::vector<std::string> tokens;
tokenize(line, tokens);
if(strcmp(tokens[0].c_str(), reference.c_str())==0)
{
std::istringstream buffer(tokens[1]);
int value = atoi(tokens[1].c_str());
t = value;
}
}
bool Game::loadConfig()
{
std::ifstream fichier(m_configFile, std::ios::in);
if(fichier) // If the file exists
{
std::string line;
while(!strcmp(line.data(), "OVER")==0)
{
getline(fichier, line);
configList.push_back(line);
}
fichier.close();
}
else
{
std::cout << "Config file not found !" << std::endl;
return false;
}
for(int l(0); l < configList.size(); ++l)
{
checkAndSave<int>(configList[l], "resolutionX", resolution.x);
checkAndSave<int>(configList[l], "resolutionY", resolution.y);
}
return true;
}
void tokenize(const std::string& str, std::vector< std::string>& tokens)
{
std::string::size_type lastPos = str.find_first_not_of(" ", 0);
//Le premier "non délimiteur"
std::string::size_type pos = str.find_first_of(" ", lastPos);
while ( std::string::npos != pos || std::string::npos != lastPos)
{
// On trouve un token, on l'ajoute au vecteur
tokens.push_back(str.substr(lastPos, pos - lastPos));
// On passe le délimiteur
lastPos = str.find_first_not_of(" ", pos);
// On repère le prochain token
pos = str.find_first_of(" ", lastPos);
}
if(tokens.empty())
tokens.push_back("empty");
}