/*----------------------------------------------------------------------------------------
	ワンダースワンカラー専用
----------------------------------------------------------------------------------------*/

#include <sys/bios.h>
#include <sys/libwwc.h>
#include <sys/fcntl.h>

extern BYTE *_heap;

/*----------------------------------------------------------------------------------------
	定義
----------------------------------------------------------------------------------------*/

#ifndef true
	#define true 1
#endif

#ifndef false
	#define false 0
#endif

#ifndef UCHAR
	typedef unsigned char UCHAR;
#endif

#ifndef SCHAR
	typedef signed char SCHAR;
#endif

#ifndef UINT
	typedef unsigned int UINT;
#endif

#ifndef ULONG
	typedef unsigned long ULONG;
#endif

/*----------------------------------------------------------------------------------------
	色定義
----------------------------------------------------------------------------------------*/

#define BLACK		0					/*黒											*/
#define DARKRED		1					/*暗赤											*/
#define DARKGREEN	2					/*暗緑											*/
#define DARKOLIVE	3					/*暗黄緑										*/
#define DARKBLUE	4					/*暗青											*/
#define DARKPURPLE	5					/*暗紫											*/
#define DARKAQUA	6					/*暗水色										*/
#define DARKGRAY	7					/*暗灰色										*/
#define GRAY		8					/*灰色											*/
#define RED			9					/*赤											*/
#define GREEN		10					/*緑											*/
#define OLIVE		11					/*黄緑											*/
#define BLUE		12					/*青											*/
#define PURPLE		13					/*紫											*/
#define AQUA		14					/*水色											*/
#define WHITE		15					/*白											*/


/*----------------------------------------------------------------------------------------
	フォーム定義
----------------------------------------------------------------------------------------*/

#define WWC_FORM_WIDTH 28				/*横置きの時の画面幅							*/
#define WWC_FORM_HEIGHT 18				/*横置きの時の画面高さ							*/

/*--------------------------------------------------------------------------------------*/
/*スプライトデータ*/
typedef UCHAR TWWCFONT;					/*R/W 幅, 高さ, データ							*/

/*--------------------------------------------------------------------------------------*/
/*フォーム*/
struct WWC_FORM{
	void (*OnKeyUp)(UINT);				/*R/W キーが放された時呼び出し					*/
	void (*OnCreate)(void);				/*R/W プログラム起動時に呼び出し				*/
	void (*OnIdle)(void);				/*R/W アイドリング中に呼び出し					*/
	void (*OnVBlank)(void);				/*R/W VBlank発生時呼び出し						*/
	void (*OnClose)(void);				/*R/W プログラム終了時呼び出し					*/
	void (*OnIdleReg[4])(void);			/*-/-											*/
	UCHAR Width;						/*R/- 画面幅(縦表示:18 横表示:28)				*/
	UCHAR Height;						/*R/- 画面高さ(縦表示:28 横表示:18)				*/
	UINT Key;							/*R/- キーの状態								*/
	int MouseX;							/*R/W マウスＸ軸座標(ドット単位)				*/
	int MouseY;							/*R/W マウスＹ軸座標(ドット単位)				*/
	TWWCFONT *MouseFont;				/*R/W マウスのフォントデータ(キャラタ番号511)	*/
	UINT MouseEnabled	: 1;			/*R/W true:マウス有効							*/
	UINT StartClose		: 1;			/*R/W true:STARTキー入力でプログラム終了		*/
	UINT TateView     	: 1;			/*R/- true:縦表示								*/
	UINT Color12bit  	: 1;			/*R/W true:12bitカラーモード					*/
	UCHAR Color;						/*R/W Image0->Enabled=false時のフォームの色		*/
	UCHAR Focused;						/*R/W フォーカス設定							*/
};
typedef struct WWC_FORM TWWC_FORM;

/*--------------------------------------------------------------------------------------*/
/*キー*/
struct WWC_KEY{
	UINT Up;							/*横表示:X1 縦表示:Y2							*/
	UINT Down;							/*横表示:X3 縦表示:Y4							*/
	UINT Left;							/*横表示:X4 縦表示:Y1							*/
	UINT Right;							/*横表示:X2 縦表示:Y3							*/
	UINT Enter;							/*横表示:A  縦表示:X3							*/
	UINT Esc;							/*横表示:B  縦表示:X4							*/
	UINT ExUp;							/*横表示:Y1 縦表示:X2							*/
	UINT ExDown;						/*横表示:Y3 縦表示:B							*/
	UINT ExLeft;						/*横表示:Y4 縦表示:X1							*/
	UINT ExRight;						/*横表示:Y2 縦表示:A							*/
};
typedef struct WWC_KEY TWWC_KEY;

/*--------------------------------------------------------------------------------------*/
/*イメージ*/
struct WWC_IMAGE{
	void (*OnKeyUp)(UINT);				/*R/W キーが放された時呼び出し					*/
	SCHAR Left;							/*R/W 左座標(キャラクタ単位)					*/
	SCHAR Top;							/*R/W 上座標(キャラクタ単位)					*/
	UCHAR Width;						/*R/W 幅(キャラクタ単位)						*/
	UCHAR Height;						/*R/W 高さ(キャラクタ単位)						*/
	UCHAR far *Image; 					/*R/W イメージを格納するバッファーのアドレス	*/
	UCHAR ImageLeft;					/*R/W Imageバッファーの左座標(キャラクタ単位)	*/
	UCHAR ImageTop;						/*R/W Imageバッファーの上座標(キャラクタ単位)	*/
	UCHAR ImageWidth;					/*R/W Imageバッファーの幅(キャラクタ単位)		*/
	UCHAR ImageHeight;					/*R/W Imageバッファーの高さ(キャラクタ単位)		*/
	UINT  Enabled		: 1;			/*R/W 有効										*/
};
typedef struct WWC_IMAGE TWWC_IMAGE;

/*--------------------------------------------------------------------------------------*/
/*タイマー*/
struct WWC_TIMER{
	void (*OnTimer)(void);				/*R/W 繰り返し時間後に呼び出し					*/
	int Interval;						/*R/W 繰り返し時間[ms]							*/
	UINT Enabled		: 1;			/*R/W 有効										*/
};
typedef struct WWC_TIMER TWWC_TIMER;

/*--------------------------------------------------------------------------------------*/
/*シリアル通信*/
struct WWC_COM{
	void (*OnReceive)(void);			/*R/W											*/
	UCHAR Speed;						/*R/W											*/
	UCHAR RxPos;						/*-/-											*/
	UCHAR RxEnd;						/*-/-											*/
	UCHAR RxBuf[256];					/*-/-											*/
};
typedef struct WWC_COM TWWC_COM;

/*----------------------------------------------------------------------------------------
	外部から利用できる変数
----------------------------------------------------------------------------------------*/

extern TWWC_FORM *Form;
extern TWWC_KEY *Key;
extern TWWC_IMAGE *Image[4];
extern TWWC_TIMER *Timer;

/*----------------------------------------------------------------------------------------
	外部から利用できる関数
----------------------------------------------------------------------------------------*/

																					/*内部-内部 ,内部-外部,外部-外部*/
void MemSet32(void far *des, UCHAR src, int Len);									/*約1600kB/s,約890kB/s			*/
void MemCpy32(void far *des, void far *src, int Len);								/*約1200kB/s,約750kB/s,約540kB/s*/
void MemCpy32Roll(void far *des, void far *src, int Len, void *End, int RoolLen);	/*約1100kB/s,約710kB/s			*/
/*	?																				/*約 810kB/s,約570kB/s			*/

/*----------------------------------------------------------------------------------------
	Form
----------------------------------------------------------------------------------------*/

void FormClose(void);
void FormViewChange(int Mode);

/*--------------------------------------------------------------------------------------*/

void SetCallback(int type, void (far *callback)());
int FileGetName(UCHAR far Name[][MAXFNAME], UCHAR far *Path, int Size);
void MonoTo16PACKED(UCHAR far *Des, UCHAR far *Src, UCHAR FColor, UCHAR BColor);

void ComOpen(TWWC_COM *Com);
void ComClose(void);
int ComGetc(void);

void DebugOpen(void);
void DebugClose(void);

/*--------------------------------------------------------------------------------------*/

void _asm_one(char near *asm);

#define ClrInterrupt() 	_asm_one("	cli");
#define SetInterrupt() 	_asm_one("	sti");

/*----------------------------------------------------------------------------------------
	Image(16色カラー用)
----------------------------------------------------------------------------------------*/

void ImagePutc(TWWC_IMAGE *Img, int x, int y, UINT Src, UCHAR FColor, UCHAR BColor);
void ImagePuts(TWWC_IMAGE *Img, int x, int y, UCHAR far *Src, UCHAR FColor, UCHAR BColor);
void ImageWPuts(TWWC_IMAGE *Img, int x, int y, UINT far *Src, UCHAR FColor, UCHAR BColor);
void ImagePutsH(TWWC_IMAGE *Img, int x, int y, UCHAR far *Src, UCHAR FColor, UCHAR BColor);
void ImageWPutsH(TWWC_IMAGE *Img, int x, int y, UINT far *Src, UCHAR FColor, UCHAR BColor);
void ImagePSet(TWWC_IMAGE *Img, int x, int y, UCHAR Color);
void ImageTateYoko(TWWC_IMAGE *Img);
int ImageLoadFromFile(TWWC_IMAGE *Img, UCHAR far *path);

#define ImageFill(Img, Color) MemSet32(Img->Image, Color, Img->ImageWidth*Img->ImageHeight);

#define	Image0	Image[0]
#define	Image1	Image[1]
#define	Image2	Image[2]
#define	Image3	Image[3]

/*----------------------------------------------------------------------------------------
	Image(12bitカラー用)
----------------------------------------------------------------------------------------*/

int Image12LoadFromFile(UCHAR far *path);

#define Image12Putc(x, y, Src, RF, GF, BF, RB, GB, BB)\
			ImagePutc(Image1, x, y, Src, RF, RB);\
			ImagePutc(Image2, x, y, Src, GF, GB);\
			ImagePutc(Image3, x, y, Src, BF, BB);

#define Image12WPuts(x, y, Src, RF, GF, BF, RB, GB, BB)\
			ImageWPuts(Image1, x, y, Src, RF, RB);\
			ImageWPuts(Image2, x, y, Src, GF, GB);\
			ImageWPuts(Image3, x, y, Src, BF, BB);

#define Image12Puts(x, y, Src, RF, GF, BF, RB, GB, BB)\
			ImagePuts(Image1, x, y, Src, RF, RB);\
			ImagePuts(Image2, x, y, Src, GF, GB);\
			ImagePuts(Image3, x, y, Src, BF, BB);

#define Image12PSet(x, y, R, G, B)\
			ImagePSet(Image1, x, y, R);\
			ImagePSet(Image2, x, y, G);\
			ImagePSet(Image3, x, y, B);

#define Image12Fill(R, G, B)\
			MemSet32(Image1->Image, R, Image1->ImageWidth*Image1->ImageHeight);\
			MemSet32(Image2->Image, G, Image2->ImageWidth*Image2->ImageHeight);\
			MemSet32(Image3->Image, B, Image3->ImageWidth*Image3->ImageHeight);

/*--------------------------------------------------------------------------------------*/

