/*
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ 葵ライブラリ                         /////  Tab Size : 4  /////  ┃
┃                                      /////  Margin : 120  /////  ┃
┃ aoi_lib2.c   v1.0 L21                                            ┃
┃                                                                  ┃
┃ (C)K.Konishi 10-Feb-94                                           ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
*/

#define	LIB

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <io.h>
#include <time.h>
#include <conio.h>
#include <msdos.cf>
#include <EGB.h>
#include <MOS.h>
#include <SND.h>
#include "Platform.h"
#include "aoi_lib2.h"


// #define	TEST 
// 上の行の//を取るとサンプルプログラムが出来上がります。


#define	MDCL1	 1	/* 色の名前 */
#define	MDCL2	 2
#define	MDCL3	 3
#define	MDCL4	 4
#define	MDCL5	 5
#define	MDCL6	 9	/* 6から9に変えています */
#define	MDCL7	 7

#define	BLACK	 8
#define	BLUE	 9
#define	RED		10
#define	PURPLE	11
#define	GREEN	12
#define	SKYBLUE	13
#define	YELLOW	14
#define	WHITE	15

int AOIpset(short x, short y, short c)
{
	EGB_color(work, 0, c);
	WORD(para + 0) = 1;
	WORD(para + 2) = x;
	WORD(para + 4) = y;
	return EGB_pset(work, para);
}

int AOIcircle(short x, short y, short r, short c)
{
	EGB_color(work, 0, c);
	WORD(para + 0) = x;
	WORD(para + 2) = y;
	WORD(para + 4) = r;
	return EGB_circle(work, para);
}

int AOIstring(short x, short y, AOIstringData *Data, char *tx)
{
	AOIstringData Dt = *Data;
	EGB_color(work, 0, Dt.sc);
	EGB_fontStyle(work, Dt.ss);
	EGB_textZoom(work, 1, Dt.sx, Dt.sy);
	EGB_textZoom(work, 0, Dt.ax, Dt.ay);
	WORD(para + 0) = x;
	WORD(para + 2) = y;
	WORD(para + 4) = strlen(tx);
	strcpy((para + 6), tx);
	return EGB_sjisString(work, para);
}

int AOIstrSetup(short zsx, short zsy, short zax, short zay, short sf, short sc)
{
	return	EGB_textZoom(work, 1, zsx, zsy)
		|	EGB_textZoom(work, 0, zax, zay)
		|	EGB_fontStyle(work, sf)
		|	EGB_color(work, 0, sc);
}

int AOIstrSjis(short x, short y, char *tx)
{
	WORD(para + 0) = x;
	WORD(para + 2) = y;
	WORD(para + 4) = strlen(tx);
	strcpy((para + 6), tx);
	return EGB_sjisString(work, para);
}

int AOIanyChar(short x, short y, short fx, short fy, char *data)
{
	WORD(para + 0) = x;
	WORD(para + 2) = y;
	WORD(para + 4) = 0;
	EGB_sjisString(work, para);	/* 座標設定用ダミー */
	return EGB_anyChar(work, fx, fy, data);
}

int AOIline(short x0, short y0, short x1, short y1, short c)
{
	EGB_color(work, 0, c);
	WORD(para + 0) = 2;
	WORD(para + 2) = x0;
	WORD(para + 4) = y0;
	WORD(para + 6) = x1;
	WORD(para + 8) = y1;
	return EGB_connect(work, para);
}

int AOIconnectLeft(short x0, short y0, short x1, short y1, short c)
{
	EGB_color(work, 0, c);
	WORD(para +  0) = 3;
	WORD(para +  2) = x0;
	WORD(para +  4) = y1;
	WORD(para +  6) = x0;
	WORD(para +  8) = y0;
	WORD(para + 10) = x1;
	WORD(para + 12) = y0;
	return EGB_connect(work, para);
}

int AOIconnectRight(short x0, short y0, short x1, short y1, short c)
{
	EGB_color(work, 0, c);
	WORD(para +  0) = 3;
	WORD(para +  2) = x0;
	WORD(para +  4) = y1;
	WORD(para +  6) = x1;
	WORD(para +  8) = y1;
	WORD(para + 10) = x1;
	WORD(para + 12) = y0;
	return EGB_connect(work, para);
}

int AOIconnectLeftIGRB(short x0, short y0, short x1, short y1, int c)
{
	EGB_colorIGRB(work, 0, c);
	WORD(para +  0) = 3;
	WORD(para +  2) = x0;
	WORD(para +  4) = y1;
	WORD(para +  6) = x0;
	WORD(para +  8) = y0;
	WORD(para + 10) = x1;
	WORD(para + 12) = y0;
	return EGB_connect(work, para);
}

int AOIconnectRightIGRB(short x0, short y0, short x1, short y1, int c)
{
	EGB_colorIGRB(work, 0, c);
	WORD(para +  0) = 3;
	WORD(para +  2) = x0;
	WORD(para +  4) = y1;
	WORD(para +  6) = x1;
	WORD(para +  8) = y1;
	WORD(para + 10) = x1;
	WORD(para + 12) = y0;
	return EGB_connect(work, para);
}

int AOIbox(short x0, short y0, short x1, short y1, short c)
{
	EGB_color(work, 0, c);
	WORD(para +  0) = x0;
	WORD(para +  2) = y0;
	WORD(para +  4) = x1;
	WORD(para +  6) = y1;
	return EGB_rectangle(work, para);
}

int AOIboxFull(short x0, short y0, short x1, short y1, int c)
{
	int ret = 0;

	if (y0 > y1) {
		y1 ^= y0;	y0 ^= y1;	y1 ^= y0;
		x1 ^= x0;	x0 ^= x1;	x1 ^= x0;
	}
	ret = EGB_color(work, 0, c);
	WORD(para + 0) = 2;
	WORD(para + 2) = x0;
	WORD(para + 6) = x1;
	for (; y0 <= y1; y0++) {
		WORD(para + 4) = y0;
		WORD(para + 8) = y0;
		ret += EGB_connect(work, para);
	}
	return ret;
}

int AOIboxFullIGRB(short x0, short y0, short x1, short y1, int c)
{
	int ret = 0;

	if (y0 > y1) {
		y1 ^= y0;	y0 ^= y1;	y1 ^= y0;
		x1 ^= x0;	x0 ^= x1;	x1 ^= x0;
	}
	ret = EGB_colorIGRB(work, 0, c);
	WORD(para + 0) = 2;
	WORD(para + 2) = x0;
	WORD(para + 6) = x1;
	for (; y0 <= y1; y0++) {
		WORD(para + 4) = y0;
		WORD(para + 8) = y0;
		ret += EGB_connect(work, para);
	}
	return ret;
}

int AOIwinBase(short x, short y, short lx, short ly, short sd)
{
	int i = 0;
	
	if (sd == 1) {
		EGB_writeMode(work, 7);
		EGB_pastel(work, 128);
		i = AOIboxFull(x + lx + 1, y + 6, x + lx + 6, y + ly + 7, MDCL1)
		|	AOIboxFull(x + 6, y + ly + 1, x + lx, y + ly + 7, MDCL1);
		EGB_writeMode(work, 0);
	}
	return	i
		|	AOIboxFull(x, y, x + lx, y + ly, MDCL6)
		|	AOIconnectLeft (x    , y    , x + lx - 1, y + ly - 1, WHITE)
		|	AOIconnectRight(x    , y    , x + lx    , y + ly    , MDCL1)
		|	AOIconnectLeft (x + 4, y + 4, x + lx - 5, y + ly - 5, MDCL1)
		|	AOIconnectRight(x + 4, y + 4, x + lx - 4, y + ly - 4, WHITE);
}

int AOIwinBase2(int x, int y, int lx, int ly, int sd)
{
	int i = 0;
	
	if (sd != 0) {
		EGB_writeMode(work, 7);
		EGB_pastel(work, 128);
		i = AOIboxFull(x + lx + 1, y + sd - 1, x + lx + sd - 1, y + ly + sd, MDCL1)
		|	AOIboxFull(x + sd - 1, y + ly + 1, x + lx, y + ly + sd, MDCL1);
		EGB_writeMode(work, 0);
	}
	return	i
		|	AOIboxFull(x, y, x + lx, y + ly, MDCL6)
		|	AOIconnectLeft (x    , y    , x + lx - 1, y + ly - 1, WHITE)
		|	AOIconnectRight(x    , y    , x + lx    , y + ly    , MDCL1)
		|	AOIconnectLeft (x + 4, y + 4, x + lx - 5, y + ly - 5, MDCL1)
		|	AOIconnectRight(x + 4, y + 4, x + lx - 4, y + ly - 4, WHITE);
}

int AOIbuttonBase(short x, short y, short lx, short ly)
{
	return	AOIboxFull(x + 1, y + 1, x + lx - 1, y + ly - 1, MDCL6)
		|	AOIconnectLeft (x, y, x + lx - 1, y + ly - 1, WHITE)
		|	AOIconnectRight(x, y, x + lx    , y + ly    , MDCL1);
}

int AOIbuttonBase2(int x, int y, int lx, int ly)
{
	return	AOIboxFull(x + 1, y + 1, x + lx - 1, y + ly - 1, MDCL6)
		|	AOIconnectLeft (x, y, x + lx - 1, y + ly - 1, WHITE)
		|	AOIconnectRight(x, y, x + lx    , y + ly    , MDCL1);
}

int AOIwinBase32k(int x, int y, int lx, int ly, int sd)
{
	//	sd : 影の大きさ
	int i = 0;
	
	if (sd != 0) {
		EGB_writeMode(work, 7);
		EGB_pastel(work, 128);
		i = AOIboxFullIGRB(x + lx + 1, y + sd - 1, x + lx + sd - 1, y + ly + sd, 0)
		|	AOIboxFullIGRB(x + sd - 1, y + ly + 1, x + lx, y + ly + sd, 0);
		EGB_writeMode(work, 0);
	}
	return	i
		|	AOIboxFullIGRB(x, y, x + lx, y + ly, LIGHTBULE32K)
		|	AOIconnectLeftIGRB (x    , y    , x + lx - 1, y + ly - 1, WHITE32K)
		|	AOIconnectRightIGRB(x    , y    , x + lx    , y + ly    , DARKBULE32K)
		|	AOIconnectLeftIGRB (x + 4, y + 4, x + lx - 5, y + ly - 5, DARKBULE32K)
		|	AOIconnectRightIGRB(x + 4, y + 4, x + lx - 4, y + ly - 4, WHITE32K);
}

int AOIbuttonBase32k(int x, int y, int lx, int ly)
{
	return	AOIboxFullIGRB(x + 1, y + 1, x + lx - 1, y + ly - 1, LIGHTBULE32K)
		|	AOIconnectLeftIGRB (x, y, x + lx - 1, y + ly - 1, WHITE32K)
		|	AOIconnectRightIGRB(x, y, x + lx    , y + ly    , DARKBULE32K);
}

int AOIsetPalette(int cn, int cg, int cr, int cb)
{
	DWORD(para + 0) = 1;
	DWORD(para + 4) = cn; 				/* cn 色識別番号 */
	BYTE(para +  8) = cb * 17;			/* cb  青 */
	BYTE(para +  9) = cr * 17;			/* cr  赤 */
	BYTE(para + 10) = cg * 17;  		/* cg  緑 */
	BYTE(para + 11) = 0;
	return EGB_palette(work, 0, para);	/* display レイヤ表示 */
}

int AOIsetPalette256(int cn, int cg, int cr, int cb)
{
	DWORD(para + 0) = 1;
	DWORD(para + 4) = cn; 				/* cn 色識別番号 */
	BYTE(para +  8) = cb * 257;			/* cb  青 */
	BYTE(para +  9) = cr * 257;			/* cr  赤 */
	BYTE(para + 10) = cg * 257;  		/* cg  緑 */
	BYTE(para + 11) = 0;
	return EGB_palette(work, 0, para);	/* display レイヤ表示 */
}

int AOIcolorIGRB(int g, int r, int b)
{
	return EGB_colorIGRB(work, 0, (((g << 19) & 0xff0000) + ((r << 11) & 0xff00) + ((b << 3) & 0xff)) & 0xffffff);
}

int AOIsetStandardPalette(void)
{
	return	AOIsetPalette( 8,  0,  0,  0)
		|	AOIsetPalette( 9,  0,  0, 15)
		|	AOIsetPalette(10,  0, 15,  0)
		|	AOIsetPalette(11,  0, 15, 15)
		|	AOIsetPalette(12, 15,  0,  0)
		|	AOIsetPalette(13, 15,  0, 15)
		|	AOIsetPalette(14, 15, 15,  0)
		|	AOIsetPalette(15, 15, 15, 15);
}

short AOIbin2short(char *bin)
{
	short	len;
	short	i;
	short	ret = 0;
	
	len = strlen(bin);
	for (i = 0; i < len; i++) {
		ret <<= 1;
		if (bin[i] == '1') ret++;
		else if (bin[i] != '0') return 0;
	}
	
	return ret;	
}

long AOIbin2long(char *bin)
{
	short	len;
	short	i;
	long	ret = 0;
	
	len = strlen(bin);
	for (i = 0; i < len; i++) {
		ret <<= 1;
		if (bin[i] == '1') ret++;
		else if (bin[i] != '0') return 0;
	}
	
	return ret;	
}

void AOIsetButton(AOIbuttonData *Data, short x, short y, short lx, short ly)
{
	AOIbuttonData bData;

	bData.x  = x;
	bData.y  = y;
	bData.lx = lx;
	bData.ly = ly;
	*Data = bData;
}

int AOIbutton(short sx, short sy, int mx, int my, AOIbuttonData *Data)
{
	AOIbuttonData *Dt = Data;
	int i;
	
	for (i = 0; i <= BUTTON; i++) {
		if (sx + Dt[i].x < mx && mx < sx + Dt[i].x + Dt[i].lx && sy + Dt[i].y < my && my < sy + Dt[i].y + Dt[i].ly) {
			return i;
		}
	}
	return -1;
}

void _AOIfigureDrawSet(short *fig, short *a)
{
	short *da = a;
	
	switch (*fig) {
		case 0 :
			da[1] = da[2] = da[3] = da[4] = da[5] = da[7] = 1;
			break;
		case 1 :
			da[4] = da[5] = 1;
			break;
		case 2 :
			da[1] = da[4] = da[6] = da[3] = da[7] = 1;
			break;
		case 3 :
			da[1] = da[4] = da[6] = da[5] = da[7] = 1;
			break;
		case 4 :
			da[2] = da[6] = da[4] = da[5] = 1;
			break;
		case 5 :
			da[1] = da[2] = da[6] = da[5] = da[7] = 1;
			break;
		case 6 :
			da[1] = da[2] = da[3] = da[6] = da[5] = da[7] = 1;
			break;
		case 7 :
			da[1] = da[2] = da[4] = da[5] = 1;
			break;
		case 8 :
			da[1] = da[2] = da[3] = da[4] = da[5] = da[6] = da[7] = 1;
			break;
		case 9 :
			da[1] = da[2] = da[4] = da[5] = da[6] = da[7] = 1;
			break;
	}
}

void AOIfigureDraw(short sx, short sy, short figure)		/* ２桁の数字を１２ドットで表示します */
{
	short	a[8];
	short	fig[3];		/* fig[1] : 10の位		fig[2] : 1の位 */
	short	i, j;
	
	for (i = 0; i <= 7; i++) a[i] = 0;
	for (i = 0; i <= 2; i++) fig[i] = 0;

	if (figure < 0) figure = 0;	/* 負の数は０にする */

	fig[1] = figure / 10;
	fig[2]  = figure - fig[1] * 10;
	for (i = 1; i != 3; i++) {
		_AOIfigureDrawSet(&fig[i], a);
		
		if (i == 2) sx += 10;
		
		AOIline(sx + 1, sy     , sx + 5, sy     , a[1] == 1 ? 15 : 8);
		AOIline(sx    , sy     , sx    , sy +  5, a[2] == 1 ? 15 : 8);
		AOIline(sx    , sy +  7, sx    , sy + 12, a[3] == 1 ? 15 : 8);
		AOIline(sx + 6, sy     , sx + 6, sy +  5, a[4] == 1 ? 15 : 8);
		AOIline(sx + 6, sy +  7, sx + 6, sy + 12, a[5] == 1 ? 15 : 8);
		AOIline(sx + 1, sy +  6, sx + 5, sy +  6, a[6] == 1 ? 15 : 8);
		AOIline(sx + 1, sy + 12, sx + 5, sy + 12, a[7] == 1 ? 15 : 8);
		for (j = 0; j <= 7; j++) a[j] = 0;
	}
}

void AOIfigureDraw2(short sx, short sy, int figure)	/* ２桁の数字を２５ドットで表示します */
{
	short	a[8];
	short	fig[3];		/* fig[1] : 10の位		fig[2] : 1の位 */
	short	i, j, k;
	short	m = 0, n;
	
	for (i = 0; i <= 7; i++) a[i] = 0;
	for (i = 0; i <= 2; i++) fig[i] = 0;

	if (figure < 0) figure = 0;	/* 負の数は０にする */

	fig[1] = figure / 10;
	fig[2] = figure - fig[1] * 10;
	for (i = 1; i != 3; i++) {
		_AOIfigureDrawSet(&fig[i], a);
		
		if (i == 2) sx += 20;
		for (k = 0; k < 4; k++) {
			sx += k;
			if (k == 0 || k == 3) m = 1;
			else m = 0;
			for (n = 0; n < 2; n++) {
				AOIline(sx + n * 11, sy +  3 + m, sx + n * 11, sy + 11 - m,	a[2 + n * 2] == 1 ? 14 : 8);
				AOIline(sx + n * 11, sy + 14 + m, sx + n * 11, sy + 22 - m,	a[3 + n * 2] == 1 ? 14 : 8);
			}
			sx -= k;
		}
		for (k = 0; k < 4; k++) {
			sy += k;
			if (k == 0 || k == 3) m = 1;
			else m = 0;
			AOIline(sx + 3 + m, sy     , sx + 11 - m, sy     , a[1] == 1 ? 14 : 8);
			AOIline(sx + 3 + m, sy + 11, sx + 11 - m, sy + 11, a[6] == 1 ? 14 : 8);
			AOIline(sx + 3 + m, sy + 22, sx + 11 - m, sy + 22, a[7] == 1 ? 14 : 8);
			sy -= k;
		}
		for (j = 0; j <= 7; j++) a[j] = 0;
	}
}

short _AOIrandom(short max)	/* 0〜99までの特殊用途乱数発生 */
{	
	short	max10, max1;
	short	ret10, ret1;
	
	max10 = max / 10;
	do {
		ret10 = (rand() % 10);
	} while (max10 < ret10);
	ret1 = (rand() % 10);
	if (max10 == ret10) {
		max1 = max - max10 * 10;
		while (max1 < ret1) {
			ret1 = (rand() % 10);
		}
	}
	return ret10 * 10 + ret1;
}

long AOIrandom(long max)	/* 0〜999999までの乱数発生 */
{
	time_t *t = 0;
	
	srand((unsigned int)time(t));
	if (max > 9999) {
		return _AOIrandom(max / 10000) * 10000
			 + _AOIrandom(99) * 100
			 + _AOIrandom(99);
	}
	if (max > 99) {
		return _AOIrandom(max / 100) * 100
			 + _AOIrandom(99);
	}
	return _AOIrandom(99);
}

int AOIbuttonLamp(short sx, short sy, AOIbuttonData *Data)	/* ボタン部分を反転 */
{
	AOIbuttonData Dt = *Data;
	
	return	EGB_writeMode(work, 4)
		|	MOS_disp(0)
		|	AOIboxFull(sx + Dt.x, sy + Dt.y, sx + Dt.x + Dt.lx, sy + Dt.y + Dt.ly, 15)
		|	MOS_disp(1)
		|	EGB_writeMode(work, 0);
}

int AOIslantingBox(short x0, short y0, short x1, short y1, short lx, short cl)
{
	if (x0 == x1) return AOIbox(x0, y0, x0 + lx, y1, cl);
	if (y0 == y1) return AOIline(x0, y0, x1 + lx, y0, cl);

	if (y0 > y1) {
		y1 ^= y0;	y0 ^= y1;	y1 ^= y0;
		x1 ^= x0;	x0 ^= x1;	x1 ^= x0;
	}
	
	EGB_color(work, 0, cl);
	WORD(para +  0) = 5;
	WORD(para +  2) = x0;
	WORD(para +  4) = y0;
	WORD(para +  6) = x1;
	WORD(para +  8) = y1;
	WORD(para + 10) = x1 + lx;
	WORD(para + 12) = y1;
	WORD(para + 14) = x0 + lx;
	WORD(para + 16) = y0;
	WORD(para + 18) = x0;
	WORD(para + 20) = y0;
	return EGB_connect(work, para);
	
}

int AOIslantingBoxFull(short x0, short y0, short x1, short y1, short lx, short cl)
{
	short dmmy_x0;
	
	if (x0 == x1) return AOIbox(x0, y0, x0 + lx, y1, cl);
	if (y0 == y1) return AOIline(x0, y0, x1 + lx, y0, cl);

	if (y0 > y1) {
		y1 ^= y0;	y0 ^= y1;	y1 ^= y0;
		x1 ^= x0;	x0 ^= x1;	x1 ^= x0;
	}
	
	EGB_color(work, 0, cl);
	WORD(para +  0) = 2;
	dmmy_x0 = x0 + lx;
	for (;x0 <= dmmy_x0; x0++, x1++) {
		WORD(para +  2) = x0;
		WORD(para +  4) = y0;
		WORD(para +  6) = x1;
		WORD(para +  8) = y1;
		EGB_connect(work, para);
	}
	return 1;
}

#if !defined(__GNUC__)

int AOIpadCheck(int *status)	/* パッドの押されたボタン番号を返します｡ */
{
	int old = 0;
	
	SND_joy_in_2(0, status);
	old = *status;
	if ((*status |   1) != old) return 0;
	if ((*status |   2) != old) return 1;
	if ((*status |   4) != old) return 2;
	if ((*status |   8) != old) return 3;
	if ((*status |  16) != old) return 4;
	if ((*status |  32) != old) return 5;
	if ((*status |  64) != old) return 6;
	if ((*status | 128) != old) return 7;
	return -1;
}

int AOIpadCheck2(int *status, int a[])
{
	int old = 0;
	int i;
	
	for (i = 0; i < 8; i++) a[i] = 0;	/* 配列初期化 */
	SND_joy_in_2(0, status);
	old = *status;
	if ((*status |   1) != old) a[0] = 1;
	if ((*status |   2) != old) a[1] = 1;
	if ((*status |   4) != old) a[2] = 1;
	if ((*status |   8) != old) a[3] = 1;
	if ((*status |  16) != old) a[4] = 1;
	if ((*status |  32) != old) a[5] = 1;
	if ((*status |  64) != old) a[6] = 1;
	if ((*status | 128) != old) a[7] = 1;
	return TRUE;
}

#endif

void ank2sjis(char string[], char str[])
{
	short	i = 0, j = 0;
	char	sjiscode[257][3] = {
		"　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　",
		"　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　",
		"　", "！", "”", "＃", "＄", "％", "＆", "’", "（", "）", "＊", "＋", "，", "−", "．", "／",
		"０", "１", "２", "３", "４", "５", "６", "７", "８", "９", "：", "；", "＜", "＝", "＞", "？",
		"＠", "Ａ", "Ｂ", "Ｃ", "Ｄ", "Ｅ", "Ｆ", "Ｇ", "Ｈ", "Ｉ", "Ｊ", "Ｋ", "Ｌ", "Ｍ", "Ｎ", "Ｏ",
		"Ｐ", "Ｑ", "Ｒ", "Ｓ", "Ｔ", "Ｕ", "Ｖ", "Ｗ", "Ｘ", "Ｙ", "Ｚ", "〔", "￥", "〕", "＾", "＿",
		"’", "ａ", "ｂ", "ｃ", "ｄ", "ｅ", "ｆ", "ｇ", "ｈ", "ｉ", "ｊ", "ｋ", "ｌ", "ｍ", "ｎ", "ｏ",
		"ｐ", "ｑ", "ｒ", "ｓ", "ｔ", "ｕ", "ｖ", "ｗ", "ｘ", "ｙ", "ｚ", "｛", "｜", "｝", "￣", "　",
		"　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　",
		"　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　",
		"　", "。", "「", "」", "、", "・", "ヲ", "ァ", "ィ", "ゥ", "ェ", "ォ", "ャ", "ュ", "ョ", "ッ",
		"ー", "ア", "イ", "ウ", "エ", "オ", "カ", "キ", "ク", "ケ", "コ", "サ", "シ", "ス", "セ", "ソ",
		"タ", "チ", "ツ", "テ", "ト", "ナ", "ニ", "ヌ", "ネ", "ノ", "ハ", "ヒ", "フ", "ヘ", "ホ", "マ",
		"ミ", "ム", "メ", "モ", "ヤ", "ユ", "ヨ", "ラ", "リ", "ル", "レ", "ロ", "ワ", "ン", "゛", "゜",
		"　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　",
		"　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　", "　"
		};
		
	while (str[i] != '\0') {
		if ((0x81 <= str[i] && str[i] <= 0x9f) || (0xe0 <= str[i] && str[i] <= 0xef)) {	// 全角のとき
			string[j++] = str[i++];
			string[j++] = str[i++];
		} else {	// 半角のとき
			string[j++] = sjiscode[str[i]][0];
			string[j++] = sjiscode[str[i]][1];
			i++;
		}
	}
	string[j] = '\0';
}


/* Oh!FMTOWNS 1992/3より抜粋 */
unsigned int sjisToJis(unsigned int sjis)
{
	unsigned int	h, l;
	
	h = (sjis >> 8) & 0xff;
	l = sjis & 0xff;
	
	if ( ((h > 0x9f) && (h < 0xe0)) || (h < 0x81) || (h > 0xef) || (l < 0x40) || (l > 0xfe) || (l == 0x7f) ) {
		return 0x2129;
	}
	if (l > 0x7f) l--;
	h -= (h <= 0x9f) ? 0x70 : 0xb0;
	h <<= 1;
	if (l >= 0x9e) l -= 0x5e + 0x1f;
	else {
		l -= 0x1f;
		h--;
	}
	
	return (h << 8) + l;
}

long _AOIgetFontAddress(char st[], short fs)
{				/* 文字列 st の先頭文字のﾌｫﾝﾄﾌｧｲﾙ上の番地を返す */
	short	i = 0, k = 0;
	long	j;
	int	jisCode;
	int	address;	/* 表示する文字のﾌｫﾝﾄﾌｧｲﾙ上の番地 */
	long	firstAdr;	/* ﾌｫﾝﾄﾌｧｲﾙ上で全角文字が始まる番地 */
	long	stepAdr;	/* ﾌｫﾝﾄﾌｧｲﾙ上の次のﾌﾞﾛｯｸまでのバイト数 */
	long	fontS;		/* 1文字分のバイト数 */
	jisCode = sjisToJis((st[0] << 8) + st[1]);	
				/* 文字列 st の先頭文字のJISコードを得る */
	do {			/* 文字列 st を1文字分ずらす */
		st[i]     = st[i + 2];
		st[i + 1] = st[i + 3];
		i += 2;
	} while (st[i] != '\0');

	if (fs == 16 && jisCode >= 0x5020) return 3072;
				/* 12ドットの第２水準は空白置き換え */
	switch (fs) {
		case 64 :	/* 60ドットファイルの各種番地 */
			firstAdr = 61440;
			stepAdr  = 45120;
			fontS = 480;
			break;
		case 48 :	/* 48ドットファイルの各種番地 */
			firstAdr = 36880;
			stepAdr  = 27072;
			fontS = 288;
			break;
		case 32 :	/* 32ドットファイルの各種番地 */
			firstAdr = 16400;
			stepAdr  = 12032;
			fontS = 128;
			break;
		case 24 : 	/* 24ドットファイルの各種番地 */
			firstAdr = 0x12;
			stepAdr  = 0x1B2C;
			fontS = 0x47 + 3;
			break;
		case 16 :	/* 12ドットファイルの各種番地 */
			firstAdr = 3072;
			stepAdr  = 2256;
			fontS = 24;
			break;
	}
				/* ﾌｫﾝﾄﾌｧｲﾙ上の番地を計算する */
	for (j = 0x2120; j < 0x7427; j += 0x100) {
		if (j <= jisCode && jisCode <= j + 0x5f) {
			address = firstAdr  + fontS * (jisCode - j - 1) + stepAdr * k;
			j = 0xFFFF;
		}
		k++;
	}
	
	return address;
}

void AOIstrFJ2(FILE *fp, short fs, short sx, short sy, char st[], short gap)
{						/* 全角の文字列を fp で示されるフォントで表示する */
	short	lenSt;		/* 残りの文字数 */
	short	fs2;		/* 8で割り切れない大きさのフォント用のダミー */
	char	*data;		/* フォントデータ格納番地 */

	fs2 = fs;
	if (fs == 60) {		/* 60ドットは64ドットとして処理 */
		fs2 = fs = 64;
		gap -= 4;
	}
	if (fs == 12) {		/* 12ドットは一部16ドットとして処理 */
		fs = 16;
		gap -= 4;
	}
	lenSt = (short)strlen(st) >> 1;
							/* lenSt に文字数を代入(全部全角文字と考え２で割っている) */
	fseek(fp, _AOIgetFontAddress(st, fs), SEEK_SET);	/* 得られた番地までシークする */
	data = (char *)malloc(fs2 * fs * sizeof(char));		/* フォントデータ格納領域確保 */
	while (!feof(fp) && lenSt) {						/* 残りの文字数が０になるまで */
		fread(data, sizeof(char), (fs >> 3) * fs2, fp);	/* ﾌｫﾝﾄﾃﾞｰﾀを１文字分読み込む */
		AOIanyChar(sx, sy, fs2, fs2, data);			/* 1文字描画 */
		sx += (fs + gap);
		lenSt--;		/*残りの文字数を1減らす */
		fseek(fp, _AOIgetFontAddress(st, fs), SEEK_SET);/* 得られた番地までシークする */
	}
	free(data);											/* フォントデータ格納領域解放 */
}

void AOIstring_FJ2(FILE *fp, short fs, short sx, short sy, char st[], short sc, short gap)
{
	EGB_color(work, 0, sc);
	EGB_textZoom(work, 1, fs, fs);
	AOIstrFJ2(fp, fs, sx, sy, st, gap);
}

void AOIstrFJ2Zoom(FILE *fp, short fs, short sx, short sy, char st[], short sc, short gap, short zx, short zy)
{
	EGB_color(work, 0, sc);
	EGB_textZoom(work, 1, zx, zy);
	AOIstrFJ2(fp, fs, sx, sy, st, gap + (zx - fs));
}

/*
short _AOIstrFJ2setup_FontSearch(char fj2File[])	// HD と CDROMにあるFJ2フォントを捜索します。(-_-;)
{
	char	fj2Drv[][3] = {"d:", "e:", "f:", "g:", "h:", "i:", "j:", "k:", "l:", "m:", "n:", "o:", "p:", "q:"};
	char	fj2FileName[50];
	short	drvNum, endMark, retFntFileChk;
	
	strcpy(fj2FileName, fj2File);
	fj2File[0] = '\0';
	endMark = 0;
	drvNum = 0;
	do {
		strcpy(fj2File, fj2Drv[drvNum]);
		strcat(fj2File, fj2FileName);
		retFntFileChk = _access(fj2File, 1);
//		printf("%s\n", fj2File);
//		printf("%d\n", retFntFileChk);
		drvNum++;
		if (drvNum == 15) endMark = 1;
	} while (retFntFileChk != 0 && endMark != 1);
	
	if (endMark == 1) {
		return FALSE;
	} else {
		return TRUE;
	}
}

enum {MINN, GOTH, MARU, KYOU, MOUH};

int _AOIstrFJ2setup_FontNameMake(char fj2File[], short fontSize)
{
	if (_AOIstrFJ2setup_FontSearch(fj2File) == FALSE) {	// fj2フォントの居所が不明のときは、FALSEを返す
		return FALSE;
	}

}

int AOIstrFJ2setup(FILE *fp, short fontName, short fontSizeX, short fontSizeY, short fontStyle, short color)
{
	char	fj2File[50];
	char	string[256];
	FILE	*fp1;
	
	if (_AOIstrFJ2setup_FontNameMake(fj2File, maxof(fontSizeX, fontSizeY)) == FALSE) {
		// fj2フォントの居所が不明のときは、FALSEを返す
		return FALSE;
	}

	EGB_textZoom(work, 1, fontSizeX, fontSizeY)
	EGB_textZoom(work, 0, fontSizeX, fontSizeY)
	EGB_fontStyle(work, fontStyle)
	EGB_color(work, 0, color);
	

}

void AOIstrFJ2(FILE *fp, short fs, short sx, short sy, char st[], short gap)
{						// 全角の文字列を fp で示されるフォントで表示する
	short	lenSt;		// 残りの文字数
	short	fs2;		// 8で割り切れない大きさのフォント用のダミー
	char	*data;		// フォントデータ格納番地

	fs2 = fs;
	if (fs == 60) {		// 60ドットは64ドットとして処理
		fs2 = fs = 64;
		gap -= 4;
	}
	if (fs == 12) {		// 12ドットは一部16ドットとして処理
		fs = 16;
		gap -= 4;
	}
	lenSt = (short)strlen(st) >> 1;
							// lenSt に文字数を代入(全部全角文字と考え２で割っている)
	fseek(fp, _AOIgetFontAddress(st, fs), SEEK_SET);	// 得られた番地までシークする
	data = (char *)malloc(fs2 * fs * sizeof(char));		// フォントデータ格納領域確保
	while (!feof(fp) && lenSt) {						// 残りの文字数が０になるまで
		fread(data, sizeof(char), (fs >> 3) * fs2, fp);	// ﾌｫﾝﾄﾃﾞｰﾀを１文字分読み込む
		AOIanyChar(sx, sy, fs2, fs2, data);			// 1文字描画
		sx += (fs + gap);
		lenSt--;		//残りの文字数を1減らす
		fseek(fp, _AOIgetFontAddress(st, fs), SEEK_SET);// 得られた番地までシークする
	}
	free(data);											// フォントデータ格納領域解放
}
*/


short _AOIsystem12FontSetup(char fnt12File[])
// ハードディスクにある12ドットフォントを捜索します。(-_-;)
{
	char	fnt12Drv[][3] = {"d:", "e:", "f:", "g:", "h:", "i:", "j:", "k:", "l:", "m:", "n:", "o:", "p:"};
	char	fnt12FileName[] = "\\sysinit\\system.fnt";
	short	drvNum, endMark, retFntFileChk;
	
	endMark = 0;
	drvNum = 0;
	do {
		strcpy(fnt12File, fnt12Drv[drvNum]);
		strcat(fnt12File, fnt12FileName);
		retFntFileChk = _access(fnt12File, 1);
//		printf("%s\n", fnt12File);
//		printf("%d\n", retFntFileChk);
		drvNum++;
		if (drvNum == 14) endMark = 1;
	} while (retFntFileChk != 0 && endMark != 1);
	
	if (endMark == 1) {
		return FALSE;
	} else {
		return TRUE;
	}
}

void AOIsystem16Font(short x, short y, short c, char *str)
{
	AOIstringData	AstringD;

	AstringD.sx = 12;
	AstringD.sy = 12;
	AstringD.ax =  6;
	AstringD.ay = 12;
	AstringD.sc = c;
	AstringD.ss = 0x0;
	AOIstring(x, y, &AstringD, str);
}

void AOIsystem12Font(short x, short y, short c, char *str)
{
	char	fnt12File[50];
	char	string[256];
	FILE	*fp1;
	
	if (_AOIsystem12FontSetup(fnt12File) == FALSE) {	// 12ドットフォントの居所が不明のときは、16ドットで代用
		AOIsystem16Font(x, y + 12, c, str);
		return;
	}
//	printf("%s", fnt12File);
	if ((fp1 = fopen(fnt12File, "rb")) == NULL) {
		perror("system.fnt のオープンを失敗しました");
	} else {
		ank2sjis(string, str);
		AOIstring_FJ2(fp1, 12, x, y + 12, string, c, 0);
	}
	
	fclose(fp1);
	return;
}

int AOIgetGraph(char *buf, short x0, short y0, short x1, short y1)	/* 画像データ画面の読み込み */
{
	DWORD(para +  0) = (unsigned int)buf;
	WORD (para +  4) = 0x14 /* getds() */;
	WORD (para +  6) = x0;
	WORD (para +  8) = y0;
	WORD (para + 10) = x1;
	WORD (para + 12) = y1;
	return EGB_getBlock(work, para);			/* ドットデータの読み込み１ */

}

int AOIgetGraphZoom(char *buf, short x0, short y0, short x1, short y1, short xs, short ys)	/* 画像データ画面の読み込み */
{
	DWORD(para +  0) = (unsigned int)buf;
	WORD (para +  4) = 0x14 /* getds() */;
	WORD (para +  6) = x0;
	WORD (para +  8) = y0;
	WORD (para + 10) = x1;
	WORD (para + 12) = y1;
	WORD (para + 14) = xs;
	WORD (para + 16) = ys;
	return EGB_getBlock(work, para);			/* ドットデータの読み込み2 */

}

int AOIputGraph(char *buf, short x0, short y0, short x1, short y1)	/* 画像データ画面の書き込み */
{
	DWORD(para +  0) = (unsigned int)buf;
	WORD (para +  4) = 0x14 /* getds() */;
	WORD (para +  6) = x0;
	WORD (para +  8) = y0;
	WORD (para + 10) = x1;
	WORD (para + 12) = y1;
	return EGB_putBlock(work, 0, para);		/* ドットデータの書き込み１ */
}

int AOIputGraphZoom(char *buf, short x0, short y0, short x1, short y1, short xs, short ys)
	/* 画像データ画面の書き込み */
{
	DWORD(para +  0) = (unsigned int)buf;
	WORD (para +  4) = 0x14 /* getds() */;
	WORD (para +  6) = x0;
	WORD (para +  8) = y0;
	WORD (para + 10) = x1;
	WORD (para + 12) = y1;
	WORD (para + 14) = xs;
	WORD (para + 16) = ys;
	return EGB_putBlockZoom(work, 0, para);		/* ドットデータの書き込み2 */
}

void AOIwindowMove(char *dispTmp, short *sx, short *sy, short lx, short ly)	/* ウインドゥ移動 */
{
	//	バグっています AOIwindowMove2を使用せよ
	int	ch, mx, my;
	int dx, dy;
	char dispTmp2[(640 / 8) * 480];				/* ドットデータの読み書き作業領域 */

	MOS_disp(0);
	AOIgetGraph(dispTmp2, *sx, *sy, *sx + lx + 7, *sy + ly + 7);
	AOIputGraph(dispTmp, *sx, *sy, *sx + lx + 7, *sy + ly + 7);
	MOS_typeRom(83 + 1, 8, 8, pat_work);	/* マウスを手の平に */
	MOS_disp(1);
	EGB_writeMode(work, 4);
	EGB_linePattern(work, 1, 0x55555555);

	MOS_rdpos(&ch, &mx, &my);
	dx = mx - *sx;
	dy = my - *sy;

	do {
		MOS_rdpos(&ch, &mx, &my);
		*sx = mx - dx;
		*sy = my - dy;
		MOS_disp(0);
		AOIbox(*sx, *sy, *sx + lx, *sy + ly, WHITE);
		AOIbox(*sx, *sy, *sx + lx, *sy + ly, WHITE);
		MOS_disp(1);
	} while (ch == 1);
	
	EGB_linePattern(work, 1, 0xffffffff);
	EGB_writeMode(work, 0);
	MOS_disp(0);
	MOS_typeRom(80 + 1, 0, 0, pat_work);	/* マウスを元通り */
	AOIgetGraph(dispTmp, *sx, *sy, *sx + lx + 7, *sy + ly + 7);
	AOIputGraph(dispTmp2, *sx, *sy, *sx + lx + 7, *sy + ly + 7);
	MOS_disp(1);
}

void AOIwindowMove2(char *winData, char *dispTmp, int *sx, int *sy, int lx, int ly, int sd)	/* ウインドゥ移動 */
{
	//	winData : ウィンドウのビットマップデータ
	//	dispTmp : ウィンドウ表示前の退避ビットマップデータ
	//	sx sy : ウィンドウ開始座標
	//	lx ly : ウィンドウ大きさ
	//	sd    : ウィンドウの影の大きさ

	int		ch, mx, my;
	int		dx, dy;

	MOS_disp(0);
	AOIputGraph(dispTmp, *sx, *sy, *sx + lx + sd, *sy + ly + sd);
	MOS_typeRom(83 + 1, 8, 8, pat_work);	/* マウスを手の平に */
	MOS_disp(1);
	EGB_writeMode(work, 4);
	EGB_linePattern(work, 1, 0x55555555);

	MOS_rdpos(&ch, &mx, &my);
	dx = mx - *sx;
	dy = my - *sy;

	do {
		MOS_rdpos(&ch, &mx, &my);
		*sx = mx - dx;
		*sy = my - dy;
		MOS_disp(0);
		AOIbox(*sx, *sy, *sx + lx, *sy + ly, WHITE);
		AOIbox(*sx, *sy, *sx + lx, *sy + ly, WHITE);
		MOS_disp(1);
	} while (ch == 1);
	
	EGB_linePattern(work, 1, 0xffffffff);
	EGB_writeMode(work, 0);
	MOS_disp(0);
	MOS_typeRom(80 + 1, 0, 0, pat_work);	/* マウスを元通り */
	AOIgetGraph(dispTmp, *sx, *sy, *sx + lx + sd, *sy + ly + sd);
	EGB_writeMode(work, 7);
	EGB_pastel(work, 128);
	AOIboxFull(*sx + sd, *sy + ly + 1, *sx + lx + sd, *sy + ly + sd, 0);
	AOIboxFull(*sx + lx + 1, *sy + sd, *sx + lx + sd, *sy + ly, 0);
	EGB_writeMode(work, 0);
	AOIputGraph(winData, *sx, *sy, *sx + lx, *sy + ly);
	MOS_disp(1);
}

int AOIgetColorMode(void)
{
	int		p0Reso, p1Reso, Reso;

	EGB_getResolution(&p0Reso, &p1Reso);
 	if (EGB_getWritePage(0, 0) == 0) Reso = p0Reso;
 	else Reso = p1Reso;
 	switch (Reso) {
 		case 1..4 : 
 			Reso = R16;
 			break;
 		case 5..11 :
 		case 15..18 :
 			Reso = R32k;
 			break;
 		case 12..14 :
 			Reso = R256;
 			break;
 	}
 	
 	return Reso;
}

int	AOImessageBox(int x, int y, int fontSize, char title[], int num, char mes[][AOImbMaxSize], int attr)
{
	/*	x : 中心X座標  y : 中心Y座標  */
	
	int		maxStrLen = 0, dmmyLen;
	int		winLenX, winLenY;
	int		winSx, winSy;	// ウィンドウ開始座標
	int		winSAx, winSAy;	// ウィンドウ作業エリア開始座標
	int		winGap = 5;
	int		winSd = 7;		// ウィンドウの影の大きさ
	int		btnSize;
	char	btnStr1[256];
	char	btnStr2[256];
	int		retID, retID_1, retID_2;
	int		ch, mx, my, button;
 	int		endmark = 0;
//	int		buttonLamp = OFF;
	int		clmode;
	char 	*dispTmp;				//  ドットデータの読み書き作業領域
	char 	*winData;				//  ウィンドウ保存用
	AOIstringData	AstringD;
	AOIbuttonData	btnMB[BUTTON];

	MOS_disp(0);

	clmode = AOIgetColorMode();

	AstringD.sx = fontSize;
	AstringD.sy = fontSize;
	AstringD.ax = fontSize / 2;
	AstringD.ay = fontSize;
	AstringD.sc = 0;
	AstringD.ss = 0x00;

	btnSize = fontSize + 2;
	
	for (int i = 0; i < num; i++) {
		dmmyLen = strlen(mes[i]);
		if (dmmyLen > maxStrLen) maxStrLen = dmmyLen;
	}
	dmmyLen = strlen(title);
	if (dmmyLen > maxStrLen) maxStrLen = dmmyLen;

	winLenX = (maxStrLen + 6) * (fontSize / 2);
	winLenY = (num + 5) * fontSize;
	if (winLenX < 150) winLenX = 150;
	
	winSx = x - winLenX / 2;
	winSy = y - winLenY / 2;
	winSAx = winSx + winGap;
	winSAy = winSy + winGap;

	dispTmp = (char *)malloc((winLenX + winSd + 1) * (winLenY + winSd + 1) * 2);	/* データ格納領域確保 */
	AOIgetGraph(dispTmp, winSx, winSy, winSx + winLenX + winSd, winSy + winLenY + winSd);	//	画面退避

	if (clmode == R32k) {
		AOIwinBase32k(winSx, winSy, winLenX, winLenY, winSd);
	} else {
		AOIwinBase2(winSx, winSy, winLenX, winLenY, winSd);
	}
	
	//	クローズボタン
	if (clmode == R32k) {
		AOIbuttonBase32k(winSAx, winSAy, btnSize, btnSize);
	} else {
		AOIbuttonBase2(winSAx, winSAy, btnSize, btnSize);
	}
	if (clmode == R32k) {
		AstringD.sc = 0x7fff;
	} else {
		AstringD.sc = 0xf;
	}
	AOIstring(winSAx + 2, winSAy + fontSize, &AstringD, "造");
	AOIsetButton(&btnMB[1], winGap, winGap, btnSize, btnSize);
	AstringD.sc = 0;

	//	タイトルボタン
	if (clmode == R32k) {
		AOIbuttonBase32k(winSAx + btnSize + 1, winSAy, winLenX - (winGap * 2 + btnSize + 1), btnSize);
	} else {
		AOIbuttonBase2(winSAx + btnSize + 1, winSAy, winLenX - (winGap * 2 + btnSize + 1), btnSize);
	}
	AOIstring(winSAx + fontSize * 2, winSAy + fontSize + 1, &AstringD, title);
	AOIsetButton(&btnMB[2], winGap + btnSize + 1, winGap, winLenX - (winGap * 2 + btnSize + 1), btnSize);
	
	for (i = 0; i < num; i++) {
		AOIstring(winSAx + fontSize, winSAy + fontSize * 5 / 2 + i * fontSize * 5 / 4, &AstringD, mes[i]);
	}
	
	switch (attr) {		//	第一ボタンの文字列設定
		case MB_OK:
		case MB_OKCANCEL:
			strcpy(btnStr1, " ＯＫ");
			retID_1 = ID_OK;
			break;
		case MB_YESNO:
			strcpy(btnStr1, " Ｙes");
			retID_1 = ID_YES;
			break;
		default:
			strcpy(btnStr1, "エラー");
			retID_1 = ID_ERR;
			break;
	}
	switch (attr) {		//	第二ボタンの文字列設定
		case MB_OKCANCEL:
			strcpy(btnStr2, "Cancel");
			retID_2 = ID_CANCEL;
			break;
		case MB_YESNO:
			strcpy(btnStr2, " Ｎo");
			retID_2 = ID_NO;
			break;
		default:
			strcpy(btnStr2, "エラー");
			retID_2 = ID_ERR;
			break;
	}

	//	第一ボタン
	if (clmode == R32k) {
		AOIbuttonBase32k(winSAx + winLenX / 2 - fontSize * 4, winSy + winLenY - fontSize * 2, fontSize * 3, btnSize);
	} else {
		AOIbuttonBase2(winSAx + winLenX / 2 - fontSize * 4, winSy + winLenY - fontSize * 2, fontSize * 3, btnSize);
	}
	AOIstring(winSAx + winLenX / 2 - fontSize * 4, winSy + winLenY - fontSize + 1, &AstringD, btnStr1);
	AOIsetButton(&btnMB[3], winGap + winLenX / 2 - fontSize * 4, winLenY - fontSize * 2, fontSize * 3, btnSize);
	
	//	第二ボタン
	if (clmode == R32k) {
		AOIbuttonBase32k(winSAx + winLenX / 2 + fontSize / 2, winSy + winLenY - fontSize * 2, fontSize * 3, btnSize);
	} else {
		AOIbuttonBase2(winSAx + winLenX / 2 + fontSize / 2, winSy + winLenY - fontSize * 2, fontSize * 3, btnSize);
	}
	AOIstring(winSAx + winLenX / 2 + fontSize / 2, winSy + winLenY - fontSize + 1, &AstringD, btnStr2);
	AOIsetButton(&btnMB[4], winGap + winLenX / 2 + fontSize / 2, winLenY - fontSize * 2, fontSize * 3, btnSize);
	
	winData = (char *)malloc((winLenX + 1) * (winLenY + 1) * 2);	/* データ格納領域確保 */
	AOIgetGraph(winData, winSx, winSy, winSx + winLenX, winSy + winLenY);	//	描き上がったウィンドウを保存

	MOS_disp(1);

	do {
		MOS_rdpos(&ch, &mx, &my);
		button = 0;
		if (ch == 1) button = AOIbutton(winSx, winSy, mx, my, btnMB);	// マウスの入力
/*
		if (button > 0) {			// ボタン反転
			AOIbuttonLamp(winSx, winSy, &btnMB[button]);
			buttonLamp = ON;
			MOS_typeRom(115 + 1, 0, 0, pat_work);	// マウスも変更
			if (button == 1) {
				MOS_typeRom(124 + 1, 0, 0, pat_work);
			}
		}
*/
		switch (button) {
			case 1 :
				endmark = 1;		// 終了条件設定
				retID = ID_ERR;
				break;
			case 2 :				// 移動
				AOIwindowMove2(winData, dispTmp, &winSx, &winSy, winLenX, winLenY, winSd);
				break;
			case 3 :
				endmark = 1;		// 値を返す
				retID = retID_1;
				break;
			case 4 :
				endmark = 1;		// 値を返す
				retID = retID_2;
				break;
		}
/*
		if (buttonLamp == ON) {							// ボタンの反転
			if (button > 0) {
				//AOIbuttonLamp(winSx, winSy, &btnMB[button]);
				buttonLamp = OFF;
				MOS_typeRom(80 + 1, 0, 0, pat_work);	// マウスも元通り
			}
		}
*/
	} while (endmark != 1);

	MOS_disp(0);
	AOIputGraph(dispTmp, winSx, winSy, winSx + winLenX + winSd, winSy + winLenY + winSd);	//	退避画面復活
	MOS_disp(1);

	return retID;
}

int AOItiff(int sx, int sy, char filename[100])	/* 廃止    拡張ライブラリIの方を使用せよ */
{
	FILE	*fp;
	char	*data;
	short	x[1], y[1], bit[1];
	long	allBit;
	
	if ((fp = fopen(filename, "rb")) == NULL) {		/* ファイルオープン */
		perror("ファイルのオープンを失敗しました");
		return FALSE;
	}
	fseek(fp, 0x001e, SEEK_SET);	fread(x, sizeof(short), 1, fp);			/* ドット数読み込み */
	fseek(fp, 0x002a, SEEK_SET);	fread(y, sizeof(short), 1, fp);
	fseek(fp, 0x0036, SEEK_SET);	fread(bit, sizeof(short), 1, fp);		/* ビット数読み込み */
/* printf("%d %d %d", x[0], y[0], bit[0]); */
	switch (bit[0]) {
		case 4 :
			allBit = x[0] * y[0] / 2;	break;
		case 8 :
			allBit = x[0] * y[0];		break;
		case 16 :
			allBit = x[0] * y[0] * 2;	break;
	}
	data = (char *)malloc(allBit * sizeof(char));	/* データ格納領域確保 */
	fseek(fp, 0x0200, SEEK_SET);	fread(data, sizeof(char), allBit, fp);	/* 画像読み込み */
	free(data);										/* 動的に確保した領域の開放 */
	fclose(fp);
	DWORD(para +  0) = (unsigned int)data;
	WORD (para +  4) = 0x14;
	WORD (para +  6) = sx;
	WORD (para +  8) = sy;
	WORD (para + 10) = sx + x[0] - 1;
	WORD (para + 12) = sx + y[0] - 1;
	return EGB_putBlock(work, 0, para);	/* 画像描画 */
}

void AOIwide17(void)		/* WIDE display set */
/* 戸田 氏 の ゴットトランスファー の ソースリスト を参照 */
{
	int	i;
	int	para[10][2] = {
				{  0,  80 },	/* HSW1 */
				{  1, 590 },	/* HSW2 */
				{  4, 669 },	/* HST  */
				{ 29,   3 },	/* CKI  */
				{  9, 130 },	/* HDS0 */
				{ 11, 130 },	/*    1 */
				{ 18, 130 },	/* HAJ0 */
				{ 22, 130 },	/*    1 */
				{ 10, 642 },	/* HDE0 */
				{ 12, 642 }		/*    1 */
	};

	for(i = 0; i < 10; i++) {
		_outw(0x440, para[i][0]);
		_outw(0x442, para[i][1]);
	}
}

void AOIwide3(void)		/* WIDE display set */
{						/* 左にズレるバグあり */
	int i;
	int para[14][2] = {
				{  0, 110 },	/* HSW1 */
				{  1, 802 },	/* HSW2 */
				{  4, 909 },	/* HST  */
				{ 29,   0 },	/* CKI  */
				{ 18, 130 },	/* HAJ0 */
				{ 22, 130 },	/*    1 */

				{  9, 138 },	/* HDS0  */
				{ 11, 138 },	/*    1 */
				{ 10, 898 },	/* HDE0	 */
				{ 12, 898 },	/*    1 */

				{ 13, 53 },	/* VDS0  */
				{ 15, 53 },	/*    1 */
				{ 14, 1200/*106*/ },	/* VDE0	 */
				{ 16, 1200/*106*/ }		/*    1 */
			};

	for(i = 0; i < 14; i++) {
		outpw(0x440, para[i][0]);
		outpw(0x442, para[i][1]);
	}
}

void AOIwide3_2(void)		/* WIDE display set */
{
	int i;
	int para[14][2] = {
				{  0, 0x006e },	/* HSW1 */
				{  1, 0x0322 },	/* HSW2 */
				{  4, 0x038d },	/* HST  */
				{ 29, 0x0000 },	/* CKI  */
				{ 18, 0x008a },	/* HAJ0 */
				{ 22, 0x008a },	/*    1 */

				{  9, 0x0082 },	/* HDS0  */
				{ 11, 0x0082 },	/*    1 */
				{ 10, 0x0382 },	/* HDE0	 */
				{ 12, 0x0382 },	/*    1 */

				{ 13, 0x0035 },	/* VDS0  */
				{ 15, 0x0035 },	/*    1 */
				{ 14, 0x04b0 },	/* VDE0	 */
				{ 16, 0x04b0 }		/*    1 */
			};

	for(i = 0; i < 14; i++) {
		outpw(0x440, para[i][0]);
		outpw(0x442, para[i][1]);
	}
}
void AOIwide3_3(void)		/* WIDE display set */
{
	int i;
	int para[16][2] = {
				{  0, 0x006e },	/* HSW1 */
				{  1, 0x0322 },	/* HSW2 */
				{  4, 0x038d },	/* HST  */
				{  9, 0x0082 },	/* HDS0 */
				{ 10, 0x0382 },	/* HDE0	*/
				{ 11, 0x0082 },	/* HDS1 */
				{ 12, 0x0382 },	/* HDE1 */
				{ 13, 0x0035 },	/* VDS0 */
				{ 14, 0x04b0 },	/* VDE0	*/
				{ 15, 0x0035 },	/* VDS1 */
				{ 16, 0x04b0 },	/* VDE1 */
				{ 18, 0x008a },	/* HAJ0 */
				{ 22, 0x008a },	/* HAJ1 */
				{ 29, 0x0000 },	/* CKI  */
			};

	for(i = 0; i < 14; i++) {
		outpw(0x440, para[i][0]);
		outpw(0x442, para[i][1]);
	}
}

short AOItownsName(void)	/* ＴＯＷＮＳの機種判別 */
{
	short m;
	
	switch (m = _inp(0x31)) {
		case 1..5 :
			break;
		default :
			m = 0;	break;
	}
	return m;	/* 1 : 初代   2 : ２代目   3 : ３代目   4 : IIＵＸ   5 : IIＣＸ */
}



// サンプルプログラムは 13行目の // を取ってください

#ifdef	TEST

void main(void)
{
	AOIstringData	AstringD;	/* AOIstringData型の変数宣言 */
	int		i;
	FILE	*fp;

	EGB_init(work, EgbWorkSize);
	EGB_resolution(work, 0, 3);
	EGB_resolution(work, 1, 3);
	EGB_writePage(work, 0);
	
	AOIsetPalette( 1,  0,  9, 15);
	AOIsetPalette( 2,  0,  8,  0);
	AOIsetPalette( 3,  0,  8,  8);
	AOIsetPalette( 4,  8,  0,  0);
	AOIsetPalette( 5,  8,  0,  8);
	AOIsetPalette( 6, 12, 12, 12);
	AOIsetPalette( 7,  7,  7,  7);
	AOIsetPalette( 8,  2,  2,  2);
	AOIsetPalette( 9, 11, 12, 15);
	AOIsetPalette(10,  0, 15,  0);
	AOIsetPalette(11,  8, 15, 15);
	AOIsetPalette(12, 15,  0,  8);
	AOIsetPalette(13, 15,  0, 15);
	AOIsetPalette(14, 15, 15,  0);
	AOIsetPalette(15, 15, 15, 15);

	for (i = 0; i <= 450; i+= 30) AOIboxFull(0, i, 639, i + 30, i / 30);	

	AstringD.sx = 16;					/* AOIstring使用例 */
	AstringD.sy = 16;
	AstringD.ax =  8;
	AstringD.ay = 16;
	AstringD.sc = 15;
	AstringD.ss = 0x02;
	AOIstring(100, 100, &AstringD, "あいうえおjkjkjk");

	AOIline(0, 0, 639, 399, 6);					/* AOIline使用例 */

	AOIbox(100, 100, 200, 399, 7);				/* AOIbox使用例 */

	AOIboxFull(201, 100, 599, 200, 1);			/* AOIboxFull使用例 */

	if ((fp = fopen("Q:\\fj2\\font\\minn60.fnt", "rb")) == NULL) {
		perror("minn60.fnt のオープンを失敗しました");
	}
	AOIstring_FJ2(fp, 60, 300, 100, "文字列ＡＢＣ", 14, 0);
	fclose(fp);

	AOIwinBase(250, 150, 350, 200, 1);			/* AOIwinBase使用例 */

	AOIbuttonBase(255, 155, 340, 16);		/* AOIbuttonBase使用例 */

	AstringD.sx = 12;					/* AOIstring使用例 */
	AstringD.sy = 12;
	AstringD.ax =  6;
	AstringD.ay = 12;
	AstringD.sc = BLACK;
	AstringD.ss = 0x01;
	AOIstring(300, 168, &AstringD, "ウインドゥの試作版です");

}


#endif