From b688144a43ee1579e630834bcd1b8c46532b0398 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Tue, 29 Oct 2024 14:12:51 +0100 Subject: [PATCH] create and use get_data_addr() --- algo.c | 15 +++------------ utils.c | 2 +- utils.h | 15 +++++++++++---- utils2.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 utils2.c diff --git a/algo.c b/algo.c index 8ce0356..07e582e 100644 --- a/algo.c +++ b/algo.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */ -/* Updated: 2024/10/17 17:43:07 by mcolonna ### ########.fr */ +/* Updated: 2024/10/29 14:10:10 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,24 +18,15 @@ void *g_win = NULL; t_map g_map; t_player g_player; -// TODO manage image format error better - void draw_screen(void) { - int bpp; - int size_line; - int endian; void *img_ptr; u_int32_t *img_data; img_ptr = mlx_new_image(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT); - img_data = (u_int32_t *) - mlx_get_data_addr(img_ptr, &bpp, &size_line, &endian); - if (bpp != 32 || endian != 0) - { - printf("image format error\n"); + img_data = get_data_addr(img_ptr); + if (!img_data) exit(1); - } render(img_data); mlx_put_image_to_window(g_mlx, g_win, img_ptr, 0, 0); mlx_destroy_image(g_mlx, img_ptr); diff --git a/utils.c b/utils.c index 6e8a570..758e64b 100644 --- a/utils.c +++ b/utils.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */ -/* Updated: 2024/10/17 15:05:15 by mcolonna ### ########.fr */ +/* Updated: 2024/10/29 14:01:59 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/utils.h b/utils.h index 93a619d..3c4f080 100644 --- a/utils.h +++ b/utils.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 16:56:47 by mcolonna #+# #+# */ -/* Updated: 2024/10/17 14:41:09 by mcolonna ### ########.fr */ +/* Updated: 2024/10/29 14:01:34 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,15 +37,22 @@ typedef __u32 t_color; /// @param green Level of green from 0 to 255. /// @param blue Level of blue from 0 to 255. /// @return The result. -t_color color_from_rgb(int red, int green, int blue); +t_color color_from_rgb(int red, int green, int blue); -void write_err(const char *str, ...); +void write_err(const char *str, ...); /// @brief Call the function in loop. /// If not enough time passed since the last call, returns without doing /// anything. /// /// @param f Function to call in loop. -void timedloop(void (*f)(void)); +void timedloop(void (*f)(void)); + +/// @brief Get an address to the image data, but only if +/// bpp == 32 and endian == 0. +/// +/// @param img_ptr mlx image to use. +/// @return Address to the data, or NULL if error. +u_int32_t *get_data_addr(void *img_ptr); #endif diff --git a/utils2.c b/utils2.c new file mode 100644 index 0000000..dcc1914 --- /dev/null +++ b/utils2.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/29 14:02:37 by mcolonna #+# #+# */ +/* Updated: 2024/10/29 14:12:14 by mcolonna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "utils.h" +#include "mlx.h" + +u_int32_t *get_data_addr(void *img_ptr) +{ + int bpp; + int size_line; + int endian; + u_int32_t *r; + + r = (u_int32_t *)mlx_get_data_addr(img_ptr, &bpp, &size_line, &endian); + if (bpp != 32 || endian != 0) + { + write_err("Wrong bpp or wrong endian when using mlx_get_data_addr().\n", + NULL); + return (NULL); + } + return (r); +}