Compare commits

...

8 commits

Author SHA1 Message Date
Theo Champion
541bc9ccf8 dev: Added sprite types and gave 'base' sprites their types 2025-08-14 21:47:21 +02:00
Theo Champion
0ac6ee9394 norme: Fixed little oopsie 2025-08-14 21:47:00 +02:00
Theo Champion
01f0ac21df feat: Made a little sprite interaction for perk type sprites
It's more for the sake of the demo but it shows we can make interactions
with sprites. Although it's badly integrated.
2025-08-14 21:41:35 +02:00
Theo Champion
ee329812fd dev: Added sprite types and gave 'base' sprites their types 2025-08-14 21:41:33 +02:00
Theo Champion
0d6cc4063a dev: changed the c var in place_base_sprites to a counter in data struct 2025-08-14 21:38:49 +02:00
Theo Champion
4a70e44697 fix: Added a check for the sprites, to avoid segfaults and/or invalid writes 2025-08-14 21:27:53 +02:00
Theo Champion
94075506f6 fix: Transferred sprite pathes to constants 2025-08-14 21:25:03 +02:00
Theo Champion
412dc89336 fix: Transferred trailing magic error messages as consts 2025-08-14 19:52:34 +02:00
11 changed files with 88 additions and 45 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */
/* Updated: 2025/08/12 14:49:36 by tchampio ### ########.fr */
/* Updated: 2025/08/14 21:23:25 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,6 +35,10 @@
// Sprite consts
# define SPRITE_TRANPARENCY_COLOR 0xff00dc
# define MAX_SPRITES 30
# define JUGG_TEX "ressources/juggernog.xpm"
# define REV_TEX "ressources/revive.xpm"
# define DBLTAP_TEX "ressources/doubletap.xpm"
# define MYSTERY_TEX "ressources/box.xpm"
# ifdef BONUS
# define COMPILED_TEXT "Compiled with bonuses"
@ -48,5 +52,12 @@
# define EMALMAP "Map is malformed (invalid chars or missing walls)"
# define ENOPLAYER "No player"
# define EHOLES "Holes in map"
# define ETRAILING "Trailing chars after map"
# define EBADCHARCOLOR "invalid characters in color definition"
# define EBADVALCOLOR "invalid value for colors"
# define ETOOMANYCOLORS "too many colors"
# define ENOTENOUGHCOLORS "Not enough colors"
# define EDUPTEX "Duplicated texture directive"
# define EMAPBFTEX "Map started before all the textures"
#endif

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */
/* Updated: 2025/08/11 11:59:21 by tchampio ### ########.fr */
/* Updated: 2025/08/14 21:36:15 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,6 +40,7 @@ typedef struct s_cub3d_data
int delta;
int last_tick;
t_sprite **sprite_list;
int sprite_counter;
double zbuffer[WIDTH];
int sprite_order[MAX_SPRITES];
double sprite_distances[MAX_SPRITES];

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:44:02 by kcolin #+# #+# */
/* Updated: 2025/07/23 13:34:33 by kcolin ### ########.fr */
/* Updated: 2025/08/14 19:46:26 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -70,7 +70,7 @@ bool populate_maps(t_mapdata *map, int fd)
{
if (line[0] != '\n' && end_reached)
{
ft_strlcpy(map->error, "Trailing chars after map", ERRLEN);
ft_strlcpy(map->error, ETRAILING, ERRLEN);
retvalue = false;
}
if (line[0] != '\n')

View file

@ -6,12 +6,13 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/21 19:35:43 by tchampio #+# #+# */
/* Updated: 2025/07/31 14:08:27 by kcolin ### ########.fr */
/* Updated: 2025/08/14 19:50:20 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../libft/includes/libft.h"
#include "../utils/frees.h"
#include "../consts.h"
#include "mapdata.h"
bool perform_color_checks(int *color, char *strcolor, t_mapdata *map)
@ -24,8 +25,7 @@ bool perform_color_checks(int *color, char *strcolor, t_mapdata *map)
{
if (!ft_isdigit(strcolor[i]))
{
ft_strlcpy(map->error, "invalid characters in color definition",
ERRLEN);
ft_strlcpy(map->error, EBADCHARCOLOR, ERRLEN);
return (false);
}
i++;
@ -33,7 +33,7 @@ bool perform_color_checks(int *color, char *strcolor, t_mapdata *map)
colorvalue = ft_atoi(strcolor);
if (colorvalue < 0 || colorvalue > 255)
{
ft_strlcpy(map->error, "invalid value for colors", ERRLEN);
ft_strlcpy(map->error, EBADVALCOLOR, ERRLEN);
return (false);
}
*color = colorvalue;
@ -53,14 +53,14 @@ unsigned int set_color(const char *s, t_mapdata *map)
while (tab[i])
{
if (i > 2)
ft_strlcpy(map->error, "too many colors", ERRLEN);
ft_strlcpy(map->error, ETOOMANYCOLORS, ERRLEN);
isok = perform_color_checks(&rgb[i], tab[i], map);
free(tab[i]);
i++;
}
free(tab);
if (i <= 2)
return (ft_strlcpy(map->error, "Not enough colors", ERRLEN), 0);
return (ft_strlcpy(map->error, ENOTENOUGHCOLORS, ERRLEN), 0);
if (!isok)
return (0);
finalcolor = ((rgb[0] & 0xff) << 16)
@ -77,7 +77,7 @@ int try_set_texture(t_mapdata *map, char **texture, char *texture_name)
{
if (*texture != NULL)
{
ft_strlcpy(map->error, "Duplicated texture directive", ERRLEN);
ft_strlcpy(map->error, EDUPTEX, ERRLEN);
return (2);
}
*texture = ft_strdup(texture_name);
@ -99,7 +99,7 @@ int set_textures(char *line, t_mapdata *map)
tab = ft_split(line, ' ');
if (tab[0][0] == '1')
return (free_tab(tab), ft_strlcpy(map->error,
"Map started before all the textures", ERRLEN), 1);
EMAPBFTEX, ERRLEN), 1);
retvalue = 0;
if (tab[0] && tab[1])
{

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 15:51:29 by kcolin #+# #+# */
/* Updated: 2025/07/29 13:28:08 by tchampio ### ########.fr */
/* Updated: 2025/08/13 15:31:17 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,11 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/12 15:43:19 by tchampio #+# #+# */
/* Updated: 2025/08/12 16:14:27 by tchampio ### ########.fr */
<<<<<<< HEAD
/* Updated: 2025/08/14 21:24:03 by tchampio ### ########.fr */
=======
/* Updated: 2025/08/13 14:53:38 by tchampio ### ########.fr */
>>>>>>> f9c846b (dev: Added sprite types and gave 'base' sprites their types)
/* */
/* ************************************************************************** */
@ -25,6 +29,7 @@ t_sprite *create_sprite(t_cub3d_data *data, char *texture,
return (NULL);
sprite->x = x;
sprite->y = y;
sprite->sprite_type = OTHER;
sprite->image = load_single_texture(data, texture);
return (sprite);
}
@ -34,15 +39,15 @@ t_sprite *place_right_sprite(t_cub3d_data *data, char c, double x, double y)
t_sprite *sprite;
if (c == 'M')
sprite = create_sprite(data, "ressources/box.xpm", x + 0.5, y + 0.5);
sprite = create_sprite(data, MYSTERY_TEX, x + 0.5, y + 0.5);
if (c == 'Q')
sprite = create_sprite(data,
"ressources/revive.xpm", x + 0.5, y + 0.5);
REV_TEX, x + 0.5, y + 0.5);
if (c == 'J')
sprite = create_sprite(data,
"ressources/juggernog.xpm", x + 0.5, y + 0.5);
JUGG_TEX, x + 0.5, y + 0.5);
if (c == 'D')
sprite = create_sprite(data,
"ressources/doubletap.xpm", x + 0.5, y + 0.5);
DBLTAP_TEX, x + 0.5, y + 0.5);
return (sprite);
}

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/05 12:59:44 by tchampio #+# #+# */
/* Updated: 2025/08/12 16:12:37 by tchampio ### ########.fr */
/* Updated: 2025/08/13 14:44:02 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,14 @@
# include "../draw/img_data.h"
typedef enum e_sprite_type
{
ZOMBIE,
PERK,
BOX,
OTHER
} t_sprite_type;
/*
* x - real position for the sprite
* y - real position for the sprite
@ -36,23 +44,24 @@
*/
typedef struct s_sprite
{
double x;
double y;
t_img_data *image;
int img_width;
int img_height;
double sprite_pos_x;
double sprite_pos_y;
double inv_det;
double transform_x;
double transform_y;
int sprite_screen_x;
int sprite_height;
int sprite_draw_start_y;
int sprite_draw_end_y;
int sprite_width;
int sprite_draw_start_x;
int sprite_draw_end_x;
double x;
double y;
t_img_data *image;
int img_width;
int img_height;
double sprite_pos_x;
double sprite_pos_y;
double inv_det;
double transform_x;
double transform_y;
int sprite_screen_x;
int sprite_height;
int sprite_draw_start_y;
int sprite_draw_end_y;
int sprite_width;
int sprite_draw_start_x;
int sprite_draw_end_x;
t_sprite_type sprite_type;
} t_sprite;
#endif // SPRITE_H

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/05 15:51:01 by tchampio #+# #+# */
/* Updated: 2025/08/07 16:57:29 by tchampio ### ########.fr */
/* Updated: 2025/08/13 15:30:55 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,8 +15,8 @@
#include "../renderer/render.h"
#include "sort_sprites.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../libft/includes/libft.h"
static void calculate_pos_and_transform(t_cub3d_data *data, t_sprite *sprite,
int i)
@ -109,6 +109,17 @@ void sprite_caster(t_cub3d_data *data)
if (!data->sprite_list)
return ;
sort_sprites(data->sprite_order, data->sprite_distances, data);
if (data->sprite_distances[3] <= 1.5)
{
if (data->sprite_list[data->sprite_order[3]]->sprite_type == PERK)
{
if (data->keypresses.is_f_pressed)
{
data->keypresses.is_f_pressed = false;
ft_printf("🎵You need a little revive🎵\n");
}
}
}
i = 0;
while (data->sprite_list[i] && i < MAX_SPRITES)
{

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */
/* Updated: 2025/07/29 13:35:29 by tchampio ### ########.fr */
/* Updated: 2025/08/13 15:25:04 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,6 +31,8 @@ int keypress_handler(int keycode, t_cub3d_data *data)
data->keypresses.is_s_pressed = true;
if (keycode == XK_d)
data->keypresses.is_d_pressed = true;
if (keycode == XK_f)
data->keypresses.is_f_pressed = true;
if (keycode == XK_Left)
data->keypresses.is_left_pressed = true;
if (keycode == XK_Right)
@ -48,6 +50,8 @@ int keyrelease_handler(int keycode, t_cub3d_data *data)
data->keypresses.is_s_pressed = false;
if (keycode == XK_d)
data->keypresses.is_d_pressed = false;
if (keycode == XK_f)
data->keypresses.is_f_pressed = false;
if (keycode == XK_Left)
data->keypresses.is_left_pressed = false;
if (keycode == XK_Right)

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
/* Updated: 2025/08/12 16:13:50 by tchampio ### ########.fr */
/* Updated: 2025/08/14 21:47:17 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -58,10 +58,8 @@ void place_base_sprites(t_cub3d_data *data, char **map)
{
int y;
int x;
int c;
y = 0;
c = 0;
while (y < data->map->mapheight)
{
x = 0;
@ -69,8 +67,11 @@ void place_base_sprites(t_cub3d_data *data, char **map)
{
if (map[y][x] == 'M' || map[y][x] == 'Q'
|| map[y][x] == 'J' || map[y][x] == 'D')
data->sprite_list[c++] = place_right_sprite(data,
map[y][x], x, y);
{
if (data->sprite_counter < MAX_SPRITES)
data->sprite_list[data->sprite_counter++]
= place_right_sprite(data, map[y][x], x, y);
}
x++;
}
y++;

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/29 13:42:39 by tchampio #+# #+# */
/* Updated: 2025/07/29 20:09:30 by tchampio ### ########.fr */
/* Updated: 2025/08/13 15:22:54 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,6 +21,7 @@ typedef struct s_keypresses
bool is_d_pressed;
bool is_left_pressed;
bool is_right_pressed;
bool is_f_pressed;
} t_keypresses;
#endif // KEYPRESSES_H