

#include <sys\types.h>
#include <gtemac.h>
#include <inline_c.h>
#include "typedefs.h"
#include "types.h"
#include "memory.h"
#include "gpu.h"
#include "3d_gen.h"
#include "3d_obj.h"
#include "specific.h"

extern void DownLoadMatrix();

extern uint32 horizon_colour;

TSV tsv_buffer[MAX_OBJECT_VERTICES];	// hmm this makes the max number of object vertices 256

extern UWORD ClutStartY;
extern void DrawSkyMesh(UWORD *);
extern long wibble;
extern void MakeMono(UWORD *s, UBYTE *d);

void	phd_PutPolygons_AtBack( sint16 *objptr, int clip, int type )
{
	if (type == 1)
		DrawMonoScreen(0x50, 0x50, 0x70);
	else
	{
//##		phd_PushMatrix();
//##		DownLoadMatrix();							// Now Download matrix to GTE
		DrawSkyMesh(objptr);
//##		phd_PopMatrix();
	}
}

/****************************************************************************
* CreateMonoScreen
*
* SYNOPSIS
*	void CreateMonoScreen()
*
* PURPOSE
*	Creates a 16-colour monochrome image of the game screen.
*
* INPUTS
*	None
*
* RETURNS
*	Nothing
*/
void CreateMonoScreen()
{
	int i;
	RECT r;
	UWORD *s;
	UBYTE *d;

/* -------- create monochrome screen */

	DrawSync(0);
	S_DumpScreen();
	DrawSync(0);

	setRECT(&r, 0, db.draw[db.current_buffer^1].clip.y, 256, 128);

	s = (UWORD *)(db.poly_buffer[0]+(PBSIZE-24576));
	d = (char *)(db.poly_buffer[0]+(PBSIZE-8192));

	for ( i=0 ; i<2 ; i++ )
	{
		StoreImage(&r, (ULONG *)s);
		DrawSync(0);
		MakeMono(s, d);

		r.y += 128;
		d += 128*128;
	}

	r.x += 256;
	r.y = 0;

	d = (char *)(db.poly_buffer[1]+(PBSIZE-8192));

	for ( i=0 ; i<2 ; i++ )
	{
		StoreImage(&r, (ULONG *)s);
		DrawSync(0);
		MakeMono(s, d);

		r.y += 128;
		d += 128*128;
	}

/* -------- transfer mono image into VRAM */

	setRECT(&r, 512, 0, 64, 256);
	StoreImage(&r, db.poly_buffer[0]+(PBSIZE-16384));
	LoadImage(&r, db.poly_buffer[0]+(PBSIZE-8192));
	r.x += 64;
	StoreImage(&r, db.poly_buffer[1]+(PBSIZE-16384));
	LoadImage(&r, db.poly_buffer[1]+(PBSIZE-8192));
	DrawSync(0);

	S_MemCpy(db.poly_buffer[0]+(PBSIZE-8192), db.poly_buffer[0]+(PBSIZE-16384), 8192*4);
	S_MemCpy(db.poly_buffer[1]+(PBSIZE-8192), db.poly_buffer[1]+(PBSIZE-16384), 8192*4);
}


void RemoveMonoScreen()
{
	RECT r;

	setRECT(&r, 512, 0, 64, 256);
	LoadImage(&r, db.poly_buffer[0]+(PBSIZE-8192));
	r.x += 64;
	LoadImage(&r, db.poly_buffer[1]+(PBSIZE-8192));
	DrawSync(0);
}

/****************************************************************************
* DrawMonoScreen
*
* SYNOPSIS
*	void DrawMonoScreen()
*
* PURPOSE
*	Displays the monochrome image of the game screen.
*
* INPUTS
*	None
*
* RETURNS
*	Nothing
*/
void DrawMonoScreen(char r, char g, char b)
{
	int i, j;
	POLY_FT4 *p = (POLY_FT4 *)db.polyptr;
	register UWORD clut = getClut(960, ClutStartY);

	for ( i=0 ; i<4 ; i++ )
	{
		for ( j=0 ; j<8 ; j++ )
		{
			UWORD x, y, w, h;

			x = j*64;
			y = i*64;
			w = x+64;
#ifdef NTSC
			h = (i == 3) ? y+48 : y+64;
#else
			h = y+64;
#endif

			setPolyFT4(p);
			setRGB0(p, r, g, b);
			p->clut = clut;
			setTPage(p, 0, 0, 512+(x>>2), 0);

			if (w == 256 || w == 512)
			{
				setXY4(p, x, y, w-1, y, x, h, w-1, h);
				if (h == 256)
					h--;
				setUV4(p, x, y, w-1, y, x, h, w-1, h);

				addPrim(&db.ot[2000], p);
				p++;

				setPolyFT4(p);
				setRGB0(p, r, g, b);
				setXY4(p, w-1, y, w, y, w-1, h, w, h);
				setUV4(p, w-1, y, w-1, y, w-1, h, w-1, h);

				setTPage(p, 0, 0, 512+(x>>2), 0);
				p->clut = clut;

				addPrim(&db.ot[2000], p);
				p++;
			}
			else
			{
				setXY4(p, x, y, w, y, x, h, w, h);
				if (h == 256)
					h--;
				setUV4(p, x, y, w, y, x, h, w, h);

				addPrim(&db.ot[2000], p);
				p++;
			}
		}
	}
	db.polyptr = (char *)p;
}
