//
// Project UnReality: The First Nintendo 64 Emulator
// Copyright (C) 1997-1998, Cold Fusion Entertainment
// All rights reserved
//
// This source code is unpublished propietary information of Cold Fusion Entertainment.  No portion of this software
// may be disclosed to any third party or copied or duplicated in any form, in whole or in part, without the prior
// written consent of Cold Fusion Entertainment.
// 

//
// $Log: rdp.h $
// Revision 1.2  1998/05/24 05:59:39  BREAKPOINT
// Fixed lots of bugs, implemented multiple tile sets, added initial decoding for RSP set other mode, and created a function table
// for Textured Rectangle drawing.
// Revision 1.1  1998/05/14 01:24:36  BREAKPOINT
// Initial revision
//

//---------------------------------------------------------------------------------------------------------------------

#ifndef RDP_H
#define RDP_H

#include <stdio.h>
#include <types.h>

// Format types
#define G_IM_FMT_RGBA		0		// RGBA, of course
#define G_IM_FMT_YUV		1		// YUV.. self-explanatory
#define G_IM_FMT_CI			2		// Color Index
#define G_IM_FMT_IA			3		// Intensity alpha
#define G_IM_FMT_I			4		// Intensity

// Size types
#define G_IM_SIZ_4b			0		// 16 color
#define G_IM_SIZ_8b			1		// 256 color
#define G_IM_SIZ_16b		2		// 5:5:5:1 encoding
#define G_IM_SIZ_32b		3		// 8:8:8:8 encoding

// Cycle types
#define G_CYC_1CYCLE		0
#define G_CYC_2CYCLE		1
#define G_CYC_COPY			2
#define G_CYC_FILL			3

void InitRDP(void);
void DoRDPEmulation(void);

#ifdef NO_EXTERN_RDP

#include "fixed32.cpp"

// Tile definition
typedef struct
{
	int format;						// G_IM_FMT_*
	int size;						// G_IM_SIZ_*
	int xsize;						// X size in texels
	int offset;						// Offset in texture memory of origin
	int palette;					// Palette to use

	bool clamp_u, clamp_v;			// Clamp U or V coordinates
	bool mirror_u, mirror_v;		// Mirror U or V coordinates
	int wrap_u, wrap_v;				// U and V coordinates to wrap texture at
	int offset_u, offset_v;			// U and V coordinates to offset origin

	Fixed32 u1, v1, u2, v2;			// U and V clamping coordinates
} RDP_TileDef;

void RDP_Unknown(void);
void RDP_NoOp(void);

void RDP_TriFill(void);
void RDP_TriFillZBuf(void);
void RDP_TriTxtr(void);
void RDP_TriTxtrZBuf(void);
void RDP_TriShade(void);
void RDP_TriShadeZBuf(void);
void RDP_TriShadeTxtr(void);
void RDP_TriShadeTxtrZBuf(void);
void RDP_TexRect(void);
void RDP_TexRect_Continue(void);
void RDP_TexRectFlip(void);

void RDP_LoadSync(void);
void RDP_PipeSync(void);
void RDP_TileSync(void);
void RDP_FullSync(void);

void RDP_SetKeyGB(void);
void RDP_SetKeyR(void);
void RDP_SetConvert(void);
void RDP_SetScissor(void);
void RDP_SetPrimDepth(void);
void RDP_SetOtherMode(void);

void RDP_LoadTLUT(void);
void RDP_SetTileSize(void);
void RDP_LoadBlock(void);
void RDP_LoadTile(void);
void RDP_SetTile(void);
void RDP_FillRect(void);
void RDP_SetFillColor(void);
void RDP_SetFogColor(void);
void RDP_SetBlendColor(void);
void RDP_SetPrimColor(void);
void RDP_SetEnvColor(void);
void RDP_SetCombine(void);

void RDP_SetTImg(void);
void RDP_SetZImg(void);
void RDP_SetCImg(void);

// Texture rectangle draws to a 16 bit CFB...  [5551]
void DTR16_RGBA16(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR16_RGBA32(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR16_YUV16(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR16_YUV32(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR16_CI(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR16_IA(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR16_I(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);

// Texture rectangle draws to a 32 bit CFB...  [8888]
void DTR32_RGBA16(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR32_RGBA32(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR32_YUV16(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR32_YUV32(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR32_CI(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR32_IA(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);
void DTR32_I(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv);

void (*RDPCommandTable[64])(void) = {
    RDP_NoOp,        RDP_Unknown,       RDP_Unknown,      RDP_Unknown,     RDP_Unknown,    RDP_Unknown,      RDP_Unknown,      RDP_Unknown,
	RDP_TriFill,     RDP_TriFillZBuf,   RDP_TriTxtr,      RDP_TriTxtrZBuf, RDP_TriShade,   RDP_TriShadeZBuf, RDP_TriShadeTxtr, RDP_TriShadeTxtrZBuf,
	RDP_Unknown,     RDP_Unknown,       RDP_Unknown,      RDP_Unknown,     RDP_Unknown,    RDP_Unknown,      RDP_Unknown,      RDP_Unknown,
	RDP_Unknown,     RDP_Unknown,       RDP_Unknown,      RDP_Unknown,     RDP_Unknown,    RDP_Unknown,      RDP_Unknown,      RDP_Unknown,
	RDP_Unknown,     RDP_Unknown,       RDP_Unknown,      RDP_Unknown,     RDP_TexRect,    RDP_TexRectFlip,  RDP_LoadSync,     RDP_PipeSync,
	RDP_TileSync,    RDP_FullSync,      RDP_SetKeyGB,     RDP_SetKeyR,     RDP_SetConvert, RDP_SetScissor,   RDP_SetPrimDepth, RDP_SetOtherMode,
	RDP_LoadTLUT,    RDP_Unknown,       RDP_SetTileSize,  RDP_LoadBlock,   RDP_LoadTile,   RDP_SetTile,      RDP_FillRect,     RDP_SetFillColor,
	RDP_SetFogColor, RDP_SetBlendColor, RDP_SetPrimColor, RDP_SetEnvColor, RDP_SetCombine, RDP_SetTImg,      RDP_SetZImg,      RDP_SetCImg
};

void (*RDP_DrawTexRect[2][5][4])(Fixed32 u, Fixed32 v, Fixed32 du, Fixed32 dv) = {
	NULL,     NULL,     DTR16_RGBA16, DTR16_RGBA32,
	NULL,     NULL,     DTR16_YUV16,  DTR16_YUV32,
	DTR16_CI, DTR16_CI, DTR16_CI,     DTR16_CI,
	DTR16_IA, DTR16_IA, DTR16_IA,     DTR16_IA,
	DTR16_I,  DTR16_I,  DTR16_I,      DTR16_I,
	NULL,     NULL,     DTR32_RGBA16, DTR32_RGBA32,
	NULL,     NULL,     DTR32_YUV16,  DTR32_YUV32,
	DTR32_CI, DTR32_CI, DTR32_CI,     DTR32_CI,
	DTR32_IA, DTR32_IA, DTR32_IA,     DTR32_IA,
	DTR32_I,  DTR32_I,  DTR32_I,      DTR32_I
};

#endif

#endif
