Skip to content

Commit

Permalink
feat: free and destory images when occur error at load_bumpmap()
Browse files Browse the repository at this point in the history
  • Loading branch information
moromin committed May 17, 2022
1 parent 6a5ac7f commit be9b5aa
Show file tree
Hide file tree
Showing 10 changed files with 1,255 additions and 24 deletions.
1,200 changes: 1,200 additions & 0 deletions images/geo.xpm

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions include/free.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef FREE_H
# define FREE_H

#include <stdlib.h>
#include "../include/miniRT.h"
#include "../include/slice.h"

void destroy_object_images(t_program *p);

#endif
13 changes: 0 additions & 13 deletions include/miniRT.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,6 @@
//# define EPSILON (1.0 / 512)
# define EPSILON 0.000001

// TODO: replace 'image.h'?
// typedef struct s_img t_img;

// struct s_img {
// void *image;
// int bits_per_pixel;
// int size_line;
// int endian;
// char *buffer;
// int height;
// int width;
// };

typedef struct s_program {
void *mlx;
void *win;
Expand Down
6 changes: 6 additions & 0 deletions scenes_bonus/err_invalid_file_path.rt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
A 0.1 255,255,255
L 0,150,-150 0.9 255,255,255
C 0,3,-5 0,-0.3,1 45

pl 0,-1,0 0,1,0 176,176,176
bm images/hoge.xpm 1 1
3 changes: 2 additions & 1 deletion scenes_bonus/err_real_invalid.rt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ C 0,0,-10 0.3,-0.1,1 30
# C 0,10,0 0,-1,-0 100

sp 6,0.5,1 2.0 176,176,176
ch 10a 10 255,255,255 0,0,255
# ch 10a 10 255,255,255 0,0,255
bm images/geo.xpm 1a 1
24 changes: 24 additions & 0 deletions src/free_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "../include/free.h"
#include "../minilibx-linux/mlx.h"

void destroy_object_images(t_program *p)
{
int i;
t_object *object;
t_bumpmap *bm;

i = 0;
while (i < (int)len(p->objects))
{
object = get_x2(p->objects, i, 0);
if (object->material.flag & 1 << MFLAG_BUMPMAP)
{
bm = (t_bumpmap *)object->image;
if (bm->super.image)
mlx_destroy_image(p->mlx, bm->super.image);
free(bm);
bm = NULL;
}
i++;
}
}
2 changes: 2 additions & 0 deletions src/mlx_hooks.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "../include/mlx_hooks.h"
#include "../include/free.h"

#define ESC_KEY 65307

static int close_window(t_program *p)
{
destroy_object_images(p);
delete_recursively(p->lights, 1);
delete_recursively(p->objects, 1);
mlx_destroy_image(p->mlx, p->img.image);
Expand Down
16 changes: 8 additions & 8 deletions src/rt_params.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ char *load_bumpmap(t_program *p, char **info)
{
t_object *object;
t_bumpmap *bm;
t_img image;

if (len(p->objects) == 0)
return (ERR_UNRESOLVED_MATERIAL);
Expand All @@ -126,21 +125,22 @@ char *load_bumpmap(t_program *p, char **info)
return (ERR_DUPLICATE_MATERIAL);
object->material.flag |= (1 << MFLAG_BUMPMAP);

// TODO: mlx error handling or wrapping
bm = x_malloc(sizeof(t_bumpmap));
image.image = mlx_xpm_file_to_image(p->mlx, info[0], &image.width, &image.height);
if (!image.image)
object->image = (t_img *)bm;
bm->super.image = NULL;
bm->super.buffer = NULL;
bm->super.image = mlx_xpm_file_to_image(p->mlx, info[0], &bm->super.width, &bm->super.height);
if (!bm->super.image)
return (ERR_MISCONFIGURED_BUMPMAP);
image.buffer = mlx_get_data_addr(image.image, &image.bits_per_pixel, &image.size_line, &image.endian);
if (!image.buffer)
bm->super.buffer = mlx_get_data_addr(bm->super.image,
&bm->super.bits_per_pixel, &bm->super.size_line, &bm->super.endian);
if (!bm->super.buffer)
return (ERR_MISCONFIGURED_BUMPMAP);
bm->super = image;

if (!(atoi_strict(info[1], &bm->freq_u) && 1 <= bm->freq_u))
return (ERR_MISCONFIGURED_BUMPMAP);
if (!(atoi_strict(info[2], &bm->freq_v) && 1 <= bm->freq_v))
return (ERR_MISCONFIGURED_BUMPMAP);

object->image = (t_img *)bm;
return (NO_ERR);
}
1 change: 0 additions & 1 deletion src/rt_params_obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ char *load_cone(t_program *p, char **info)
ft_putendl_fd(WARNING_NOT_NORMALIZED, STDERR_FILENO);
normal = vec_normalize(normal);
}
// TODO: define aperture range
if (!(ft_strtod(info[2], &aperture) && 0.0 <= aperture && aperture < 180.0))
return (ERR_MISCONFIGURED_CONE);
if (!(get_color_from_str(info[3], &k_diffuse) && check_color_range(k_diffuse, 0.0, 255.0)))
Expand Down
4 changes: 3 additions & 1 deletion src/scene.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../include/scene.h"
#include "../include/free.h"
#include "../minilibx-linux/mlx.h"

static const char *g_env_idents[] = {"A", "C", "L", NULL};
Expand Down Expand Up @@ -98,9 +99,10 @@ static void read_rt_file(char *filename, t_program *p)
x_close(fd);
if (err != NO_ERR)
{
// TODO: free mlx
destroy_object_images(p);
delete_recursively(p->lights, 1);
delete_recursively(p->objects, 1);
mlx_destroy_display(p->mlx);
exit_with_error_message(err);
}
}
Expand Down

0 comments on commit be9b5aa

Please sign in to comment.