create and use get_data_addr()

This commit is contained in:
mcolonna 2024-10-29 14:12:51 +01:00
parent aaf2b0adc8
commit b688144a43
4 changed files with 46 additions and 17 deletions

15
algo.c
View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

15
utils.h
View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

31
utils2.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}