/*
 *   EGB graphics function     By N.Takahashi
 *
 *       ver0.1 1993/11/07
 */

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <EGB.h>
#include "GRAPH.H"

/* Global variable */
char Egb[1536];
char Para[64];


/* 関数 : G_enlarge
   mx,my = 画面の拡大率
   tx,ty = 表示開始位置
   sx,sy = 画面の大きさ
*/
void G_enlarge( int mx, int my, int tx, int ty, int sx, int sy ) {
	EGB_displayStart( Egb, 3, 0, 0 );
	EGB_displayStart( Egb, 2, mx, my );
	EGB_displayStart( Egb, 0, tx, ty );
	EGB_displayStart( Egb, 1, 0, 0 );
	EGB_displayStart( Egb, 3, sx, sy );
}

/* 関数 : G_cls
   引数なし
*/
void G_cls( void ) {
	EGB_clearScreen( Egb );
}

/* 関数 : G_screen
   smode = 画面のモ−ド (1 - 18)
   page  = ペ−ジ (0,1)
*/
void G_screen( int smode, int page ) {
	EGB_resolution( Egb, page, smode );
	EGB_writePage( Egb, page );
	EGB_displayPage( Egb, 1, page+1 );
	EGB_color( Egb, 0, 0x7fff );
	EGB_color( Egb, 1, 0x8000 );
	EGB_color( Egb, 2, 0 );
	EGB_clearScreen( Egb );
}

/* 関数 : G_active
   page = ペ−ジ  (0,1)
   disp = 表示ペ−ジ  (0 - 3)
   pri  = プライオリティ  (0,1)
*/
void G_active( int page, int disp, int pri ) {
	EGB_writePage( Egb, page );
	EGB_displayPage( Egb, pri, disp );
}

/* 関数 : G_palette
   color = 色 (0-15-255)
   green,red,blue = 設定階調 (0 - 255)
   vsync = vsyncを待つかどうか (0,1) 1:待つ
*/
void G_palette( int color, int green, int red, int blue, int vsync ) {
	DWORD( Para+0 )=1;
	DWORD( Para+4 )=color;
	BYTE( Para+8 )=blue;
	BYTE( Para+9 )=red;
	BYTE( Para+10 )=green;
	BYTE( Para+11 )=0;
	EGB_palette( Egb, vsync, Para );
}

/* 関数 : G_put
   x1,y1,x2,y2 = 座標
   pbuf = パタ−ンバッファ
*/
void G_put( int x1, int y1, int x2, int y2, void *pbuf ) {
struct SREGS seg;
	_segread( &seg );
	DWORD( Para+0 )=(unsigned int)pbuf;
	WORD( Para+4 )=seg.ds;
	WORD( Para+6 )=x1;
	WORD( Para+8 )=y1;
	WORD( Para+10 )=x2;
	WORD( Para+12 )=y2;
	EGB_putBlock( Egb, 3, Para );
}

/* 関数 : G_get
   x1,y1,x2,y2 = 座標
   pbuf = パタ−ンバッファ
*/
void G_get( int x1, int y1, int x2, int y2, void *pbuf ) {
struct SREGS seg;
	_segread( &seg );
	DWORD( Para+0 )=(unsigned int)pbuf;
	WORD( Para+4 )=seg.ds;
	WORD( Para+6 )=x1;
	WORD( Para+8 )=y1;
	WORD( Para+10 )=x2;
	WORD( Para+12 )=y2;
	EGB_getBlock( Egb, Para );
}

/* 関数 : G_pset
   x,y = 座標
   color = 色 (0-15-255-32767 | 32768)
*/
void G_pset( int x, int y, int color ) {
	WORD( Para+0 )=1;
	WORD( Para+2 )=x;
	WORD( Para+4 )=y;
	EGB_color( Egb, 0, color );
	EGB_pset( Egb, Para );
}

/* 関数 : G_color32k
   green,red,blue = 設定階調 (0 - 255)
*/
int G_color32k( int green, int red, int blue ) {
	return( ((green<<7)&0x7c00)+((red<<2)&0x03e0)+((blue>>3)&0x001f) );
}

/* 関数 : G_rectangle
   x1,y1,x2,y2 = 座標
*/
void G_rectangle( int x1, int y1, int x2, int y2 ) {
	WORD( Para+0 )=x1;
	WORD( Para+2 )=y1;
	WORD( Para+4 )=x2;
	WORD( Para+6 )=y2;
	EGB_rectangle( Egb, Para );
}

/* 関数 : G_dosterm
   DOSコンソールに戻す｡
*/
void G_dosterm( void ) {
int p0=4,p1=4,pr=1,dp=3,wp=0;
char pal[]={ 16,0,0,0,
			1,0,0,0, 0xb0,0,0, 0,
			2,0,0,0, 0,0xb0,0, 0,
			3,0,0,0, 0xb0,0xb0,0, 0,
			4,0,0,0, 0,0,0xb0, 0,
			5,0,0,0, 0xb0,0,0xb0, 0,
			6,0,0,0, 0,0xb0,0xb0, 0,
			7,0,0,0, 0xb0,0xb0,0xb0, 0,
			8,0,0,0, 0x40,0x40,0x40, 0,
			9,0,0,0, 0xf0,0,0, 0,
			10,0,0,0, 0,0xf0,0, 0,
			11,0,0,0, 0xf0,0xf0,0, 0,
			12,0,0,0, 0,0,0xf0, 0,
			13,0,0,0, 0xf0,0,0xf0, 0,
			14,0,0,0, 0,0xf0,0xf0, 0,
			15,0,0,0, 0xf0,0xf0,0xf0, 0,
			0,0,0,0, 0,0,0, 0
		   };

	G_screen( p0, 0 );
	EGB_palette( Egb, 1, pal );
	G_screen( p1, 1 );
	EGB_palette( Egb, 1, pal );
	G_active( wp, dp, pr );
	G_cls();
}

/* 関数 : G_boxfull
   x1,y1,x2,y2 = 座標
   color = 色 (0-15-255-32767 | 32768)
*/
void G_boxfull( int x1, int y1, int x2, int y2, int color ) {
	EGB_color( Egb, 0, color );
	EGB_color( Egb, 2, color );
	EGB_paintMode( Egb, 0x22 );
	WORD( Para+0 )=x1;
	WORD( Para+2 )=y1;
	WORD( Para+4 )=x2;
	WORD( Para+6 )=y2;
	EGB_rectangle( Egb, Para );
}

/* 関数 : G_box
   x1,y1,x2,y2 = 座標
   color = 色 (0-15-255-32767 | 32768)
*/
void G_box( int x1, int y1, int x2, int y2, int color ) {
	EGB_color( Egb, 0, color );
	EGB_paintMode( Egb, 0x02 );
	WORD( Para+0 )=x1;
	WORD( Para+2 )=y1;
	WORD( Para+4 )=x2;
	WORD( Para+6 )=y2;
	EGB_rectangle( Egb, Para );
}
