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

void my_clear_vram(sfml_t *sfml, uint32_t color)
{
    uint8_t red = (color >> 24) & 0xff;
    uint8_t green = (color >> 16) & 0xff;
    uint8_t blue = (color >> 8) & 0xff;
    uint8_t alpha = color & 0xff;
    uint8_t *vram = (uint8_t*)sfml->vram;
    int i;

    i = -1;
    while (++i < sfml->width * sfml->height){
        vram[i << 2] = red;
        vram[(i << 2) + 1] = green;
        vram[(i << 2) + 2] = blue;
        vram[(i << 2) + 3] = alpha;
    }
}

void my_display_vram(sfml_t *sfml)
{
    sfTexture_updateFromPixels(sfml->texture, (sfUint8*)sfml->vram, sfml->width,
    sfml->height, 0, 0);
    sfSprite_setTexture(sfml->vram_sprite, sfml->texture, 0);
    sfRenderWindow_drawSprite(sfml->window, sfml->vram_sprite, NULL);
    sfRenderWindow_display(sfml->window);
}

void my_pixel(sfml_t *sfml, int x, int y, uint32_t color)
{
    uint8_t *vram = (uint8_t*)sfml->vram;
    if (x < 0 || x >= sfml->width || y < 0 || y >= sfml->height)
        return;
    vram[(y * sfml->width + x) << 2] = (color >> 24) & 0xff;
    vram[((y * sfml->width + x) << 2) + 1] = (color >> 16) & 0xff;
    vram[((y * sfml->width + x) << 2) + 2] = (color >> 8) & 0xff;
    vram[((y * sfml->width + x) << 2) + 3] = color & 0xff;
}

void my_horizontal_line(sfml_t *sfml, int h_line[3], uint32_t color)
{
    int start;
    int end;

    end = (h_line[1] < h_line[2]) ? h_line[2] : h_line[1];
    start = (h_line[1] < h_line[2]) ? h_line[1] - 1 : h_line[2] - 1;
    while (++start < end)
        my_pixel(sfml, start, h_line[0], color);
}
