aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_warp_level.c
blob: d6a3d7fa378cb2bc32801142cdc6879487d16bbb (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
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
132
133
134
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_warp_level.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/28 20:54:28 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/28 20:54:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <mlx.h>

static void
	ft_del_map(t_map *ml)
{
	ft_memdel((void*)&ml->filename);
	ft_memdel((void*)&ml->no_tex_path);
	ft_memdel((void*)&ml->so_tex_path);
	ft_memdel((void*)&ml->ea_tex_path);
	ft_memdel((void*)&ml->we_tex_path);
	ft_memdel((void*)&ml->nl_tex_path);
	ft_memdel((void*)&ml->fl_tex_path);
	ft_memdel((void*)&ml->ce_tex_path);
	ft_memdel((void*)&ml->nlevel_path);
	ft_memdel((void*)&ml->skybox_path);
	ft_memdel((void*)&ml->traps_path);
	ft_memdel((void*)&ml->music_path);
	ft_memdel((void*)&ml->music_cmd);
	ft_memdel((void*)&ml->mapl);
	ft_free_sprites(ml->sprite_path);
	ft_free_words(ml->map);
}

static void
	ft_del_some(t_cub *cl)
{
	int8_t	i;

	cl->plist = ft_init_player();
	cl->f_rgb = ft_init_rgb();
	cl->c_rgb = ft_init_rgb();
	cl->rlist = ft_init_s_ray();
	i = -1;
	while (++i < 5)
		mlx_destroy_image(cl->wlist.wlx, cl->tlist[i].img);
	if (cl->mlist.isnlvl && cl->tlist[5].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tlist[5].img);
	if (cl->mlist.isftex && cl->tlist[6].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tlist[6].img);
	if (cl->mlist.isctex && cl->tlist[7].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tlist[7].img);
	if (cl->mlist.istraps && cl->tlist[15].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tlist[15].img);
	if (cl->tlist[16].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tlist[16].img);
	if (cl->mlist.isheals && cl->tlist[17].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tlist[17].img);
	if (cl->mlist.isweapon[0] && cl->tlist[18].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tlist[18].img);
	if (cl->mlist.isweapon[1] && cl->tlist[19].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tlist[19].img);
	if (cl->tweap[0].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tweap[0].img);
	if (cl->tweap[1].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tweap[1].img);
	if (cl->tweap[2].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tweap[2].img);
	if (cl->tweap[3].img)
		mlx_destroy_image(cl->wlist.wlx, cl->tweap[3].img);
	ft_del_extra_sprites(cl);
	i = -1;
	while (++i < 8)
		ft_memdel((void*)&cl->sprites[i]);
	ft_memdel((void*)&cl->sprites);
}

static void
	ft_treat_music(uint8_t isoldmus, char *tmp_mup, t_cub *cl)
{
	if (isoldmus && !cl->mlist.ismusic)
	{
		pthread_cancel(cl->mtid);
		pthread_join(cl->mtid, NULL);
		cl->isoldmus = 0;
	}
	else if (isoldmus && cl->mlist.ismusic
		&& ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1))
	{
		pthread_cancel(cl->mtid);
		pthread_join(cl->mtid, NULL);
		ft_enable_music(cl);
	}
	else if (isoldmus && cl->mlist.ismusic
		&& !ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1))
		return ;
}

int8_t
	ft_warp_level(char *path, t_cub *cl)
{
	char	*next_path;
	char	*tmp_mup;
	uint8_t	isoldmus;

	if (!(next_path = (char *)malloc((ft_strlen(path) + 1) * sizeof(char))))
		return (-1);
	ft_sprintf(next_path, "%s", path);
	if ((isoldmus = cl->mlist.ismusic))
		tmp_mup = ft_strdup(cl->mlist.music_path);
	ft_del_some(cl);
	ft_del_map(&cl->mlist);
	if (ft_init_sprites(&cl->sprites) < 0)
		return (-1);
	if (ft_init_map(&cl->mlist) < 0)
		return (-1);
	ft_parse_map(next_path, cl);
	ft_treat_music(isoldmus, tmp_mup, cl);
	if (isoldmus)
		ft_memdel((void*)&tmp_mup);
	ft_wall_tex_init(cl);
	ft_weap_tex_init(cl);
	ft_memdel((void*)&next_path);
	return (0);
}