aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_exit.c
blob: 4f2494b064ad68bb69ae53dbf1d5a542b6ecdef0 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   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>
#include <signal.h>

static void
	ft_del_last_remains(t_cub *clist)
{
	uint16_t	i;

	i = 0;
	while (i < clist->big_t_val)
	{
		ft_memdel((void*)&clist->big_t[i]);
		i++;
	}
	ft_memdel((void*)&clist->big_t);
	if (!clist->wlist.inited)
	{
		ft_memdel((void*)&clist->wlist.winptr);
		ft_memdel((void*)&clist->wlist.wlx);
	}
	ft_memdel((void*)&clist);
}

static void
	ft_cancel_threads(t_cub *clist)
{
	uint8_t	i;

	if (clist->isoldmus && clist->wlist.inited)
	{
		if (FT_OS == 2)
		{
			pthread_cancel(clist->mtid);
			pthread_join(clist->mtid, NULL);
		}
		else
			system(FT_SND_TERM_CMD);
	}
	i = -1;
	while (++i < FT_TOTAL_SFX)
	{
		pthread_mutex_unlock(&clist->sfx[i].mutex);
		if (FT_OS == 2)
		{
			pthread_cancel(clist->sfx[i].tid);
			pthread_join(clist->sfx[i].tid, NULL);
		}
		else
			system(FT_SND_TERM_CMD);
	}
}

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);
	}
	ft_cancel_threads(clist);
	ft_del_map(&clist->mlist);
	ft_del_sprites_lists(clist);
	ft_del_sfx_cmd(clist);
	ft_del_last_remains(clist);
	ft_printf("Exiting program\n");
	exit(exit_code);
	return (0);
}