-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
190 lines (167 loc) · 4.84 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
#include <SDL2/SDL.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include "GameEntity.h"
#include "Player.h"
#include "Alien.h"
#include "Bullet.h"
#include "constants.h"
bool running = true;
SDL_Window* window = NULL;
SDL_Surface* surface = NULL;
bool outsideWindow(GameEntity* gameEntity);
bool collision(GameEntity* geA, GameEntity* geB);
int main(int argc, char** argv) {
// Initialize
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "SDL couldn't initialize: " << SDL_GetError() << std::endl;
return 0;
}
window = SDL_CreateWindow("Space Invaders", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
std::cout << "Window couldn't be created: " << SDL_GetError() << std::endl;
return 0;
}
surface = SDL_GetWindowSurface(window);
Player* player = new Player;
std::vector<GameEntity*> gameEntities;
std::vector<Alien*> aliens;
std::vector<Bullet*> bullets;
gameEntities.push_back(player);
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 5; ++j) {
Alien *a = new Alien(ENEMY_MARGIN + 55 * i, 0, j);
gameEntities.push_back(a);
aliens.push_back(a);
}
}
Uint32 lastTime = SDL_GetTicks();
Uint32 lastShot = 0;
// Main loop
while (running) {
// Timing
Uint32 currentTime = SDL_GetTicks();
Uint32 delta = currentTime - lastTime;
lastTime = currentTime;
/* std::cout << "size: " << gameEntities.size() << std::endl << std::flush; */
// Clear, update, and draw
SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0x00, 0x00, 0x00));
for (auto &gameEntity : gameEntities) {
gameEntity->update(delta);
SDL_BlitScaled(gameEntity->bitmap, gameEntity->srcrect, surface, &(gameEntity->dstrect));
}
SDL_UpdateWindowSurface(window);
// Check for collision between bullets and aliens. Also destroys bullets
// outside of the window.
// Todo: Clean this up.
for (int i = 0; i < bullets.size(); ++i) {
for (int j = 0; j < aliens.size(); ++j) {
if (collision(bullets[i], aliens[j])) {
for (int k = 0; k < gameEntities.size(); ++k) {
if (gameEntities[k] == aliens[j]) {
gameEntities.erase(gameEntities.begin() + k);
delete aliens[j];
aliens.erase(aliens.begin() + j);
}
if (gameEntities[k] == bullets[i]) {
gameEntities.erase(gameEntities.begin() + k);
delete bullets[i];
bullets.erase(bullets.begin() + i);
}
}
}
}
if (outsideWindow(bullets[i])) {
for (int k = 0; k < gameEntities.size(); ++k) {
if (gameEntities[k] == bullets[i]) {
gameEntities.erase(gameEntities.begin() + k);
delete bullets[i];
bullets.erase(bullets.begin() + i);
continue;
}
}
}
}
// Handle events
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
running = false;
} else if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_ESCAPE:
running = false;
break;
default:
break;
}
}
}
// Handle keystate
const Uint8* state = SDL_GetKeyboardState(NULL);
// Movement
if (state[SDL_SCANCODE_LEFT]) {
player->velocity = -0.2f;
} else if (state[SDL_SCANCODE_RIGHT]) {
player->velocity = 0.2f;
} else {
player->velocity = 0;
}
// Shooting
if (state[SDL_SCANCODE_SPACE]) {
// Check cooldown on shoot.
Uint32 time = SDL_GetTicks();
if (time - lastShot < 500) {
continue;
}
int centerX = player->x + player->dstrect.w / 2;
Bullet* b = new Bullet(centerX, player->y);
gameEntities.push_back(b);
bullets.push_back(b);
lastShot = time;
}
}
delete player;
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
bool outsideWindow(GameEntity* gameEntity) {
if (gameEntity->x < 0) {
return true;
}
if (gameEntity->x + gameEntity->dstrect.w > SCREEN_WIDTH) {
return true;
}
if (gameEntity->y < 0) {
return true;
}
if (gameEntity->y + gameEntity->dstrect.h > SCREEN_HEIGHT) {
return true;
}
return false;
}
bool collision(GameEntity* geA, GameEntity* geB) {
int topA = geA->dstrect.y;
int rightA = geA->dstrect.x + geA->dstrect.w;
int bottomA = geA->dstrect.y + geA->dstrect.h;
int leftA = geA->dstrect.x;
int topB = geB->dstrect.y;
int rightB = geB->dstrect.x + geB->dstrect.w;
int bottomB = geB->dstrect.y + geB->dstrect.h;
int leftB = geB->dstrect.x;
if (bottomA <= topB) {
return false;
}
if (topA >= bottomB) {
return false;
}
if (rightA <= leftB) {
return false;
}
if (leftA >= rightB) {
return false;
}
return true;
}