/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_sfx_trap.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 <stdlib.h>
#include <pthread.h>

void
	*ft_sfx_pain_thread(void *vargp)
{
	t_sfx			*sfx;
	static uint8_t	ref = 0;

	if (FT_OS == 1)
		pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
	else
		pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
	sfx = (t_sfx *)vargp;
	pthread_mutex_lock(&sfx[3].mutex);
	while (1)
	{
		pthread_mutex_lock(&sfx[3].mutex);
		ref = (ref > 201) ? (0) : (ref + 1);
		if (ref % 3)
		{
			if (system(sfx[3].cmd))
				pthread_exit(NULL);
		}
		else if (system(sfx[3].cmd_alt))
			pthread_exit(NULL);
	}
	return (NULL);
}

void
	ft_sfx_pain(t_sfx *sfx)
{
	static uint8_t	ref = 0;

	ref = (ref > 201) ? (0) : (ref + 1);
	if (ref % 2)
		pthread_mutex_unlock(&sfx[3].mutex);
}