Skip to content

Commit

Permalink
Solv realloc (#26)
Browse files Browse the repository at this point in the history
* test de realloc avec une struc static

* use type more than pointer

* ""

* end

* sizeof
  • Loading branch information
Kumatetsu authored and Enach committed Jul 3, 2018
1 parent e29d2f3 commit 22cc2d1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 93 deletions.
88 changes: 7 additions & 81 deletions client/client.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
** client.c for Bomberman in /Users/kumatetsu/projet-etna/DVC4/Bomberman/Bomberman
**
**
** Made by BILLAUD Jean
** Login <billau_j@etna-alternance.net>
**
**
** Started on Wed Jun 27 17:03:07 2018 BILLAUD Jean
** Last update Tue Jul 3 20:29:30 2018 MASERA Mathieu
*/
Expand Down Expand Up @@ -35,7 +35,7 @@ void init_client(t_sdl *sdl)
cr = create_player_request();
start_map(sdl, cs, cr);
free_player_request(cr);

}

char *enter_addr(t_sdl *sdl)
Expand All @@ -49,8 +49,8 @@ char *enter_addr(t_sdl *sdl)
SDL_Color black = {0, 0, 0, 0};
TTF_Font *police;
int x;
int y;
int y;

police = TTF_OpenFont("ressources/bm.ttf",60);
addr = malloc(sizeof(*addr));
memset(addr, 0, sizeof(*addr));
Expand Down Expand Up @@ -86,7 +86,7 @@ char *enter_addr(t_sdl *sdl)
case SDL_MOUSEBUTTONDOWN:
x = event_queue.button.x;
y = event_queue.button.y;

if (( x > connect.x ) && ( x < connect.x + connect.w ) && ( y > connect.y ) && ( y < connect.y + connect.h ) )
if (strlen(addr) >= 7 && strlen(addr) <= 15)
quit = 1;
Expand All @@ -95,84 +95,10 @@ char *enter_addr(t_sdl *sdl)
SDL_RenderClear(sdl->renderer);
SDL_RenderCopy(sdl->renderer, sdl->white_back, NULL, NULL);
SDL_RenderCopy(sdl->renderer, sdl->server_welcome, NULL, &join_position);
SDL_RenderCopy(sdl->renderer, sdl->join_game, NULL, &connect);
SDL_RenderCopy(sdl->renderer, sdl->join_game, NULL, &connect);
SDL_RenderPresent(sdl->renderer);
SDL_SetRenderTarget(sdl->renderer, NULL);
}

return addr;
}

void client_loop(t_sdl *sdl, int socket, t_player_request *cr) {
int quit = 0;
//int x;
//int y;
SDL_Event event_queue;
SDL_Rect join_position = {200, 300, 400, 60};
SDL_Color black = {0, 0, 0, 0};
TTF_Font *police;
fd_set fd_read;

police = TTF_OpenFont("ressources/bm.ttf",60);
sdl->server_welcome = SDL_CreateTextureFromSurface(sdl->renderer, TTF_RenderText_Blended(police, "PATATE", black));
printf("%d", socket);
cr->command = 1;

while(!quit) {
//FD_SET(STDIN_FILENO, &fd_read);
//printf("toto");
if (select((socket + 1), &fd_read, NULL, NULL, NULL) == -1)
{
quit = 1;
}

while(SDL_PollEvent(&event_queue)) {
switch(event_queue.type){
case SDL_QUIT:
quit = 1;
break;
case SDL_KEYUP:
break;
case SDL_KEYDOWN:
switch(event_queue.key.keysym.sym){
case SDLK_UP:
printf("up\n");
send_request(socket, cr);
fseek(stdin,0,SEEK_END);
break;
case SDLK_DOWN:
printf("down\n");
send_request(socket, cr);
fseek(stdin,0,SEEK_END);
break;
case SDLK_RIGHT:
printf("right\n");
send_request(socket, cr);
fseek(stdin,0,SEEK_END);
break;
case SDLK_LEFT:
printf("left\n");
send_request(socket, cr);
fseek(stdin,0,SEEK_END);
break;
}
}
}

if (FD_ISSET(socket, &fd_read))
{
printf("tata\n");
if (get_message(socket) == 0){
quit = 1;
}
}

SDL_RenderClear(sdl->renderer);
SDL_RenderCopy(sdl->renderer, sdl->white_back, NULL, NULL);
SDL_RenderCopy(sdl->renderer, sdl->server_welcome, NULL, &join_position);
SDL_RenderPresent(sdl->renderer);
SDL_SetRenderTarget(sdl->renderer, NULL);
}

return;
}
10 changes: 8 additions & 2 deletions client/client_get_game_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@

int get_message(int s)
{
char buff[1024];
char buff[sizeof(t_game_info)];
int r;
t_game_info *game_info;

game_info = NULL;
r = recv(s, buff, 1024 - 1, 0);
r = recv(s, buff, sizeof(t_game_info), 0);
if (r > 0)
{
printf("%d", r);
deserialize_game_info(buff);
game_info = get_game_info();
printf("%d \n", game_info->game_status);
printf("%d \n", game_info->tick_time);
printf("%ld \n", (long int)game_info->checksum);
if (game_info->players[0] != NULL){
printf("%d \n", game_info->players[0]->num_player);
}
return 1;
}
else
Expand Down
17 changes: 11 additions & 6 deletions common/game_info_serialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@

char *serialize_game_info()
{
char *game_info_str;
char *game_info_str;
t_game_info *game_info;
t_game_info *tmp;

game_info = get_game_info();
tmp = malloc(sizeof(t_game_info));
//maybe not good at all
memcpy(tmp, &game_info, sizeof(t_game_info) + 1);
if ((game_info_str = calloc(1, sizeof(t_game_info))) == NULL)
return NULL;
game_info_str = (char*) game_info;
if ((game_info_str = realloc(game_info_str, sizeof(t_game_info) + 1)) == NULL)
game_info_str = (char*) tmp;
printf("before realloc\n");
if ((game_info_str = (char*)realloc(game_info_str, sizeof(t_game_info) + 1)) == NULL)
return NULL;
game_info_str[sizeof(t_game_info)] = '\0';
printf("before return\n");
return game_info_str;
}

void deserialize_game_info(char *serialized_game_info)
{
t_game_info *game_info;

game_info = (t_game_info*) serialized_game_info;
set_game_info(game_info);
}
Expand All @@ -33,7 +39,7 @@ int get_game_info_checksum()
unsigned char *p;
t_game_info *game_info;
int i;

game_info = get_game_info();
checksum = game_info->tick_time;
p = (unsigned char *)&game_info->players;
Expand All @@ -44,4 +50,3 @@ int get_game_info_checksum()
checksum += p[i];
return checksum;
}

4 changes: 2 additions & 2 deletions common/request_communication.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ char* request_serialization(t_player_request* client_request)
return NULL;
client_request->checksum = get_request_checksum(client_request);
request_string = (char*) client_request;
request_string = realloc(request_string,sizeof(t_player_request)+1);
request_string = realloc(request_string, sizeof(t_player_request)+1);
if (request_string == NULL)
return NULL;
request_string[sizeof(t_player_request)] = '\0';
Expand All @@ -52,4 +52,4 @@ t_player_request* request_deserialize(char* request_serialized)
client_request = (t_player_request*)request_serialized;

return client_request;
}
}
2 changes: 1 addition & 1 deletion common/request_serialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ int get_request_checksum(t_player_request* client_request)
{
int checksum = 0;
int i;

unsigned char *p = (unsigned char *)&client_request->magic;
for (i = 0; i<(int)sizeof(client_request->magic); i++) {
checksum += p[i];
Expand Down
2 changes: 1 addition & 1 deletion server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void *init_server() // sdl provient de old/old_server.c

// PROVIENT DE OLD_SERVER.c dans le folder old/
// sdl->server_welcome = NULL;

pthread_join(tick_thread, NULL);
pthread_join(main_thread, NULL);
return (NULL);
Expand Down

0 comments on commit 22cc2d1

Please sign in to comment.