/* << High C V1.4 >> **********************************************************
**
**	ｸﾞﾗﾌｨｯｸﾙｰﾁﾝ
**
**	1991.03.02 : CREATE
**	1991.03.02 : FINISH
**
**	< HISTORY >
**	1991.03.02 : CREATE
**	1991.04.23 : 清書
**
**	< note > : TABS = 4
**
**	Programmed by Y.Hirata ( Nifty ID : NAB03321 )
**
******************************************************************************/

#include <stdio.h>
#include <string.h>
#include <msdos.cf>
#include <egb.h>
#include "hc.h"

/*=============================================================================
**	使用上の注意事項
**
**	1. 次の関数は、EGBﾗｲﾌﾞﾗﾘの代わりに当ﾗｲﾌﾞﾗﾘの関数を使用すること.
**		EGB_init() ;			-->		GRP_init() ;
**		EGB_paintMode() ;		-->		GRP_paintMode() ;
**		EGB_writeMode() ;		-->		GRP_writeMode() ;
**		EGB_color() ;			-->		GRP_color() ;
**		EGB_writePage() ;		-->		GRP_writePage() ;
=============================================================================*/

char	*GRPwork ;								/*  EGB作業域の先頭ｱﾄﾞﾚｽ	*/
int		GRPpmode ;								/*  EGB用面塗りﾓｰﾄﾞ			*/
int		GRPwmode ;								/*  EGB用描画ﾓｰﾄﾞ			*/
int		GRPfcolor ;								/*  前景色					*/
int		GRPbcolor ;								/*  背景色					*/
int		GRPpcolor ;								/*  面塗り色				*/
int		GRPmcolor ;								/*  透過色					*/
int		GRPwpage ;								/*  書き込みﾍﾟｰｼﾞ			*/
int		GRPlocx ;								/*  ﾃｷｽﾄ表示位置(桁)		*/
int		GRPlocy ;								/*  ﾃｷｽﾄ表示位置(行)		*/

int		VRAMfcolor ;							/*  VRAM描画用文字色		*/
int		VRAMbcolor ;							/*  VRAM描画用背景色		*/

/**************************  ｸﾞﾗﾌｨｯｸﾗｲﾌﾞﾗﾘの初期化  **************************/
int GRP_init( char *egbwork,int size )
/*=============================================================================
**	ｸﾞﾗﾌｨｯｸﾗｲﾌﾞﾗﾘの初期化を行う.
**
**	< INPUT  > : EGB用作業域の先頭ｱﾄﾞﾚｽ
**			   : EGB用作業域のｻｲｽﾞ
**	< OUTPUT > : EGBﾗｲﾌﾞﾗﾘ用作業域
**	< RETURN > : ｴﾗｰ情報(EGB_init()に同じ)
=============================================================================*/
{
	GRPwork   = egbwork ;						/*  EGB作業域先頭ｱﾄﾞﾚｽ		*/
	GRPpmode  = 0x02 ;							/*  境界ﾍﾞﾀ					*/
	GRPwmode  = PSET ;							/*  PSET					*/
	GRPfcolor = 15 ;							/*  最大識別番号(白)		*/
	GRPbcolor = 0 ;								/*  色識別番号(黒)			*/
	GRPpcolor = 0 ;								/*  色識別番号(黒)			*/
	GRPmcolor = 0 ;								/*  色識別番号(黒)			*/
	GRPlocx   = 1 ;								/*  左上隅(1桁目)			*/
	GRPlocy   = 1 ;								/*  左上隅(1行目)			*/

	VRAM_page( 0 ) ;
	VRAMfcolor = GRPfcolor ;
	VRAMbcolor = GRPbcolor ;
	VRAM_color( VRAMfcolor,VRAMbcolor ) ;

	return( EGB_init( GRPwork,size ) ) ;
}

/******************************  面塗りﾓｰﾄﾞの設定  ***************************/
int GRP_paintMode( int mode )
/*=============================================================================
**	面塗りﾓｰﾄﾞの設定を行う.
**
**	< INPUT  > : 面塗りﾓｰﾄﾞ
**	< OUTPUT > : なし
**	< RETURN > : ｴﾗｰ情報(EGB_paintMode()に同じ)
=============================================================================*/
{
	GRPpmode = mode ;

	return( EGB_paintMode( GRPwork,mode ) ) ;
}

/*******************************  描画ﾓｰﾄﾞの設定  ****************************/
int GRP_writeMode( int mode )
/*=============================================================================
**	描画ﾓｰﾄﾞの設定を行う.
**
**	< INPUT  > : 描画ﾓｰﾄﾞ
**	< OUTPUT > : なし
**	< RETURN > : ｴﾗｰ情報(EGB_writeMode()に同じ)
=============================================================================*/
{
	GRPwmode = mode ;

	return( EGB_writeMode( GRPwork,mode ) ) ;
}

/********************************  描画色の設定  *****************************/
int GRP_color( int n,int color )
/*=============================================================================
**	描画色の設定を行う.
**
**	< INPUT  > : 設定色
**			   : 色識別番号
**	< OUTPUT > : なし
**	< RETURN > : ｴﾗｰ情報(EGB_color()に同じ)
=============================================================================*/
{
	switch ( n ) {
	case 0 :									/*  前景色					*/
		GRPfcolor = color ;
		VRAMfcolor = GRPfcolor ;
		VRAM_color( VRAMfcolor,VRAMbcolor ) ;
		break ;
	case 1 :									/*  背景色					*/
		GRPbcolor = color ;
		VRAMbcolor = GRPbcolor ;
		VRAM_color( VRAMfcolor,VRAMbcolor ) ;
		break ;
	case 2 :									/*  面塗り色				*/
		GRPpcolor = color ;
		break ;
	case 3 :									/*  透過色					*/
		GRPmcolor = color ;
	}

	return( EGB_color( GRPwork,n,color ) ) ;
}

/*****************************  書き込みﾍﾟｰｼﾞの設定  *************************/
int GRP_writePage( int page )
/*=============================================================================
**	書き込みﾍﾟｰｼﾞ(ｱｸﾃｨﾌﾞ ﾍﾟｰｼﾞ)の設定を行う.
**
**	< INPUT  > : ﾍﾟｰｼﾞ
**	< OUTPUT > : なし
**	< RETURN > : ｴﾗｰ情報(EGB_writePage()に同じ)
=============================================================================*/
{
	GRPwpage = page ;

	if ( page == 0 || page == 1 ) {
		VRAM_page( page ) ;
	}

	return( EGB_writePage( GRPwork,page ) ) ;
}

/******************************  描画領域設定  *******************************/
int GRP_viewport( int x1,int y1,int x2,int y2 )
/*=============================================================================
**	描画領域(ﾋﾞｭｰﾎﾟｰﾄ)の設定を行う.
**
**	< INPUT  > : x1,y1 左上座標
**			   : x2,y2 右下座標
**	< OUTPUT > : なし
**	< RETURN > : なし
=============================================================================*/
{
	struct {
		short	x1, y1, x2, y2 ;
	} pview ;									/*  描画領域設定用			*/

	pview.x1 = x1 ;
	pview.y1 = y1 ;
	pview.x2 = x2 ;
	pview.y2 = y2 ;

	return( EGB_viewport( GRPwork,(char *)&pview ) ) ;
}

/*******************************  文字属性設定  ******************************/
void GRP_textatr( int t_sp,int f_st )
/*=============================================================================
**	文字属性(字体,文字間隔)の設定を行う.
**
**	< INPUT  > : t_sp  文字間隔(0〜)
**			   : f_st  字体
**	< OUTPUT > : なし
**	< RETURN > : なし
=============================================================================*/
{
	EGB_textDirection( GRPwork,0 ) ;
	EGB_textDisplayDirection( GRPwork,0 ) ;
	EGB_textSpace( GRPwork,t_sp ) ;					/* 文字間スペース [dot]	*/
	EGB_textZoom( GRPwork,0,XCDOT,YCDOT ) ;			/* ANK  :  8 x 16		*/
	EGB_textZoom( GRPwork,1,XCDOT+XCDOT,YCDOT ) ;	/* 漢字 : 16 x 16		*/
	EGB_fontStyle( GRPwork,f_st ) ;					/* 字体					*/
/*
**	f_st : 字体
**	... 7 6 5 4 3 2 1 0
**	... -+-+-+-+-+-+-+-| >LSB
**		0
**		  1				消し線
**		  0 1			オーバーライン
**		  0 0 1			アンダーライン
**		  0 0 0 1		縁取り
**		  0 0 0 0 1		影付
**		  0 0 0 0 0 1	斜体
**		  0 0 0 0 0 0 1	太文字
*/
}

/*****************************  ﾃｷｽﾄ表示位置設定  ****************************/
void GRP_locate( int row,int column )
/*=============================================================================
**	文字の表示位置の設定を行う.
**
**	< INPUT  > : row     行
**			   : column  桁
**	< OUTPUT > : なし
**	< RETURN > : なし
=============================================================================*/
{
	GRPlocx = column ;
	GRPlocy = row ;
}

/********************************  ﾃｷｽﾄ表示  *********************************/
int GRP_putstr( char *str )
/*=============================================================================
**	前景色で文字列の表示を行う.
**
**	< INPUT  > : str  文字列格納先頭ｱﾄﾞﾚｽ
**	< OUTPUT > : なし
**	< RETURN > : なし
**
**	☆ 注意 ☆
**	　文字列の長さは、127文字以内です.
=============================================================================*/
{
	struct {
		short	x, y, len ;
		char	s[128] ;
	} ptext ;									/*  ﾃｷｽﾄ表示用				*/

	ptext.x = GRPlocx * XCDOT ;
	ptext.y = GRPlocy * YCDOT ;
	ptext.len = strlen( str ) ;
	if ( ptext.len > 127 ) {
		strncpy( ptext.s,str,127 ) ;
		ptext.s[127] = '\0' ;
		ptext.len = 127 ;
	} else {
		strcpy( ptext.s,str ) ;
	}
	GRPlocx += ptext.len ;

	return( EGB_sjisString( GRPwork,(char *)&ptext ) ) ;	
}

/*****************************  書式付ﾃｷｽﾄ表示  ******************************/
int GRP_printf( char *str,... )
/*=============================================================================
**	前景色でprintf()のように文字列の表示を行う.
**
**	< INPUT  > : str  文字列
**			   : ...  文字列内の書式制御文字列に対する引数
**	< OUTPUT > : なし
**	< RETURN > : 表示した文字列の長さ(ﾊﾞｲﾄ)
**
**	☆ 注意 ☆
**	　文字列の長さは、127文字以内です.
**	　文字列中の制御ｺｰﾄﾞ(00h〜19h)はそのままｺｰﾄﾞが出力されます.
**	　EGBの描画ﾓｰﾄﾞ,面塗りﾓｰﾄﾞ等に従って単純に文字列を表示しているため,EGBの
**	設定はあらかじめしておくこと.
=============================================================================*/
{
	char	buf[128] ;
	va_list	argptr ;
	int		ret ;

	va_start( argptr,str ) ;
	ret = vsprintf( buf,str,argptr ) ;
	GRP_putstr( buf ) ;
	va_end( argptr ) ;

	return( ret ) ;
}

/******************************  矩形領域取得  *******************************/
int GRP_get( int x1,int y1,int x2,int y2,unsigned int p,int bpp )
/*=============================================================================
**	矩形領域のﾃﾞｰﾀ取得を行う.
**
**	< INPUT  > : x1,y1 左上座標
**			   : x2,y2 右下座標
**			   : p     ﾃﾞｰﾀ格納領域先頭ｱﾄﾞﾚｽ
**			   : bpp   1ﾄﾞｯﾄ当たりのﾋﾟｸｾﾙ数
**	< OUTPUT > : なし
**	< RETURN > : なし
=============================================================================*/
{
	struct {
		unsigned int	p ;
		short	dseg, x1, y1, x2, y2 ;
	} pgetput ;									/*  EGB GET/PUT用			*/

	pgetput.p    = p ;
	pgetput.dseg = getds() ;
	pgetput.x1   = x1 ;
	pgetput.y1   = y1 ;
	pgetput.x2   = x2 ;
	pgetput.y2   = y2 ;

	if ( bpp == 1 ) {							/* ２色モードの時		*/
		return( EGB_getBlockColor( GRPwork,(char *)&pgetput ) ) ;
	} else {									/* 多色モードの時		*/
		return( EGB_getBlock( GRPwork,(char *)&pgetput ) ) ;
	}
}

/******************************  矩形領域設定  *******************************/
int GRP_put( int x1,int y1,int x2,int y2,unsigned int p,int bpp )
/*=============================================================================
**	矩形領域にﾃﾞｰﾀを書き込む.
**
**	< INPUT  > : x1,y1 左上座標
**			   : x2,y2 右下座標
**			   : p     ﾃﾞｰﾀ格納領域先頭ｱﾄﾞﾚｽ
**			   : bpp   1ﾄﾞｯﾄ当たりのﾋﾟｸｾﾙ数
**	< OUTPUT > : なし
**	< RETURN > : なし
=============================================================================*/
{
	struct {
		unsigned int	p ;
		short	dseg, x1, y1, x2, y2 ;
	} pgetput ;									/*  EGB GET/PUT用			*/

	pgetput.p    = p ;
	pgetput.dseg = getds() ;
	pgetput.x1   = x1 ;
	pgetput.y1   = y1 ;
	pgetput.x2   = x2 ;
	pgetput.y2   = y2 ;

	if ( bpp == 1 ) {							/* ２色モードの時		*/
		return( EGB_putBlockColor( GRPwork,0,(char *)&pgetput ) ) ;
	} else {									/* 多色モードの時		*/
		return( EGB_putBlock( GRPwork,0,(char *)&pgetput ) ) ;
	}
}

/********************************  直線描画  *********************************/
int GRP_line( int x1,int y1,int x2,int y2 )
/*=============================================================================
**	2点間を結ぶ直線を描く.
**
**	< INPUT  > : x1,y1 1点目の座標
**			   : x2,y2 2点目の座標
**	< OUTPUT > : なし
**	< RETURN > : なし
=============================================================================*/
{
	struct {
		short	n ;
		short	x1, y1, x2, y2 ;
	} pline ;									/*  直線表示用				*/

	pline.n  = 2 ;
	pline.x1 = x1 ;
	pline.y1 = y1 ;
	pline.x2 = x2 ;
	pline.y2 = y2 ;

	return( EGB_connect( GRPwork,(char *)&pline ) ) ;
}

/********************************  矩形描画  *********************************/
int GRP_box( int x1,int y1,int x2,int y2 )
/*=============================================================================
**	矩形を描く.
**
**	< INPUT  > : x1,y1 左上座標
**			   : x2,y2 右下座標
**	< OUTPUT > : なし
**	< RETURN > : なし
=============================================================================*/
{
	struct {
		short	x1, y1, x2, y2 ;
	} pbox ;									/*  矩形表示用				*/

	pbox.x1 = x1 ;
	pbox.y1 = y1 ;
	pbox.x2 = x2 ;
	pbox.y2 = y2 ;

	return( EGB_rectangle( GRPwork,(char *)&pbox ) ) ;
}

/*******************************  三角形描画  ********************************/
int GRP_triangle( int x1,int y1,int x2,int y2,int x3,int y3 )
/*=============================================================================
**	3点を結ぶ三角形を描く.
**
**	< INPUT  > : x1,y1 1点目の座標
**			   : x2,y2 2点目の座標
**			   : x3,y3 3点目の座標
**	< OUTPUT > : なし
**	< RETURN > : なし
=============================================================================*/
{
	struct {
		short	x1, y1, x2, y2, x3, y3 ;
	} ptriangle ;								/*  三角形表示用			*/

	ptriangle.x1 = x1 ;
	ptriangle.y1 = y1 ;
	ptriangle.x2 = x2 ;
	ptriangle.y2 = y2 ;
	ptriangle.x3 = x3 ;
	ptriangle.y3 = y3 ;

	return( EGB_triangle( GRPwork,(char *)&ptriangle ) ) ;
}

