/
main.cpp
742 lines (656 loc) · 19.6 KB
/
main.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#include <stdio.h>
#include "SDL.h"
#include "SDL/SDL_mixer.h"
#include "block.h"
#include "hero.h"
#include <vector>
#include "CollisionPair.h"
#include "background.h"
#include "Bomb.h"
#include "Explosion.h"
#include <algorithm>
#include "upgrade.h"
#include "SDL_ttf.h"
#include <map>
#include "hellomessage.h"
#include "slotmessage.h"
#include "bombmessage.h"
#include "blockmessage.h"
#include <stdlib.h> /* srand, rand */
#define MULTIPLE 1
#define EXIT 2
#define MENU 3
Hero * hero;
int max_players=-1;
Bomb * bomb; vector<Block * > blocks;
//vector<Hero *> heroGroup;
map<int, Hero*> heroGroup;
vector<Bomb *> bombGroup;
vector<Explosion *> explosionGroup;
vector<Upgrade *> upgradeGroup; bool Connected;
CClientSocket* tcpclient;
CIpAddress* remoteip;
CUdpSocket* udpclient;
vector<CollisionPair * > colList;
int myId;
bool moved=false;
bool bombed=false;
Background * background;
SDL_Surface *text_image[5];
char textbuf[5][80];
SDL_Rect textDest[5];
TTF_Font *text_font;
SDL_Color font_color;
bool start=false;
Mix_Music *menuMusic = NULL;
Mix_Chunk *menuSFX = NULL;
Mix_Music *mainMusic = NULL;
Background * winScreen;
Background * loseScreen;
void handleServer();
void handleNetwork();
void handleClients();
int handle_key(SDLKey k) {
if(!start)
return 0;
switch(k) {
// case SDLK_ESCAPE:
// return 1;
case SDLK_LEFT:
moved=true;
heroGroup[myId]->moveLeft();
break;
case SDLK_RIGHT:
moved=true;
heroGroup[myId]->moveRight();
break;
case SDLK_UP:
moved=true;
heroGroup[myId]->moveUp();
break;
case SDLK_DOWN:
moved=true;
heroGroup[myId]->moveDown();
break;
// case SDLK_d:
// return MENU;
}
return 0;
}
int handle_keyup(SDLKey k) {
if(!start)
return 0;
switch (k) {
case SDLK_LEFT:
heroGroup[myId]->stopMoving();
break;
case SDLK_RIGHT:
heroGroup[myId]->stopMoving();
break;
case SDLK_UP:
heroGroup[myId]->stopMoving();
break;
case SDLK_DOWN:
heroGroup[myId]->stopMoving();
break;
case SDLK_SPACE:
//bombed=true;
heroGroup[myId]->placeBomb();
bombed = heroGroup[myId]->getIsBomb();
break;
case SDLK_ESCAPE:
Mix_HaltMusic();
return MENU;
}
return 0;
}
int handle_menu_key(SDLKey k, int & arrowPos) {
switch(k) {
case SDLK_ESCAPE:
return EXIT;
case SDLK_RETURN:
Mix_HaltMusic();
if (arrowPos==0)
return MULTIPLE;
if (arrowPos==1)
return EXIT;
break;
}
return 0;
}
int handle_menu_keyup(SDLKey k, int & arrowPos) {
switch(k) {
case SDLK_UP:
if (Mix_PlayChannel(-1, menuSFX, 0) == -1)
fprintf(stderr, "Unable to play WAV file: %s\n", Mix_GetError());
arrowPos--;
if (arrowPos<0)
arrowPos = 1;
break;
case SDLK_DOWN:
if (Mix_PlayChannel(-1, menuSFX, 0) == -1)
fprintf(stderr, "Unable to play WAV file: %s\n", Mix_GetError());
arrowPos++;
if(arrowPos>1)
arrowPos = 0;
break;
case SDLK_SPACE:
Mix_HaltMusic();
if (arrowPos==0)
return MULTIPLE;
if (arrowPos==1)
return EXIT;
break;
}
return 0;
}
void initBlock() {
bool initialized =false;
for (int j=0; j<WINDOW_WIDTH/UNIT; j++) {
int x = j*UNIT;
int y = 2*UNIT;
blocks.push_back(new Block(x, y, true,3));
y = (WINDOW_HEIGHT/UNIT-1)*UNIT;
blocks.push_back(new Block(x, y, true,3));
}
for (int i=3; i<WINDOW_HEIGHT/UNIT-1; i++) {
int x = 0;
int y = i*UNIT;
blocks.push_back(new Block(x, y, true,3));
x = (WINDOW_WIDTH/UNIT-1)*UNIT;
blocks.push_back(new Block(x, y, true,3));
}
for (int i=2; i<WINDOW_WIDTH/UNIT-1; i+=2){
for (int j=4; j<WINDOW_HEIGHT/UNIT-1; j+=2){
blocks.push_back(new Block(i*50, j*50, true,3));
}
}
blockmessage bmsg;
while(1)
{
if(tcpclient->Ready())
{
if(tcpclient->Receive(bmsg))
{
int x, y, r;
bmsg.UnLoadByte(x, y, r);
if(r==9)
break;
blocks.push_back(new Block(x*50, y*50, false, r));
}
}
}
}
//void initEnemy() {
// int locationX[2] = {6*UNIT, 7*UNIT};
// int locationY[2] = {9*UNIT, 4*UNIT};
// for (int i = 0; i < 2; i++){
// Enemy * tmp = new Enemy(locationX[i], locationY[i]);
// enemyGroup.push_back(tmp);
// }
//}
void sendHello()
{
hellomessage hello;
Uint16 port = udpclient->getPort();
hello.LoadByte(port,0,0);
tcpclient->Send(hello);
}
void initFont() {
text_font = TTF_OpenFont("fonts/FreeSerif.ttf", 20);
if (text_font == NULL) {
printf("Could not load font\n");
exit(1);
}
font_color.r = 0;
font_color.g = 0; //very green. If you want black, make this 0.
font_color.b = 0;
}
void init(string ip)
{
tcpclient = new CClientSocket();
remoteip = new CIpAddress(ip, 1234);
Uint16 port=1234;
udpclient=new CUdpSocket(port);
if(udpclient==NULL)
{
cout<<"couldn't create udp endpoint\n";
exit(EXIT_FAILURE);
}
while(1)
{
if(!Connected)
{
if(tcpclient->Connect(*remoteip))
{
if(tcpclient->Ok()){
Connected=true;
cout<<"connected to server"<<endl;
}
}
}
else{
break;
}
}
initBlock();
sendHello();
background = new Background("img/background.bmp");
background->setCoords(0,0);
winScreen = new Background("img/win.bmp");
winScreen->setCoords(0,0);
loseScreen = new Background("img/lose.bmp");
loseScreen->setCoords(0,0);
mainMusic = Mix_LoadMUS("sound/mainbgm.wav");
int totalScroll =0;
}
void handleNetwork()
{
while(!start){
if(tcpclient->Ready())
{
handleServer();
}
if(heroGroup.size()==max_players)
start=true;
}
if(udpclient->Ready()){
handleClients();
}
}
void handleServer()
{
slotmessage msg;
if(tcpclient->Receive(msg))
{
int id;
int ch;
int x;
msg.UnLoadByte(ch, id, x);
max_players=x;
cout<<"max players: "<<max_players<<endl;
if(ch=='2')
{
delete heroGroup[id];
heroGroup.erase(id);
return;
}
Hero* newhero = new Hero(id);
heroGroup[id]=newhero;
switch (id) {
case 0:
heroGroup[id]->setCoords(UNIT, 3*UNIT);
break;
case 1:
heroGroup[id]->setCoords((WINDOW_WIDTH/UNIT-2)*UNIT, (WINDOW_HEIGHT/UNIT-2)*UNIT);
break;
case 2:
heroGroup[id]->setCoords((WINDOW_WIDTH/UNIT-2)*UNIT, 3*UNIT);
break;
case 3:
heroGroup[id]->setCoords(UNIT, (WINDOW_HEIGHT/UNIT-2)*UNIT);
break;
default:
break;
}
if(ch=='0')
myId=id;
}
}
void handleClients()
{
CNetMessage* msg;
int channel;
if(udpclient->Receive(msg, channel))
{
if(msg->getType()=='h')
{
hero_pos pos;
msg->UnLoadByte(pos.x, pos.y, pos.id);
if(pos.id!=myId)
{
int oriX = heroGroup[pos.id]->getX();
int oriY = heroGroup[pos.id]->getY();
heroGroup[pos.id]->setCoords(pos.x, pos.y);
int frame = heroGroup[pos.id]->getFrame();
if (pos.y<oriY){
frame++;
if (frame>2)
frame = 0;
heroGroup[pos.id]->setAnimFrame(frame+6);
}
else if (pos.x>oriX){
frame++;
if (frame>2)
frame = 0;
heroGroup[pos.id]->setAnimFrame(frame+3);
}
else if (pos.y>oriY){
frame++;
if (frame>2)
frame = 0;
heroGroup[pos.id]->setAnimFrame(frame);
}
else if (pos.x<oriX){
frame++;
if (frame>2)
frame = 0;
heroGroup[pos.id]->setAnimFrame(frame+9);
}
heroGroup[pos.id]->setFrame(frame);
}
}else if(msg->getType()=='b')
{
int bx, by, lvl;
msg->UnLoadByte(bx, by, lvl);
Bomb* newbomb=new Bomb(bx, by, 4000, SDL_GetTicks(), lvl);
bombGroup.push_back(newbomb);
}
}
delete msg;
}
void gameLoopCleanUp() {
delete background;
while(!colList.empty()) {
CollisionPair * tmp = colList.back();
colList.pop_back();
delete tmp;
}
cout<<"collist"<<colList.size()<<endl;
for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
delete it->second;
heroGroup.erase(it);
}
// while (!heroGroup.empty()){
// Hero * tmp = heroGroup[0];
// heroGroup.erase(0);
// delete tmp;
// }
cout<<"hero"<<heroGroup.size()<<endl;
while(!blocks.empty()){
Block * tmp = blocks.back();
blocks.pop_back();
delete tmp;
}
cout<<"block"<<blocks.size()<<endl;
while(!explosionGroup.empty()){
Explosion * tmp = explosionGroup.back();
explosionGroup.pop_back();
delete tmp;
}
cout<<"exp"<<explosionGroup.size()<<endl;
// while(!enemyGroup.empty()){
// Enemy * tmp = enemyGroup.back();
// enemyGroup.pop_back();
// delete tmp;
// }
// cout<<"enemy"<<enemyGroup.size()<<endl;
while(!bombGroup.empty()){
Bomb * tmp = bombGroup.back();
bombGroup.pop_back();
delete tmp;
}
cout<<"bomb"<<bombGroup.size()<<endl;
while(!upgradeGroup.empty()){
Upgrade * tmp = upgradeGroup.back();
upgradeGroup.pop_back();
delete tmp;
}
cout<<"up"<<upgradeGroup.size()<<endl;
TTF_CloseFont(text_font);
delete tcpclient;
delete udpclient;
delete remoteip;
Mix_HaltMusic();
Mix_FreeMusic(mainMusic);
delete winScreen;
delete loseScreen;
}
int eventLoop(SDL_Surface * screen) {
SDL_Event event;
int animationCounter = 0;
while(1) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYUP:
if(handle_keyup(event.key.keysym.sym)==MENU){
gameLoopCleanUp();
return MENU;
}
break;
case SDL_KEYDOWN:
handle_key(event.key.keysym.sym);
break;
}
}
handleNetwork();
std::map<int, Hero*>::iterator it;
for(it=heroGroup.begin();it!=heroGroup.end();++it)
{
if (it->second->getVisible())
it->second->update(blocks, colList, heroGroup, bombGroup, explosionGroup);
}
if(moved){
hero_pos newPos=heroGroup[myId]->getPos();
heromessage msg;
msg.LoadByte(newPos.x, newPos.y, newPos.id);
udpclient->Send(msg,tcpclient->getIpAddress() ,-1);
moved=false;
}
if(bombed)
{
int bombx, bomby, level;
bombx=heroGroup[myId]->getBombx();
bomby=heroGroup[myId]->getBomby();
level=heroGroup[myId]->getBombLevel();
bombmessage bmsg;
bmsg.LoadByte(bombx, bomby, level);
udpclient->Send(bmsg, tcpclient->getIpAddress(), -1);
bombed=false;
}
for (int i = 0; i < blocks.size(); i++) {
blocks[i]->update(colList, heroGroup, explosionGroup, upgradeGroup);
}
for (int i = 0; i < bombGroup.size(); i++) {
bombGroup.at(i)->update(blocks, colList, heroGroup, bombGroup, explosionGroup);
}
for (int i = 0; i < explosionGroup.size(); i++) {
explosionGroup.at(i)->update();
}
for (int i = 0; i < upgradeGroup.size(); i++) {
upgradeGroup.at(i)->update();
}
for (int i = 0; i < colList.size(); i++){
CollisionPair * tmp = colList.at(i);
if (tmp->isCollided()){
tmp->onCollision();
}
}
background->blit(screen);
if (Mix_PlayingMusic() == 0) {
if (Mix_PlayMusic(mainMusic, -1) == -1)
fprintf(stderr, "Unable to play WAV file: %s\n", Mix_GetError());
}
for (int j = 0; j < bombGroup.size(); j++) {
bombGroup.at(j)->blit(screen);
}
for (int j = 0; j < explosionGroup.size(); j++) {
SDL_Rect tmp = explosionGroup.at(j)->getShowPart();
explosionGroup.at(j)->blit(screen, &tmp);
}
for (int i = 0; i < blocks.size(); i++) {
blocks.at(i)->blit(screen);
}
for (int i = 0; i < upgradeGroup.size(); i++) {
upgradeGroup.at(i)->blit(screen);
}
for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
it->second->blit(screen);
}
for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
sprintf(textbuf[it->first], "Player %d = %d", it->first+1, it->second->getLife());
text_image[it->first] = TTF_RenderText_Solid(text_font, textbuf[it->first], font_color);
int offsetX = it->first<2 ? 0:1;
int offsetY = it->first%2==0 ? 0:1;
textDest[it->first].x = 50+750*offsetX;
textDest[it->first].y = 10+50*offsetY;
textDest[it->first].w = text_image[it->first]->w;
textDest[it->first].h = text_image[it->first]->h;
SDL_BlitSurface(text_image[it->first], NULL, screen, &(textDest[it->first]));
SDL_FreeSurface(text_image[it->first]);
}
// game over
bool iWin = false;
if (heroGroup[myId]->getLife()!=0){
iWin = true;
for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
if (it->first != myId){
if (it->second->getLife() != 0){
iWin = false;
break;
}
}
}
}
bool iLose=false;
if (heroGroup[myId]->getLife()==0){
iLose = true;
int counter = 0;
for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
if (it->first != myId){
if (it->second->getLife() != 0){
counter++;
if (counter>1) {
iLose = false;
break;
}
}
}
}
}
if (iWin) {
animationCounter++;
if (animationCounter>20){
winScreen->blit(screen);
SDL_Flip(screen);
SDL_Delay(3000);
gameLoopCleanUp();
return MENU;
}
}
else if (iLose){
animationCounter++;
if (animationCounter>20){
loseScreen->blit(screen);
SDL_Flip(screen);
SDL_Delay(3000);
gameLoopCleanUp();
return MENU;
}
}
/* since its double buffered, make
the changes show up*/
SDL_Flip(screen);
SDL_Delay(50);
}
return 0;
}
int menu(SDL_Surface * screen) {
SDL_Event event;
Background * menuBackground = new Background("img/menu/menu.bmp");
menuBackground->setCoords(0,0);
Background * arrow = new Background("img/menu/arrow.bmp");
menuMusic = Mix_LoadMUS("sound/menubgm.wav");
menuSFX = Mix_LoadWAV("sound/arrow.wav");
int arrowX = 250;
int arrowY = 515;
arrow->setCoords(arrowX,arrowY);
int arrowPos = 0;
int step = 83;
while(1) {
int stage;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
stage = handle_menu_key(event.key.keysym.sym, arrowPos);
break;
case SDL_KEYUP:
stage = handle_menu_keyup(event.key.keysym.sym, arrowPos);
break;
}
if (stage == EXIT){
delete menuBackground;
delete arrow;
Mix_FreeChunk(menuSFX);
Mix_HaltMusic();
Mix_FreeMusic(menuMusic);
menuMusic = NULL;
return EXIT;
}
else if (stage == MULTIPLE){
delete menuBackground;
delete arrow;
Mix_FreeChunk(menuSFX);
Mix_HaltMusic();
Mix_FreeMusic(menuMusic);
menuMusic = NULL;
return MULTIPLE;
}
}
arrow->setCoords(arrowX, arrowY+arrowPos*step);
menuBackground->setCoords(0,0);
menuBackground->blit(screen);
arrow->blit(screen);
if (Mix_PlayingMusic() == 0) {
if (Mix_PlayMusic(menuMusic, -1) == -1)
fprintf(stderr, "Unable to play WAV file: %s\n", Mix_GetError());
}
SDL_Flip(screen);
SDL_Delay(10);
}
return 0;
}
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
int audio_rate = 44100;
Uint16 audio_format = AUDIO_S16SYS;
int audio_channels = 2;
int audio_buffers = 4096;
if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) {
fprintf(stderr, "Unable to initialize audio: %s\n", Mix_GetError());
exit(1);
}
SDL_Surface * screen = SDL_SetVideoMode(WINDOW_WIDTH,
WINDOW_HEIGHT,
32,
SDL_HWSURFACE |
SDL_DOUBLEBUF
);
SDL_EnableKeyRepeat(10,10);
if(screen == NULL) {
cout<<SDL_GetError()<<endl;
fprintf(stderr, "Can't inialtize video\n");
SDL_Quit();
return EXIT_FAILURE;
}
int stage = menu(screen);
while (stage != EXIT) {
if (stage == MENU)
stage = menu(screen);
if (stage == MULTIPLE){
SDLNet_Init();
start=false;
init(argv[1]);
TTF_Init();
initFont();
stage = eventLoop(screen);
SDLNet_Quit();
Connected = false;
TTF_Quit();
}
}
Mix_CloseAudio();
Mix_Quit();
SDL_Quit();
return EXIT_SUCCESS;
}