-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
244 lines (225 loc) · 6.88 KB
/
main.c
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
#include "includes.h"
/*
* Initialisation of graphics, music, controller, levels
* and call to the function that runs the game.
* Destroys game resources on exit
*/
int main(int argc, char **argv) {
// command line argument pi, to run on pi
is_pi = argc > 1 && !strcmp(argv[1], "pi");
initialise_graphics();
initialise_music();
initialise_levels();
initialise_controller();
run();
destroy_graphics();
destroy_music();
}
/*
* Run one game tick,
* call corresponding function depending on the game_state
* Game Loop that repeats until player exits the game
*/
void run() {
game_state_t game_state = START_MENU;
bar_t bar;
ball_t ball;
int32_t running = 1;
int32_t bricks[BRICKS_PER_LEVEL];
SDL_Event event;
const uint8_t *keystate = SDL_GetKeyboardState(NULL);
play_start_title();
while (running) {
reset_controller();
reset_cheat();
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = 0;
} else if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_SPACE:
set_controller(SPACE_FLAG);
break;
case SDLK_ESCAPE:
if (game_state == PLAY_GAME) {
game_state = PAUSE_SCREEN;
break;
}
if (game_state == PAUSE_SCREEN || game_state == WAIT_FOR_RESTART) {
game_state = EXIT_GAME;
break;
}
break;
case SDLK_RETURN:
if (game_state == PAUSE_SCREEN) {
game_state = PLAY_GAME;
}
if (game_state == WAIT_FOR_RESTART) {
game_state = START_GAME;
}
if (game_state == START_MENU) {
game_state = START_GAME;
}
}
}
}
if (keystate[SDL_SCANCODE_A]) {
set_controller(A_FLAG);
}
if (keystate[SDL_SCANCODE_D]) {
set_controller(D_FLAG);
}
if (keystate[SDL_SCANCODE_I]) {
set_cheat(FLAG_1);
}
if (keystate[SDL_SCANCODE_O]) {
set_cheat(FLAG_2);
}
if (keystate[SDL_SCANCODE_P]) {
set_cheat(FLAG_3);
}
check_cheat(&game_state);
switch (game_state) {
case START_GAME:
start_game(&bar, &ball, &game_state, bricks);
break;
case START_MENU:
start_menu(&bar, &ball, &game_state, bricks);
break;
case PLAY_GAME:
play_game(&bar, &ball, &game_state, bricks);
refresh_screen(&bar, &ball, bricks);
break;
case PAUSE_SCREEN:
pause_screen(&bar, &ball, &game_state, bricks);
break;
case LOSE_GAME:
lose_game(&bar, &ball, &game_state, bricks);
break;
case GAME_OVER:
game_over(&bar, &ball, &game_state, bricks);
break;
case WIN_LEVEL:
win_level(&bar, &ball, &game_state, bricks);
break;
case WIN_GAME:
win_game(&bar, &ball, &game_state, bricks);
break;
case EXIT_GAME:
return;
break;
case WAIT_FOR_RESTART:
restart_on_keypress();
break;
default:
perror("Error game state not found");
exit(EXIT_FAILURE);
}
SDL_Delay(16);
}
}
/*
* Draws the start title
*/
void play_start_title(void) {
render_text("BRICKBREAKER");
SDL_Delay(2000);
}
/*
* Called to initialise tha bar, ball, bricks and draw the game
*/
void start_game(bar_t *bar, ball_t *ball, game_state_t *game_state, int32_t *bricks) {
init_bar(bar);
init_ball(ball);
init_powerup();
load_level(bar, ball, game_state, bricks);
}
/*
* Draws starting menu that explains the player controls
*/
void start_menu(bar_t *bar, ball_t *ball, game_state_t *game_state, int32_t *bricks) {
render_text_two_lines("Move with A, D", "PRESS ENTER TO CONTINUE");
}
/*
* Called to simulate a game second
* Updates ball, bar, bricks and calls appropriate functions
* to handle possible collisions
*/
void play_game(bar_t *bar, ball_t *ball, game_state_t *game_state, int32_t *bricks) {
update_bar(bar, get_controller_state());
int collision = update_ball(ball, bar, game_state);
update_bricks(ball, bricks, game_state);
update_powerups(bar, ball);
if(collision) {
play_collision_sound();
}
}
/*
* Paused screen
*/
void pause_screen(bar_t *bar, ball_t *ball, game_state_t *game_state, int32_t *bricks) {
render_text_two_lines("GAME PAUSED", "PRESS ENTER TO CONTINUE");
}
/*
* Updates the player when he loses a life
* and displays to him the lives he has left
*/
void lose_game(bar_t *bar, ball_t *ball, game_state_t *game_state, int32_t *bricks) {
reset_bar(bar);
reset_ball(ball);
reset_powerup();
lose_life(bar, game_state);
char prompt[20];
if (bar->lives != 1) {
sprintf(prompt, "%d LIVES LEFT", bar->lives);
} else {
sprintf(prompt, "1 LIFE LEFT");
}
render_text(prompt);
SDL_Delay(2000);
}
/*
* Loads bricks of level and calls function to draw the game
*/
void load_level(bar_t *bar, ball_t *ball, game_state_t *game_state, int32_t *bricks) {
memcpy(bricks, bricks_level[bar->level], sizeof(int32_t) * BRICKS_PER_LEVEL);
*game_state = PLAY_GAME;
draw_game(bar, ball, bricks);
}
/*
* Displays the win level message and updates the bar, ball, bricks
* for the next level
*/
void win_level(bar_t *bar, ball_t *ball, game_state_t *game_state, int32_t *bricks) {
render_text("LEVEL COMPLETE!");
SDL_Delay(2000);
bar->level = bar->level + 1;
reset_bar(bar);
bar->lives = START_LIVES;
reset_ball(ball);
if (bar->level == LEVELS) {
*game_state = WIN_GAME;
return;
}
load_level(bar, ball, game_state, bricks);
}
/*
* Displays the "win game" message
*/
void win_game(bar_t *bar, ball_t *ball, game_state_t *game_state, int32_t *bricks) {
draw_win_screen();
*game_state = WAIT_FOR_RESTART;
}
/*
* Displays "game over" message and option to restart
*/
void game_over(bar_t *bar, ball_t *ball, game_state_t *game_state, int32_t *bricks) {
draw_gameover_screen();
*game_state = WAIT_FOR_RESTART;
}
/*
* Displays message restart on keypress
*/
void restart_on_keypress(void) {
render_text_two_lines("RESTART?", "PRESS ENTER TO CONTINUE");
}