aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_exit.c
blob: 6622eb19bbde31efe28395c0531866f2381aedfe (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   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 <stdint.h>
#include <pthread.h>

static void
	ft_free_lists(t_cub *clist)
{
	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.sprite_path);
	ft_memdel((void**)&clist.mlist.nl_tex_path);
	ft_memdel((void**)&clist.mlist.nlevel_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_memdel((void**)&clist.mlist);
	ft_memdel((void**)&clist.plist);
	if (!clist.wlist.inited)
		ft_memdel((void**)&clist.wlist.winptr);
	ft_memdel((void**)&clist.wlist);
	ft_memdel((void**)&clist);
}

int
	ft_exit(uint8_t exit_code, t_cub *clist)
{
	if (clist.walltexgood)
	{
		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.wlist.inited)
	{
		mlx_destroy_window(clist.wlist.wlx, clist.wlist.winptr);
		clist.wlist.winptr = NULL;
	}
	if (clist.isoldmus && clist.wlist.inited)
	{
		pthread_cancel(clist.tid);
		pthread_join(clist.tid, NULL);
	}
	ft_free_lists(clist);
	ft_printf("Exiting program\n");
	exit(exit_code);
	return (0);
}