/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   keyboard.c                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: qmennen <qmennen@student.codam.nl>         +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/04/17 19:29:29 by qmennen           #+#    #+#             */
/*   Updated: 2025/05/28 15:04:21 by qmennen          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "keyboard.h"

int	keyboard_create(t_game **game)
{
	t_keyboard	*keyboard;
	int			i;

	keyboard = malloc(sizeof(t_keyboard));
	if (!keyboard)
		return (FAILURE);
	(*game)->keyboard = keyboard;
	i = -1;
	while (++i < NUM_KEYS)
	{
		(*game)->keyboard->keys[i] = 0;
	}
	return (SUCCESS);
}

void	keyboard_update(t_game *game)
{
	int	i;

	i = 0;
	while (i < NUM_KEYS)
	{
		game->keyboard->last_keys[i] = get_key(game, i);
		i++;
	}
}

int	get_key(t_game *game, int k)
{
	return (mlx_is_key_down(game->screen->mlx, k));
}

int	get_key_down(t_game *game, int k)
{
	return (get_key(game, k) && !game->keyboard->last_keys[k]);
}

int	get_key_up(t_game *game, int k)
{
	return (!get_key(game, k) && game->keyboard->last_keys[k]);
}
