/*----------------------------------------------------------------
   Name   : marble.c  (ARC1 demo) 
   Author : Carlos Sarria            
   Date   : September 1997 (Updated Jan98)
    
   Copyright : 1997 by VideoLogic Limited. All rights reserved.
   Modified  : 29, June, 1998 by Takeshi Muto / NEC Software,Ltd. for KAMUI
 -----------------------------------------------------------------*/

/*
#define		_X86_
*/
/* _X86_ flag for win95 : SET2 */

#define		_DISPLAY_STRING_
/* if defined then display strings on screen */

#define		_DISP_PERFORMANCE_
/* Display performance : for windows */

/* Set Display Mode */
#define NTSC 1

#include	<math.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<float.h>

#include	<shinobi.h>

/*--
#include	<conio.h>
#include	<malloc.h>
#include	<memory.h>
--*/

#if defined(_X86_)
	#include	<time.h>
	#include	<windows.h>
#endif

#pragma warning (disable : 4244) /* Disables float to double conversion warning */

#include	"kamui.h"				/* for Kamui Call */
#include	"dispstr.h"				/* for DisplayString */
/* #include	"sgl2.h" */
#include	"soj_tool.h"
#include	"kmMarble.h"  /* This is the file with 3D data */

#define _KM_USE_VERTEX_MACRO_
#define _KM_USE_VERTEX_MACRO_L3_
#include	"kamuix.h"

#if defined(_DISP_PERFORMANCE_)
#if defined(_X86_)
#else
	#include    "QPF.H"                 /* for QueryPerformance */
#endif
#endif

/****************************************************************************/
/**	Valuables for KAMUI *****************************************************/
/****************************************************************************/
	KMSTATUS			status;					/* return status of KAMUI API */
	KMVERSIONINFO		VersionInfo;			/* KAMUI Version information */
	KMSURFACEDESC		PrimarySurfaceDesc;		/* Primary frame buffer */
	KMSURFACEDESC		BackSurfaceDesc;		/* Back-buffer frame buffer */
	KMVERTEXBUFFDESC	VertexBufferDesc;		/* Vertex-buffer */
	KMVERTEXCONTEXT		VertexContextMain01;	/* Vertex context */
	KMVERTEXCONTEXT		VertexContextShadow01;	/* Vertex context */
	KMVERTEXCONTEXT		VertexContextMain02;	/* Vertex context */
	KMVERTEXCONTEXT		VertexContextShadow02;	/* Vertex context */
	KMVERTEXCONTEXT		ModifireContextFirst;	/* Vertex context */
	KMVERTEXCONTEXT		ModifireContextNormal;	/* Vertex context */
	KMVERTEXCONTEXT		ModifireContextLast;	/* Vertex context */
	KMDWORD				FogDensity;				/* Fog density */
	KMDWORD				FogD = 1;

	PKMSURFACEDESC			pFB[2];
	KMSYSTEMCONFIGSTRUCT	SystemConfig;
	PKMDWORD				dwDataPtr;

/* 性能測定用変数 */
	int			frame;				/* 画面フレーム */
	int			frame_2 = 0;		/* 画面フレーム上位 */
#if defined(_DISP_PERFORMANCE_)
	int			pcount_f;			/* Hight-resolution counter supported? */
	double		nFPS;				/* 現在の性能(Frame/Second) */
	double		aFPS;				/* 性能の平均値 */
	double		MxFPS;				/* 最高性能 */
	double		MnFPS;				/* 最低性能 */
	__int64 	Time3i;				/* render API 完了時刻 */
	__int64		freqi;				/* カウンタの精度 */
	double		freq;				/* カウンタの精度 */
	double		Time3;				/* render API 完了時刻 */
	double		maxTime = 0.00001;	/* 最高描画時間(msec) */
	double		minTime = 99999;	/* 最低描画時間(msec) */
	double		OldTime3;			/* １フレーム前のrender直後の時間(msec) */
	double		nowTime;			/* 現在の描画時間(msec) */
	double		totalTime	= 0;	/* 累積render時間(msec) */
	double		aveTime;			/* 平均描画時間(msec) */
#if defined(_X86_)
	time_t		start_time;			/* APの開始時間 */
#endif
#endif

/*------------------------------- Global Variables -----------------------------------*/
/* Globals */

/* Not counted. */
unsigned int nTrees = 0;
unsigned int Type = 0;

float   Frame = 0;
#if 0
unsigned   TexOpaq, TexForest, TexBark;
#endif
KMSURFACEDESC	TexOpaq, TexShadow, TexBark;

/* Info Structure */
INFO Info = 
{
	FALSE,		/* OpaqMap       */
	TRUE,       /* Textured      */
	TRUE,		/* Gouraud       */
	FALSE,      /* GlobalTrans   */
	FALSE,      /* VertexAlpha   */
	FALSE,		/* TableFog      */
	FALSE,      /* VertexFog     */
	TRUE,       /* Bilinear      */
	TRUE,       /* Highlight     */
};

/* Prototypes */

static void SetTextures			(void);
static void NextFrame			(void);
static void DisplayInformation	(void);

static void ReadMaple			(void);		/* Maple data read routine */

/* Textures */
extern unsigned char Marble;
extern unsigned char Marble2;
extern unsigned char Bark;

/*-------------------------------------------------------------------------------------*/
/*                                       MAIN                                          */
/*-------------------------------------------------------------------------------------*/

NJS_PERIPHERAL *per;

int main (void)
{
KMPACKEDARGB   cBorder;
int Control = 0, Device, Val = 0;

#ifdef __GNUC__
	shinobi_workaround();
#endif

#if 0
    Device = sgl_create_screen_device (0, 640, 480, sgl_device_16bit, FALSE);
	if (Device<0) return -1;
#endif

	txt_ClearScreen();	/* Clear text screen */
	PRINTOUT(("** Marble : Modifier Volume **\n"));

	/*----------------------------------------------------------------------------------- */
	/* Initialize Hardware */

#if NTSC
	sbInitSystem( NJD_RESOLUTION_640x480_NTSCNI, NJD_FRAMEBUFFER_MODE_RGB565, 1 );
#else
	sbInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB565, 1 );
#endif

		/* Black Border */
	cBorder.dwPacked	=	0x00000000;
	kmSetBorderColor(cBorder);

	kmGetVersionInfo(&VersionInfo);
#if 0
	PRINTOUT(("MajorVersion      0x0%08x\n",VersionInfo.kmMajorVersion));
	PRINTOUT(("LocalVersion      0x0%08x\n",VersionInfo.kmLocalVersion));
	PRINTOUT(("kmFrameBufferSize 0x0%08x\n",VersionInfo.kmFrameBufferSize));
#endif

	/*----------------------------------------------------------------------------------- */
	/* Create frame buffer and vertex buffer */

	pFB[0] = &PrimarySurfaceDesc;
	pFB[1] = &BackSurfaceDesc;

	/* VertexBufferPointer(set5) */
	dwDataPtr = (PKMDWORD)0xACB00000;

	SystemConfig.dwSize	=	sizeof(KMSYSTEMCONFIGSTRUCT);
	SystemConfig.flags	=	KM_CONFIGFLAG_ENABLE_CLEAR_FRAMEBUFFER;

	/* for Frame buffer */
	SystemConfig.ppSurfaceDescArray	=	pFB;	/* Array of SurfaceDesc	pointer     */
	SystemConfig.nNumOfFrameBuffer	=	2;		/* Number Of Frame Buffer			*/
	SystemConfig.nWidth				=	0;		/* Width of Frame buffer			*/
	SystemConfig.nHeight			=	0;		/* Height of Frame buffer			*/
	SystemConfig.nBpp				=	0;		/* Bit Per Depth for Frame buffer	*/
	/* for Texture Memory */
	SystemConfig.nTextureMemorySize	=	262144 * 2;	/* Texture Memory size : 1MB * 2 */
	/* for Vertex buffer */
	SystemConfig.pBufferDesc		=	&VertexBufferDesc;	/* pointer to VERTEXBUFFDESC 		*/
	SystemConfig.pVertexBuffer		=	dwDataPtr;			/* pointer to Vertex buffer 		*/
	SystemConfig.nVertexBufferSize	=	262144 * 4;	/* size of Vertex buffer : 1MB * 3 */
	SystemConfig.fBufferSize[0]		=	40.0f;		/* Opaque polygon buffer size        : 35% */
	SystemConfig.fBufferSize[1]		=	40.0f;		/* Opaque modifier buffer size       :  0% */
	SystemConfig.fBufferSize[2]		=	20.0f;		/* Trans polygon buffer size         : 30% */
	SystemConfig.fBufferSize[3]		=	 0.0f;		/* Trans modifier buffer size        :  0% */
	SystemConfig.fBufferSize[4]		=	 0.0f;		/* Punchthrough polygon buffer size  : 35% */

	SystemConfig.nVbufModel			=	 KM_VERTEXBUFMODEL_NORMAL;	/* VertexBuffer Model		*/
	status = kmSetSystemConfiguration( &SystemConfig );
	Check_Status(status, "kmSetSystemConfiguration");

	/*-----------------------------------------------------------------------------------*/
	/* Initialize DisplayString routine */
	txt_InitDisplayString();

	/*----------------------------------------------------------------------------------- */
	/* some initializes */
	SetTextures ();
/*	SetContext  (Info); */

	/****************************/
	/* 2 parameter context main */
	/****************************/
	InitContext (&VertexContextMain01, Info, &TexOpaq);
	/* Clear render state */
	VertexContextMain01.RenderState	= 0;
	/* Set select modifier mode */
	VertexContextMain01.RenderState	|= KM_MODIFIER;
	VertexContextMain01.SelectModifier	= KM_MODIFIER_A;
	/* Process & Set Texture */
	status = kmProcessVertexRenderState(&VertexContextMain01);
	Check_Status(status, "kmProcessVertexRenderState");

	/******************************/
	/* 2 parameter context shadow */
	/******************************/
	InitContext (&VertexContextShadow01, Info, &TexShadow);

	/****************************/
	/* 2 parameter context main */
	/****************************/
	InitContext (&VertexContextMain02, Info, &TexBark);
	/* Clear render state */
	VertexContextMain02.RenderState	= 0;
	/* Set select modifier mode */
	VertexContextMain02.RenderState	|= KM_MODIFIER;
	VertexContextMain02.SelectModifier	= KM_MODIFIER_A;
	/* Process & Set Texture */
	status = kmProcessVertexRenderState(&VertexContextMain02);
	Check_Status(status, "kmProcessVertexRenderState");

	/******************************/
	/* 2 parameter context shadow */
	/******************************/
	InitContext (&VertexContextShadow02, Info, &TexShadow);

	/**************************/
	/* Modifier context First */
	/**************************/
	InitContext (&ModifireContextFirst, Info, &TexOpaq);
	ModifireContextFirst.RenderState	= KM_SHADINGMODE;
	ModifireContextFirst.ShadingMode	= KM_NOTEXTUREGOURAUD;
	/* Set param type */
	ModifireContextFirst.RenderState	|= KM_PARAMTYPE;
	ModifireContextFirst.ParamType		= KM_MODIFIERVOLUME;
	/* Set list type */
	ModifireContextFirst.RenderState	|= KM_LISTTYPE;
	ModifireContextFirst.ListType	= KM_OPAQUE_MODIFIER;
	/* Shadow & BBOX */
	ModifireContextFirst.ModifierInstruction = KM_MODIFIER_INCLUDE_FIRST_POLY;
	ModifireContextFirst.fBoundingBoxXmin =   0.0f;
	ModifireContextFirst.fBoundingBoxYmin =   0.0f;
	ModifireContextFirst.fBoundingBoxXmax = 639.0f;
	ModifireContextFirst.fBoundingBoxYmax = 479.0f;
	/* Process & Set */
	status = kmProcessVertexRenderState(&ModifireContextFirst);
	Check_Status(status, "ModifireContextFirst");

	/***************************/
	/* Modifier context Normal */
	/***************************/
	InitContext (&ModifireContextNormal, Info, &TexOpaq);
	ModifireContextNormal.RenderState	= KM_SHADINGMODE;
	ModifireContextNormal.ShadingMode	= KM_NOTEXTUREGOURAUD;
	/* Set param type */
	ModifireContextNormal.RenderState	|= KM_PARAMTYPE;
	ModifireContextNormal.ParamType		= KM_MODIFIERVOLUME;
	/* Set list type */
	ModifireContextNormal.RenderState	|= KM_LISTTYPE;
	ModifireContextNormal.ListType	= KM_OPAQUE_MODIFIER;
	/* Shadow & BBOX */
	ModifireContextNormal.ModifierInstruction = KM_MODIFIER_NORMAL_POLY;
	ModifireContextNormal.fBoundingBoxXmin =   0.0f;
	ModifireContextNormal.fBoundingBoxYmin =   0.0f;
	ModifireContextNormal.fBoundingBoxXmax = 639.0f;
	ModifireContextNormal.fBoundingBoxYmax = 479.0f;
	/* Process & Set */
	status = kmProcessVertexRenderState(&ModifireContextNormal);
	Check_Status(status, "ModifireContextNormal");

	/*************************/
	/* Modifier context Last */
	/*************************/
	InitContext (&ModifireContextLast, Info, &TexOpaq);
	ModifireContextLast.RenderState	= KM_SHADINGMODE;
	ModifireContextLast.ShadingMode	= KM_NOTEXTUREGOURAUD;
	/* Set param type */
	ModifireContextLast.RenderState	|= KM_PARAMTYPE;
	ModifireContextLast.ParamType		= KM_MODIFIERVOLUME;
	/* Set list type */
	ModifireContextLast.RenderState	|= KM_LISTTYPE;
	ModifireContextLast.ListType	= KM_OPAQUE_MODIFIER;
	/* Shadow & BBOX */
	ModifireContextLast.ModifierInstruction = KM_MODIFIER_INCLUDE_LAST_POLY;
	ModifireContextLast.fBoundingBoxXmin =   0.0f;
	ModifireContextLast.fBoundingBoxYmin =   0.0f;
	ModifireContextLast.fBoundingBoxXmax = 639.0f;
	ModifireContextLast.fBoundingBoxYmax = 479.0f;
	/* Process & Set */
	status = kmProcessVertexRenderState(&ModifireContextLast);
	Check_Status(status, "ModifireContextLast");

	DisplayInformation ();

	/*-------------------------------------------------------------------------------- */
	/* 性能測定用各種変数の初期化 */
	frame		= 0;
#if defined(_DISP_PERFORMANCE_)
	totalTime	= 0;

	pcount_f = QueryPerformanceFrequency((LARGE_INTEGER*)&freqi);	/* カウンタの単位の読み出し */
	if(pcount_f) {	/*	QueryPerformanceCounterがサポートされている */
		QueryPerformanceCounter((LARGE_INTEGER*)&Time3i);	/* ダミー値による初期化 */
		freq = 1000.0f / (double)freqi;						/* msec変換用係数の生成 */
		OldTime3 = Time3 = (double)Time3i * freq;			/* countからmsecへ変換 */
	}
	else {	/* QueryPerformanceCounterがサポートされていない */
		Time3 = (double)timeGetTime();						/* ダミー値による初期化 */
	}

	/* Get AP start time ---------------------------------------------*/
#if defined(_X86_)
	 time( &start_time );
#endif
#endif	/* defined(_DISP_PERFORMANCE_) */

	/* This function creates an array of structures with 3D data */
	CreateMeshesArray ();
	InitMeshes (NUM_MESHES, (CASTMESHES *) Mesh);

   	while (TRUE)
	{
	
#if defined(_X86_)
	   while(!kbhit())  NextFrame ();
	   Val = _getch ();
      
	   switch (Val)
	   {
			case K_ESC:
			case K_F12:
				 goto Finish;    

	      	case K_F1 :
			     Info.Bilinear = (Info.Bilinear) ? FALSE:TRUE;
				 DisplayInformation ();
				 break;
	
			case K_F2 :
				 Info.Gouraud = (Info.Gouraud) ? FALSE:TRUE;
				 DisplayInformation ();
				 break;
			case K_F3 :
				 Info.Textured = (Info.Textured) ? FALSE:TRUE;
				 DisplayInformation ();
				 break;
	        
			case K_F4 :
				 Info.Highlight = (Info.Highlight) ? FALSE:TRUE;
				 DisplayInformation ();
				 break;
	   }
	   Val = 0;
#else	/* not _X86_ */
		ReadMaple ();
		NextFrame ();
#endif
	}

Finish:
    FreeMeshes (NUM_MESHES);
/*	FreeAllBMPTextures (); */
/*	sgl_delete_device(Device); */

	/*----------------------------------------------------------------------------------- */
	/* Free vertex buffer */
	status = kmDiscardVertexBuffer(&VertexBufferDesc);
	Check_Status(status, "kmDiscardVertexBuffer");

	/*----------------------------------------------------------------------------------- */
	/* End display */
	status = kmStopDisplayFrameBuffer();
	Check_Status(status, "kmStopDisplayFrameBuffer");

    return 0;
}
/*-------------------------------------------------------------------------------------*/
void SetTextures (void)
{
#if 0
	TexOpaq = LoadBMPTexture ("Marble.bmp", FALSE, FALSE, FALSE);
	/*TexBark = LoadBMPTexture ("Bark.bmp", FALSE, TRUE, FALSE); */

	/*TexOpaq = LoadVQTexture("Marble.vqf"); */
    TexBark = LoadVQTexture("Bark.vqf");
#endif
	PKMDWORD			pTexture01;				/* Pointer to texture */
	KMTEXTURETYPE		nTextureType;			/* texture type definition */

	/*----------------------------------------------------------------------------------- */
	/* Create texture surface */
	nTextureType = KM_TEXTURE_TWIDDLED_MM | KM_TEXTURE_RGB565;	/* Set texture type */
	status = kmCreateTextureSurface(
		&TexOpaq,				/* OUT	PKMSURFACEDESC	pSurfaceDesc */
		256,					/*	IN	KMINT32			nWidth  */
		256,					/*	IN	KMINT32			nHeight  */
		nTextureType			/*	IN	KMTEXTURETYPE	nTextureType */
	);
	Check_Status(status, "kmCreateTextureSurface #1");
	/*----------------------------------------------------------------------------------- */
	/* Create texture surface */
	nTextureType = KM_TEXTURE_TWIDDLED_MM | KM_TEXTURE_RGB565;	/* Set texture type */
	status = kmCreateTextureSurface(
		&TexShadow,				/* OUT	PKMSURFACEDESC	pSurfaceDesc */
		256,					/*	IN	KMINT32			nWidth  */
		256,					/*	IN	KMINT32			nHeight  */
		nTextureType			/*	IN	KMTEXTURETYPE	nTextureType */
	);
	Check_Status(status, "kmCreateTextureSurface #2");
	/*----------------------------------------------------------------------------------- */
	/* Create texture surface */
	nTextureType = KM_TEXTURE_TWIDDLED_MM | KM_TEXTURE_RGB565;	/* Set texture type */
	status = kmCreateTextureSurface(
		&TexBark,				/* OUT	PKMSURFACEDESC	pSurfaceDesc */
		32,						/*	IN	KMINT32			nWidth  */
		32,						/*	IN	KMINT32			nHeight  */
		nTextureType			/*	IN	KMTEXTURETYPE	nTextureType */
	);
	Check_Status(status, "kmCreateTextureSurface #3");
	/*----------------------------------------------------------------------------------- */
	/* Load and set texture */
	pTexture01 = (unsigned long *)&Marble;
	status = kmLoadTexture(&TexOpaq,			/* PKMSURFACEDESC pSurfaceDesc */
							pTexture01 + (16 / sizeof(KMDWORD)),			/* PKMDWORD pTexture */
							FALSE,				/* KMBOOLEAN bAutoMipMap */
							FALSE				/* KMBOOLEAN bDither */
							);
	Check_Status(status, "kmLoadTexture #1");
	/*----------------------------------------------------------------------------------- */
	/* Load and set texture */
	pTexture01 = (unsigned long *)&Marble2;
	status = kmLoadTexture(&TexShadow,			/* PKMSURFACEDESC pSurfaceDesc */
							pTexture01 + (16 / sizeof(KMDWORD)),			/* PKMDWORD pTexture */
							FALSE,				/* KMBOOLEAN bAutoMipMap */
							FALSE				/* KMBOOLEAN bDither */
							);
	Check_Status(status, "kmLoadTexture #2");
	/*----------------------------------------------------------------------------------- */
	/* Load and set texture */
	pTexture01 = (unsigned long *)&Bark;
	status = kmLoadTexture(&TexBark,			/* PKMSURFACEDESC pSurfaceDesc */
							pTexture01 + (16 / sizeof(KMDWORD)),			/* PKMDWORD pTexture */
							FALSE,				/* KMBOOLEAN bAutoMipMap */
							FALSE				/* KMBOOLEAN bDither */
							);
	Check_Status(status, "kmLoadTexture #3");

}

/*-------------------------------------------------------------------------------------*/
void NextFrame (void)
{
register i;
float v[3]= {0.0f,0.0f,1.0f};

  InitScene ();
  
 

  for (i=0;i<NUM_MESHES; i++)  ResetMatrix (i);
  

	for (i=0;i<NUM_MESHES; i++) {
		if (i==SHADOW) {
			Shadow (ModifireContextFirst, ModifireContextNormal, ModifireContextLast);
		}
		else
		{
/*			SetContext (Info); */
		    if (i==8 || i==11) Translate(i, 7, 0,0);
			SetSmoothShading (i, C_WHITE);
/*			SetVertices (i,  (i==HEAD || i==BACK || i==BASE)?TexOpaq:TexBark, FX_NONE); */
			if((i==HEAD)||(i==BACK)||(i==BASE)) {
				SetVertices( i, VertexContextMain01, VertexContextShadow01, FX_NONE );
			}
			else {
				SetVertices( i, VertexContextMain02, VertexContextShadow02, FX_NONE );
			}
		}
	}

/*  Timer (DISPLAY_TIME); */

  EndScene();
  
  	pdExecPeripheralServer();

     Frame++;
}
/*-------------------------------------------------------------------------------------*/

void DisplayInformation (void)
{
char *Flag[2]   = { "FALSE", "TRUE" };

	txt_SetCursor(0,17);
#if 0
	PRINTOUT(( " A : Bilinear    %s\n", Flag[Info.Bilinear]));
	PRINTOUT(( " B : Gouraud     %s\n", Flag[Info.Gouraud]));
	PRINTOUT(( " X : Textured    %s\n", Flag[Info.Textured]));
	PRINTOUT(( " Y : HighLight   %s\n", Flag[Info.Highlight]));
#endif
/*
	Timer (RESET_TIME);
*/
}

/*----------------------------------------------------------------------*/
/*  Function Name   : ReadMaple                                         */
/*  Inputs          :                                                   */
/*  Outputs         :                                                   */
/*  Returns         :                                                   */
/*  Globals Used    : PosZ, BlendSrc, BlendDst, AlphaSource,            */
/*                    BlendModulate                                     */
/*  Description     : Read MAPLE status                                 */
/*----------------------------------------------------------------------*/
void ReadMaple (void)
{
	static	int	swadelay = 0;	/* flag for cancel chattering of A button */
	static	int	swbdelay = 0;	/* flag for cancel chattering of B button */
	static	int	swxdelay = 0;	/* flag for cancel chattering of X button */
	static	int	swydelay = 0;	/* flag for cancel chattering of Y button */

	per = (NJS_PERIPHERAL *) njGetPeripheral(NJD_PORT_A0);

		/* A button */
		if ( swadelay == 0 ) {
			if ( per->on & NJD_DGT_TA ){
				Info.Bilinear = (Info.Bilinear) ? FALSE:TRUE;
				SetContext (&VertexContextShadow01, Info, &TexShadow);
				SetContext (&VertexContextShadow02, Info, &TexShadow);
#if 0
				SetContext (&VertexContextMain01, Info, &TexOpaq);
				SetContext (&VertexContextMain02, Info, &TexBark);
				SetContext (&ModifireContextFirst, Info, &TexOpaq);
				SetContext (&ModifireContextNormal, Info, &TexOpaq);
				SetContext (&ModifireContextLast, Info, &TexOpaq);
#endif
				DisplayInformation ();
				swadelay = 1;
			}
		} else {
			if ( per->off & NJD_DGT_TA ){
				swadelay = 0;
			}
		}

		/* B button */
		if ( swbdelay == 0 ) {
			if ( per->on & NJD_DGT_TB ){
				Info.Gouraud = (Info.Gouraud) ? FALSE:TRUE;
				SetContext (&VertexContextShadow01, Info, &TexShadow);
				SetContext (&VertexContextShadow02, Info, &TexShadow);
#if 0
				SetContext (&VertexContextMain01, Info, &TexOpaq);
				SetContext (&VertexContextMain02, Info, &TexBark);
				SetContext (&ModifireContextFirst, Info, &TexOpaq);
				SetContext (&ModifireContextNormal, Info, &TexOpaq);
				SetContext (&ModifireContextLast, Info, &TexOpaq);
#endif
				DisplayInformation ();
				swbdelay = 1;
			}
		} else {
			if ( per->off & NJD_DGT_TB ){
				swbdelay = 0;
			}
		}

		/* X button */
		if ( swxdelay == 0 ) {
			if ( per->on & NJD_DGT_TX ){
				Info.Textured = (Info.Textured) ? FALSE:TRUE;
				SetContext (&VertexContextShadow01, Info, &TexShadow);
				SetContext (&VertexContextShadow02, Info, &TexShadow);
#if 0
				SetContext (&VertexContextMain01, Info, &TexOpaq);
				SetContext (&VertexContextMain02, Info, &TexBark);
				SetContext (&ModifireContextFirst, Info, &TexOpaq);
				SetContext (&ModifireContextNormal, Info, &TexOpaq);
				SetContext (&ModifireContextLast, Info, &TexOpaq);
#endif
				DisplayInformation ();
				swxdelay = 1;
			}
		} else {
			if ( per->off & NJD_DGT_TX ){
				swxdelay = 0;
			}
		}

		/* Y button */
		if ( swydelay == 0 ) {
			if ( per->on & NJD_DGT_TY ){
				Info.Highlight = (Info.Highlight) ? FALSE:TRUE;
				SetContext (&VertexContextShadow01, Info, &TexShadow);
				SetContext (&VertexContextShadow02, Info, &TexShadow);
#if 0
				SetContext (&VertexContextMain01, Info, &TexOpaq);
				SetContext (&VertexContextMain02, Info, &TexBark);
				SetContext (&ModifireContextFirst, Info, &TexOpaq);
				SetContext (&ModifireContextNormal, Info, &TexOpaq);
				SetContext (&ModifireContextLast, Info, &TexOpaq);
#endif
				DisplayInformation ();
				swydelay = 1;
			}
		} else {
			if ( per->off & NJD_DGT_TY ){
				swydelay = 0;
			}
		}

}

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

