From a2ed84f6da6b5ca2226e2bbc5cc65e142810d8c6 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Fri, 13 Mar 2020 05:39:06 +0100
Subject: New sfx, clean exit

---
 src/ft_exit.c             | 25 ++++++++++++++++++++-----
 src/ft_sfx_death.c        | 24 ++++++++++++++++++++++++
 src/ft_sfx_trap.c         |  2 +-
 src/ft_suffer_animation.c |  3 ++-
 4 files changed, 47 insertions(+), 7 deletions(-)
 create mode 100644 src/ft_sfx_death.c

(limited to 'src')

diff --git a/src/ft_exit.c b/src/ft_exit.c
index b0d7ada..5bcc653 100644
--- a/src/ft_exit.c
+++ b/src/ft_exit.c
@@ -68,6 +68,25 @@ static void
 		mlx_destroy_image(clist->wlist.wlx, clist->tlist[15].img);
 }
 
+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->death_pid, NULL, WNOHANG)))
+		kill(clist->death_pid, SIGTERM);
+	wait(&clist->death_pid);
+	if (!(tmp = waitpid(clist->trap_pid, NULL, WNOHANG)))
+		kill(clist->trap_pid, SIGTERM);
+	wait(&clist->trap_pid);
+	if (!(tmp = waitpid(clist->pain_pid, NULL, WNOHANG)))
+		kill(clist->pain_pid, SIGTERM);
+	wait(&clist->pain_pid);
+}
+
 int
 	ft_exit(uint8_t exit_code, t_cub *clist)
 {
@@ -81,11 +100,7 @@ int
 		mlx_destroy_window(clist->wlist.wlx, clist->wlist.winptr);
 		clist->wlist.winptr = NULL;
 	}
-	if (clist->isoldmus && clist->wlist.inited)
-	{
-		kill(clist->mpid, SIGTERM);
-		wait(&clist->mpid);
-	}
+	ft_kill_forks(clist);
 	ft_free_lists(clist);
 	ft_printf("Exiting program\n");
 	exit(exit_code);
diff --git a/src/ft_sfx_death.c b/src/ft_sfx_death.c
new file mode 100644
index 0000000..e093cf3
--- /dev/null
+++ b/src/ft_sfx_death.c
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_sfx_death.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/02/24 17:17:54 by rbousset          #+#    #+#             */
+/*   Updated: 2020/02/24 17:17:56 by rbousset         ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <cub3d.h>
+#include <unistd.h>
+
+void
+	ft_sfx_death(t_cub *cl)
+{
+	cl->death_pid = fork();
+	if (cl->death_pid == 0)
+	{
+		execve(*(cl->sfx.death + 0), cl->sfx.death, cl->envp);
+	}
+}
diff --git a/src/ft_sfx_trap.c b/src/ft_sfx_trap.c
index bf9e0b2..32fc35a 100644
--- a/src/ft_sfx_trap.c
+++ b/src/ft_sfx_trap.c
@@ -1,7 +1,7 @@
 /* ************************************************************************** */
 /*                                                                            */
 /*                                                        :::      ::::::::   */
-/*   ft_music.c                                         :+:      :+:    :+:   */
+/*   ft_sfx_trap.c                                      :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
 /*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
diff --git a/src/ft_suffer_animation.c b/src/ft_suffer_animation.c
index 67a557f..4a1bc2d 100644
--- a/src/ft_suffer_animation.c
+++ b/src/ft_suffer_animation.c
@@ -44,9 +44,10 @@ void
 	{
 		cl->plist.isdead = 1;
 		ft_warp_level(cl->mlist.filename, cl);
+		ft_sfx_death(cl);
+		/* TODO death screen here */
 	}
 	ft_sfx_trap(cl);
-	/* TODO death screen here */
 
 }
 
-- 
cgit v1.2.3