typedef	unsigned int	uint;

char	EGB_work[1536];

/*
void	ginit()
	{
	KYB_init();
	EGB_init(EGB_work, 1536);
	}
*/

/*===============================================
				キーボード関係
===============================================*/

char	key_get()
	{
	uint	code, encode;
	for (;;) {
		code = KYB_read( 0, &encode );
		if ( (code & 0xffff) == 0xffff  &&  (encode & 0xff00) == 0xff00 )
			continue;
		break;
		}
	return (char) code;
	}

int		key_chk()		/* キー入力があるなら 1   なければ 0 */
	{
	uint	code, encode;
	int		chrcnt;
	code = KYB_inpchk( &chrcnt, &encode );
	if ( chrcnt == 0 )
		return 0;
	else
		return 1;
	}

char	key_read()		/* キー入力があれば そのコード なければ 0 */
	{
	if (key_chk())
		return key_get();
	else
		return 0;
	}

void	key_clrbuf()
	{
	KYB_clrbuf();
	}

/*====================================================
				グラフィック関係
====================================================*/

void	grp_init()
	{
	EGB_init ( EGB_work, 1536 );
	}

void	grp_clr()
	{
	EGB_clearScreen( EGB_work );
	}

void	grp_putstr( x, y, str, col )
	int		x, y;		/* 文字列の表示位置 */
	char	*str;		/* シフト JIS 文字列(100バイト以内) */
	int		col;
	{
	char	parabuf[110];
	EGB_color(EGB_work, 0, col);
	* (short*) ( parabuf     ) = x;
	* (short*) ( parabuf + 2 ) = y + 16;
	* (short*) ( parabuf + 4 ) = strlen( str );
	strcpy( parabuf+6, str );
	EGB_sjisString( EGB_work, parabuf );
	}

void	grp_pset( x, y, col, mode )
	int		x, y;
	int		col;
	int		mode;
	{
	short para[3];
	para[0] = 1;
	para[1] = x;
	para[2] = y;
	EGB_writeMode( EGB_work, mode );
	EGB_color( EGB_work, 0, col );
	EGB_pset( EGB_work, (char*) para );
	}

void	grp_line( x1, y1, x2, y2, col, mode )
	int		x1, y1, x2, y2;
	int		col;
	int		mode;
	{
	short para[5];
	para[0] = 2;
	para[1] = x1;
	para[2] = y1;
	para[3] = x2;
	para[4] = y2;
	EGB_writeMode( EGB_work, mode );
	EGB_color( EGB_work, 0, col );
	EGB_connect( EGB_work, (char*) para );
	}

void	grp_boxline( x1, y1, xlen, ylen, col, mode )
	int		x1, y1;
	int		xlen, ylen;
	int		col;
	int		mode;
	{
	short	para[4];
	para[0] = x1;
	para[1] = y1;
	para[2] = x1 + xlen -1;
	para[3] = y1 + ylen -1;
	EGB_writeMode( EGB_work, mode );
	EGB_paintMode( EGB_work, 0x202 );
	EGB_color( EGB_work, 0, col );
	EGB_rectangle( EGB_work, (char*) para );
	}

void	grp_boxfill( x1, y1, xlen, ylen, col, mode )
	int		x1, y1;
	int		xlen, ylen;
	int		col;
	int		mode;
	{
	short	para[4];
	para[0] = x1;
	para[1] = y1;
	para[2] = x1 + xlen -1;
	para[3] = y1 + ylen -1;
	EGB_writeMode( EGB_work, mode );
	EGB_paintMode( EGB_work, 0x222 );
	EGB_color( EGB_work, 0, col );
	EGB_color( EGB_work, 2, col );
	EGB_rectangle( EGB_work, (char*) para );
	}

int		grp_getWrtpage()
	{
	return (int) EGB_getWritePage( EGB_work, 0 );
	}

void	grp_setWrtpage( page )
	int		page;
	{
	EGB_writePage( EGB_work, page );
	}

void	grp_copy( sx1, sy1, sxlen, sylen, sp, dx1, dy1, dp, mode )
	int		sx1, sy1, sxlen, sylen;
	int		dx1, dy1;
	int		sp, dp;
	int		mode;
	{
	int		n, sx2, sy2, dmy, nowpage;
	char	para[18], para2[8];
	nowpage = grp_getWrtpage();
	grp_setWrtpage( sp );
	sx2 = sy1 + sxlen - 1;
	sy2 = sy1 + sylen - 1;
	WORD(para + 0) = 4;
	WORD(para + 2) = sx1;
	WORD(para + 4) = sy1;
	WORD(para + 6) = sx1;
	WORD(para + 8) = sy2;
	WORD(para + 10) = sx2;
	WORD(para + 12) = sy2;
	WORD(para + 14) = sx2;
	WORD(para + 16) = sy1;
	n = 0x80;
	EGB_region( EGB_work, &n, &dmy, &dmy, &dmy, &dmy, &dmy, para );
	para2[0] = dp;
	para2[1] = 0;
	WORD(para2 + 2) = dx1 - sx1;
	WORD(para2 + 4) = dy1 - sy1;
	EGB_copy( EGB_work, 0, para2, NULL );
	EGB_writePage( EGB_work, (int) nowpage );
	}

void	grp_setplt( plt, rgb )
	int		plt;
	int		rgb;	/* bit23-16:赤  bit15-8:緑  bit7-0:青 */
	{
	char	para[12];
	DWORD(para + 0) = 1;
	DWORD(para + 4) = plt;
	BYTE(para +  8) = (  rgb        & 0xff );
	BYTE(para +  9) = ( (rgb >>  8) & 0xff );
	BYTE(para + 10) = ( (rgb >> 16) & 0xff );
	BYTE(para + 11) = 0;
	EGB_palette( EGB_work, 0, para );
	}

int		grp_getplt( plt )	/* ret : bit23-16:赤  bit15-8:緑  bit7-0:青  */
	int		plt;
	{
	char	pltdat[4+8*256], *p;
	int		ret;
	EGB_getPalette( grp_getWrtpage(), pltdat );
	p = pltdat + 4 + 8 * plt + 4;
	ret = *p  |  ((int)*(p+1) << 16)  |  ((int)*(p+2) << 8);
	return ret;
	}

void	grp_getblk( x1, y1, xlen, ylen, buf )
	int		x1, y1, xlen, ylen;
	char	*buf;
	{
	char	para[14];
	DWORD(para + 0) = (unsigned long) ( buf + 4 );
	WORD(para + 4) = 0;
	WORD(para + 6) = x1;
	WORD(para + 8) = y1;
	WORD(para + 10) = x1 + xlen - 1;
	WORD(para + 12) = y1 + ylen - 1;
	WORD(buf + 0) = xlen;
	WORD(buf + 2) = ylen;
	EGB_getBlock( EGB_work, para );
	}

void	grp_putBlk( data, x1, y1 )
	char	*data;
	int		x1, y1;
	{
	char	para[14];
	DWORD(para + 0) = (unsigned long) ( data + 4 );
	WORD(para + 4) = 0;
	WORD(para + 6) = x1;
	WORD(para + 8) = y1;
	WORD(para + 10) = x1 + (*(int*)  data   ) - 1;
	WORD(para + 12) = y1 + (*(int*) (data+2)) - 1;
	EGB_putBlock( EGB_work, 0, para );
	}

int		grp_point ( x, y )
	int		x, y;
	{
	int		ret;
	EGB_point( EGB_work, 0, x, y, &ret);
	return ret;
	}

void	grp_mode ( int mode )
	{
	EGB_resolution( EGB_work, 0, mode );
	if ( 1 <= mode && mode <= 11 )
		EGB_resolution( EGB_work, 1, mode );
	}

void	grp_mode2 ( int mode )
	{
	EGB_resolution( EGB_work, 1, mode );
	}

void	grp_dispPos ( int x, int y )
	{
	EGB_displayStart( EGB_work, 1, x, y );
	}

int		grp_getMode()
	{
	int page, p0,p1;
	page = grp_getWrtpage();
	EGB_getResolution( &p0, &p1 );
	return ( page == 0  ?  p0 : p1 );
	}

void	grp_scrZoom( int xrate, int yrate )
	{
	static struct { int x,y } resolution[] =
		{ { 640,400 },  { 640,200 }, { 640,480 }, { 640,400 },
		  { 256,256 },  { 256,256 }, { 256,240 }, { 256,240 },
		  { 360,240 },  { 320,240 }, { 320,240 },
		  { 640,480 },  { 640,400 }, { 720,480 },
		  { 320,480 },  { 320,480 }, { 512,480 }, { 512,480 } };
	int mode;
	EGB_displayStart( EGB_work, 2, xrate, yrate );
	mode = grp_getMode() - 1;
	EGB_displayStart( EGB_work, 3, resolution[mode].x, resolution[mode].y );
	}
