aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_key_events.c
blob: bb0afee1245dd54f17294490cb5ace3e3c1edd59 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_key_events.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: joelecle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:28:55 by joelecle          #+#    #+#             */
/*   Updated: 2020/02/14 17:28:55 by joelecle         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <cub3d.h>
#include <stdlib.h>
#include <stdint.h>

static void
	ft_insert_key(uint16_t keycode, t_cub *clist)
{
	uint8_t	i;

	i = 0;
	while (i < 5 && clist->key_input[i] != keycode && clist->key_input[i] != -1)
		i++;
	clist->key_input[i] = keycode;
}

int
	ft_key_event(int keycode, t_cub *clist)
{
	const int32_t	tmp_code = keycode;

	keycode = ft_convert_keycode(tmp_code);
	if (keycode == FT_ESC_KEY)
		return (ft_exit(0, (clist)));
	else if (keycode == FT_F1_KEY || keycode == FT_TAB_KEY)
		return (ft_f1_key(clist));
	else if (keycode == FT_SPC_KEY && clist->plist.handles_weapon > -1)
		return (ft_space_key(clist));
	else if (keycode == FT_ONE_KEY && clist->plist.fire == 0
		&& clist->plist.has_weapon[0] == 1 && clist->plist.handles_weapon != 0)
		return (ft_switch_weap_one(clist));
	else if (keycode == FT_TWO_KEY && clist->plist.fire == 0
		&& clist->plist.has_weapon[1] == 1 && clist->plist.handles_weapon != 2)
		return (ft_switch_weap_two(clist));
	else if (keycode == FT_THREE_KEY && clist->plist.fire == 0
		&& clist->plist.has_weapon[2] == 1 && clist->plist.handles_weapon != 4)
		return (ft_switch_weap_three(clist));
	ft_insert_key(keycode, clist);
	return (0);
}