/*****************************************************************************\
     Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
                This file is licensed under the Snes9x License.
   For further information, consult the LICENSE file in the root directory.
\*****************************************************************************/

#ifndef _GFX_H_
#define _GFX_H_

#include "port.h"

struct SGFX
{
	SNESPixel	*Screen;
	SNESPixel	*SubScreen;	
	uint8	*ZBuffer;
	uint8	*SubZBuffer;
	SNESPixel	*ZERO;
//	uint32	Pitch;
	uint32	ScreenSize;
	SNESPixel	*S;	
	uint8	*DB;
	uint32	PPL;				// number of pixels on each line
	SNESPixel	*ScreenColors;		// screen colors for rendering main
	SNESPixel	*RealScreenColors;	// screen colors, ignoring color window clipping
	uint8	Z1;					// depth for comparison
	uint8	Z2;					// depth to save
	uint32	FixedColour;
	uint32	StartY;
	uint32	EndY;
	uint32	ClipColors;
	uint8	OBJWidths[128];
	uint8	OBJVisibleTiles[128];

	struct ClipData	*Clip;

	struct
	{
		int16	RTOFlags;
		int16	Tiles;

		struct
		{
			int16	Sprite;
			int16	Line;
		}	OBJ[32];
	}	OBJLines[SNES_HEIGHT_EXTENDED];

	void	(*DrawBackdropMath) (uint32, uint32, uint32);
	void	(*DrawBackdropNomath) (uint32, uint32, uint32);
	void	(*DrawTileMath) (uint32, uint32, uint32, uint32);
	void	(*DrawTileNomath) (uint32, uint32, uint32, uint32);
	void	(*DrawClippedTileMath) (uint32, uint32, uint32, uint32, uint32, uint32);
	void	(*DrawClippedTileNomath) (uint32, uint32, uint32, uint32, uint32, uint32);
	void	(*DrawMosaicPixelMath) (uint32, uint32, uint32, uint32, uint32, uint32);
	void	(*DrawMosaicPixelNomath) (uint32, uint32, uint32, uint32, uint32, uint32);
	void	(*DrawMode7BG1Math) (uint32, uint32, int);
	void	(*DrawMode7BG1Nomath) (uint32, uint32, int);
	void	(*DrawMode7BG2Math) (uint32, uint32, int);
	void	(*DrawMode7BG2Nomath) (uint32, uint32, int);

	const char	*InfoString;
	uint32	InfoStringTimeout;
};

struct SBG
{
	uint8	(*ConvertTile) (uint8 *, uint32, uint32);
	uint8	(*ConvertTileFlip) (uint8 *, uint32, uint32);

	uint32	TileSizeH;
	uint32	TileSizeV;
	uint32	OffsetSizeH;
	uint32	OffsetSizeV;
	uint32	TileShift;
	uint32	TileAddress;
	uint32	NameSelect;
	uint32	SCBase;

	uint32	StartPalette;
	uint32	PaletteShift;
	uint32	PaletteMask;
	uint32	EnableMath;

	uint32	Buffered;
	uint32	BufferedFlip;
	uint32	DirectColourMode;
};

struct SLineMatrixData
{
	int16	MatrixA;
	int16	MatrixB;
	int16	MatrixC;
	int16	MatrixD;
	int16	CentreX;
	int16	CentreY;
	int16	M7HOFS;
	int16	M7VOFS;
};

struct SLineData
{
	struct
	{
		uint16	VOffset;
		uint16	HOffset;
	}	BG[4];
};

extern struct SBG	BG;
extern struct SGFX	GFX;

#define FONT_WIDTH  8
#define FONT_HEIGHT 9

#define H_FLIP		0x4000
#define V_FLIP		0x8000
#define BLANK_TILE	2

void S9xStartScreenRefresh (void);
void S9xEndScreenRefresh (void);
void S9xRenderLine (int);
void S9xGraphicsScreenResize (void);
void S9xDisplayMessages (SNESPixel *, int, int, int, int);
void S9xUpdateScreen (void);

// external port interface which must be implemented or initialised for each port
bool8 S9xGraphicsInit (void);
void S9xGraphicsDeinit (void);
bool8 S9xBlitUpdate (int, int);
void S9xSyncSpeed (void);
void S9xAutoSaveSRAM (void);

#endif
