feat: Made sprites dynamic in memory and allocated in heap

This commit is contained in:
Theo Champion 2025-08-07 12:35:01 +02:00
parent 2e0fd93831
commit 10576f5751
5 changed files with 35 additions and 76 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */ /* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */
/* Updated: 2025/08/06 14:23:35 by tchampio ### ########.fr */ /* Updated: 2025/08/07 11:38:18 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,7 +26,7 @@
# define TEXTURE_SIZE 64 # define TEXTURE_SIZE 64
# define SPRITE_TRANPARENCY_COLOR 0xff00dc # define SPRITE_TRANPARENCY_COLOR 0xff00dc
// 4 static ones, 3 perks, the box and 25 zombies at max // 4 static ones, 3 perks, the box and 25 zombies at max
# define MAX_SPRITES 1 // FIXME: Change to 30 # define MAX_SPRITES 30 // FIXME: Change to 30
# ifdef BONUS # ifdef BONUS
# define COMPILED_TEXT "Compiled with bonuses" # define COMPILED_TEXT "Compiled with bonuses"
# else # else

View file

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

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
/* Updated: 2025/08/06 13:36:07 by tchampio ### ########.fr */ /* Updated: 2025/08/07 12:27:47 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -61,9 +61,15 @@ int main(int argc, char **argv)
return (ft_printf("Error: Missing cub3d file\n"), 1); return (ft_printf("Error: Missing cub3d file\n"), 1);
init_cub3d_data(&data, argv); init_cub3d_data(&data, argv);
// placing a sprite next to player to ease debugging // placing a sprite next to player to ease debugging
data.static_sprite[0].x = data.map->startx + 1; data.sprite_list = ft_calloc(sizeof(t_sprite *), 3);
data.static_sprite[0].y = data.map->starty; data.sprite_list[0] = ft_calloc(sizeof(t_sprite), 1);
data.static_sprite[0].image = load_single_texture(&data, "ressources/box_no_transparent.xpm"); data.sprite_list[1] = ft_calloc(sizeof(t_sprite), 1);
data.sprite_list[0]->x = data.map->startx + 1;
data.sprite_list[0]->y = data.map->starty;
data.sprite_list[0]->image = load_single_texture(&data, "ressources/box.xpm");
data.sprite_list[1]->x = data.map->startx;
data.sprite_list[1]->y = data.map->starty - 2;
data.sprite_list[1]->image = load_single_texture(&data, "ressources/test.xpm");
mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data); mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data);
mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask, mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask,
keyrelease_handler, &data); keyrelease_handler, &data);

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/06 14:17:58 by tchampio #+# #+# */ /* Created: 2025/08/06 14:17:58 by tchampio #+# #+# */
/* Updated: 2025/08/06 14:21:16 by tchampio ### ########.fr */ /* Updated: 2025/08/07 12:25:43 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,18 +25,18 @@ static void swap(int i, int j, int *spriteorder, double *spritedist)
spritedist[j] = tmp_dist; spritedist[j] = tmp_dist;
} }
static void bubble_sort(double *dist, int *order) static void bubble_sort(double *dist, int *order, int count)
{ {
int j; int j;
int k; int k;
j = 0; j = 0;
while (j < MAX_SPRITES) while (j < count)
{ {
k = 0; k = 0;
while (j < MAX_SPRITES - j - 1) while (k < count - j - 1)
{ {
if (dist[k] < dist[k + 1] && dist[k] != -1) if (dist[k] < dist[k + 1])
swap(k, k + 1, order, dist); swap(k, k + 1, order, dist);
k++; k++;
} }
@ -51,20 +51,23 @@ void sort_sprites(int *spriteorder, double *spritedist, t_cub3d_data *data)
int i; int i;
int order[MAX_SPRITES]; int order[MAX_SPRITES];
double dist[MAX_SPRITES]; double dist[MAX_SPRITES];
int spritescount;
i = 0; i = 0;
while (i < MAX_SPRITES) spritescount = 0;
while (data->sprite_list[i] && i < MAX_SPRITES)
{ {
order[i] = i; order[i] = i;
dist[i] = ((data->player.x - data->static_sprite[i].x) * (data->player.x - data->static_sprite[i].x) + (data->player.y - data->static_sprite[i].y) * (data->player.y - data->static_sprite[i].y)); dist[i] = ((data->player.x - data->sprite_list[i]->x) * (data->player.x - data->sprite_list[i]->x) + (data->player.y - data->sprite_list[i]->y) * (data->player.y - data->sprite_list[i]->y));
spritescount++;
i++; i++;
} }
bubble_sort(dist, order); bubble_sort(dist, order, spritescount);
i = 0; i = 0;
while (i < MAX_SPRITES) while (i < spritescount)
{ {
spritedist[i] = dist[MAX_SPRITES - i - 1]; spritedist[i] = dist[i];
spriteorder[i] = order[MAX_SPRITES - i - 1]; spriteorder[i] = order[i];
i++; i++;
} }
} }

View file

@ -6,68 +6,18 @@
/* 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/06 13:46:34 by tchampio ### ########.fr */ /* Updated: 2025/08/07 12:07:36 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../cub3d_data.h" #include "../cub3d_data.h"
#include "../draw/drawutils.h" #include "../draw/drawutils.h"
#include "../renderer/render.h" #include "../renderer/render.h"
#include "sort_sprites.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
static void swap(int i, int j, int *spriteorder, double *spritedist)
{
double tmp_dist;
int tmp_order;
tmp_order = spriteorder[i];
tmp_dist = spritedist[i];
spriteorder[i] = spriteorder[j];
spritedist[i] = spritedist[j];
spriteorder[j] = tmp_order;
spritedist[j] = tmp_dist;
}
// the sorting part is a readaptation of the std::sort function from c++
// more info on here: https://stackoverflow.com/questions/23816797/how-does-stdsort-work-for-list-of-pairs
static void sort_sprites(int *spriteorder, double *spritedist, t_cub3d_data *data)
{
int i;
int j;
int k;
int order[MAX_SPRITES];
double dist[MAX_SPRITES];
i = 0;
while (i < MAX_SPRITES)
{
order[i] = i;
dist[i] = ((data->player.x - data->static_sprite[i].x) * (data->player.x - data->static_sprite[i].x) + (data->player.y - data->static_sprite[i].y) * (data->player.y - data->static_sprite[i].y));
i++;
}
j = 0;
while (j < MAX_SPRITES)
{
k = 0;
while (j < MAX_SPRITES - j - 1)
{
if (dist[k] < dist[k + 1] && dist[k] != -1)
swap(k, k + 1, order, dist);
k++;
}
j++;
}
i = 0;
while (i < MAX_SPRITES)
{
spritedist[i] = dist[MAX_SPRITES - i - 1];
spriteorder[i] = order[MAX_SPRITES - i - 1];
i++;
}
}
void sprite_caster(t_cub3d_data *data) void sprite_caster(t_cub3d_data *data)
{ {
int i; int i;
@ -92,10 +42,10 @@ void sprite_caster(t_cub3d_data *data)
sort_sprites(data->sprite_order, data->sprite_distances, data); sort_sprites(data->sprite_order, data->sprite_distances, data);
i = 0; i = 0;
while (i < MAX_SPRITES) while (data->sprite_list[i] && i < MAX_SPRITES)
{ {
spriteX = data->static_sprite[i].x - data->player.x; spriteX = data->sprite_list[data->sprite_order[i]]->x - data->player.x;
spriteY = data->static_sprite[i].y - data->player.y; spriteY = 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); invDet = 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); transformX = invDet * (data->player.dir_y * spriteX - data->player.dir_x * spriteY);
transformY = invDet * (-data->player.plane_y * spriteX + data->player.plane_x * spriteY); transformY = invDet * (-data->player.plane_y * spriteX + data->player.plane_x * spriteY);
@ -125,8 +75,8 @@ void sprite_caster(t_cub3d_data *data)
{ {
d = (j) * 256 - HEIGHT * 128 + spriteHeight * 128; d = (j) * 256 - HEIGHT * 128 + spriteHeight * 128;
texY = ((d * SIZE) / spriteHeight) / 256; texY = ((d * SIZE) / spriteHeight) / 256;
color = my_mlx_pixel_get(data->static_sprite[0].image, texX, texY); // data->static_sprite[0].image->img[SIZE * texY + texX]; color = my_mlx_pixel_get(data->sprite_list[data->sprite_order[i]]->image, texX, texY);
if ((color & SPRITE_TRANPARENCY_COLOR) != 0) if (color != SPRITE_TRANPARENCY_COLOR)
matrix_set(data, stripe, j, color); matrix_set(data, stripe, j, color);
j++; j++;
} }