Skip to content

Commit

Permalink
Merge pull request #22 from beergame/staging
Browse files Browse the repository at this point in the history
V1.2
  • Loading branch information
victorbalssa authored Jun 17, 2017
2 parents 7083968 + 61b73d4 commit 96ff702
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@
*.dSYM/
*.su

beerbomber
beerbomber
/CMakeLists.txt
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# BeerBomber [![Build Status](https://travis-ci.org/beergame/BeerBomber.svg?branch=staging)](https://travis-ci.org/beergame/BeerBomber)
Bomberman clone in C/SDL

# Contributor
[Aurelien Paranavithana](https://github.com/AurelienParana), [Roland Coulibaly](https://github.com/rolandcoulibaly), [Victor Balssa](https://github.com/Nandotk), [Simon Garreau-Férandin](https://github.com/garreas)
Bomberman clone in C/SDL 360 controller ready
<img src="http://i.imgur.com/K4qCCiq.png" width="3%"/>
<br />
<br />
<br />
<img src="http://i.imgur.com/Lc3yNCj.gif" width="70%" />


# Build BeerBomber

Expand All @@ -15,7 +19,7 @@ $ brew install sdl2 sdl2_image sdl2_ttf sdl2_mixer
Or with `apt-source` for Linux based OS

``` shell
$ sudo apt-get install libsdl2-2.0-0 libsdl2-dev libsdl2-image-2.0-0 libsdl2-image-dev &&
$ sudo apt-get install libsdl2-2.0-0 libsdl2-dev libsdl2-image-2.0-0 libsdl2-image-dev \
libsdl2-mixer-2.0-0 libsdl2-mixer-dev libsdl2-ttf-2.0-0 libsdl2-ttf-dev
```

Expand All @@ -26,4 +30,8 @@ $ make all

You will have an executable named `beerbomber` that you can run

#### Enjoy ! :bomb::bomb::fire::beers:
#### :beers::bomb::fire:


# Contributor
[Aurelien Paranavithana](https://github.com/AurelienParana), [Roland Coulibaly](https://github.com/rolandcoulibaly), [Victor Balssa](https://github.com/Nandotk), [Simon Garreau-Férandin](https://github.com/garreas)
38 changes: 24 additions & 14 deletions src/client.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "client.h"

int client_connect()
int client_connect(char *ip)
{
struct protoent *pe;
struct sockaddr_in sin;
Expand All @@ -12,7 +12,7 @@ int client_connect()
return (-1);
sin.sin_family = AF_INET;
sin.sin_port = htons(5000);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
sin.sin_addr.s_addr = inet_addr(ip);
if (connect(s, (const struct sockaddr *) &sin, sizeof(sin)) == -1) {
printf("Error connect()\n");
return (-1);
Expand Down Expand Up @@ -76,12 +76,13 @@ void unserialize_response(char *buffer, t_game *g)
if (g->player[i] != NULL) {
g->player[i]->x = atoi(buff2[0]);
g->player[i]->y = atoi(buff2[1]);
g->player[i]->ammo = atoi(buff2[2]);
g->player[i]->reload = atoi(buff2[3]);
g->player[i]->frags = atoi(buff2[4]);
g->player[i]->connected = atoi(buff2[5]);
g->player[i]->life = atoi(buff2[6]);
g->player[i]->speed = atoi(buff2[7]);
g->player[i]->dir = atoi(buff2[2]);
g->player[i]->ammo = atoi(buff2[3]);
g->player[i]->reload = atoi(buff2[4]);
g->player[i]->frags = atoi(buff2[5]);
g->player[i]->connected = atoi(buff2[6]);
g->player[i]->life = atoi(buff2[7]);
g->player[i]->speed = atoi(buff2[8]);
}
}
}
Expand All @@ -100,7 +101,8 @@ int get_response(int sock, t_game *g)
char buffer[BUFF_SIZE];

if (recv(sock, buffer, BUFF_SIZE, 0) < 0) {
puts("recv failed");
puts("connection server failed");
puts("trying to get response ...");
return (1);
}
unserialize_response(buffer, g);
Expand Down Expand Up @@ -128,7 +130,7 @@ int one_left(t_game *g)

for (int i = 0; i < MAX_PLAYER; ++i) {
if (g->player[i]->connected == 1 &&
g->player[i]->life > 0) {
g->player[i]->life > 0) {
a++;
}
}
Expand All @@ -143,20 +145,25 @@ void client_beer_bomber(t_game *game)
{
unsigned int frame_limit = SDL_GetTicks() + 16;
int go = 0;
char ip[15] = "";

init_client(game);

/* connect to server */
SDL_Delay(500);
int server = client_connect();
SDL_Delay(400);
printf("Server beerbomber IP: ");
fgets(ip, sizeof(ip), stdin);
int server = client_connect(ip);
send_request(server, game);
SDL_Delay(100);
get_response(server, game);

while (!go) {
if (game->info->status == IN_REDEFINE) {
/* Handle the key redefining */
go = do_redefine(game);
} else if (game->info->status == IN_GAME ||
game->info->status == IN_CONFIG_NEW_GAME) {
game->info->status == IN_CONFIG_NEW_GAME) {
go = get_input(game);
send_request(server, game);
get_response(server, game);
Expand All @@ -174,7 +181,10 @@ void client_beer_bomber(t_game *game)
delay(frame_limit);
frame_limit = SDL_GetTicks() + 16;
}
draw_winner(game);
if (go == 1) {
draw_winner(game);
}

send_deco(server);
clean_client(game);
}
3 changes: 2 additions & 1 deletion src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
# define BOARD_HEIGHT (SCREEN_HEIGHT / 2) - (MAP_SIZE * 32 / 2)
# define MAX_PLAYER 4
# define MAX_SPRITES 40
# define DEAD_ZONE 3200
# define DEAD_ZONE 4200
# define X360_BACK 13
# define BUFF_SIZE 3000

enum sprite
Expand Down
5 changes: 1 addition & 4 deletions src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ 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);
Expand All @@ -86,5 +83,5 @@ void draw_winner(t_game *g)
/* Update the buffer */
SDL_RenderPresent(g->renderer);
/* Sleep briefly for better perf */
SDL_Delay(5000);
SDL_Delay(2500);
}
9 changes: 5 additions & 4 deletions src/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void loadSprite(t_game *game, int index, char *path)
sprite[index].image = loadImage(game, path);
}

SDL_Texture *getSprite(int index)
SDL_Texture *get_sprite(int index)
{
if (index >= MAX_SPRITES || index < 0) {
printf("Invalid index for sprite! Index: %d Maximum: %d\n", index, MAX_SPRITES);
Expand Down Expand Up @@ -98,6 +98,7 @@ void loadPlayerTexture(t_game *game, char *path, int index)

void load_all_sprites(t_game *game)
{
loadPlayerTexture(game, "gfx/players/one.png", PLAYER_ONE_UP);
loadPlayerTexture(game, "gfx/players/one.png", PLAYER_ONE_UP);
loadPlayerTexture(game, "gfx/players/two.png", PLAYER_TWO_UP);
loadPlayerTexture(game, "gfx/players/three.png", PLAYER_THREE_UP);
Expand Down Expand Up @@ -129,18 +130,18 @@ void draw_background(t_game *game, int index)
pos.y = 0;
pos.h = SCREEN_HEIGHT;
pos.w = SCREEN_WIDTH;
SDL_RenderCopy(game->renderer, getSprite(index), NULL, &pos);
SDL_RenderCopy(game->renderer, get_sprite(index), NULL, &pos);
}

void drawBtn(t_game *game, int x, int y, int index)
{
SDL_Rect pos;
int w;
int h;
SDL_QueryTexture(getSprite(index), NULL, NULL, &w, &h);
SDL_QueryTexture(get_sprite(index), NULL, NULL, &w, &h);
pos.x = x;
pos.y = y;
pos.h = h;
pos.w = w;
SDL_RenderCopy(game->renderer, getSprite(index), NULL, &pos);
SDL_RenderCopy(game->renderer, get_sprite(index), NULL, &pos);
}
62 changes: 31 additions & 31 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int get_input(t_game *game)
/* Closing the Window will exit the program */
case SDL_QUIT:
game->info->status = END_GAME;
return (1);
return (2);

case SDL_KEYDOWN:
key = event.key.keysym.sym;
Expand Down Expand Up @@ -73,31 +73,31 @@ int get_input(t_game *game)
game->input->fire = 0;
break;

case SDL_JOYAXISMOTION:
/* Horizontal movement */
if (event.jaxis.axis == 0) {
if (event.jaxis.value < -DEAD_ZONE)
game->input->left = 1;
else if (event.jaxis.value > DEAD_ZONE)
game->input->right = 1;
else {
game->input->left = 0;
game->input->right = 0;
}
}

/* Vertical movement */
if (event.jaxis.axis == 1) {
if (event.jaxis.value < -DEAD_ZONE) {
game->input->up = 1;
} else if (event.jaxis.value > DEAD_ZONE) {
game->input->down = 1;
} else {
game->input->up = 0;
game->input->down = 0;
}
}
break;
// case SDL_JOYAXISMOTION:
// /* Horizontal movement */
// if (event.jaxis.axis == 0) {
// if (event.jaxis.value < -DEAD_ZONE)
// game->input->left = 1;
// else if (event.jaxis.value > DEAD_ZONE)
// game->input->right = 1;
// else {
// game->input->left = 0;
// game->input->right = 0;
// }
// }
//
// /* Vertical movement */
// if (event.jaxis.axis == 1) {
// if (event.jaxis.value < -DEAD_ZONE) {
// game->input->up = 1;
// } else if (event.jaxis.value > DEAD_ZONE) {
// game->input->down = 1;
// } else {
// game->input->up = 0;
// game->input->down = 0;
// }
// }
// break;
}
}

Expand Down Expand Up @@ -126,11 +126,11 @@ int get_single_input()
key = event.key.keysym.sym;
break;

case SDL_JOYAXISMOTION:
if (abs(event.jaxis.value) > DEAD_ZONE) {
key = -3;
}
break;
// case SDL_JOYAXISMOTION:
// if (abs(event.jaxis.value) > DEAD_ZONE) {
// key = -3;
// }
// break;

case SDL_JOYBUTTONDOWN:
key = event.jbutton.button;
Expand Down
24 changes: 13 additions & 11 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ int main(void)
}

/* new game: start server thread */
if (choice == 2) {
game->info->playermax = nb_player;
if (pthread_create(&server, NULL, server_beer_bomber, game->info)) {
perror("pthread_create server");
return (EXIT_FAILURE);
if (go == 1) {
if (choice == 2) {
game->info->playermax = nb_player;
if (pthread_create(&server, NULL, server_beer_bomber, game->info)) {
perror("pthread_create server");
return (EXIT_FAILURE);
}
}
}
client_beer_bomber(game);
if (choice == 2) {
if (pthread_join(server, NULL)) {
perror("pthread_join server");
return (EXIT_FAILURE);
client_beer_bomber(game);
if (choice == 2) {
if (pthread_join(server, NULL)) {
perror("pthread_join server");
return (EXIT_FAILURE);
}
}
}

Expand Down
Loading

0 comments on commit 96ff702

Please sign in to comment.