/*
    conio.c --
    Text printing routines.
    (uses NGB0 as a 256-color bitmap)
*/

#include "shared.h"
#include "vga_font.h"
//#include "vga_pal.h"

static char hextable[16] = {"0123456789ABCDEF"};

void conio_init(void)
{
    int i, j, k;

    /* Transparent color */
    conio_set_color_to_palette(COLOR_RGB16(0,0,0), 0);
    /* White */
    for(i = 1; i < 16; i++)
        conio_set_color_to_palette(COLOR_RGB16(31,31,31), i);

    /* HTML4 color keywords */
    conio_set_color_to_palette(COLOR_RGB24(  0,  0,  0), COLOR_BLACK   );
    conio_set_color_to_palette(COLOR_RGB24(192,192,192), COLOR_SILVER  );
    conio_set_color_to_palette(COLOR_RGB24(128,128,128), COLOR_GRAY    );
    conio_set_color_to_palette(COLOR_RGB24(255,255,255), COLOR_WHITE   );
    conio_set_color_to_palette(COLOR_RGB24(128,  0,  0), COLOR_MAROON  );
    conio_set_color_to_palette(COLOR_RGB24(255,  0,  0), COLOR_RED     );
    conio_set_color_to_palette(COLOR_RGB24(128,  0,128), COLOR_PURPLE  );
    conio_set_color_to_palette(COLOR_RGB24(255,  0,255), COLOR_FUCHSIA );
    conio_set_color_to_palette(COLOR_RGB24(  0,128,  0), COLOR_GREEN   );
    conio_set_color_to_palette(COLOR_RGB24(  0,255,  0), COLOR_LIME    );
    conio_set_color_to_palette(COLOR_RGB24(128,128,  0), COLOR_OLIVE   );
    conio_set_color_to_palette(COLOR_RGB24(255,255,  0), COLOR_YELLOW  );
    conio_set_color_to_palette(COLOR_RGB24(  0,  0,128), COLOR_NAVI    );
    conio_set_color_to_palette(COLOR_RGB24(  0,  0,255), COLOR_BLUE    );
    conio_set_color_to_palette(COLOR_RGB24(  0,128,128), COLOR_TEAL    );
    conio_set_color_to_palette(COLOR_RGB24(  0,255,255), COLOR_AQUA    );


    /* 6*6*6 colors #trivil# */
    for(i = 0; i < 6; i++)
        for(j = 0; j < 6; j++)
            for(k = 0; k < 6; k++)
                conio_set_color_to_palette(COLOR_RGB16(5*k+1, 5*j+1, 5*i+1),
                                                          6*6*k + 6*j + i+32);

    RAMCTL = 0x0000;
}

void conio_set_color_to_palette(uint16 col, uint8 id)
{
    vdp2_cram[id] = col;
}



/*
temp = 0x8000;
if(pad.new & PAD_LBUTTON) // Half size, height
    temp |= 0x00C0;
if(pad.new & PAD_RBUTTON) // Half size, width
    temp |= 0x0002;
TVMD = temp;
*/

void conio_set_screen_size(int16 s)
{
    TVMD = 0x8000 | s;
}


void _printchar(int16 x, int16 y, uint16 color, char v)
{
    int r, c;
    vuint8 *bmp = (vuint8 *)VDP2_VRAM;
    uint8 fg = (color >> 0) & 0xFF;
    uint8 bg = (color >> 8) & 0xFF;

    for(r = 0; r < 8; r++)
    {
        uint8 b = conio_font[v<<3|r];
        for(c = 0; c < 8; c++)
        {
            uint8 d = (b & (0x80 >> c)) ? fg : bg;
            bmp[((y<<3|r)<<10/* 9-> half size */)+(x<<3|c)] = d;
        }            
    }
}

/* A tester ! -> OK !!
 * ajouter une fct du genre slLocate
 * ajouter un support des couleurs (comment faire ? ^^)
 
 * suggestion : printf(xstart, ystart, couleur, texte, ...)
 * Puis ajouter une fct clear_screen, set_size, set_current_color, set_default_color, etc
 */

void clear_screen(void)
{
    int i,j;
    for(i=0; i<40; i++)
        for(j=0; j<28; j++)
            _printchar(i, j, COLOR_WHITE, ' ');
}



void _printf(int16 x, int16 y, uint16 color, char* msg, ...)
{
    char buff[50];
    va_list ap;
    va_start(ap,msg);
    vsprintf(buff, msg,ap);
    va_end(ap);
    _printstr(x, y, color, buff);
}


void _printstr(int16 x, int16 y, uint16 color, char *s)
{
    int i;
    for(i = 0; s[i] != 0; i++)
        _printchar(x+i,y,color,s[i]);
}

void _printhexd(int16 x, int16 y, uint16 color, uint8 v)
{
    _printchar(x,y,color,hextable[v&0x0F]);
}

void _printhexb(int16 x, int16 y, uint16 color, uint8 v)
{
    _printchar(x+0,y,color,hextable[(v>>4)&0x0F]);
    _printchar(x+1,y,color,hextable[v & 0x0F]);
}

void _printhexw(int16 x, int16 y, uint16 color, uint16 v)
{
    _printchar(x+0,y,color,hextable[(v>>12)&0x0F]);
    _printchar(x+1,y,color,hextable[(v>>8)&0x0F]);
    _printchar(x+2,y,color,hextable[(v>>4)&0x0F]);
    _printchar(x+3,y,color,hextable[v & 0x0F]);
}

void _printhexl(int16 x, int16 y, uint16 color, uint32 v)
{
    _printchar(x+0,y,color,hextable[(v>>28)&0x0F]);
    _printchar(x+1,y,color,hextable[(v>>24)&0x0F]);
    _printchar(x+2,y,color,hextable[(v>>20)&0x0F]);
    _printchar(x+3,y,color,hextable[(v>>16)&0x0F]);
    _printchar(x+4,y,color,hextable[(v>>12)&0x0F]);
    _printchar(x+5,y,color,hextable[(v>>8)&0x0F]);
    _printchar(x+6,y,color,hextable[(v>>4)&0x0F]);
    _printchar(x+7,y,color,hextable[v & 0x0F]);
}

