feat: allocate screen_matrix on the heap

This commit is contained in:
Khaïs COLIN 2025-07-31 13:17:27 +02:00
parent 8d0a6d841b
commit e625e254f7
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
7 changed files with 30 additions and 36 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */
/* Updated: 2025/07/30 16:07:36 by tchampio ### ########.fr */
/* Updated: 2025/07/31 13:23:32 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,7 @@ typedef struct s_cub3d_data
t_mapdata *map;
t_player player;
t_keypresses keypresses;
int screen_matrix[HEIGHT][WIDTH];
int *screen_matrix;
} t_cub3d_data;
#endif // CUB3D_DATA_H

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
/* Updated: 2025/07/31 11:28:04 by tchampio ### ########.fr */
/* Updated: 2025/07/31 13:25:36 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -64,6 +64,7 @@ int main(int argc, char **argv)
return (ft_printf("Error: Failed to initalize mlx\n"),
free_map(data.map), 1);
data.mlx_win = mlx_new_window(data.mlx, WIDTH, HEIGHT, "Cub3d");
data.screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT);
data.img_data = ft_calloc(sizeof(t_img_data), 1);
data.img_data->img = mlx_new_image(data.mlx, WIDTH, HEIGHT);
data.img_data->addr = mlx_get_data_addr(data.img_data->img,

View file

@ -3,16 +3,17 @@
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: freddy <freddy@student.42.fr> +#+ +:+ +#+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/30 12:50:10 by tchampio #+# #+# */
/* Updated: 2025/07/31 11:24:59 by tchampio ### ########.fr */
/* Created: 2025/07/31 13:17:39 by kcolin #+# #+# */
/* Updated: 2025/07/31 13:38:07 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "../consts.h"
#include "ray.h"
#include "../renderer/render.h"
int get_cardinal(t_ray *ray)
{
@ -76,7 +77,7 @@ void render_walls(t_cub3d_data *data, t_ray *ray, int x)
pos += step;
color = get_color(dir);
(void)pos;
data->screen_matrix[ray->draw_start][x] = color;
matrix_set(data, x, ray->draw_start, color);
ray->draw_start++;
}
}

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/30 14:53:39 by tchampio #+# #+# */
/* Updated: 2025/07/30 16:39:51 by tchampio ### ########.fr */
/* Created: 2025/07/31 13:18:17 by kcolin #+# #+# */
/* Updated: 2025/07/31 13:38:49 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,20 +18,17 @@
void reset_matrix(t_cub3d_data *data)
{
int x;
int y;
ft_bzero(data->screen_matrix, sizeof(int) * WIDTH * HEIGHT);
}
y = 0;
while (y < HEIGHT)
{
x = 0;
while (x < WIDTH)
{
data->screen_matrix[y][x] = 0;
x++;
}
y++;
}
int matrix_get(t_cub3d_data *data, int x, int y)
{
return (data->screen_matrix[y + x * WIDTH]);
}
void matrix_set(t_cub3d_data *data, int x, int y, int color)
{
data->screen_matrix[y + x * WIDTH] = color;
}
void matrix_to_image(t_cub3d_data *data)
@ -45,9 +42,9 @@ void matrix_to_image(t_cub3d_data *data)
x = 0;
while (x < WIDTH)
{
if (data->screen_matrix[y][x] > 0)
if (matrix_get(data, x, y) > 0)
my_mlx_pixel_put(data->img_data,
x, y, data->screen_matrix[y][x]);
x, y, matrix_get(data, x, y));
x++;
}
y++;

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* render.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/30 14:52:12 by tchampio #+# #+# */
/* Updated: 2025/07/30 16:20:57 by tchampio ### ########.fr */
/* Created: 2025/07/31 13:36:36 by kcolin #+# #+# */
/* Updated: 2025/07/31 13:36:43 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,5 +17,6 @@
void reset_matrix(t_cub3d_data *data);
void matrix_to_image(t_cub3d_data *data);
void matrix_set(t_cub3d_data *data, int x, int y, int color);
#endif // RENDER_H

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 13:59:27 by kcolin #+# #+# */
/* Updated: 2025/07/23 12:18:08 by tchampio ### ########.fr */
/* Updated: 2025/07/31 13:26:06 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -69,6 +69,7 @@ int destroy(t_cub3d_data *data)
if (data->mlx)
mlx_destroy_display(data->mlx);
free(data->mlx);
free(data->screen_matrix);
exit(0);
return (0);
}