#include "../include/drawstuff.h"
#include "../parameters.h"
#include <gint/display.h>
#include <gint/dma.h>
#include "../fixed.h"

extern uint8_t minYRoad;

void gint_dhline(int x1, int x2, int y, uint16_t color)
{
    if((uint)y >= 224) return;
    if(x1 > x2) swap(x1, x2);
    if(x1 >= 396 || x2 < 0) return;
    if(x1 < 0) x1 = 0;
    if(x2 >= 396) x2 = 395;

    int offset = 396 * y;

    gint_vram[offset + x1] = color;
    gint_vram[offset + x2] = color;

    x1 = x1 + (x1 & 1);
    x2 = (x2 + 1) & ~1;

    uint32_t *start = (void *)(gint_vram + offset + x1);
    uint32_t *end   = (void *)(gint_vram + offset + x2);
    uint32_t op = (color << 16) | color;

    while(end > start) *--end = op;
};


void drawGrass( int y1, int y2, uint8_t R, uint8_t G, uint8_t B )
{
    uint16_t color = C_RGB(R,G,B);
    for (int y=y1; y<=y2; y++) gint_dhline( 0, SCREEN_WIDTH, y, color );
};


void drawGrass( int y1, int y2, uint16_t color )
{
    for (int y=y1; y<=y2; y++) gint_dhline( 0, SCREEN_WIDTH, y, color );
};


void drawPolygon( int x1min, int x1max, int y1, int x2min, int x2max, int y2, uint8_t R, uint8_t G, uint8_t B )
{

     uint16_t color = C_RGB(R,G,B);
     if (y1==y2)
     {
         gint_dhline( x1min, x1max, y1, color );
         return;
     }

    fixed_t deltay = fdiv(fix(1), fix(y2-y1));

    fixed_t xmin = fix(x1min);
    fixed_t xmax = fix(x1max);
    fixed_t dxmin = fix(x2min-x1min);
    fixed_t dxmax = fix(x2max-x1max);

    for (int y=y1; y<=y2; y++)
    {
        if (y>0 && y<SCREEN_HEIGHT)
        {
            xmin += fmul(deltay, dxmin);
            xmax += fmul(deltay, dxmax);
            gint_dhline( fround(xmin), fround(xmax), y, color );
        }
        //deltay+=Ddeltay;
    }
};


void drawPolygon( int x1min, int x1max, int y1, int x2min, int x2max, int y2, uint16_t color  )
{
     if (y1==y2)
     {
         gint_dhline( x1min, x1max, y1, color );
         return;
     }

    fixed_t deltay = fdiv(fix(1), fix(y2-y1));

    fixed_t xmin = fix(x1min);
    fixed_t xmax = fix(x1max);
    fixed_t dxmin = fix(x2min-x1min);
    fixed_t dxmax = fix(x2max-x1max);

    for (int y=y1; y<=y2; y++)
    {
        if (y>0 && y<SCREEN_HEIGHT)
        {
            xmin += fmul(deltay, dxmin);
            xmax += fmul(deltay, dxmax);
            gint_dhline( fround(xmin), fround(xmax), y, color );
        }
    }
};


void drawSkyFull( uint16_t color )
{
    //dclear( color );
    dma_memset(gint_vram, (color << 16) | color, 396 * 174 * 2);
};


void drawSky( uint16_t color )
{
	dma_memset(gint_vram, (color << 16) | color, 396 * 110 * 2);
};

void drawSkyOptimised( uint16_t color )
{
	dma_memset(gint_vram, (color << 16) | color, 396 * (minYRoad+2) * 2);
};

void drawSkyQuick( uint16_t color )
{
	dma_memset(gint_vram, (color << 16) | color, 396 * 150 * 2);
};

void drawSky( uint8_t R, uint8_t G, uint8_t B )
{
    dclear( C_RGB(R,G,B) );
};


void drawSky( int ymin, int ymax )
{
    uint16_t color = C_RGB(0,45,255);
    for(int y=ymin; y<=ymax; y++ )
        gint_dhline( 0, SCREEN_WIDTH, y, color );
};

