1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 17:28:46 by rbousset #+# #+# */
/* Updated: 2020/02/14 17:28:46 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <cub3d.h>
#include <mlx.h>
#include <stddef.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdint.h>
static void
ft_free_some_extras(t_cub *clist)
{
uint8_t i;
i = 0;
while (i < 8)
{
ft_memdel((void**)&clist->sprites[i]);
i++;
}
ft_memdel((void**)&clist->sprites);
ft_memdel((void**)&clist);
}
static void
ft_free_lists(t_cub *clist)
{
ft_memdel((void**)&clist->mlist.filename);
ft_memdel((void**)&clist->mlist.no_tex_path);
ft_memdel((void**)&clist->mlist.so_tex_path);
ft_memdel((void**)&clist->mlist.ea_tex_path);
ft_memdel((void**)&clist->mlist.we_tex_path);
ft_memdel((void**)&clist->mlist.nl_tex_path);
ft_memdel((void**)&clist->mlist.fl_tex_path);
ft_memdel((void**)&clist->mlist.ce_tex_path);
ft_memdel((void**)&clist->mlist.nlevel_path);
ft_memdel((void**)&clist->mlist.skybox_path);
ft_memdel((void**)&clist->mlist.traps_path);
ft_memdel((void**)&clist->mlist.music_path);
ft_memdel((void**)&clist->mlist.music_cmd);
ft_memdel((void**)&clist->mlist.mapl);
ft_free_words(clist->mlist.map);
ft_free_words(clist->mlist.mcmd_words);
ft_free_words(clist->sfx.death);
ft_free_words(clist->sfx.pain_one);
ft_free_words(clist->sfx.pain_two);
ft_free_words(clist->sfx.new_lvl);
ft_free_words(clist->sfx.trap);
ft_free_sprites(clist->mlist.sprite_path);
if (!clist->wlist.inited)
ft_memdel((void**)&clist->wlist.winptr);
}
static void
ft_del_tex(t_cub *clist)
{
if (clist->tlist[0].img)
mlx_destroy_image(clist->wlist.wlx, clist->tlist[0].img);
if (clist->tlist[1].img)
mlx_destroy_image(clist->wlist.wlx, clist->tlist[1].img);
if (clist->tlist[2].img)
mlx_destroy_image(clist->wlist.wlx, clist->tlist[2].img);
if (clist->tlist[3].img)
mlx_destroy_image(clist->wlist.wlx, clist->tlist[3].img);
if (clist->tlist[4].img)
mlx_destroy_image(clist->wlist.wlx, clist->tlist[4].img);
if (clist->mlist.isnlvl && clist->tlist[5].img)
mlx_destroy_image(clist->wlist.wlx, clist->tlist[5].img);
if (clist->mlist.isftex && clist->tlist[6].img)
mlx_destroy_image(clist->wlist.wlx, clist->tlist[6].img);
if (clist->mlist.isctex && clist->tlist[7].img)
mlx_destroy_image(clist->wlist.wlx, clist->tlist[7].img);
if (clist->mlist.istraps && clist->tlist[15].img)
mlx_destroy_image(clist->wlist.wlx, clist->tlist[15].img);
ft_del_extra_sprites(clist);
}
static void
ft_kill_forks(t_cub *clist)
{
pid_t tmp;
if (!(tmp = waitpid(clist->mpid, NULL, WNOHANG)))
kill(clist->mpid, SIGTERM);
wait(&clist->mpid);
if (!(tmp = waitpid(clist->sfx.death_pid, NULL, WNOHANG)))
kill(clist->sfx.death_pid, SIGTERM);
wait(&clist->sfx.death_pid);
if (!(tmp = waitpid(clist->sfx.new_lvl_pid, NULL, WNOHANG)))
kill(clist->sfx.new_lvl_pid, SIGTERM);
wait(&clist->sfx.new_lvl_pid);
if (!(tmp = waitpid(clist->sfx.pain_pid, NULL, WNOHANG)))
kill(clist->sfx.pain_pid, SIGTERM);
wait(&clist->sfx.pain_pid);
if (!(tmp = waitpid(clist->sfx.trap_pid, NULL, WNOHANG)))
kill(clist->sfx.trap_pid, SIGTERM);
wait(&clist->sfx.trap_pid);
}
int
ft_exit(uint8_t exit_code, t_cub *clist)
{
if (clist->walltexgood)
ft_del_tex(clist);
if (clist->wlist.inited)
{
if (FT_OS == 1)
mlx_destroy_image(clist->wlist.wlx, clist->img.img);
mlx_destroy_window(clist->wlist.wlx, clist->wlist.winptr);
clist->wlist.winptr = NULL;
}
ft_kill_forks(clist);
ft_free_lists(clist);
ft_free_some_extras(clist);
ft_printf("Exiting program\n");
exit(exit_code);
return (0);
}
|