/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: yantoine <yantoine@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/02/17 19:54:03 by yantoine          #+#    #+#             */
/*   Updated: 2025/02/17 23:35:02 by yantoine         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "miniRT.h"

static int	init_app_config(t_app *app, int argc, char **argv)
{
	t_scene scene;

	if (argc > 1)
		scene = load_config(argv[1]);
	app->win_width = WIDTH;
	app->win_height = HEIGHT;
	app->scene = scene;
	return (0);
}

static int	init_mlx_and_image(t_app *app)
{
	app->mlx = mlx_init();
	if (!app->mlx)
	{
		ft_putstr_fd("Erreur mlx_init\n", 2);
		return (1);
	}
	app->win = mlx_new_window(app->mlx, app->win_width,
			app->win_height, "Raytracer interactif");
	if (!app->win)
	{
		ft_putstr_fd("Erreur mlx_new_window\n", 2);
		return (1);
	}
	app->img = mlx_new_image(app->mlx, app->win_width, app->win_height);
	if (!app->img)
	{
		ft_putstr_fd("Erreur mlx_new_image\n", 2);
		return (1);
	}
	app->pixels = (int *)mlx_get_data_addr(app->img, &app->bpp,
			&app->size_line, &app->endian);
	if (!app->pixels)
	{
		ft_putstr_fd("Erreur mlx_get_data_addr\n", 2);
		return (1);
	}
	return (0);
}

static void	setup_hooks(t_app *app)
{
	mlx_hook(app->win, 2, 1L << 0, key_press, app);
	mlx_hook(app->win, 3, 1L << 1, key_release, app);
	mlx_hook(app->win, 6, 1L << 6, mouse_move, app);
	mlx_loop_hook(app->mlx, update_frame, app);
}

int	main(int argc, char **argv)
{
	t_app	app;

	ft_bzero(&app, sizeof(t_app));
	init_app_config(&app, argc, argv);
	if (init_mlx_and_image(&app))
		return (1);
	setup_hooks(&app);
	mlx_loop(app.mlx);
	return (0);
}
