-
Notifications
You must be signed in to change notification settings - Fork 0
/
Duell.cpp
318 lines (291 loc) · 6.09 KB
/
Duell.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <SDL.h>
#include <SDL_mixer.h>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <iostream>
#include "Duell.h"
#include "Player.h"
#include "Utils.h"
SDL_Texture *bg, * bullet;
const Uint8 *keys;
int state = SPLASH;
bool done = false;
Player *player1, *player2;
Mix_Chunk *shoot, *explode;
Mix_Music *music;
// The function where everything begins...
int main(int argc, char *argv[])
{
SDL_Event event;
// Inits
if(InitSDL() != 0)
{
std::cout << "Can't init SDL!" << std::endl;
return 1;
}
// Main loop
while(!done)
{
// Message processing
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
done = true;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDL_SCANCODE_ESCAPE) {
done = true;
}
break;
default:
break;
}
}
// Gameworks
switch(state)
{
default:
case SPLASH:
ShowSplash();
break;
case START:
InitGame();
break;
case RUNNING:
UpdateGame();
break;
}
}
if(bg != nullptr)
SDL_DestroyTexture(bg);
if(bullet != nullptr)
SDL_DestroyTexture(bullet);
delete player1, player2;
Mix_FreeChunk(shoot);
Mix_FreeChunk(explode);
Mix_FreeMusic(music);
return 0;
}
// Show Splashscreen until pressing enter
void ShowSplash()
{
SDL_Texture *splashimg;
splashimg = LoadBMP("splash.jpg");
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, splashimg, nullptr, nullptr);
SDL_RenderPresent(renderer);
while(!done)
{
// First we need to refresh the keytable array
SDL_PumpEvents();
// And then we read it out...
keys = SDL_GetKeyboardState(nullptr);
// Now we look, if some special key is pressed
if(keys[SDL_SCANCODE_ESCAPE])
{
done = true;
exit(0);
}
if (keys[SDL_SCANCODE_RETURN]) {
break;
}
}
// Free the memory from the splashscreen
SDL_DestroyTexture(splashimg);
state = START;
}
// Init the game
void InitGame()
{
shoot = Mix_LoadWAV((ASSET_PATH + "shoot.wav").c_str());
explode = Mix_LoadWAV((ASSET_PATH + "explode.wav").c_str());
music = Mix_LoadMUS((ASSET_PATH + "music.wav").c_str());
bg = LoadBMP("bg.jpg");
if(bg == nullptr)
{
printf("Can't load background image!");
exit(1);
}
bullet = LoadBMP("bullet.bmp");
if(bullet == nullptr)
{
printf("Can't load bullet image!");
exit(1);
}
SDL_RenderCopy(renderer, bg, nullptr, nullptr);
// Init Players
player1 = new Player(1);
player2 = new Player(2);
state = RUNNING;
if(Mix_PlayMusic(music, -1) == -1)
printf("Can't load music!");
}
// Update the complete game stuff
void UpdateGame()
{
// Get the time of the start of this frame
Uint32 lframe = SDL_GetTicks();
// Input handling
keys = SDL_GetKeyboardState(nullptr);
// Player 1 Controls
if(keys[SDL_SCANCODE_UP])
{
if(player1->GetRect()->y > 0)
player1->Move(UP);
}
if(keys[SDL_SCANCODE_DOWN])
{
if(player1->GetRect()->y + player1->GetRect()->h < SCREEN_H)
player1->Move(DOWN);
}
if(keys[SDL_SCANCODE_RIGHT])
{
if(player1->GetRect()->x + player1->GetRect()->w < SCREEN_W / 2)
player1->Move(RIGHT);
}
if(keys[SDL_SCANCODE_LEFT])
{
if(player1->GetRect()->x > 0)
player1->Move(LEFT);
}
if(keys[SDL_SCANCODE_RCTRL])
{
player1->Shoot();
}
// Player 2 Controls
if(keys[SDL_SCANCODE_W])
{
if(player2->GetRect()->y > 0)
player2->Move(UP);
}
if(keys[SDL_SCANCODE_S])
{
if(player2->GetRect()->y + player2->GetRect()->h < SCREEN_H)
player2->Move(DOWN);
}
if(keys[SDL_SCANCODE_D])
{
if(player2->GetRect()->x + player2->GetRect()->w < SCREEN_W)
player2->Move(RIGHT);
}
if(keys[SDL_SCANCODE_A])
{
if(player2->GetRect()->x > SCREEN_W / 2)
player2->Move(LEFT);
}
if(keys[SDL_SCANCODE_LCTRL])
{
player2->Shoot();
}
if (keys[SDL_SCANCODE_ESCAPE]) {
exit(0);
}
// Move all Objects that should move
player1->MoveBullets(BULLETSPEED);
player2->MoveBullets(BULLETSPEED);
// Test Colls
TestColl();
// Redraw the Background
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bg, nullptr, nullptr);
// Draws all Objects that should draw
player1->DrawBullets();
player2->DrawBullets();
// Draw players
player1->Draw();
player2->Draw();
SDL_RenderPresent(renderer);
// Wait, that the game won't become faster as 24fps
while(SDL_GetTicks() - lframe < 40);
if(player1->GetHealth() == 0)
{
SDL_Delay(3000);
exit(0);
}
if(player2->GetHealth() == 0)
{
SDL_Delay(3000);
exit(0);
}
}
// Look if the 2 given rectangles touch each other
bool Coll(SDL_Rect *rect1, SDL_Rect *rect2)
{
int left1 = rect1->x;
int left2 = rect2->x;
int right1 = rect1->x + rect1->w;
int right2 = rect2->x + rect2->w;
int top1 = rect1->y;
int top2 = rect2->y;
int bottom1 = rect1->y + rect1->h;
int bottom2 = rect2->y + rect2->h;
return !((bottom1 < top2) || (top1 > bottom2) || (right1 < left2) || (left1 > right2));
}
// React on Collision
void TestColl()
{
int i;
// Check if a bullet touch another
for(int a=0; a<BULLETS; a++)
{
for(int b=0; b<BULLETS; b++)
{
if(player1->BulletVisible(a) && player2->BulletVisible(b))
{
if(Coll(player1->GetBulletRect(a), player2->GetBulletRect(b)))
{
player1->InvisibleBullet(a);
player2->InvisibleBullet(b);
}
}
}
}
// Check if a bullet from p1 touch p2
for(i=0; i<BULLETS; i++)
{
if(player1->BulletVisible(i))
{
if(Coll(player2->GetRect(), player1->GetBulletRect(i)))
{
player2->Hurt();
player1->InvisibleBullet(i);
}
}
}
// Check if a bullet from p2 touch p1
for(i=0; i<BULLETS; i++)
{
if(player2->BulletVisible(i))
{
if(Coll(player1->GetRect(), player2->GetBulletRect(i)))
{
player1->Hurt();
player2->InvisibleBullet(i);
}
}
}
// Check if a bullet from p1 goes out of the screen
SDL_Rect *bulletrect;
for(i=0; i<BULLETS; i++)
{
if(player1->BulletVisible(i))
{
bulletrect = player1->GetBulletRect(i);
if(bulletrect->x + bulletrect->w > SCREEN_W)
player1->InvisibleBullet(i);
}
}
// Check if a bullet from p2 goes out of the screen
for(i=0; i<BULLETS; i++)
{
if(player2->BulletVisible(i))
{
bulletrect = player2->GetBulletRect(i);
if(bulletrect->x <= 0)
player2->InvisibleBullet(i);
}
}
}