diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-04 23:55:12 +0100 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-04 23:55:12 +0100 |
commit | 998d2df1356f83049c30f71bd5dc4c0597fd4824 (patch) | |
tree | 660eef6063088eea2b844984c49c5aa1b2b6e2f2 | |
parent | Clean (diff) | |
download | 42-cub3d-998d2df1356f83049c30f71bd5dc4c0597fd4824.tar.gz 42-cub3d-998d2df1356f83049c30f71bd5dc4c0597fd4824.tar.bz2 42-cub3d-998d2df1356f83049c30f71bd5dc4c0597fd4824.tar.xz 42-cub3d-998d2df1356f83049c30f71bd5dc4c0597fd4824.tar.zst 42-cub3d-998d2df1356f83049c30f71bd5dc4c0597fd4824.zip |
Nice bmp
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | inc/cub3d.h | 4 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 10 | ||||
-rw-r--r-- | src/ft_draw_scene.c | 2 | ||||
-rw-r--r-- | src/ft_hex_to_rgb.c | 25 | ||||
-rw-r--r-- | src/ft_init_bmp.c | 14 | ||||
-rw-r--r-- | src/ft_parse_map.c | 14 | ||||
-rw-r--r-- | src/ft_save_to_bmp.c | 99 |
9 files changed, 103 insertions, 69 deletions
@@ -175,4 +175,5 @@ tags # End of https://www.gitignore.io/api/c,vim,linux,macos,emacs cub3d -Cub3D
\ No newline at end of file +Cub3D +*.bmp
\ No newline at end of file @@ -55,6 +55,7 @@ SRCS_NAME += ft_basic_keys.c SRCS_NAME += ft_extra_keys.c SRCS_NAME += ft_draw_verline.c SRCS_NAME += ft_rgb_to_hex.c +SRCS_NAME += ft_hex_to_rgb.c SRCS_NAME += ft_raycasting.c SRCS_NAME += ft_init_s_ray.c SRCS_NAME += ft_init_map.c diff --git a/inc/cub3d.h b/inc/cub3d.h index a80cfd5..732ccbe 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -31,7 +31,6 @@ t_rgb ft_init_rgb(void); int8_t ft_init_map(t_map *mlist); t_bmp_file ft_init_bmp(void); t_bmp_info ft_init_bmp_info(void); -t_bmp_colors ft_init_bmp_colors(void); /* ** ====== HOOKS ====== @@ -123,11 +122,12 @@ void ft_set_minimap_scale(t_cub *clist); void *ft_music_thread(void *vargp); void ft_detect(t_cub *cl); void ft_castray(t_cub *cl); -int8_t ft_save_to_bmp(void *img, t_cub *cl); +int8_t ft_save_to_bmp(t_cub *cl); int ft_error(uint8_t retval, const char *errmsg, t_cub *clist); uint8_t ft_free_words(char **words); int8_t ft_warp_level(t_cub *cl); int ft_exit(uint8_t exit_code, t_cub *clist); uint32_t ft_rgb_to_hex(t_rgb rgb); +t_rgb ft_hex_to_rgb(int32_t color); # endif diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 1eb6f08..0773e11 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -54,16 +54,6 @@ typedef struct s_bmp_info uint32_t colors_important; } t_bmp_info; -typedef struct s_bmp_colors -{ - uint32_t red_mask; - uint32_t green_mask; - uint32_t blue_mask; - uint32_t alpha_mask; - uint32_t color_space_type; - uint32_t unused[16]; -} t_bmp_colors; - # pragma pack(pop) typedef struct s_img diff --git a/src/ft_draw_scene.c b/src/ft_draw_scene.c index cbe002f..9461eef 100644 --- a/src/ft_draw_scene.c +++ b/src/ft_draw_scene.c @@ -38,7 +38,7 @@ void clist->img.ptr = mlx_get_data_addr(clist->img.img, &clist->img.bpp, &clist->img.sizeline, &clist->img.endian); ft_castray(clist); - if (ft_save_to_bmp(clist->img.img, clist) < 0) + if (ft_save_to_bmp(clist) < 0) ft_error(FT_RET_BMP_ERR, FT_ERR_WR_BMP, clist); mlx_destroy_image(clist->wlist.wlx, clist->img.img); } diff --git a/src/ft_hex_to_rgb.c b/src/ft_hex_to_rgb.c new file mode 100644 index 0000000..03e9643 --- /dev/null +++ b/src/ft_hex_to_rgb.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_hex_to_rgb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:28:58 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:28:58 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <cub3d.h> +#include <stdint.h> + +t_rgb + ft_hex_to_rgb(int32_t color) +{ + t_rgb rgb; + + rgb.r = ((color >> 16) & 0xff) / 255.0; + rgb.g = ((color >> 8) & 0xff) / 255.0; + rgb.b = ((color) & 0xff) / 255.0; + return (rgb); +} diff --git a/src/ft_init_bmp.c b/src/ft_init_bmp.c index 8e94eba..18c6002 100644 --- a/src/ft_init_bmp.c +++ b/src/ft_init_bmp.c @@ -14,20 +14,6 @@ #include <cub3d.h> #include <stdint.h> -t_bmp_colors - ft_init_bmp_colors(void) -{ - t_bmp_colors bmp_colors; - - bmp_colors.red_mask = 0x00ff0000; - bmp_colors.green_mask = 0x0000ff00; - bmp_colors.blue_mask = 0x000000ff; - bmp_colors.alpha_mask = 0xff000000; - bmp_colors.color_space_type = 0x73524742; - ft_bzero(bmp_colors.unused, 16); - return (bmp_colors); -} - t_bmp_info ft_init_bmp_info(void) { diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c index 7dd652e..3c1fd9c 100644 --- a/src/ft_parse_map.c +++ b/src/ft_parse_map.c @@ -84,19 +84,6 @@ void ft_sprintf(mlist->filename, "%s", map_path); } -static void -ft_print_map(char **map) -{ - size_t i; - - i = 0; - while(map[i]) - { - ft_printf("[%s]\n", map[i]); - i++; - } -} - void ft_parse_map(const char *map_path, t_cub *clist) { @@ -121,7 +108,6 @@ void ft_get_player_spawn(&clist->plist, clist); ft_get_nlvl_pos(&clist->mlist); ft_check_missing(clist); - ft_print_map(clist->mlist.map); ft_set_minimap_scale(clist); clist->currlvl += 1; close(fd); diff --git a/src/ft_save_to_bmp.c b/src/ft_save_to_bmp.c index 66f8b86..7d242c6 100644 --- a/src/ft_save_to_bmp.c +++ b/src/ft_save_to_bmp.c @@ -14,6 +14,8 @@ #include <cub3d.h> #include <stddef.h> #include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> #include <stdint.h> static char @@ -23,56 +25,99 @@ static char if (ft_strrchr(ml->filename, '/')) { - if (!(str = (char*)malloc((ft_strlen(FT_BMP_SAVE_DIR) + ft_strlen(ft_strrchr(ml->filename, '/') + 1) + 1) * sizeof(char)))) + if (!(str = (char*)malloc((ft_strlen(FT_BMP_SAVE_DIR) + + ft_strlen(ft_strrchr(ml->filename, '/')+ 1) + + 1) * sizeof(char)))) return (NULL); - ft_printf("%lu\n", (ft_strlen(FT_BMP_SAVE_DIR) + ft_strlen((ft_strrchr(ml->filename, '/') + 1)) + 1)); ft_sprintf(str, "%s%s", FT_BMP_SAVE_DIR, ft_strrchr(ml->filename, '/') + 1); } else { if (!(str = (char*)malloc((ft_strlen(FT_BMP_SAVE_DIR) + - ft_strlen(ml->filename) + 1)* sizeof(char)))) + ft_strlen(ml->filename) + 1) * sizeof(char)))) return (NULL); - ft_sprintf(str, "%s%s.bmp", FT_BMP_SAVE_DIR, ml->filename); + ft_sprintf(str, "%s%s", FT_BMP_SAVE_DIR, ml->filename); } ft_memcpy(ft_strrchr(str, '.') + 1, "bmp", 3); return (str); - /* return (NULL); */ } -/* static int8_t */ -/* ft_convert_image_to_bmp(t_bmp_file bmp_file, void *img, t_cub *cl) */ -/* { */ -/* int32_t filelen; */ -/* uint8_t *bmp; */ +static uint8_t + *ft_convert_image_to_bmp(t_img img, t_cub *cl) +{ + uint8_t *bmp; + t_rgb px; + uint32_t y; + uint32_t x; + uint32_t i; -/* (void)img; */ -/* (void)bmp_file; */ -/* filelen = 54 + 3 * cl->wlist.x_size * cl->wlist.y_size; */ -/* if (!(bmp = (uint8_t*)malloc((filelen - 54) * sizeof(uint8_t)))) */ -/* return (-1); */ -/* ft_memdel((void**)&bmp); */ -/* return (0); */ -/* } */ + if (!(bmp = (uint8_t*)malloc((3 * cl->wlist.x_size + * cl->wlist.y_size) * sizeof(uint8_t)))) + return (NULL); + y = cl->wlist.y_size; + i = 0; + while (--y > 0) + { + x = -1; + while (++x < cl->wlist.x_size) + { + px = ft_hex_to_rgb(*(int*)(img.ptr + (x * 4 + (y * img.sizeline)))); + *(bmp + i++) = (uint8_t)px.b; + *(bmp + i++) = (uint8_t)px.g; + *(bmp + i++) = (uint8_t)px.r; + } + } + return (bmp); +} -int8_t - ft_save_to_bmp(void *img, t_cub *cl) +static int8_t + ft_write_bmp(const char *fname, uint32_t h, uint32_t w, uint8_t *bmp) { t_bmp_file bmp_file; t_bmp_info bmp_info; - t_bmp_colors bmp_colors; - char *fname; + uint32_t size; + int fd; - (void)img; + size = w * h * 3; bmp_file = ft_init_bmp(); bmp_info = ft_init_bmp_info(); - bmp_colors = ft_init_bmp_colors(); + bmp_file.file_size = size + sizeof(t_bmp_file) + sizeof(t_bmp_info); + bmp_file.offset_data = bmp_file.file_size - size; + bmp_info.size = 40; + bmp_info.width = w; + bmp_info.height = h; + bmp_info.bit_count = 24; + bmp_info.size_image = size; + if (!(fd = open(fname, O_RDWR | O_CREAT , 0644))) + return (-1); + write(fd, &bmp_file, sizeof(t_bmp_file)); + write(fd, &bmp_info, sizeof(t_bmp_info)); + write(fd, bmp, size); + close(fd); + return (0); +} + +int8_t + ft_save_to_bmp(t_cub *cl) +{ + uint8_t *bmp; + char *fname; + if (!(fname = ft_set_file_name(&cl->mlist))) return (-1); - ft_printf("%s\n", fname); - /* if (ft_convert_image_to_bmp(bmp_file, img, cl) < 0) */ - /* return (-1); */ + if (!(bmp = ft_convert_image_to_bmp(cl->img, cl))) + { + ft_memdel((void**)&fname); + return (-1); + } + if (ft_write_bmp(fname, cl->wlist.y_size, cl->wlist.x_size, bmp) < 0) + { + ft_memdel((void**)&bmp); + ft_memdel((void**)&fname); + return (-1); + } + ft_memdel((void**)&bmp); ft_memdel((void**)&fname); return (0); } |