-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
214 lines (204 loc) · 7.01 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
/*
*Author:-Pukar Giri
*Created On:-07Th February 2019 at 17:45
*File Name:-main.cpp
*Project Name:-streetfight
*Licence:- MIT
*Email:[email protected]
*/
#include <iostream>
#include <string>
#include<SDL2/SDL.h>
#include "player.h"
#include "AI.h"
#include "network.h"
#include "menu.h"
bool running= true;
using namespace std;
void loose(menu & mainmenu,SDL_Renderer * renderer);
void winner(menu & mainmenu,SDL_Renderer * renderer);
void handle_event(player &p1,player &p2)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN :
switch(event.key.keysym.sym)
{
case SDLK_RIGHT:
p1.walkf();
break;
case SDLK_UP:
p1.jump();
break;
case SDLK_LEFT:
p1.walkb();
break;
case SDLK_SLASH:
p1.kick();
break;
case SDLK_KP_0:
p1.punch();
break;
case SDLK_d:
p2.walkf();
break;
case SDLK_w:
p2.jump();
break;
case SDLK_a:
p2.walkb();
break;
case SDLK_q:
p2.kick();
break;
case SDLK_e:
p2.punch();
break;
case SDLK_ESCAPE:
exit(0);
}
break;
case SDL_QUIT:
running= false;
SDL_Quit();
exit(0);
}
}
}
void banner()
{
SDL_Window *window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 400, SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_ALWAYS_ON_TOP);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface *banner1 = SDL_LoadBMP("wel1.bmp");
SDL_Texture *b1 = SDL_CreateTextureFromSurface(renderer, banner1);
SDL_FreeSurface(banner1);
int value=0;
while(value<100)
{
SDL_Rect loadbar={0,380,int(800*(value/100.0)),2};
SDL_Rect leadbar={int(800*(value/100.0)),379,4,4};
SDL_RenderCopy(renderer, b1, NULL, NULL);
SDL_SetRenderDrawColor(renderer,242, 226, 53,255);
SDL_RenderFillRect(renderer,&loadbar);
SDL_RenderFillRect(renderer,&leadbar);
SDL_RenderPresent(renderer);
SDL_Delay(60);
value+=rand()%4;
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
int play(SDL_Window *win,SDL_Renderer *Ren);
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
banner();
SDL_Window *win=SDL_CreateWindow("Street fight Neo",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,1000,600,SDL_WINDOW_SHOWN);
SDL_Renderer *Ren=SDL_CreateRenderer(win,0,1);
while(running)
{
play(win,Ren);
}
}
int play(SDL_Window *win,SDL_Renderer *Ren)
{
menu mainmenu(Ren);
mainmenu.display();
SDL_Surface *temp=SDL_LoadBMP("stage.bmp");
SDL_Texture *stage=SDL_CreateTextureFromSurface(Ren,temp);
SDL_FreeSurface(temp);
player dummy(Ren);
player self(Ren,!mainmenu.team_red);
player opponent(Ren,mainmenu.team_red);
AI computer1(opponent,self);
opponent.set_opponent(&self);
self.set_opponent(&opponent);
network net(opponent, self, mainmenu.team_red);
if (mainmenu.net)
{
net.initialize();
}
while (running)
{
if (!mainmenu.two_on_one) handle_event(self,dummy);
if (mainmenu.two_on_one) handle_event(self,opponent);
if (mainmenu.single_player) computer1.drive();
if (mainmenu.net) net.send_state();
SDL_RenderCopy(Ren, stage, nullptr, nullptr);
self.update();
opponent.update();
if (self.health<=0)
{
loose(mainmenu,Ren);
break;
}
if (opponent.health<=0)
{
winner(mainmenu,Ren);
break;
}
SDL_RenderPresent(Ren);
SDL_Delay(25);
}
return 0;
}
void loose(menu & mainmenu,SDL_Renderer * renderer)
{
string text;
if(mainmenu.two_on_one){
if (mainmenu.team_red) text+="Red Has Won!";
else text+="Blue has won!";
}else
{
text+="YOU have lost!";
}
SDL_Rect current={200,200,800,200};
TTF_Font *font = TTF_OpenFont("Xanadu.ttf", 60);
SDL_Color color={0,0,0};
SDL_Surface * surface = TTF_RenderText_Solid(font,text.c_str(),color);
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface);
int texW = 0;
int texH = 0;
SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
current.x=current.x+(current.w-texW)/2;
current.y=current.y+(current.h-texH)/2;
current.w=texW;
current.h=texH;
SDL_RenderCopy(renderer, texture, NULL,¤t);
SDL_RenderPresent(renderer);
SDL_Delay(1000);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}
void winner(menu & mainmenu,SDL_Renderer * renderer)
{
string text;
if(mainmenu.two_on_one){
if (mainmenu.team_red) text+="Red Has Won";
else text+="Blue has won";
}
else
{
text+="YOU Have WON!";
}
SDL_Rect current={200,200,800,200};
TTF_Font *font = TTF_OpenFont("Xanadu.ttf", 60);
SDL_Color color={0,0,0};
SDL_Surface * surface = TTF_RenderText_Solid(font,text.c_str(),color);
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface);
int texW = 0;
int texH = 0;
SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
current.x=current.x+(current.w-texW)/2;
current.y=current.y+(current.h-texH)/2;
current.w=texW;
current.h=texH;
SDL_RenderCopy(renderer, texture, NULL,¤t);
SDL_RenderPresent(renderer);
SDL_Delay(1000);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}