/*---------------------------------------------------------------------------
	SPRITE
---------------------------------------------------------------------------*/

#include "soh.h"
#include "ipx.h"
#include "light.h"
#include "math.h"

extern long ltflag;
extern long NUM;
extern u_long CalcVertex(float x, float y, u_long col);
/*-------------------------------------------------------------------------*/
u_long CalcVertex(float x, float y, u_long col)
{
	SX_COLOR C;
	LIGHT *LT;
	register float R,G,B,V;
	register float sx,sy;
	long i;
	
	if (ltflag==0) return col;
	
	C.color = col;
	R=0.f;
	G=0.f;
	B=0.f;
	
	for (i=0; i<LIGHT_MAX; i++) {
		LT = lt[i];
		if (LT->used) {
			sx = x - LT->world_x;
			sy = y - LT->world_y;
			V  = LT->range/(sx*sx + sy*sy);
			R += LT->diff_r * V;
			G += LT->diff_g * V;
			B += LT->diff_b * V;
		}
	}
	
	if (R>1.f) R=1.f;
	if (G>1.f) G=1.f;
	if (B>1.f) B=1.f;
	
	C.argb.r = (u_char)((float)C.argb.r * R);
	C.argb.g = (u_char)((float)C.argb.g * G);
	C.argb.b = (u_char)((float)C.argb.b * B);
	
	return C.color;
}
/*-------------------------------------------------------------------------*/
void ixDispSpriteLight(SPRITE *spr)
{
	SX_VERTEX stx[4];
	u_long TEX=ixGetHeader(IXD_HEAD_TEXTBL);
	u_long PTN=ixGetHeader(IXD_HEAD_PTNTBL);
	PTNTBL *P;
	TEXTBL *T;
	register float r,sx,sy,cx,cy;
	register float sinr,cosr,zm;
	
	if ( (spr->attr&SPR_HIDE)||(spr->grp<0)||(spr->ptn<0) ) return;
	
	PTN = ixGetAddress( PTN + spr->grp*4 );
	P = (PTNTBL *)(PTN + sizeof(PTNTBL) * spr->ptn);
	T = (TEXTBL *)(ixGetAddress( TEX + P->texno*4 ));
	
	r = spr->roll*PI/32768.f;
	zm = spr->zoom / 512.f;
	sinr = (float)sin(r) * zm;
	cosr = (float)cos(r) * zm;
	
	sx = sinr * (P->x1-P->x0);
	sy = sinr * (P->y1-P->y0);
	cx = cosr * (P->x1-P->x0);
	cy = cosr * (P->y1-P->y0);
	
	stx[0].z = (float)spr->prio;
	stx[1].z = stx[0].z;
	stx[2].z = stx[0].z;
	stx[3].z = stx[0].z;
	
	if (spr->attr & SPR_HFLIP) {
		stx[0].u = GETWORD(P,4) / 256.f;
		stx[1].u = GETWORD(P,4) / 256.f;
		stx[2].u = GETWORD(P,0) / 256.f;
		stx[3].u = GETWORD(P,0) / 256.f;
	} else {
		stx[0].u = GETWORD(P,0) / 256.f;
		stx[1].u = GETWORD(P,0) / 256.f;
		stx[2].u = GETWORD(P,4) / 256.f;
		stx[3].u = GETWORD(P,4) / 256.f;
	}
	if (spr->attr & SPR_VFLIP) {
		stx[0].v = GETWORD(P,6) / 256.f;
		stx[1].v = GETWORD(P,2) / 256.f;
		stx[2].v = GETWORD(P,6) / 256.f;
		stx[3].v = GETWORD(P,2) / 256.f;
	} else {
		stx[0].v = GETWORD(P,2) / 256.f;
		stx[1].v = GETWORD(P,6) / 256.f;
		stx[2].v = GETWORD(P,2) / 256.f;
		stx[3].v = GETWORD(P,6) / 256.f;
	}
	
	stx[0].x = (float)spr->px - (cx - sy);
	stx[0].y = (float)spr->py - (sx + cy);
	stx[1].x = (float)spr->px - (cx + sy);
	stx[1].y = (float)spr->py - (sx - cy);
	stx[2].x = (float)spr->px + (cx + sy);
	stx[2].y = (float)spr->py + (sx - cy);
	stx[3].x = (float)spr->px + (cx - sy);
	stx[3].y = (float)spr->py + (sx + cy);
	
	stx[0].c.color = CalcVertex(stx[0].x , stx[0].y, spr->mate);
	stx[1].c.color = CalcVertex(stx[1].x , stx[1].y, spr->mate);
	stx[2].c.color = CalcVertex(stx[2].x , stx[2].y, spr->mate);
	stx[3].c.color = CalcVertex(stx[3].x , stx[3].y, spr->mate);
	
	sxSetTexture( GETWORD(T,2) );
	sxDraw2D( SX_PRIM_STRIP, stx, 4, 0);
	NUM++;
}
/*-------------------------------------------------------------------------*/
void ixDispSprite(SPRITE *spr)
{
	SX_VERTEX stx[4];
	u_long TEX=ixGetHeader(IXD_HEAD_TEXTBL);
	u_long PTN=ixGetHeader(IXD_HEAD_PTNTBL);
	PTNTBL *P;
	TEXTBL *T;
	register float r,sx,sy,cx,cy;
	register float sinr,cosr,zm;
	
	if ( (spr->attr&SPR_HIDE)||(spr->grp<0)||(spr->ptn<0) ) return;
	
	PTN = ixGetAddress( PTN + spr->grp*4 );
	P = (PTNTBL *)(PTN + sizeof(PTNTBL) * spr->ptn);
	T = (TEXTBL *)(ixGetAddress( TEX + P->texno*4 ));
	
	r = spr->roll*PI/32768.f;
	zm = spr->zoom / 512.f;
	sinr = (float)sin(r) * zm;
	cosr = (float)cos(r) * zm;
	
	sx = sinr * (P->x1-P->x0);
	sy = sinr * (P->y1-P->y0);
	cx = cosr * (P->x1-P->x0);
	cy = cosr * (P->y1-P->y0);
	
	stx[0].x = (float)spr->px - cx + sy;
	stx[0].y = (float)spr->py - sx - cy;
	stx[1].x = (float)spr->px - cx - sy;
	stx[1].y = (float)spr->py - sx + cy;
	stx[2].x = (float)spr->px + cx + sy;
	stx[2].y = (float)spr->py + sx - cy;
	stx[3].x = (float)spr->px + cx - sy;
	stx[3].y = (float)spr->py + sx + cy;
	
	stx[0].z = (float)spr->prio;
	stx[1].z = stx[0].z;
	stx[2].z = stx[0].z;
	stx[3].z = stx[0].z;
	
	stx[0].c.color = spr->mate;
	stx[1].c.color = spr->mate;
	stx[2].c.color = spr->mate;
	stx[3].c.color = spr->mate;
	
	if (spr->attr & SPR_HFLIP) {
		stx[0].u = GETWORD(P,4) / 256.f;
		stx[1].u = GETWORD(P,4) / 256.f;
		stx[2].u = GETWORD(P,0) / 256.f;
		stx[3].u = GETWORD(P,0) / 256.f;
	} else {
		stx[0].u = GETWORD(P,0) / 256.f;
		stx[1].u = GETWORD(P,0) / 256.f;
		stx[2].u = GETWORD(P,4) / 256.f;
		stx[3].u = GETWORD(P,4) / 256.f;
	}
	if (spr->attr & SPR_VFLIP) {
		stx[0].v = GETWORD(P,6) / 256.f;
		stx[1].v = GETWORD(P,2) / 256.f;
		stx[2].v = GETWORD(P,6) / 256.f;
		stx[3].v = GETWORD(P,2) / 256.f;
	} else {
		stx[0].v = GETWORD(P,2) / 256.f;
		stx[1].v = GETWORD(P,6) / 256.f;
		stx[2].v = GETWORD(P,2) / 256.f;
		stx[3].v = GETWORD(P,6) / 256.f;
	}
	
	sxSetTexture( GETWORD(T,2) );
	sxDraw2D( SX_PRIM_STRIP, stx, 4, 0);
	NUM++;
}
/*-------------------------------------------------------------------------*/
void ixDispSprite2(SPRITE *spr)
{
	SX_VERTEX stx[4];
	u_long TEX=ixGetHeader(IXD_HEAD_TEXTBL);
	u_long PTN=ixGetHeader(IXD_HEAD_PTNTBL);
	PTNTBL *P;
	register float cx,cy;
	
	PTN = ixGetAddress( PTN + spr->grp*4 );
	P = (PTNTBL *)(PTN + sizeof(PTNTBL) * spr->ptn);
	
	cx = (P->x1-P->x0) * 0.5f;
	cy = (P->y1-P->y0) * 0.5f;
	
	stx[0].x = (float)spr->px - cx;
	stx[0].y = (float)spr->py - cy;
	stx[1].x = (float)spr->px - cx;
	stx[1].y = (float)spr->py + cy;
	stx[2].x = (float)spr->px + cx;
	stx[2].y = (float)spr->py - cy;
	stx[3].x = (float)spr->px + cx;
	stx[3].y = (float)spr->py + cy;
	
	stx[0].z = (float)spr->prio;
	stx[1].z = stx[0].z;
	stx[2].z = stx[0].z;
	stx[3].z = stx[0].z;
	
	stx[0].u = GETWORD(P,0) / 256.f;
	stx[1].u = GETWORD(P,0) / 256.f;
	stx[2].u = GETWORD(P,4) / 256.f;
	stx[3].u = GETWORD(P,4) / 256.f;
	stx[0].v = GETWORD(P,2) / 256.f;
	stx[1].v = GETWORD(P,6) / 256.f;
	stx[2].v = GETWORD(P,2) / 256.f;
	stx[3].v = GETWORD(P,6) / 256.f;
	
	stx[0].c.color = 0xffffffff;
	stx[1].c.color = 0xffffffff;
	stx[2].c.color = 0xffffffff;
	stx[3].c.color = 0xffffffff;
	
	sxDraw2D( SX_PRIM_STRIP, stx, 4, 0);
	NUM++;
}
/*-------------------------------------------------------------------------*/
