Skip to content

Commit

Permalink
Merge pull request #18 from beergame/staging
Browse files Browse the repository at this point in the history
V1.0
  • Loading branch information
victorbalssa authored Apr 24, 2017
2 parents 780b20d + 7fbd685 commit 7083968
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 16 deletions.
Binary file added gfx/btn/2_player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gfx/btn/2_player_b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gfx/btn/3_player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gfx/btn/3_player_b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gfx/btn/4_player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gfx/btn/4_player_b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,37 @@ int get_response(int sock, t_game *g)
return (0);
}

void all_players_connected(t_game *g)
{
int a = 0;

for (int i = 0; i < MAX_PLAYER; ++i) {
if (g->player[i]->connected == 1) {
a++;
}
}
if (g->info->playermax == a) {
g->info->status = IN_CONFIG_NEW_GAME;
}
}

int one_left(t_game *g)
{
int a = 0;

for (int i = 0; i < MAX_PLAYER; ++i) {
if (g->player[i]->connected == 1 &&
g->player[i]->life > 0) {
a++;
}
}
if (a == 1) {
return (1);
}

return (0);
}

void client_beer_bomber(t_game *game)
{
unsigned int frame_limit = SDL_GetTicks() + 16;
Expand All @@ -130,10 +161,20 @@ void client_beer_bomber(t_game *game)
send_request(server, game);
get_response(server, game);
draw(game);
if (one_left(game)) {
go = 1;
}
} else if (game->info->status == WAIT_FOR_PLAYER) {
go = get_input(game);
send_request(server, game);
get_response(server, game);
all_players_connected(game);
draw_wait_for_player(game);
}
delay(frame_limit);
frame_limit = SDL_GetTicks() + 16;
}
draw_winner(game);
send_deco(server);
clean_client(game);
}
4 changes: 4 additions & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ int get_input(t_game *);

void draw(t_game *);

void draw_wait_for_player(t_game *);

void draw_winner(t_game *);

void load_all_sprites(t_game *);

TTF_Font *loadFont(char *, int);
Expand Down
22 changes: 11 additions & 11 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void is_new_game(t_game *game, int *c)
if (*c == 2) {
game->info->status = IN_CONFIG_NB_PLAYER;
} else {
game->info->status = IN_CONFIG_IP_SERVER;
game->info->status = WAIT_FOR_PLAYER;
}
}
(*c < 2) ? *c = 2: 0;
Expand All @@ -29,7 +29,7 @@ void choose_nb_player(t_game *game, int *nb)
} else if (game->input->right) {
*nb = 4;
} else if (game->input->fire) {
game->info->status = IN_GAME;
game->info->status = WAIT_FOR_PLAYER;
}
draw_nb_player(game, *nb);
}
Expand All @@ -44,19 +44,19 @@ void draw_nb_player(t_game *game, int c)

switch (c) {
case 2:
drawBtn(game, 250, 380, BTN_NEWGAME);
drawBtn(game, 550, 380, BTN_NEWGAME_B);
drawBtn(game, 850, 380, BTN_JOINGAME_B);
drawBtn(game, 250, 380, BTN_2_PLAYER);
drawBtn(game, 550, 380, BTN_3_PLAYER_B);
drawBtn(game, 850, 380, BTN_4_PLAYER_B);
break;
case 3:
drawBtn(game, 250, 380, BTN_NEWGAME_B);
drawBtn(game, 550, 380, BTN_NEWGAME);
drawBtn(game, 850, 380, BTN_JOINGAME_B);
drawBtn(game, 250, 380, BTN_2_PLAYER_B);
drawBtn(game, 550, 380, BTN_3_PLAYER);
drawBtn(game, 850, 380, BTN_4_PLAYER_B);
break;
case 4:
drawBtn(game, 250, 380, BTN_NEWGAME_B);
drawBtn(game, 550, 380, BTN_NEWGAME_B);
drawBtn(game, 850, 380, BTN_JOINGAME);
drawBtn(game, 250, 380, BTN_2_PLAYER_B);
drawBtn(game, 550, 380, BTN_3_PLAYER_B);
drawBtn(game, 850, 380, BTN_4_PLAYER);
break;
default:
break;
Expand Down
10 changes: 7 additions & 3 deletions src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,24 @@ enum sprite
PLAYER_FOUR_DOWN,
PLAYER_FOUR_LEFT,
PLAYER_FOUR_RIGHT,

BOMB_SPRITE,
BOMB_SPRITE2,
BOMB_SPRITE3,

MAP_SPRITE_BASE,
MAP_SPRITE_BLOCK,
MAP_SPRITE_BUSH,
MAP_SPRITE_FIRE,

MAP_BACK_ONE,
BTN_NEWGAME,
BTN_NEWGAME_B,
BTN_JOINGAME,
BTN_JOINGAME_B,
BTN_2_PLAYER,
BTN_2_PLAYER_B,
BTN_3_PLAYER,
BTN_3_PLAYER_B,
BTN_4_PLAYER,
BTN_4_PLAYER_B,
};

enum status
Expand All @@ -65,6 +68,7 @@ enum status
IN_CONFIG_NEW_GAME,
IN_CONFIG_NB_PLAYER,
IN_CONFIG_IP_SERVER,
WAIT_FOR_PLAYER,
IN_GAME,
END_GAME,
};
Expand Down
49 changes: 48 additions & 1 deletion src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void draw(t_game *g)
/* Blank the screen and draw background */
SDL_RenderClear(g->renderer);
draw_background(g, MAP_BACK_ONE);
/* Draw the score */
/* Draw stats */
for (int i = 0; i < MAX_PLAYER; ++i) {
sprintf(text, "PLAYER %d", i + 1);
draw_log_string(g, text, 50, 20 + (i * 150), g->font);
Expand Down Expand Up @@ -41,3 +41,50 @@ void delay(unsigned int frame_limit)
else
SDL_Delay(frame_limit - ticks);
}

void draw_wait_for_player(t_game *g)
{
char text[20];

/* Blank the screen and draw background */
SDL_RenderClear(g->renderer);
draw_background(g, MAP_BACK_ONE);
/* Draw co info */
sprintf(text, "WAIT FOR PLAYER ...");
drawString(g, text, 50, 100, g->font, 1, 0);
for (int i = 0; i < MAX_PLAYER; ++i) {
if (g->player[i]->connected == 1) {
sprintf(text, "PLAYER %d CO !", i + 1);
drawString(g, text, 50, 150 + (i * 150), g->font, 1, 0);
}
}
/* Update the buffer */
SDL_RenderPresent(g->renderer);
/* Sleep briefly for better perf */
SDL_Delay(1);
}

void draw_winner(t_game *g)
{
char text[50];

/* Blank the screen and draw background */
SDL_RenderClear(g->renderer);
draw_background(g, MAP_BACK_ONE);
/* Draw winner info */
sprintf(text, "AND THE WINNER IS ...");
drawString(g, text, 50, 100, g->font, 1, 0);
for (int i = 0; i < MAX_PLAYER; ++i) {
if (g->player[i]->connected == 1 &&
g->player[i]->life > 0) {
sprintf(text, "PLAYER %d !!!", i + 1);
drawString(g, text, 50, 150, g->font, 1, 0);
}
}
sprintf(text, "DISCONNECT FROM SERVER IN 3 SEC ...");
drawString(g, text, 50, 300, g->font, 1, 0);
/* Update the buffer */
SDL_RenderPresent(g->renderer);
/* Sleep briefly for better perf */
SDL_Delay(5000);
}
6 changes: 6 additions & 0 deletions src/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ void load_all_sprites(t_game *game)
loadSprite(game, BTN_NEWGAME_B, "gfx/btn/newgame_b.png");
loadSprite(game, BTN_JOINGAME, "gfx/btn/joingame.png");
loadSprite(game, BTN_JOINGAME_B, "gfx/btn/joingame_b.png");
loadSprite(game, BTN_2_PLAYER, "gfx/btn/2_player.png");
loadSprite(game, BTN_3_PLAYER, "gfx/btn/3_player.png");
loadSprite(game, BTN_4_PLAYER, "gfx/btn/4_player.png");
loadSprite(game, BTN_2_PLAYER_B, "gfx/btn/2_player_b.png");
loadSprite(game, BTN_3_PLAYER_B, "gfx/btn/3_player_b.png");
loadSprite(game, BTN_4_PLAYER_B, "gfx/btn/4_player_b.png");
}

void draw_background(t_game *game, int index)
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ int main(void)
go = get_input(game);
choose_nb_player(game, &nb_player);
} else if (game->info->status == IN_GAME ||
game->info->status == IN_CONFIG_IP_SERVER){
game->info->status == IN_CONFIG_IP_SERVER ||
game->info->status == WAIT_FOR_PLAYER){
go = 1;
}
delay(frame_limit);
Expand Down

0 comments on commit 7083968

Please sign in to comment.