Skip to content

Commit

Permalink
Added Pause function
Browse files Browse the repository at this point in the history
Pressing escape pauses game
  • Loading branch information
leviaviv28 authored Oct 28, 2018
1 parent 15a89ce commit 9422d83
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 34 deletions.
59 changes: 33 additions & 26 deletions GameScene.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "Paddle.h"
#include "Renderer.h"

void eventHandler(SDL_Event event, Paddle* player1, Paddle* player2, int* running){
void eventHandler(SDL_Event event, Paddle* player1, Paddle* player2, int* running, int* paused){
while(SDL_PollEvent(&event))
switch(event.type){
case SDL_QUIT:
Expand All @@ -25,6 +25,9 @@ void eventHandler(SDL_Event event, Paddle* player1, Paddle* player2, int* runnin
case SDL_SCANCODE_DOWN:
player2->controls[1]=1;
break;
case SDL_SCANCODE_ESCAPE:
*paused *= -1;
break;
default:
break;
}
Expand All @@ -49,35 +52,39 @@ void eventHandler(SDL_Event event, Paddle* player1, Paddle* player2, int* runnin
}
}

void one_player_logic(SDL_Event event, int* running, Ball* ball, Paddle* player, Paddle* cpu, char* score_str){
void one_player_logic(SDL_Event event, int* running, Ball* ball, Paddle* player, Paddle* cpu, char* score_str, int* paused){
//Exit and Input Handling
eventHandler(event, player, cpu, running);
//Player Movement Logic
playerLogic(player);
//Ball Movement Logic
ballLogic(ball, player, cpu, score_str);
//CPU Logic
cpuLogic(cpu, ball);
//Win Conditions
if(player->score >= 5 || cpu->score >= 5){
sprintf(score_str, "%d %d", player->score, cpu->score);
player->score = 0;
cpu->score = 0;
eventHandler(event, player, cpu, running, paused);
if(*paused == -1){
//Player Movement Logic
playerLogic(player);
//Ball Movement Logic
ballLogic(ball, player, cpu, score_str);
//CPU Logic
cpuLogic(cpu, ball);
//Win Conditions
if(player->score >= 5 || cpu->score >= 5){
sprintf(score_str, "%d %d", player->score, cpu->score);
player->score = 0;
cpu->score = 0;
}
}
}

void two_player_logic(SDL_Event event, int* running, Ball* ball, Paddle* player1, Paddle* player2, char* score_str){
void two_player_logic(SDL_Event event, int* running, Ball* ball, Paddle* player1, Paddle* player2, char* score_str, int* paused){
//Exit and Input Handling
eventHandler(event, player1, player2, running);
//Player Movement Logic
playerLogic(player1);
playerLogic(player2);
//Ball Movement Logic
ballLogic(ball, player1, player2, score_str);
//Win Conditions
if(player1->score >= 5 || player2->score >= 5){
sprintf(score_str, "%d %d", player1->score, player2->score);
player1->score = 0;
player2->score = 0;
eventHandler(event, player1, player2, running, paused);
if(*paused == -1){
//Player Movement Logic
playerLogic(player1);
playerLogic(player2);
//Ball Movement Logic
ballLogic(ball, player1, player2, score_str);
//Win Conditions
if(player1->score >= 5 || player2->score >= 5){
sprintf(score_str, "%d %d", player1->score, player2->score);
player1->score = 0;
player2->score = 0;
}
}
}
6 changes: 3 additions & 3 deletions GameScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include "Ball.h"
#include "Paddle.h"

void eventHandler(SDL_Event event, Paddle* player1, Paddle* player2, int* running);
void one_player_logic(SDL_Event event, int* running, Ball* ball, Paddle* player, Paddle* cpu, char* score_str);
void two_player_logic(SDL_Event event, int* running, Ball* ball, Paddle* player1, Paddle* player2, char* score_str);
void eventHandler(SDL_Event event, Paddle* player1, Paddle* player2, int* running, int* paused);
void one_player_logic(SDL_Event event, int* running, Ball* ball, Paddle* player, Paddle* cpu, char* score_str, int* paused);
void two_player_logic(SDL_Event event, int* running, Ball* ball, Paddle* player1, Paddle* player2, char* score_str, int* paused);

#endif /* GAMESCENE_H_ */
12 changes: 11 additions & 1 deletion Rederer.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void drawGame(SDL_Renderer *renderer, Paddle player, Paddle cpu, Ball ball){
return;
}

void drawMenuText(SDL_Renderer *renderer, TTF_Font* font, int players){
void drawMenu(SDL_Renderer *renderer, TTF_Font* font, int players){
SDL_Color color;
SDL_Color White = { 255, 255, 255 };
SDL_Color Grey = { 169, 169, 169};
Expand All @@ -124,3 +124,13 @@ void drawMenuText(SDL_Renderer *renderer, TTF_Font* font, int players){
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}

void drawPaused(SDL_Renderer *renderer, TTF_Font* font){
SDL_Color White = { 255, 255, 255 };
SDL_Surface * surface = TTF_RenderText_Solid(font, "Paused", White);
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect pause_rect = {(WINDOW_WIDTH / 2) - 150, WINDOW_HEIGHT/2 - 150, 300, 200};
SDL_RenderCopy(renderer, texture, NULL, &pause_rect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
9 changes: 6 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,23 @@ int main(int argc, char **argv){
SDL_Rect score_rect;
//Event Handling Variables
int running = 0;
int paused = -1;
SDL_Event event;
//Game Loop
int players = 1;
while(!running){
if(selected == 0){
eventHandler_Menu(event, &running, &players, &selected);
drawMenuText(renderer, font, players);
drawMenu(renderer, font, players);
}else{
if(players == 1)
one_player_logic(event, &running, &ball, &player1, &player2, score_str);
one_player_logic(event, &running, &ball, &player1, &player2, score_str, &paused);
else
two_player_logic(event, &running, &ball, &player1, &player2, score_str);
two_player_logic(event, &running, &ball, &player1, &player2, score_str, &paused);
renderScore(renderer, score_str, font, &score_rect, &White);
drawGame(renderer, player1, player2, ball);
if(paused == 1)
drawPaused(renderer, font);
}
//Display updates on screen
redraw(renderer);
Expand Down
3 changes: 2 additions & 1 deletion renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ void quit(SDL_Renderer* renderer, SDL_Window* window, TTF_Font* font);
void renderScore(SDL_Renderer *renderer, const char *score_str, TTF_Font *font, SDL_Rect *rect, SDL_Color *color);
void redraw(SDL_Renderer* renderer);
void drawGame(SDL_Renderer *renderer, Paddle player, Paddle cpu, Ball ball);
void drawMenuText(SDL_Renderer *renderer, TTF_Font* font, int players);
void drawMenu(SDL_Renderer *renderer, TTF_Font* font, int players);
void drawPaused(SDL_Renderer *renderer, TTF_Font* font);
#endif /* RENDERER_H_ */

0 comments on commit 9422d83

Please sign in to comment.