/******************************************************************************************
	CRTEST PS   (c) 1999-2001 ISL

	overlays.h:			Polygon & sprite overlays
******************************************************************************************/

#ifndef _OVERLAYS_H_
#define _OVERLAYS_H_



// ---- Transparent Debug Polygons ----
// A Debug Polygon is a semitrans polygon in the designated colour,
// with a bright outline border (Unless you've asked for it not to have one)
// Large debug polygons can be optionally subdivided so they always show up.
// You can also ask to have it at (slightly closer than) it's 3d depth, rather than the
// default of sortlist-position 10

#define DEBUGPOLY_NOBORDER 1
#define DEBUGPOLY_USEDEPTH 2
#define DEBUGPOLY_DUBSIDED 4
#define DEBUGPOLY_SUBDIV   8

#define DEBUGPOLY_NOTEDGE0	256
#define DEBUGPOLY_NOTEDGE1	512
#define DEBUGPOLY_NOTEDGE2	1024
#define DEBUGPOLY_NOTEDGE3	2048

#define OVL_LOAD_MAX 4096

// ---- Flags used by overlayAlphaPoly
#define ALPHA_POLY_ST_SUB   1





void overlayShowDebugPolygon(VECTOR *worldvecs, int n_verts, unsigned int bgr, int flags);
void overlayShowDebugLine(VECTOR *v1, VECTOR *v2, unsigned int bgr, int flags);

// Calls showdebugpolygon 6 times, with subdiv & usedepth flags both set, for a big transparent cube.
void overlayShowBigBox(int xl, int xh, int yl, int yh, int zl, int zh, unsigned int bgr);

// Similarly, here's three overlapping polygons to represent a sphere.
void overlayShowSphere(int x, int y, int z, int radius, unsigned int bgr);



// ---- Show the collision mesh in debugpolygons ----
void overlayShowMapCollision();			// called by "showcollision"...
void overlayShowPlatformCollision();	// called by "showcollision"...
void overlayShowCollision();

// Pad 2's CROSS button cycles between this little lot
enum
{
	DEBUG_OVERLAY_NONE,
	DEBUG_OVERLAY_LEVEL_COLLISION,
	DEBUG_OVERLAY_PLATFORM_COLLISION,
	DEBUG_OVERLAY_PUZZLE_BOXES,
	DEBUG_OVERLAY_ENEMY_INFO,
	DEBUG_OVERLAY_ROUTE_FIND,
	N_DEBUG_OVERLAYS
};

extern int overlay_show_collision;



// ---- Pre-production display stuff ----
// For having "Chicken Run Preview, Interactive Manta, Date" scrawled all over the screen.
void overlayPreviewOverlay();


// Some simple things for prototyping purposes. Filled rectangles & line-draws.
// (actually, they're quick & easy to do direct, but if speed's not too important, use these for clarity)
void overlayFilledRectangle(int x, int y, int w, int h, unsigned int bgr);
void overlayLine(int x1,int y1, int x2, int y2, unsigned int bgr);
void overlayOutlineRectangle(int x1,int y1, int w, int h, unsigned int bgr);



// ---- True-Alpha sprites ----

// Extra flags define I put into DosChop, which isn't in ISLTEX
#define NVSPLIT		64

extern USHORT overlaySolidAlphaClut;
extern USHORT overlayAlphaClut;
extern USHORT overlayAlphaClut256;
void overlayGrabAlphaCluts();
// High byte of colour is used for the alpha mask, Low byte is the additive, with 128 being normal, remember.
// (So for smooth alpha, use "(0...255)<<24, and (0...128)* 0x10101 )
int overlayAlphaSprite(TextureType *spr, int x, int y, int depth, unsigned long colour);

// And a solid one
int overlaySprite(TextureType *spr, int x, int y, int depth, unsigned long colour);

// Show a sprite flipped about the y axis
int overlaySpriteFlipped(TextureType *spr, int x, int y, int depth, unsigned long colour);
// scales are 256 for normal
int overlaySpriteScaled(TextureType *spr, int x, int y, int depth, unsigned long colour, int xscale, int yscale);
int overlaySpriteNoScale(TextureType *spr, int x, int y, int depth, unsigned long colour);


// And a +ve/-ve/semitrans playstation-stylee one
#define OV_TRANSSPR_ADD		((SEMITRANS_ADD-1) << (24+5))
#define OV_TRANSSPR_SUB		((SEMITRANS_SUB-1) << (24+5))
#define OV_TRANSSPR_SEMI	((SEMITRANS_SEMI-1) << (24+5))
int overlayTranspSprite(TextureType *spr, int x, int y, int depth, unsigned long colour);




// ---- Loading "Egg" overlay on the irq ----

extern int overlayLoadingDisplay;

void overlayLoadingBegin(int effect);	// call at the start of a loading session
void overlayLoadingVsync();	// call on the vsync if loadingdisplay is non-zero
void overlayLoadingEnd();	// call at the end of a loading session

// Screen whiteouts
//extern int overlayOVtimer;
void overlayScreenAdd(int b, int g, int r, int transp);
void overlayStartOverlay(int timer, int whitetime, int colour);
void overlayDraw();

// =================== Enhancements to ISLFONT ======================


int fontCentredMultiLine(psFont *font, int x, int y, int width, char *text, unsigned char r, unsigned char g, unsigned char b);
#define fontPrintCentred(f,x,y,str,r,g,b) fontPrint((f),(x)-fontExtentW((f),(str))/2, (y),(str),(r),(g),(b))
#define fontPrintRightAligned(f,x,y,str,r,g,b) fontPrint((f),(x)-fontExtentW((f),(str)), (y),(str),(r),(g),(b))

void fontPrintMultiLineBox(psFont *font, int x, int y, int width, char *text, unsigned char r, unsigned char g, unsigned char b);
int crFontFitToWidth(psFont *font, int width, char *text, char *buffer);



// the return value here is to an internal char[100]
// So don't send long strings in, and don't expect the result to be preserved
char *fontUpper(char *text);

psFont *fontTidy(psFont * font);
TextureBankType *textureLoadInOneGo(char *filename);

// Message overlaid bottom-centre of screen DEBUG only
void overlayDebugMessage(char *msg, int time);

// Message overlaid bottom-centre of screen DEBUG or EIDOS_DEBUG_STUFF only
void overlayBurnDebugMessage(char *msg, int time);


// =================== Enhancements to ISLTEX ======================


TextureBankType *textureTidyBank(TextureBankType *bank);


// =================== Animating Texture Management ======================

#define BFF_TEXANIM_ID (('T'<<0) | ('A'<<8) | ('N'<<16) | (0<<24))

#define CLUT_16S_REQUIRED 256
#define CLUT_256S_REQUIRED 16

extern int noEggFade;

typedef struct TEXANIMFRAME
{
	TextureType *tmap;	// name CRC, converted to a tmap pointer postload
	int end_time;
}TEXANIMFRAME;
typedef struct TEXANIMHEADER
{
	struct TEXANIM *next;
	int tag;
	int n_frames;

	int duration;	// summation of the frame counts. Fixed point
	TextureType *target;	// name CRC, converted to a tmap pointer postload
	int speed;

	int current_time;	// fixed point, between 0 & duration
	int current_speed;

}TEXANIMHEADER;

typedef struct TEXANIM
{
	TEXANIMHEADER header;
	TEXANIMFRAME frames[1];
}TEXANIM;


typedef struct TEXANIMLIST
{
	unsigned long id;		// Standard BFF header
	unsigned long len;
	unsigned long crc;

// Followed by the first header of the list
}TEXANIMLIST;


void texanimInit();
void texanimLinkBFF(TEXANIMLIST *list);
void texanimDraw();
void texanimCycle();
TEXANIM *texanimFind(int tag);
void texanimSetSpeed(TEXANIM *anim, int speed);

// This should only be used for needles on dials
int overlaySpriteRotate(TextureType *spr, int x, int y, int xoffs, int yoffs, int depth, int rot,unsigned long colour);

// Animating textures on PSIs with more than one instance
// (eg - multiple dogs with independantly blinking eyes)

// To do this needs to hit...
// tpage
// Let's assume the clut stays the same
// tu0,tv0,tu1,tv1,tu2,tv2,tu3,tv3
#define MAX_INSTANCE_POINTERS 100
typedef struct TEXINSTANCE
{
	TextureType tex;

	int n_pointers;
// 4 pointers per TG3, 5 pointers per TG4
	short *pointers[MAX_INSTANCE_POINTERS];
	short values[MAX_INSTANCE_POINTERS];
}TEXINSTANCE;
TEXINSTANCE *texanimSetupInstanceModel(PSIMODEL *model, int textureCrc, int new_texture);
void texanimInstanceModel(TEXINSTANCE *instance);

void overlayAlphaPoly(TextureType *spr,VECTOR *worldvecs, int n_verts, unsigned int bgr, int flags);

void overlayAlphaBox(int x, int y, int w, int h, int b, int g, int r);
void fontPrintMultiLineBoxSetDepth(psFont *font, int x, int y, int width, char *text, unsigned char r, unsigned char g, unsigned char b, int depth);
void fontPrintMultiLineBox(psFont *font, int x, int y, int width, char *text, unsigned char r, unsigned char g, unsigned char b);
int overlaySpriteScaled2(TextureType *spr, int x, int y, int depth, unsigned long colour, int xscale, int yscale);


#endif
