feat: Added the round counter on the hud

This commit is contained in:
Theo Champion 2025-09-03 18:39:30 +02:00
parent 82d7941e81
commit f80eb125f5
24 changed files with 1217 additions and 115 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */
/* Updated: 2025/09/03 16:31:54 by tchampio ### ########.fr */
/* Updated: 2025/09/03 18:22:15 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,6 +32,8 @@ typedef struct s_cub3d_data
t_img_data *img_data;
t_img_data *point_figures[10];
t_img_data *perk_logos[3];
t_img_data *tally_marks[5];
t_img_data *round_figures[10];
t_mapdata *map;
t_player player;
t_keypresses keypresses;
@ -43,6 +45,7 @@ typedef struct s_cub3d_data
double zbuffer[WIDTH];
int sprite_order[MAX_SPRITES];
double sprite_distances[MAX_SPRITES];
int round;
} t_cub3d_data;
#endif // CUB3D_DATA_H

95
src/hud/load_texture.c Normal file
View file

@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* load_texture.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/03 17:02:08 by tchampio #+# #+# */
/* Updated: 2025/09/03 18:24:18 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../libft/includes/libft.h"
#include "../cub3d_data.h"
#include "../utils/frees.h"
#include "../utils/inits.h"
#include "../../mlx/mlx.h"
t_img_data *load_hud_texture(t_cub3d_data *data, char *path)
{
int width;
int height;
void *img;
t_img_data *img_data;
img = mlx_xpm_file_to_image(data->mlx, path, &width, &height);
if (img == NULL)
{
ft_printf("Error: failed to open image at %s\n", path);
destroy(data, 1);
}
ft_printf("image: %p\n", img);
img_data = ft_calloc(sizeof(t_img_data), 1);
img_data->img = img;
img_data->addr = mlx_get_data_addr(img_data->img,
&img_data->bits_per_pixel, &img_data->line_length,
&img_data->endian);
img_data->height = height;
img_data->width = width;
return (img_data);
}
void load_points_textures(t_cub3d_data *data)
{
data->point_figures[0] = load_hud_texture(data, "ressources/zero.xpm");
data->point_figures[1] = load_hud_texture(data, "ressources/one.xpm");
data->point_figures[2] = load_hud_texture(data, "ressources/two.xpm");
data->point_figures[3] = load_hud_texture(data, "ressources/three.xpm");
data->point_figures[4] = load_hud_texture(data, "ressources/four.xpm");
data->point_figures[5] = load_hud_texture(data, "ressources/five.xpm");
data->point_figures[6] = load_hud_texture(data, "ressources/six.xpm");
data->point_figures[7] = load_hud_texture(data, "ressources/seven.xpm");
data->point_figures[8] = load_hud_texture(data, "ressources/eight.xpm");
data->point_figures[9] = load_hud_texture(data, "ressources/nine.xpm");
}
void load_perk_logos(t_cub3d_data *data)
{
data->perk_logos[0] = load_hud_texture(data,
"ressources/juggernog_logo.xpm");
data->perk_logos[1] = load_hud_texture(data,
"ressources/revive_logo.xpm");
data->perk_logos[2] = load_hud_texture(data,
"ressources/doubletap_logo.xpm");
}
void load_round_indicators(t_cub3d_data *data)
{
data->tally_marks[4] = load_hud_texture(data, "ressources/tally_five.xpm");
data->tally_marks[3] = load_hud_texture(data, "ressources/tally_four.xpm");
data->tally_marks[2] = load_hud_texture(data, "ressources/tally_three.xpm");
data->tally_marks[1] = load_hud_texture(data, "ressources/tally_two.xpm");
data->tally_marks[0] = load_hud_texture(data, "ressources/tally_one.xpm");
data->round_figures[0] = load_hud_texture(data, "ressources/round_zero.xpm");
data->round_figures[1] = load_hud_texture(data, "ressources/round_one.xpm");
data->round_figures[2] = load_hud_texture(data, "ressources/round_two.xpm");
data->round_figures[3] = load_hud_texture(data, "ressources/round_three.xpm");
data->round_figures[4] = load_hud_texture(data, "ressources/round_four.xpm");
data->round_figures[5] = load_hud_texture(data, "ressources/round_five.xpm");
data->round_figures[6] = load_hud_texture(data, "ressources/round_six.xpm");
data->round_figures[7] = load_hud_texture(data, "ressources/round_seven.xpm");
data->round_figures[8] = load_hud_texture(data, "ressources/round_eight.xpm");
data->round_figures[9] = load_hud_texture(data, "ressources/round_nine.xpm");
}
void load_textures(t_cub3d_data *data)
{
data->no_texture = load_single_texture(data, data->map->no_texture);
data->so_texture = load_single_texture(data, data->map->so_texture);
data->we_texture = load_single_texture(data, data->map->we_texture);
data->ea_texture = load_single_texture(data, data->map->ea_texture);
load_points_textures(data);
load_perk_logos(data);
load_round_indicators(data);
}

21
src/hud/load_texture.h Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* load_texture.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/03 17:06:02 by tchampio #+# #+# */
/* Updated: 2025/09/03 17:06:52 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LOAD_TEXTURE_H
# define LOAD_TEXTURE_H
# include "../cub3d_data.h"
t_img_data *load_hud_texture(t_cub3d_data *data, char *path);
void load_textures(t_cub3d_data *data);
#endif // LOAD_TEXTURE_H

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
/* Updated: 2025/09/03 16:32:13 by tchampio ### ########.fr */
/* Updated: 2025/09/03 18:31:54 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -74,12 +74,33 @@ void draw_perks(t_cub3d_data *data)
}
}
void draw_round(t_cub3d_data *data)
{
int i;
int pos;
char round_str[5];
if (data->round <= 5)
return (matrix_image_put(data, data->tally_marks[data->round - 1], 20, HEIGHT - 85));
pos = 20;
i = 0;
ft_itoa_static(data->round, round_str, 5);
while (round_str[i])
{
matrix_image_put(data, data->round_figures[round_str[i] - '0'], pos, HEIGHT - 85);
pos += 50;
i++;
}
}
void create_hud(t_cub3d_data *data)
{
// draw points
draw_points(data);
// draw perks
draw_perks(data);
// draw round
draw_round(data);
// draw weapon
// draw map
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */
/* Updated: 2025/09/01 15:39:07 by tchampio ### ########.fr */
/* Updated: 2025/09/03 17:54:36 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,6 +39,8 @@ int keypress_handler(int keycode, t_cub3d_data *data)
data->keypresses.is_right_pressed = true;
if (keycode == XK_p)
data->player.points += 500;
if (keycode == XK_u)
data->round++;
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
/* Updated: 2025/09/01 17:49:33 by tchampio ### ########.fr */
/* Updated: 2025/09/03 17:53:41 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
#include "../../libft/includes/libft.h"
#include "../../mlx/mlx.h"
#include "../map/map_checker.h"
#include "../hud/load_texture.h"
#include "../sprites/create_sprite.h"
#include "frees.h"
#include <stdio.h>
@ -32,11 +33,11 @@ t_img_data *load_single_texture(t_cub3d_data *data, char *path)
ft_printf("Error: failed to open image at %s\n", path);
destroy(data, 1);
}
// if (width != height || width != TEXTURE_SIZE)
// {
// ft_printf("Error: textures are not the right size\n");
// destroy(data, 1);
// }
// if (width != height || width != TEXTURE_SIZE)
// {
// ft_printf("Error: textures are not the right size\n");
// destroy(data, 1);
// }
ft_printf("image: %p\n", img);
img_data = ft_calloc(sizeof(t_img_data), 1);
img_data->img = img;
@ -48,27 +49,6 @@ t_img_data *load_single_texture(t_cub3d_data *data, char *path)
return (img_data);
}
void load_textures(t_cub3d_data *data)
{
data->no_texture = load_single_texture(data, data->map->no_texture);
data->so_texture = load_single_texture(data, data->map->so_texture);
data->we_texture = load_single_texture(data, data->map->we_texture);
data->ea_texture = load_single_texture(data, data->map->ea_texture);
data->point_figures[0] = load_single_texture(data, "ressources/zero.xpm");
data->point_figures[1] = load_single_texture(data, "ressources/one.xpm");
data->point_figures[2] = load_single_texture(data, "ressources/two.xpm");
data->point_figures[3] = load_single_texture(data, "ressources/three.xpm");
data->point_figures[4] = load_single_texture(data, "ressources/four.xpm");
data->point_figures[5] = load_single_texture(data, "ressources/five.xpm");
data->point_figures[6] = load_single_texture(data, "ressources/six.xpm");
data->point_figures[7] = load_single_texture(data, "ressources/seven.xpm");
data->point_figures[8] = load_single_texture(data, "ressources/eight.xpm");
data->point_figures[9] = load_single_texture(data, "ressources/nine.xpm");
data->perk_logos[0] = load_single_texture(data, "ressources/juggernog_logo.xpm");
data->perk_logos[1] = load_single_texture(data, "ressources/revive_logo.xpm");
data->perk_logos[2] = load_single_texture(data, "ressources/doubletap_logo.xpm");
}
void place_base_sprites(t_cub3d_data *data, char **map)
{
int y;
@ -96,6 +76,7 @@ void init_cub3d_data(t_cub3d_data *data, char **argv)
{
ft_bzero(data, sizeof(*data));
data->map = ft_calloc(sizeof(t_mapdata), 1);
data->round = 1;
if (!check_cubfile(argv[1], data->map))
return (ft_printf("Error: Wrong map file. Reason: %s\n",
data->map->error), free_map(data->map), exit(1));