fix: moved almost all variables to the sprite structure

This commit is contained in:
Theo Champion 2025-08-07 12:59:43 +02:00
parent 10576f5751
commit c8da6964d0
2 changed files with 61 additions and 42 deletions

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/05 12:59:44 by tchampio #+# #+# */ /* Created: 2025/08/05 12:59:44 by tchampio #+# #+# */
/* Updated: 2025/08/06 12:13:12 by tchampio ### ########.fr */ /* Updated: 2025/08/06 14:41:17 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,25 @@
# include "../draw/img_data.h" # include "../draw/img_data.h"
/*
* x - real position for the sprite
* y - real position for the sprite
* image - texture
* img_width - width of texture
* img_height - height of texture
* sprite_pos_x - Position of the sprite relative to the player
* sprite_pos_y - Position of the sprite relative to the player
* inv_det - Inversion of the matrix (playerpos and spritepos)
* transform_x - Result of the inversion
* transform_y - Result of the inversion
* sprite_screen_x - Position of the sprite on screen (horizontal)
* sprite_height - height of the sprite on screen
* sprite_draw_start_y - for the while loop when rendering (col)
* sprite_draw_end_y - for the while loop when rendering (col)
* sprite_width - width of the sprite on screen
* sprite_draw_start_x - for the while loop when rendering (row)
* sprite_draw_end_x - for the while loop when rendering (row)
*/
typedef struct s_sprite typedef struct s_sprite
{ {
double x; double x;
@ -22,6 +41,18 @@ typedef struct s_sprite
t_img_data *image; t_img_data *image;
int img_width; int img_width;
int img_height; 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; } t_sprite;
#endif // SPRITE_H #endif // SPRITE_H

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/05 15:51:01 by tchampio #+# #+# */ /* Created: 2025/08/05 15:51:01 by tchampio #+# #+# */
/* Updated: 2025/08/07 12:07:36 by tchampio ### ########.fr */ /* Updated: 2025/08/07 12:58:45 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,18 +21,6 @@
void sprite_caster(t_cub3d_data *data) void sprite_caster(t_cub3d_data *data)
{ {
int i; int i;
double spriteX;
double spriteY;
double invDet;
double transformX;
double transformY;
int spriteScreenX;
int spriteHeight;
int spriteDrawStartY;
int spriteDrawEndY;
int spriteWidth;
int spriteDrawStartX;
int spriteDrawEndX;
int stripe; int stripe;
int texX; int texX;
int d; int d;
@ -44,37 +32,37 @@ void sprite_caster(t_cub3d_data *data)
i = 0; i = 0;
while (data->sprite_list[i] && i < MAX_SPRITES) while (data->sprite_list[i] && i < MAX_SPRITES)
{ {
spriteX = data->sprite_list[data->sprite_order[i]]->x - data->player.x; data->sprite_list[i]->sprite_pos_x = data->sprite_list[data->sprite_order[i]]->x - data->player.x;
spriteY = data->sprite_list[data->sprite_order[i]]->y - data->player.y; data->sprite_list[i]->sprite_pos_y = data->sprite_list[data->sprite_order[i]]->y - data->player.y;
invDet = 1.0 / (data->player.plane_x * data->player.dir_y - data->player.dir_x * data->player.plane_y); data->sprite_list[i]->inv_det = 1.0 / (data->player.plane_x * data->player.dir_y - data->player.dir_x * data->player.plane_y);
transformX = invDet * (data->player.dir_y * spriteX - data->player.dir_x * spriteY); data->sprite_list[i]->transform_x = data->sprite_list[i]->inv_det * (data->player.dir_y * data->sprite_list[i]->sprite_pos_x - data->player.dir_x * data->sprite_list[i]->sprite_pos_y);
transformY = invDet * (-data->player.plane_y * spriteX + data->player.plane_x * spriteY); data->sprite_list[i]->transform_y = data->sprite_list[i]->inv_det * (-data->player.plane_y * data->sprite_list[i]->sprite_pos_x + data->player.plane_x * data->sprite_list[i]->sprite_pos_y);
spriteScreenX = (int)((WIDTH / 2) * (1 + transformX / transformY)); data->sprite_list[i]->sprite_screen_x = (int)((WIDTH / 2) * (1 + data->sprite_list[i]->transform_x / data->sprite_list[i]->transform_y));
spriteHeight = (int)fabs((HEIGHT / transformY)); data->sprite_list[i]->sprite_height = (int)fabs((HEIGHT / data->sprite_list[i]->transform_y));
spriteDrawStartY = -spriteHeight / 2 + HEIGHT / 2; data->sprite_list[i]->sprite_draw_start_y = -data->sprite_list[i]->sprite_height / 2 + HEIGHT / 2;
if (spriteDrawStartY < 0) if (data->sprite_list[i]->sprite_draw_start_y < 0)
spriteDrawStartY = 0; data->sprite_list[i]->sprite_draw_start_y = 0;
spriteDrawEndY = spriteHeight / 2 + HEIGHT / 2; data->sprite_list[i]->sprite_draw_end_y = data->sprite_list[i]->sprite_height / 2 + HEIGHT / 2;
if (spriteDrawEndY >= HEIGHT) if (data->sprite_list[i]->sprite_draw_end_y >= HEIGHT)
spriteDrawEndY = HEIGHT - 1; data->sprite_list[i]->sprite_draw_end_y = HEIGHT - 1;
spriteWidth = (int)fabs((HEIGHT / transformY)); data->sprite_list[i]->sprite_width = (int)fabs((HEIGHT / data->sprite_list[i]->transform_y));
spriteDrawStartX = -spriteWidth / 2 + spriteScreenX; data->sprite_list[i]->sprite_draw_start_x = -data->sprite_list[i]->sprite_width / 2 + data->sprite_list[i]->sprite_screen_x;
if (spriteDrawStartX < 0) if (data->sprite_list[i]->sprite_draw_start_x < 0)
spriteDrawStartX = 0; data->sprite_list[i]->sprite_draw_start_x = 0;
spriteDrawEndX = spriteWidth / 2 + spriteScreenX; data->sprite_list[i]->sprite_draw_end_x = data->sprite_list[i]->sprite_width / 2 + data->sprite_list[i]->sprite_screen_x;
if (spriteDrawEndX >= WIDTH) if (data->sprite_list[i]->sprite_draw_end_x >= WIDTH)
spriteDrawEndX = WIDTH - 1; data->sprite_list[i]->sprite_draw_end_x = WIDTH - 1;
stripe = spriteDrawStartX; stripe = data->sprite_list[i]->sprite_draw_start_x;
while (stripe < spriteDrawEndX) while (stripe < data->sprite_list[i]->sprite_draw_end_x)
{ {
texX = (int)(256 * (stripe - (-spriteWidth / 2 + spriteScreenX)) * SIZE / spriteWidth) / 256; texX = (int)(256 * (stripe - (-data->sprite_list[i]->sprite_width / 2 + data->sprite_list[i]->sprite_screen_x)) * SIZE / data->sprite_list[i]->sprite_width) / 256;
if (transformY > 0 && stripe > 0 && stripe < WIDTH && transformY < data->zbuffer[stripe]) if (data->sprite_list[i]->transform_y > 0 && stripe > 0 && stripe < WIDTH && data->sprite_list[i]->transform_y < data->zbuffer[stripe])
{ {
j = spriteDrawStartY; j = data->sprite_list[i]->sprite_draw_start_y;
while (j < spriteDrawEndY) while (j < data->sprite_list[i]->sprite_draw_end_y)
{ {
d = (j) * 256 - HEIGHT * 128 + spriteHeight * 128; d = (j) * 256 - HEIGHT * 128 + data->sprite_list[i]->sprite_height * 128;
texY = ((d * SIZE) / spriteHeight) / 256; texY = ((d * SIZE) / data->sprite_list[i]->sprite_height) / 256;
color = my_mlx_pixel_get(data->sprite_list[data->sprite_order[i]]->image, texX, texY); color = my_mlx_pixel_get(data->sprite_list[data->sprite_order[i]]->image, texX, texY);
if (color != SPRITE_TRANPARENCY_COLOR) if (color != SPRITE_TRANPARENCY_COLOR)
matrix_set(data, stripe, j, color); matrix_set(data, stripe, j, color);