/********************************************************************/
/* Kamui function test program subroutine							*/
/* string display routine for KAMUI									*/
/********************************************************************/
/*   COPYRIGHT (C) NEC Corporation 1997 - 1998                      */
/*   All rights reserved by NEC Corporation. This program must be   */
/*   used solely for the purpose for which it was furnished by NEC  */
/*   Corporation. No part of this program may be reproduced or      */
/*   disclosed to others, in any form, without the prior written    */
/*   permission of NEC Corporation.                                 */
/*   Use of copyright notice does not evidence publication of this  */
/*   program.                                                       */
/*                                                                  */
/*   Author   : Takeshi Muto / NEC Software, Ltd.                   */
/*   Created  : October 19 1997 by NEC Software, Ltd.               */
/*   Modified : April 9 1998 for SET5                               */
/********************************************************************/

#define		TEX_TYPE			KM_TEXTURE_ARGB4444		/* Font Texture type */
#define		TEXCHAR_U			(256)					/* Font Texture width */
#define		TEXCHAR_V			(256)					/* Font Texture height */
#define		CHAR_SIZE_X			(16.0f)					/* Font display size (width) */
#define		CHAR_SIZE_Y			(32.0f)					/* Font display size (height) */
#define		CHAR_PITCH			(-6.0f)					/* Character pitch 0=standard */
#define		LINE_PITCH			(-10.0f)				/* Line pitch 0=standard */

#include	<stdio.h>
#include	<stdarg.h>
#include	"kamui.h"				/* for Kamui Call */

/* Texture */
extern const unsigned char character[];

/***************************************************************************/
/**	グローバル変数 *********************************************************/
/***************************************************************************/
#define	MAXLINE		21		/* = 480 / ( CHAR_SIZE_Y + LINE_PITCH ) */
#define	MAXCOLMN	64		/* = 640 / ( CHAR_SIZE_X + CHAR_PITCH ) */
static	KMUINT32	colmn = 0;			/* cursor x number */
static	KMUINT32	line = 0;			/* cursor y number */
static	KMUINT32	initialized = 0;	/* initialized ? or not ? */
static	KMUINT32	scrn[MAXCOLMN][MAXLINE];		/* screen image */

/****************************************************************************/
/**	Valuables for KAMUI *****************************************************/
/****************************************************************************/
static	KMSURFACEDESC		kmiDispStrTexSurfaceDesc;	/* texture surface */
static	KMVERTEXCONTEXT		kmiDispStrVertexContext;	/* Vertex context(Normal Object) */

/***************************************************************************/
/**	サブルーチン ***********************************************************/
/***************************************************************************/

/****************************************************************************/
/** Set Cursor **************************************************************/
/****************************************************************************/
void 
txt_SetCursor(KMUINT32 x, KMUINT32 y)
{
	colmn = x; line = y;
}

/****************************************************************************/
/** Clear screen ************************************************************/
/****************************************************************************/
void 
txt_ClearScreen(void)
{
	KMUINT32	x, y;

	for(y = 0; y < MAXLINE; y++) {
		for(x = 0; x < MAXCOLMN; x++) {
			scrn[x][y] = 0;
		}
	}
	colmn = 0; line = 0;
}

/****************************************************************************/
/** Init Display String *****************************************************/
/****************************************************************************/
void 
txt_InitDisplayString(void)
{
	KMSTATUS			status;						/* return status of KAMUI API */
	PKMDWORD			pTexture01;					/* Pointer to texture */
	KMTEXTURETYPE		nTextureType;				/* texture type definition */

	/*-----------------------------------------------------------------------------------*/
	/* Create texture surface */
	nTextureType = KM_TEXTURE_TWIDDLED | TEX_TYPE;	/* Set texture type */
	status = kmCreateTextureSurface(
		&kmiDispStrTexSurfaceDesc,		/* OUT	PKMSURFACEDESC	pSurfaceDesc */
		TEXCHAR_U,						/*	IN	KMINT32			nWidth */
		TEXCHAR_V,						/*	IN	KMINT32			nHeight */
		nTextureType					/*	IN	KMTEXTURETYPE	nTextureType */
	);
	if(status != KMSTATUS_SUCCESS) return;

	/*-----------------------------------------------------------------------------------*/
	/* Load and set texture */
	pTexture01 = (unsigned long *)&character;
	status = kmLoadTexture(&kmiDispStrTexSurfaceDesc,	/* PKMSURFACEDESC pSurfaceDesc */
							pTexture01 + (16 / sizeof(KMDWORD)),	/* PKMDWORD pTexture */
							FALSE,						/* KMBOOLEAN bAutoMipMap */
							FALSE						/* KMBOOLEAN bDither */
							);
	if(status != KMSTATUS_SUCCESS) return;

	/*-----------------------------------------------------------------------------------*/
	/* Initialize VertexContext */

	/* Clear VertexContext */
	memset(&kmiDispStrVertexContext,0,sizeof(KMVERTEXCONTEXT));

	/* 頂点CONTEXTの生成 */
	kmiDispStrVertexContext.RenderState
								= KM_DEPTHMODE  		| KM_CULLINGMODE
								| KM_SCREENCOORDINATION	| KM_SHADINGMODE
								| KM_MODIFIER 			| KM_ZWRITEDISABLE
								| KM_SRCBLENDINGMODE	| KM_DSTBLENDINGMODE
								| KM_SRCSELECT			| KM_DSTSELECT
								| KM_FOGMODE			| KM_USESPECULAR
								| KM_USEALPHA			| KM_IGNORETEXTUREALPHA
								| KM_FLIPUV 			| KM_CLAMPUV 
								| KM_FILTERMODE			| KM_SUPERSAMPLE
								| KM_MIPMAPDADJUST 		| KM_TEXTURESHADINGMODE
								| KM_PARAMTYPE 			| KM_LISTTYPE
								| KM_COLORTYPE			| KM_USESPECULAR
								| KM_UVFORMAT;

	/* for ParameterControlWord */
	kmiDispStrVertexContext.ParamType			= KM_POLYGON;
	kmiDispStrVertexContext.ListType			= KM_TRANS_POLYGON;
	kmiDispStrVertexContext.ColorType			= KM_FLOATINGCOLOR;
	kmiDispStrVertexContext.bUseSpecular		= FALSE;
	kmiDispStrVertexContext.UVFormat			= KM_32BITUV;

	/* for ISP/TSP InstructionWord */
	kmiDispStrVertexContext.DepthMode			= KM_GREATER;
	kmiDispStrVertexContext.CullingMode			= KM_NOCULLING;
	kmiDispStrVertexContext.ScreenCoordination	= KM_SCREEN;
	kmiDispStrVertexContext.ShadingMode			= KM_TEXTUREGOURAUD;
	kmiDispStrVertexContext.SelectModifier		= KM_NOMODIFIER;
	kmiDispStrVertexContext.bZWriteDisable		= FALSE;			/* ARC1にはない */

	/* for TSP ControlWord */
	kmiDispStrVertexContext.SRCBlendingMode		= KM_SRCALPHA;
	kmiDispStrVertexContext.DSTBlendingMode		= KM_INVSRCALPHA;
	kmiDispStrVertexContext.bSRCSel				= FALSE;			/* ARC1にはない */
	kmiDispStrVertexContext.bDSTSel				= FALSE;			/* ARC1にはない */
	kmiDispStrVertexContext.FogMode				= KM_NOFOG;			/* 11はリザーブ */
	kmiDispStrVertexContext.bUseSpecular		= FALSE;
	kmiDispStrVertexContext.bUseAlpha			= TRUE;
	kmiDispStrVertexContext.FlipUV				= KM_NOFLIP;
	kmiDispStrVertexContext.ClampUV				= KM_CLAMP_UV;
	kmiDispStrVertexContext.FilterMode			= KM_POINT_SAMPLE;
	kmiDispStrVertexContext.bSuperSample		= FALSE;
	kmiDispStrVertexContext.MipMapAdjust		= 0x00000004;		/* D=1.00 */
	kmiDispStrVertexContext.TextureShadingMode	= KM_MODULATE;

	/* for Texture ControlBit */

	/* No effect on ARC1 */
	kmiDispStrVertexContext.bColorClamp		= FALSE;
	kmiDispStrVertexContext.PaletteBank		= 0;

	/* Set texture surface */
	kmiDispStrVertexContext.pTextureSurfaceDesc	= &kmiDispStrTexSurfaceDesc;

	/* for GlobalColorData */
	kmiDispStrVertexContext.fFaceColorAlpha		= 1.0f;		/* Face Color Alpha */
	kmiDispStrVertexContext.fFaceColorRed		= 1.0f;		/* Face Color Red */
	kmiDispStrVertexContext.fFaceColorGreen		= 1.0f;		/* Face Color Green */
	kmiDispStrVertexContext.fFaceColorBlue		= 1.0f;		/* Face Color Blue */
	kmiDispStrVertexContext.fOffsetColorAlpha	= 1.0f;		/* Specular Color Alpha */
	kmiDispStrVertexContext.fOffsetColorRed		= 1.0f;		/* Specular Color Red */
	kmiDispStrVertexContext.fOffsetColorGreen	= 1.0f;		/* Specular Color Green */
	kmiDispStrVertexContext.fOffsetColorBlue	= 1.0f;		/* Specular Color Blue */

	status = kmProcessVertexRenderState(&kmiDispStrVertexContext);
	if(status != KMSTATUS_SUCCESS) return;

	initialized = 1;	/* set initialize flag */
}

/****************************************************************************/
/** Display String **********************************************************/
/****************************************************************************/
void 
txt_DispString(char *instring,...)
{
	KMINT32		i = 0;
	KMINT32		j;

	char		string[4096];
	va_list		vaArgs;

/*--
	if(initialized == 0) return;
--*/

	va_start (vaArgs, instring);
	vsprintf (string, instring, vaArgs);

	do{
		if(string[i] == '\n') {
			for(j = colmn; j < MAXCOLMN; j++) {
				scrn[j][line] = 0;
			}
			colmn = 0; line++;
			if(line >= MAXLINE) {
				line = 0;
			}
		}
		else if(string[i] >= ' ') {
			scrn[colmn][line] = string[i];
			colmn++;
			if(colmn >= MAXCOLMN) {
				colmn = 0; line++;
				if(line >= MAXLINE) {
					line = 0;
				}
			}
		}
		else {
			scrn[colmn][line] = 0;
			colmn++;
			if(colmn >= MAXCOLMN) {
				colmn = 0; line++;
				if(line >= MAXLINE) {
					line = 0;
				}
			}
		}
	} while(string[++i] != 0);
}

/****************************************************************************/
/** Display String **********************************************************/
/****************************************************************************/
void 
txt_DispScreen(KMVERTEXBUFFDESC BufferDesc)
{
	KMINT32			x, y;
	KMVERTEX_05		Ver[4];
	KMSTATUS		status;		/* return status of KAMUI API */

	if(initialized == 0) return;

	status = kmSetVertexRenderState(&kmiDispStrVertexContext);
	if(status != KMSTATUS_SUCCESS) return;

	for(y = 0; y < MAXLINE; y++) {
		for(x = 0; x < MAXCOLMN; x++) {
			if(scrn[x][y] >= ' ') {
				status = kmStartVertexStrip(&BufferDesc);
				if(status != KMSTATUS_SUCCESS) return;

				Ver[0].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
				Ver[0].fX	= x * (CHAR_SIZE_X + CHAR_PITCH);	Ver[0].fY	= y * (CHAR_SIZE_Y + LINE_PITCH) + CHAR_SIZE_Y;
#if defined(_STRICT_UNION_)
				Ver[0].u.fZ	= 256.0f;
#else
				Ver[0].fZ	= 256.0f;
#endif
				Ver[0].fU	= 0.0625f * ((scrn[x][y] - 32) % 16);
				Ver[0].fV	= 0.125f * ( 7.0f - (float)((scrn[x][y] - 32) / 16));
				Ver[0].fBaseAlpha	= 1.0f;	Ver[0].fBaseRed		= 1.0f;	Ver[0].fBaseGreen	= 1.0f;	Ver[0].fBaseBlue	= 1.0f;
				Ver[0].fOffsetAlpha	= 1.0f;	Ver[0].fOffsetRed	= 1.0f;	Ver[0].fOffsetGreen	= 1.0f;	Ver[0].fOffsetBlue	= 1.0f;
				status = kmSetVertex(&BufferDesc,&Ver[0],KM_VERTEXTYPE_05,sizeof(KMVERTEX5) );
				if(status != KMSTATUS_SUCCESS) return;

				Ver[1].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
				Ver[1].fX	= x * (CHAR_SIZE_X + CHAR_PITCH);	Ver[1].fY	= y * (CHAR_SIZE_Y + LINE_PITCH);
#if defined(_STRICT_UNION_)
				Ver[1].u.fZ	= 256.0f;
#else
				Ver[1].fZ	= 256.0f;
#endif
				Ver[1].fU	= Ver[0].fU;
				Ver[1].fV	= Ver[0].fV + 0.125f;
				Ver[1].fBaseAlpha	= 1.0f;	Ver[1].fBaseRed		= 1.0f;	Ver[1].fBaseGreen	= 1.0f;	Ver[1].fBaseBlue	= 1.0f;
				Ver[1].fOffsetAlpha	= 1.0f;	Ver[1].fOffsetRed	= 1.0f;	Ver[1].fOffsetGreen	= 1.0f;	Ver[1].fOffsetBlue	= 1.0f;
				status = kmSetVertex(&BufferDesc,&Ver[1],KM_VERTEXTYPE_05,sizeof(KMVERTEX5) );
				if(status != KMSTATUS_SUCCESS) return;

				Ver[2].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
				Ver[2].fX	= x * (CHAR_SIZE_X + CHAR_PITCH) + CHAR_SIZE_X;	Ver[2].fY	= Ver[0].fY;
#if defined(_STRICT_UNION_)
				Ver[2].u.fZ	= 256.0f;
#else
				Ver[2].fZ	= 256.0f;
#endif
				Ver[2].fU	= Ver[0].fU + 0.0625f;
				Ver[2].fV	= Ver[0].fV;
				Ver[2].fBaseAlpha	= 1.0f;	Ver[2].fBaseRed		= 1.0f;	Ver[2].fBaseGreen	= 1.0f;	Ver[2].fBaseBlue	= 1.0f;
				Ver[2].fOffsetAlpha	= 1.0f;	Ver[2].fOffsetRed	= 1.0f;	Ver[2].fOffsetGreen	= 1.0f;	Ver[2].fOffsetBlue	= 1.0f;
				status = kmSetVertex(&BufferDesc,&Ver[2],KM_VERTEXTYPE_05,sizeof(KMVERTEX5) );
				if(status != KMSTATUS_SUCCESS) return;

				Ver[3].ParamControlWord	= KM_VERTEXPARAM_ENDOFSTRIP;
				Ver[3].fX	= x * (CHAR_SIZE_X + CHAR_PITCH) + CHAR_SIZE_X;	Ver[3].fY	= Ver[1].fY;
#if defined(_STRICT_UNION_)
				Ver[3].u.fZ	= 256.0f;
#else
				Ver[3].fZ	= 256.0f;
#endif
				Ver[3].fU	= Ver[2].fU;
				Ver[3].fV	= Ver[1].fV;
				Ver[3].fBaseAlpha	= 1.0f;	Ver[3].fBaseRed		= 1.0f;	Ver[3].fBaseGreen	= 1.0f;	Ver[3].fBaseBlue	= 1.0f;
				Ver[3].fOffsetAlpha	= 1.0f;	Ver[3].fOffsetRed	= 1.0f;	Ver[3].fOffsetGreen	= 1.0f;	Ver[3].fOffsetBlue	= 1.0f;
				status = kmSetVertex(&BufferDesc,&Ver[3],KM_VERTEXTYPE_05,sizeof(KMVERTEX5) );
				if(status != KMSTATUS_SUCCESS) return;
			}
		}
	}
}

/*--------------------------- End of File --------------------------------*/
