-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSDL_Handler.cpp
More file actions
305 lines (277 loc) · 8.55 KB
/
SDL_Handler.cpp
File metadata and controls
305 lines (277 loc) · 8.55 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "SDL_Handler.h"
/* Handles all input events*/
void handle_Events(SDL_Event event, Map *map, Player *manual_Player)
{
switch (event.type) {
/* close button clicked */
case SDL_QUIT:
gameover = 1;
break;
/* handle the keyboard */
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
case SDLK_q:
gameover = 1;
break;
case SDLK_LEFT:
if(manual_Player != NULL && map->can_Move(manual_Player, -1) && manual_Player->is_Alive())
manual_Player->move(LEFT);
break;
case SDLK_RIGHT:
if(manual_Player != NULL && map->can_Move(manual_Player, 1) && manual_Player->is_Alive())
manual_Player->move(RIGHT);
break;
case SDLK_UP:
if(manual_Player != NULL && map->can_Move(manual_Player, -NUM_COLS) && manual_Player->is_Alive())
manual_Player->move(UP);
break;
case SDLK_DOWN:
if(manual_Player != NULL && map->can_Move(manual_Player, NUM_COLS) && manual_Player->is_Alive())
manual_Player->move(DOWN);
break;
case SDLK_SPACE:
if(manual_Player != NULL && map->can_Move(manual_Player, 0) && manual_Player->is_Alive() && manual_Player->can_Place())
{ // Can only plant 1 bomb per location
Bomb* temp = new Bomb(manual_Player);
map->add_bomb(temp);
}
break;
case SDLK_RETURN:
if(manual_Player != NULL && manual_Player->has_detonator() && manual_Player->is_Alive())
{ // Detonates all is bombs
map->detonate(manual_Player->get_Id());
}
break;
}
break;
}
}
void handle_Replay_Controls( SDL_Event event , int &state,Logger *lg, std::vector<Player*> &all_Players)
{
switch (event.type) {
/* close button clicked */
case SDL_QUIT:
gameover = 1;
break;
/* handle the keyboard */
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
case SDLK_q:
gameover = 1;
break;
case SDLK_LEFT:
if ( state > 0)
state--;
break;
case SDLK_RIGHT:
state++;
break;
case SDLK_UP:
state += 20;
break;
case SDLK_DOWN:
state -= 20;
break;
}
lg->read_state(state, all_Players);
}
}
/* Initializes SDL windows surface*/
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "Bomberman Learning Platform", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
return success;
}
/* Loads the bithmap and converts it to fisplay format*/
SDL_Surface* bitmap_Loader(const char* path)
{
// load bitmap to temp surface
SDL_Surface* loadedSurface = SDL_LoadBMP(path);
if( loadedSurface == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", path, SDL_GetError() );
gameover = 1;
return NULL;
}
//Convert surface to screen format
SDL_Surface* optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL );
if( optimizedSurface == NULL )
{
printf( "Unable to optimize image %s! SDL Error: %s\n", path, SDL_GetError() );
}
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
return optimizedSurface;
}
/* Free resources and close SDL */
void close()
{
//Deallocate all surfaces
SDL_FreeSurface(grass);
SDL_FreeSurface(wall);
SDL_FreeSurface(stone);
SDL_FreeSurface(bomb);
SDL_FreeSurface(explosion);
SDL_FreeSurface(bomb_pu);
SDL_FreeSurface(range_pu);
SDL_FreeSurface(ghost_pu);
SDL_FreeSurface(speed_pu);
SDL_FreeSurface(switch_pu);
SDL_FreeSurface(slide_pu);
//Deallocate surface
SDL_FreeSurface( gScreenSurface );
gScreenSurface = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
bool load_Media(char* lvl)
{
grass = bitmap_Loader("Images/grass.bmp");
wall = bitmap_Loader("Images/brick_wall.bmp");
stone = bitmap_Loader("Images/stone_wall.bmp");
bomb = bitmap_Loader("Images/bomb.bmp");
explosion = bitmap_Loader("Images/explosion.bmp");
bomb_pu = bitmap_Loader("Images/bomb_powerup.bmp");
range_pu = bitmap_Loader("Images/range_powerup.bmp");
ghost_pu = bitmap_Loader("Images/ghost_powerup.bmp");
speed_pu = bitmap_Loader("Images/speed_powerup.bmp");
switch_pu = bitmap_Loader("Images/switch_powerup.bmp");
slide_pu = bitmap_Loader("Images/slide_powerup.bmp");
return true;
}
void draw_Player(std::vector<Player*> players)
{
/*Draw all live players */
for ( Player* p : players){
if( p != NULL && p->is_Alive())
{
load_Object(p);
}
}
}
void draw_Map(Map *map)
{
SDL_Rect rcPosition;
/* Textures */
char ch;
// std::cout << "d1" << std::endl;
for (int y = 0; y < NUM_ROWS; y++)
{
for (int x = 0; x < NUM_COLS; x++)
{
char ch = (*map)[ mIndex(x,y)];
rcPosition.x = x * SPRITE_SIZE;
rcPosition.y = y * SPRITE_SIZE;
if(ch == WALL)
SDL_BlitSurface(wall, NULL, gScreenSurface, &rcPosition);
else if (ch == GRASS)
SDL_BlitSurface(grass, NULL, gScreenSurface, &rcPosition);
else if (ch == STONE || ch == STONE_PUP)
SDL_BlitSurface(stone, NULL, gScreenSurface, &rcPosition);
else if (ch == BOMB){
SDL_BlitSurface(grass, NULL, gScreenSurface, &rcPosition);
/* setup sprite colorkey and turn on RLE */
int colorkey = SDL_MapRGB(gScreenSurface->format, 255, 0, 255);
SDL_SetColorKey(bomb, SDL_TRUE | SDL_RLEACCEL, colorkey);
SDL_BlitSurface(bomb, NULL, gScreenSurface, &rcPosition);
}
else if (ch == EXPLOSION)
{
SDL_BlitSurface(grass, NULL, gScreenSurface, &rcPosition);
/* setup sprite colorkey and turn on RLE */
int colorkey = SDL_MapRGB(gScreenSurface->format, 255, 0, 255);
SDL_SetColorKey(explosion, SDL_TRUE | SDL_RLEACCEL, colorkey);
SDL_BlitSurface(explosion, NULL, gScreenSurface, &rcPosition);
}
else if (ch == BOMB_PUP)
{
SDL_BlitSurface(bomb_pu, NULL, gScreenSurface, &rcPosition);
}
else if (ch == RANGE_PUP)
{
SDL_BlitSurface(range_pu, NULL, gScreenSurface, &rcPosition);
}
else if (ch == GHOST_PUP)
{
SDL_BlitSurface(ghost_pu, NULL, gScreenSurface, &rcPosition);
}
else if (ch == SPEED_PUP)
{
SDL_BlitSurface(speed_pu, NULL, gScreenSurface, &rcPosition);
}
else if (ch == SWITCH_PUP)
{
SDL_BlitSurface(switch_pu, NULL, gScreenSurface, &rcPosition);
}
else if (ch == SLIDE_PUP)
{
SDL_BlitSurface(slide_pu, NULL, gScreenSurface, &rcPosition);
}
}
}
// std::cout << "d2" << std::endl;
}
bool load_Map(const char* path)
{
char ch;
std::fstream fin( path, std::fstream::in);
if ( fin == NULL)
return false;
/* Gets the map from the input */
for (int y = 0; y < NUM_ROWS; y++)
{
for (int x = 0; x < NUM_COLS; x++)
{
fin >> std::noskipws >> ch;
if(ch == '\n')
fin >> std::noskipws >> ch;
if ( map_randomizer && ch == '+'){
int rand_decision = rand() % 5;
if ( rand_decision == 0 )
ch = '-';
}
world_Map[ mIndex(x,y)] = ch;
}
}
return true;
}
template <typename Type>
void load_Object(Type object)
{
SDL_Rect rcobject;
rcobject.x = object->get_X();
rcobject.y = object->get_Y();
SDL_Surface* object_surface = bitmap_Loader(object->get_Skin());
/* setup sprite colorkey and turn on RLE */
int colorkey = SDL_MapRGB(gScreenSurface->format, 255, 0, 255);
SDL_SetColorKey(object_surface, SDL_TRUE | SDL_RLEACCEL, colorkey);
SDL_BlitSurface(object_surface, NULL, gScreenSurface, &rcobject);
SDL_FreeSurface(object_surface);
}