/*
        charactor line display for 16 color graphic screen

                      1993.6.27 v1.0
                      copyright Y.Ouchi
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <jctype.h>
#include "egb.h"

extern	void	ctblset(int, int);
extern	void	cwrt_kan(int, int);
extern	void	cwrt_ank(int, int);

extern	char	egbwork[1536];

/*
        charactor line display for 16 color graphic screen with BIOS
			in		x : display x posision
					y : display y posision
					*s: display charactor
*/
void linedsp(int x, int y, char *s)
{
	char	*errormes={"line too long!"};
    char para[256];
    int     len;
    int     i;

	len = strlen(s);
	if (len<=256){
		WORD(para + 0) = x;
		WORD(para + 2) = y;
		WORD(para + 4) = len;
		for (i = 6; i < len + 6; i++)
			para[i] = s[i - 6];
		para[i] = 0;
		EGB_sjisString(egbwork, para);
	}
	else {
		len = strlen(errormes);
		WORD(para + 0) = 300;
		WORD(para + 2) = 200;
		WORD(para + 4) = len;
		for (i = 6; i < len + 6; i++)
			para[i] = errormes[i - 6];
		para[i] = 0;
		EGB_sjisString(egbwork, para);
	}
	return;
}

/*
     charactor line display for 16 color graphic screen by VRAM write
			in		x : display x posision
					y : display y posision
					*s: display charactor
					page : display page (0 or 1)
					fcol : charactor color
					bcol : back color
*/
void linedspv(int x, int y, unsigned char *s, int page,
							unsigned int fcol, unsigned int bcol)
{
	char	*errormes={"line too long!"};
    char	para[256];
    int     len;
    int     i,adr,ch;

	ctblset(fcol & 0x0f,bcol & 0x0f);
	if (page==0) adr = (y-15)*512+(x/2);
	else adr = (y-16)*512+(x/2)+0x40000 ;

	i=0;
	while (i<256){
		if (s[i]==0) break;
		if (iskanji(s[i])){
			ch=s[i]*0x100+s[i+1];
			cwrt_kan(ch,adr+4*i);
			i=i+2;
		}
		else{
			ch=s[i];
			cwrt_ank(ch,adr+4*i);
			i=i+1;
		}
	}

	if (i==256){
		len = strlen(errormes);
		WORD(para + 0) = 300;
		WORD(para + 2) = 200;
		WORD(para + 4) = len;
		for (i = 6; i < len + 6; i++)
			para[i] = errormes[i - 6];
		para[i] = 0;
		EGB_sjisString(egbwork, para);
	}
	return;
}

/*
     charactor line display for 16 color graphic screen by VRAM write
			in		x : display x posision
					y : display y posision
					*s: display charactor
					leng : text length
					page : display page (0 or 1)
					fcol : charactor color
					bcol : back color
*/
void linedspvl(int x, int y, unsigned char *s, int leng, int page,
							unsigned int fcol, unsigned int bcol)
{
	char	*errormes={"line too long!"};
    char	para[256];
    int     i,adr,ch;
    int		len;

	ctblset(fcol & 0x0f,bcol & 0x0f);
	if (page==0) adr = (y-15)*512+(x/2);
	else adr = (y-15)*512+(x/2)+0x40000 ;
	if (leng<=256){
		i=0;
		while (i<leng){
			if (iskanji(s[i])){
				ch=s[i]*0x100+s[i+1];
				cwrt_kan(ch,adr+4*i);
				i=i+2;
			}
			else{
				ch=s[i];
				cwrt_ank(ch,adr+4*i);
				i=i+1;
			}
		}
	}
	else {
		len = strlen(errormes);
		WORD(para + 0) = 300;
		WORD(para + 2) = 200;
		WORD(para + 4) = len;
		for (i = 6; i < len + 6; i++)
			para[i] = errormes[i - 6];
		para[i] = 0;
		EGB_sjisString(egbwork, para);
	}
	return;
}

