#include    <stdio.h>
#include    <stdarg.h>
#include    <ctype.h>
#include    <fmc.h>
#include    <egb.h>
#include    "defs.h"

static char	work[2048];
static int	c_x=0;
static int	c_y=0;
static int	c_f=STD_COL;
static int	c_b=BAK_COL;
static short	c_bak =CUR_OFF;

extern void wrtank(int ch,int x,int y,int fc,int bc,int of);
extern void wrtkan(int ch,int x,int y,int fc,int bc,int of);
extern void disp_cur(int x,int y,int of);

void	wrtstr(char *str)
{
    while ( *str != '\0' ) {
	if ( iskanji(*str) ) {
	    wrtkan((*(str++) << 8) | *(str++),c_x,c_y,c_f,c_b,0);
	    if ( (c_x += 2) >= 80 ) {
		c_x = 0;
		if ( ++c_y >= 25 )
		    c_y = 0;
	    }
	} else {
	    wrtank(*(str++),c_x,c_y,c_f,c_b,0);
	    if ( ++c_x >= 80 ) {
		c_x = 0;
		if ( ++c_y >= 25 )
		    c_y = 0;
	    }
	}
    }
/***************************************
    char    para[128];

    WORD(para + 0) = c_x * 8;
    WORD(para + 2) = c_y * 16 + 16;
    WORD(para + 4) = strlen(str);
    strcpy(para + 6,str);
    EGB_color(work,0,c_f),
    EGB_color(work,1,c_b),
    EGB_sjisString(work,para);
    c_x += WORD(para + 4);
*******************************************/
}
void	putch(int ch)
{
    wrtank(ch,c_x,c_y,c_f,c_b,0);
    if ( ++c_x >= 80 ) {
	c_x = 0;
	if ( ++c_y >= 25 )
	    c_y = 0;
    }
/*************************************
    char    para[64];

    WORD(para + 0) = c_x * 8;
    WORD(para + 2) = c_y * 16 + 16;
    WORD(para + 4) = 1;
    BYTE(para + 6) = (char)ch;
    EGB_color(work,0,c_f),
    EGB_color(work,1,c_b),
    EGB_asciiString(work,1,para);
    c_x++;
***************************************/
}
void    echo(sw)
int     sw;
{
    if ( sw == CUR_ON ) {
	disp_cur(c_x,c_y,0);
	c_bak = CUR_ON;
    }
    if ( sw == CUR_OFF && c_bak == CUR_ON ) {
	disp_cur(c_x,c_y,0);
	c_bak = CUR_OFF;
    }
}
void	locate(int x,int y)
{
    c_x = x;
    c_y = y;
}
void	color(int cl)
{
    if ( (cl & 0x10) != 0 ) {
	c_f = BAK_COL; c_b = cl & 0x0F;
    } else {
	c_b = BAK_COL; c_f = cl & 0x0F;
    }
}
void	con_printf(char *form,...)
{
    va_list arg;
    char    tmp[80];

    va_start(arg,form);
    vsprintf(tmp,form,arg);
    wrtstr(tmp);
    va_end(arg);
}
void	line(x1,y1,x2,y2,md,cl,fc,st)
int     x1,y1,x2,y2,md,cl,fc,st;
{
    char    para[10];
    int	    pattn[]={ 0xFFFFFFFF,0x33333333,0xF0F0F0F0,0xFFF0FFF0,0xFF18FF18 };

    EGB_writeMode(work,md);
    EGB_color(work,0,cl),
    EGB_linePattern(work,1,pattn[fc >= 3 ? st:0]);
    if ( fc == 0 || fc == 3 ) {
	EGB_paintMode(work,0x002);
	WORD(para + 0) = 2;
	WORD(para + 2) = x1;
	WORD(para + 4) = y1;
	WORD(para + 6) = x2;
	WORD(para + 8) = y2;
	EGB_connect(work,para);
    } else if ( fc == 1 || fc == 4 ) {
	EGB_paintMode(work,0x002);
	WORD(para + 0) = x1;
	WORD(para + 2) = y1;
	WORD(para + 4) = x2;
	WORD(para + 6) = y2;
	EGB_rectangle(work,para);
    } else if ( fc == 2 ) {
	EGB_paintMode(work,0x022);
	EGB_color(work,2,cl);
	WORD(para + 0) = x1;
	WORD(para + 2) = y1;
	WORD(para + 4) = x2;
	WORD(para + 6) = y2;
	EGB_rectangle(work,para);
    }
}
void	Palet_init()
{
    int     i,p;
    char    para[64];

    for ( i = 0 ; i < 16 ; i++ ) {
	p = i << 4;
	DWORD(para + 0) = 1;
	DWORD(para + 4) = i;
	BYTE(para + 8) = p;
	BYTE(para + 9) = p;
	BYTE(para + 10) = p;
	BYTE(para + 11) = 0;
	EGB_palette(work,0,para);
/******************************************
	outp(0xFD90,i);
	outp(0xFD92,p);
	outp(0xFD94,p);
	outp(0xFD96,p);
*******************************************/
    }
}
void	G_init()
{
    EGB_init(work,2048);
    EGB_resolution(work, 0, 3);
/*************
    EGB_resolution(work, 1, 3);
    EGB_displayPage(work, 0, 3);
**************/
    Palet_init();
    EGB_pen(work,0);
    EGB_penSize(work,1);
    line(0,0,639,479,PSET,BAK_COL,FBOX,LINE_1);
}
void	G_era()
{
    line(GRA_OFFX-2,GRA_OFFY-2,GRA_MAXX,GRA_MAXY,PSET,BAK_COL,FBOX,LINE_1);
}
int	kbhit()
{
    int	     cnt;
    unsigned ec;

    KYB_inpchk(&cnt,&ec);
    return cnt;
}
int	getch()
{
    unsigned ec;

    while ( kbhit() == 0 );
    return KYB_read(0,&ec);
}
void	wind(x,y,s,w)
int	x,y,s,w;
{
    x = x * 8; s++; s = x + s * 8;
    y = y * 16; w++; w = y + w * 16;
    x += 4; s += 4;
    y += 10; w += 6;

    line(x,y,s,y,PSET,10,LINE,LINE_1);
    line(x+1,y+1,s-1,y+1,PSET,10,LINE,LINE_1);
    line(s,y,s,w,PSET,0,LINE,LINE_1);
    line(s-1,y+1,s-1,w-1,PSET,0,LINE,LINE_1);
    line(x,w,s,w,PSET,0,LINE,LINE_1);
    line(x+1,w-1,s-1,w-1,PSET,0,LINE,LINE_1);
    line(x,y,x,w,PSET,10,LINE,LINE_1);
    line(x+1,y+1,x+1,w-1,PSET,10,LINE,LINE_1);
}
