// C/C++ File
// AUTHOR:   foton
// FILE:     main.c
// ROLE:     to rule them all
// CREATED:  2017-01-30 21:51:28
// MODIFIED: 2017-02-13 20:30:48

#include "fdf.h"

void	swap_x_y(t_point *p1, t_point *p2)
{
	int temp;

	temp = (*p1).x;
	(*p1).x = (*p1).y;
	(*p1).y = temp;
	temp = (*p2).x;
	(*p2).x = (*p2).y;
	(*p2).y = temp;
}

void	swap_points(t_point *p1, t_point *p2)
{
	t_point temp;

	temp = *p1;
	*p1 = *p2;
	*p2 = temp;

}

void	draw_line(t_point p1, t_point p2, t_mlx m, t_swap s)
{
	int dx = abs(p2.x - p1.x);
	int dy = abs(p2.y - p1.y);
	int derr = 2 * dy - dx;
	while (p1.x < p2.x)
	{
		if (s.swap_coord)
			mlx_pixel_put(m.mlx, m.win, p1.y, p1.x, p1.color);
		else
			mlx_pixel_put(m.mlx, m.win, p1.x, p1.y, p1.color);
		if (derr > 0)
		{
			p1.y += s.swap_x;
			derr -= dx;
		}
		p1.x++;
		derr += dy;
	}
}

void	test_line(t_mlx mlx)
{
	t_point p1;
	t_point p2;

	p1.x = 0;
	p1.y = 0;
	p2.x = 500;
	p1.color = 0xC100E5;
	p2.color = 0xC100E5;
	for (p2.y = 0; p2.y <= 500; p2.y += 4)
	{
		line(p1, p2, mlx);
	}
	for (p2.x = 500; p2.x >= 0; p2.x -= 4)
	{
		line(p1, p2, mlx);
	}
	p1.x = 500;
	p1.y = 0;
	p2.x = 0;
	p1.color = 0x1EBF00;
	p2.color = 0x1EBF00;
	for (p2.y = 0; p2.y <= 500; p2.y += 4)
	{
		line(p1, p2, mlx);
	}
	for (p2.x = 0; p2.x <= 500; p2.x += 4)
	{
		line(p1, p2, mlx);
	}
	p1.x = 500;
	p1.y = 500;
	p2.x = 500;
	p2.y = 0;
	p1.color = 0xFF000D;
	p2.color = 0xFF000D;
	for (p2.x = 500; p2.x >= 0; p2.x -= 4)
	{
		line(p1, p2, mlx);
	}
	for (p2.y = 0; p2.y <= 500; p2.y += 4)
	{
		line(p1, p2, mlx);
	}
	p1.x = 0;
	p1.y = 500;
	p2.x = 500;
	p2.y = 500;
	p1.color = 0xFAEB19;
	p2.color = 0xFAEB19;
	for (p2.y = 500; p2.y >= 0; p2.y -= 4)
	{
		line(p1, p2, mlx);
	}
	for (p2.x = 500; p2.x >= 0; p2.x -= 4)
	{
		line(p1, p2, mlx);
	}
	mlx_clear_window(mlx.mlx, mlx.win);
	p1.x = 0;
	p1.y = 0;
	p2.y = 500;
	p1.color = 0xC100E5;
	p2.color = 0xC100E5;
	for (p2.x = 0; p2.x <= 500; p2.x += 1)
	{
		line(p1, p2, mlx);
		p1.x += 1;
	}
	mlx_clear_window(mlx.mlx, mlx.win);
	p1.x = 0;
	p1.y = 0;
	p2.x = 500;
	p1.color = 0x1EBF00;
	p2.color = 0x1EBF00;
	for (p2.y = 0; p2.y <= 500; p2.y += 1)
	{
		line(p1, p2, mlx);
		p1.y += 1;
	}
	mlx_clear_window(mlx.mlx, mlx.win);
	p1.x = 500;
	p1.y = 0;
	p2.x = 500;
	p1.color = 0xFF000D;
	p2.color = 0xFF000D;
	for (; p2.x >= 0; p2.x -= 1)
	{
		line(p1, p2, mlx);
		p1.x -= 1;
	}
	mlx_clear_window(mlx.mlx, mlx.win);
	p1.x = 0;
	p1.y = 500;
	p2.x = 500;
	p1.color = 0xFAEB19;
	p2.color = 0xFAEB19;
	for (p2.y = 500; p2.y >= 0; p2.y -= 1)
	{
		line(p1, p2, mlx);
		p1.y -= 1;
	}
}

void	line(t_point p1, t_point p2, t_mlx m)
{
	int dx;
	int dy;
	t_swap swap;

	dx = abs(p2.x - p1.x);
	dy = abs(p2.y - p1.y);
	swap.swap_coord = 0;
	swap.swap_x = 1;
	if (dy > dx)
	{
		swap_x_y(&p1, &p2);
		swap.swap_coord= 1;
	}
	if (p1.x > p2.x)
	{
		swap_points(&p1, &p2);
	}
	if (p1.y > p2.y)
	{
		swap.swap_x = -1;
	}
	draw_line(p1, p2, m, swap);
}

int		main(int argc, char **argv)
{
	t_mlx m;

	argc = argc + 1;
	argv = argv;
	m.mlx = mlx_init();
	m.win = mlx_new_window(m.mlx, 500, 500, "fdf");
	//line(p1, p2, m);
	test_line(m);
	mlx_loop(m.mlx);
	return (0);
}
