Skip to content

Commit

Permalink
player
Browse files Browse the repository at this point in the history
  • Loading branch information
bda-mota committed Aug 20, 2024
1 parent 0e49c7c commit bcab457
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions includes_bonus/cub_bonus.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ void handle_error(char *error);
/* FREE_MEMORY */
void free_data(t_data *data);
void free_memory(t_cub *game);
void free_sprite(t_cub *game, t_images *sprite);

#endif
1 change: 1 addition & 0 deletions includes_bonus/graphic_bonus.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mlx_texture_t *init_images(char *path);
void load_textures(t_cub *game);
t_images *init_generic_images(t_cub *game, char *path);
uint32_t get_texture_color(mlx_texture_t *texture, int y, int x);
void update_player(t_cub *game);

/* DRAW SQUARES */
void draw_background(t_cub *game);
Expand Down
1 change: 1 addition & 0 deletions includes_bonus/structs_bonus.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef struct s_keys
bool left;
bool right;
bool minimap;
bool player;
} t_keys;

typedef struct s_validate
Expand Down
11 changes: 11 additions & 0 deletions sources_bonus/free_memory_bonus.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,15 @@ void free_memory(t_cub *game)
mlx_delete_texture(game->west);
if (game->east)
mlx_delete_texture(game->east);
if (game->player_1)
free_sprite(game, game->player_1);
if (game->player_2)
free_sprite(game, game->player_2);
}

void free_sprite(t_cub *game, t_images *sprite)
{
mlx_delete_image(game->mlx, sprite->image);
mlx_delete_texture(sprite->texture);
free(sprite);
}
1 change: 1 addition & 0 deletions sources_bonus/graphic_bonus/draw_loop_bonus.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void draw_playerview(void *param)
game = (t_cub *)param;
mlx_get_mouse_pos(game->mlx, &x, &y);
update_fps(game);
update_player(game);
draw_minimap(game);
update_minimap(game);
process_input(game, x);
Expand Down
2 changes: 2 additions & 0 deletions sources_bonus/graphic_bonus/hooks_bonus.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ void hook_key_press(mlx_key_data_t keydata, void *param)
}
if (keydata.key == MLX_KEY_M && keydata.action == MLX_PRESS)
game->keys.minimap = !game->keys.minimap;
if (keydata.key == MLX_KEY_SPACE && keydata.action == MLX_PRESS)
game->keys.player = !game->keys.player;
else if (keydata.action == MLX_PRESS || keydata.action == MLX_REPEAT)
pressed(keydata, game);
else if (keydata.action == MLX_RELEASE)
Expand Down
27 changes: 25 additions & 2 deletions sources_bonus/graphic_bonus/images_bonus.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ void load_textures(t_cub *game)
game->south = init_images(game->data->so);
game->west = init_images(game->data->we);
game->east = init_images(game->data->ea);
game->player_1 = init_generic_images(game, "textures/player1.png");
game->player_2 = init_generic_images(game, "textures/player2.png");
game->player_1 = init_generic_images(game, "textures/player_1.png");
game->player_2 = init_generic_images(game, "textures/player_2.png");
game->player_2->image->enabled = false;
}

mlx_texture_t *init_images(char *path)
Expand Down Expand Up @@ -53,3 +54,25 @@ uint32_t get_texture_color(mlx_texture_t *texture, int y, int x)
pixel = &texture->pixels[texture_pos];
return (pixel[0] << 24 | pixel[1] << 16 | pixel[2] << 8 | pixel[3]);
}

void update_player(t_cub *game)
{
static double last_time;
double current_time;

last_time = 0;
current_time = mlx_get_time();
if (game->keys.player)
{
game->player_1->image->enabled = false;
game->player_2->image->enabled = true;
last_time = current_time;
if (current_time - last_time >= 0.5)
game->keys.player = false;
}
if (!game->keys.player)
{
game->player_1->image->enabled = true;
game->player_2->image->enabled = false;
}
}
1 change: 1 addition & 0 deletions sources_bonus/init_bonus.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void init_cub(t_cub *game)
game->keys.left = false;
game->keys.right = false;
game->keys.minimap = false;
game->keys.player = false;
game->north = NULL;
game->south = NULL;
game->west = NULL;
Expand Down
1 change: 1 addition & 0 deletions sources_bonus/main_bonus.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ int init_game(t_cub *game)
handle_mlx_actions(NEW_IMAGE, game);
load_textures(game);
mlx_image_to_window(game->mlx, game->player_1->image, WIDTH / 3, 350);
mlx_image_to_window(game->mlx, game->player_2->image, WIDTH / 3, 350);
setup(game);
mlx_set_mouse_pos(game->mlx, WIDTH / 2, HEIGHT / 2);
mlx_key_hook(game->mlx, hook_key_press, game);
Expand Down
Binary file removed textures/player1.png
Binary file not shown.
Binary file removed textures/player2.png
Binary file not shown.
File renamed without changes
File renamed without changes

0 comments on commit bcab457

Please sign in to comment.