-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
170 lines (134 loc) · 4.58 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
#include"commonFunc.h"
#include "BaseObject.h"
#include "mainObject.h"
#include "game_map.h"
#include "ThreatsObject.h"
#include<ctime>
BaseObject g_background;
SDL_Surface* g_object = NULL;
bool InitData()
{
bool success = true;
int ret = SDL_Init(SDL_INIT_VIDEO);
if(ret < 0)
return false;
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,"1");
g_window = SDL_CreateWindow("Flappy Bird", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,SCREEN_WIDTH,
SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
if(g_window == NULL)
{
success = false;
}
else
{
g_screen = SDL_CreateRenderer(g_window, -1, SDL_RENDERER_ACCELERATED);
if(g_screen == NULL)
success = false;
else
{
SDL_SetRenderDrawColor(g_screen, RENDER_DRAW_COLOR,RENDER_DRAW_COLOR,RENDER_DRAW_COLOR,RENDER_DRAW_COLOR);
int imgFlags = IMG_INIT_PNG;
if(!(IMG_Init(imgFlags) && imgFlags))
success = false;
}
}
return success;
}
bool LoadBackGround()
{
bool ret = g_background.LoadImg("img//background.png", g_screen);
if(ret = false)
return false;
else return true;
}
void ApplySurface(SDL_Surface* src, SDL_Surface* des, int x, int y)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(src, NULL, des, &offset);
}
void close()
{
g_background.Free();
SDL_DestroyRenderer(g_screen);
g_screen = NULL;
SDL_DestroyWindow(g_window);
g_window = NULL;
IMG_Quit();
SDL_Quit();
}
int main(int argc, char* argv[])
{
if(InitData() == false)
return -1;
if(LoadBackGround() == false)
return -1;
bool is_quit = false;
MainObject p_player;
p_player.LoadImg("img//bird2.png",g_screen);
p_player.set_clips();
//make threats
ThreatObject* p_threat1 = new ThreatObject();
bool ret1 = p_threat1 ->LoadImg("img/threat.png",g_screen);
if(ret1 == false) return 10;
p_threat1 ->SetRect(2 * SCREEN_WIDTH / 3 - WIDTH_THREAT /3,-300);
p_threat1->set_x_val(0.6);
ThreatObject* p_threat2 = new ThreatObject();
bool ret2 = p_threat2 ->LoadImg("img/threat2.png",g_screen);
if(ret2 == false) return 10;
p_threat2 ->SetRect(2 * SCREEN_WIDTH / 3 - WIDTH_THREAT /3,450);
p_threat2->set_x_val(0.6);
ThreatObject* p_threat3 = new ThreatObject();
bool ret3 = p_threat3 ->LoadImg("img/threat.png",g_screen);
if(ret3 == false) return 10;
p_threat3 ->SetRect(SCREEN_WIDTH,-200);
p_threat3 ->set_x_val(0.6);
ThreatObject* p_threat4 = new ThreatObject();
bool ret4 = p_threat4 ->LoadImg("img/threat2.png",g_screen);
if(ret4 == false) return 10;
p_threat4 ->SetRect(SCREEN_WIDTH,550);
p_threat4->set_x_val(0.6);
ThreatObject* p_threat5 = new ThreatObject();
bool ret5 = p_threat5 ->LoadImg("img/threat.png",g_screen);
if(ret5 == false) return 10;
p_threat5 ->SetRect(4 * SCREEN_WIDTH / 3 + WIDTH_THREAT / 3,-400);
p_threat5 ->set_x_val(0.6);
ThreatObject* p_threat6 = new ThreatObject();
bool ret6 = p_threat6 ->LoadImg("img/threat2.png",g_screen);
if(ret6 == false) return 10;
p_threat6 ->SetRect(4 * SCREEN_WIDTH / 3 + WIDTH_THREAT / 3,350);
p_threat6->set_x_val(0.6);
while(!is_quit)
{
while(SDL_PollEvent(&g_event) != 0)
{
if(g_event.type == SDL_QUIT)
{
is_quit = true;
}
p_player.HandleInputAction(g_event, g_screen);
}
SDL_SetRenderDrawColor(g_screen, RENDER_DRAW_COLOR, RENDER_DRAW_COLOR, RENDER_DRAW_COLOR, RENDER_DRAW_COLOR );
SDL_RenderClear(g_screen);
g_background.Render(g_screen,NULL);
p_player.Show(g_screen);
//draw threats
p_threat1->Show_1(g_screen);
p_threat1->HandleMove1(SCREEN_WIDTH,SCREEN_HEIGHT);
p_threat2->Show_1(g_screen);
p_threat2->HandleMove2(SCREEN_WIDTH,SCREEN_HEIGHT);
p_threat3->Show_1(g_screen);
p_threat3->HandleMove1(SCREEN_WIDTH,SCREEN_HEIGHT);
p_threat4->Show_1(g_screen);
p_threat4->HandleMove2(SCREEN_WIDTH,SCREEN_HEIGHT);
p_threat5->Show_1(g_screen);
p_threat5->HandleMove1(SCREEN_WIDTH,SCREEN_HEIGHT);
p_threat6->Show_1(g_screen);
p_threat6->HandleMove2(SCREEN_WIDTH,SCREEN_HEIGHT);
SDL_RenderPresent(g_screen);
}
close();
return 0;
}