/*
** EPITECH PROJECT, 2018
** task01
** File description:
** I do task
*/
#include <stdlib.h>
#include "../../include/lib/s_sfml.h"
#include "../../include/lib/my_graphics.h"

static void draw_filled_polygon(sfml_t *sfml,
polygon_t *polygon, int *xmax, int *xmin)
{
    int h_line[3];
    int i;

    i = -1;
    while (++i < polygon->ymax - polygon->ymin + 1){
        h_line[0] = polygon->ymin + i;
        h_line[1] = xmin[i];
        h_line[2] = xmax[i];
        my_horizontal_line(sfml, h_line, polygon->color);
    }
}

static void polygon_get_info(polygon_t *polygon)
{
    int i;

    i = 0;
    polygon->ymin = polygon->y[0];
    polygon->ymax = polygon->y[0];
    while (++i < polygon->nb_vertices){
        if (polygon->y[i] < polygon->ymin)
            polygon->ymin = polygon->y[i];
        if (polygon->y[i] > polygon->ymax)
            polygon->ymax = polygon->y[i];
    }
}

void my_filled_polygon(sfml_t *sfml, polygon_t polygon)
{
    char *empty;
    int *xmin;
    int *xmax;
    int i;

    i = -1;
    polygon_get_info(&polygon);
    xmin = (int*)malloc(sizeof(int) * (polygon.ymax - polygon.ymin + 1));
    xmax = (int*)malloc(sizeof(int) * (polygon.ymax - polygon.ymin + 1));
    empty = (char*)malloc(polygon.ymax - polygon.ymin + 1);
    while (++i < polygon.ymax - polygon.ymin + 1)
        empty[i] = 1;
    get_drawline(&polygon, xmin, xmax, empty);
    draw_filled_polygon(sfml, &polygon, xmax, xmin);
    free(empty);
    free(xmin);
    free(xmax);
}
