-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
274 lines (225 loc) · 5.87 KB
/
Game.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "Game.h"
#include "InputHandler.h"
Game::Game()
{
currentMapID = 1;
}
Game::~Game()
{
}
void Game::allocMap() {
gameMap_ = new Map(currentMapID, this);
}
void Game::createMap() {
allocMap();
}
bool Game::initialize() {
printf("Program started\n");
if (!initSDL()) {
return false;
printf("Cannot initialize SDL\n");
}
if (!loadTextures()) {
printf("Cannot load textures\n");
return false;
}
mapColors();
initTime();
createMap();
victory = false;
return true;
}
bool Game::initSDL() {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("SDL_Init error: %s\n", SDL_GetError());
return false;
}
if (!initRenderer()) {
printf("Cannot initialize renderer\n");
return false;
}
return true;
}
bool Game::initRenderer() {
// tryb pe³noekranowy / fullscreen mode
// rc = SDL_CreateWindowAndRenderer(0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP,
// &window, &renderer);
rc = SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, 0,
&window, &renderer);
if (rc != 0) {
SDL_Quit();
printf("SDL_CreateWindowAndRenderer error: %s\n", SDL_GetError());
return false;
};
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_SetWindowTitle(window, "Sokoban");
screen = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 32,
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
scrtex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
SCREEN_WIDTH, SCREEN_HEIGHT);
//disable mouse cursor in window
SDL_ShowCursor(SDL_DISABLE);
return true;
}
SDL_Surface* Game::loadBMP(char* path) {
return SDL_LoadBMP(path);
}
bool Game::loadSurface(SDL_Surface** surface, char* path) {
*surface = loadBMP(path);
if (surface == NULL) {
printf("SDL_LoadBMP(%s) error: %s\n", path, SDL_GetError());
cleanSDL();
SDL_Quit();
return false;
}
return true;
}
bool Game::loadTextures() {
if (!loadSurface(&charset, "./cs8x8.bmp")) {
printf("Cannot load charset\n");
return false;
}
//black - transparent (just for charset)
SDL_SetColorKey(charset, SDL_TRUE, 0x000000);
return true;
}
void Game::mapColors() {
black = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
green = SDL_MapRGB(screen->format, 0x00, 0xFF, 0x00);
red = SDL_MapRGB(screen->format, 0xFF, 0x00, 0x00);
blue = SDL_MapRGB(screen->format, 0x11, 0x11, 0xCC);
coolBlue = SDL_MapRGB(screen->format, 0x6F, 0xA8, 0xFF);
}
void Game::initTime() {
t1 = SDL_GetTicks();
quit = false;
worldTime = 0;
}
void Game::drawMenu() {
//info text
//rectangular background
DrawRectangle(screen, 4, 4, SCREEN_WIDTH - 8, 30, red, blue);
//first line of text
sprintf(text, "Sokoban, elapsed time = %.1lf s", worldTime);
DrawString(screen, screen->w / 2 - strlen(text) * 8 / 2, 10, text, charset);
//second line of text
sprintf(text, "ESC - exit, N - New Game, P - next level");
DrawString(screen, screen->w / 2 - strlen(text) * 8 / 2, 20, text, charset);
}
void Game::drawVictoryMenu() {
DrawRectangle(screen, 4, 4, SCREEN_WIDTH - 8, 30, red, blue);
sprintf(text, "Congratulations, you solved the puzzle!");
DrawString(screen, screen->w / 2 - strlen(text) * 8 / 2, 10, text, charset);
sprintf(text, "Time = %.1lf s, P - next level", worldTime);
DrawString(screen, screen->w / 2 - strlen(text) * 8 / 2, 20, text, charset);
}
// cleans all SDL Related things
void Game::freeSpace() {
SDL_FreeSurface(charset);
cleanSDL();
SDL_Quit();
}
// renders everything on screen
void Game::render() {
//updates everything on texture
SDL_UpdateTexture(scrtex, NULL, screen->pixels, screen->pitch);
//connects the new texture with renderer
SDL_RenderCopy(renderer, scrtex, NULL, NULL);
//renders
SDL_RenderPresent(renderer);
}
void Game::handleEvents() {
InputHandler* handler = new InputHandler();
switch (handler->handleKeyStates()) {
case MOVE_UP:
gameMap_->getPlayer()->movePlayer(0, -1);
break;
case MOVE_DOWN:
gameMap_->getPlayer()->movePlayer(0, 1);
break;
case MOVE_LEFT:
gameMap_->getPlayer()->movePlayer(-1, 0);
break;
case MOVE_RIGHT:
gameMap_->getPlayer()->movePlayer(1, 0);
break;
case NEW_GAME:
initTime();
//call a function reverting the current field
delete gameMap_;
createMap();
break;
case QUIT:
quit = true;
break;
case NEXT_MAP:
delete gameMap_;
initTime();
currentMapID++;
if (currentMapID > MAP_COUNT) {
currentMapID = 1;
}
createMap();
SDL_FillRect(screen, NULL, coolBlue);
drawMenu();
gameMap_->drawMap();
render();
case NO_EVENT:
break;
}
if (handler->handleQuit() == QUIT) {
quit = true;
}
delete handler;
}
void Game::update() {
t2 = SDL_GetTicks();
// here t2-t1 is the time in miliseconds since
// the last screen was drawn
// delta is the same time in seconds
if (!victory) {
delta = (t2 - t1)*0.001;
// clear time difference
t1 = t2;
//increase world time
worldTime += delta;
}
// fill the screen with nice blue
SDL_FillRect(screen, NULL, coolBlue);
//info text
victory = (gameMap_->getCratesOnPosition() == gameMap_->getNumberOfCrates());
if (victory) {
drawVictoryMenu();
}
else
drawMenu();
gameMap_->drawMap();
//render everything on screen
render();
handleEvents();
}
void Game::gameLoop() {
while (!quit) {
//move to update
update();
}
}
// frees all key SDL structures
void Game::cleanSDL() {
SDL_FreeSurface(screen);
SDL_DestroyTexture(scrtex);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
}
void Game::setQuit(const bool value) {
quit = value;
}
Map* Game::getMap() {
return gameMap_;
}
SDL_Surface* Game::getScreen() {
return screen;
}