#include <sys\types.h>
#include <libgte.h>
#include <libgpu.h>
#include "typedefs.h"
#include "3d_gen.h"
#include "gpu.h"

/*
 * Rather than clear the whole screen, back polygons are calculated to cover
 * the parts of rooms not drawn because they are so far in the distance
 */
void S_InsertBackPolygon(sint32 min_x, sint32 min_y, sint32 max_x, sint32 max_y, uint32 col)
{
	POLY_G4 *p;
//	return;

	if (db.polyptr>=db.polybuf_limit)
		return;
//	printf("Hello\n");
	p = (POLY_G4 *)db.polyptr;
	*((uint32*)&(p->r0)) = col;
	*((uint32*)&(p->r1)) = col;
	*((uint32*)&(p->r2)) = col;
	*((uint32*)&(p->r3)) = col;
	setPolyG4(p);                          		// Gouraud Shaded Texture Map Quad
	p->x0 = min_x;
	p->y0 = min_y;
	p->x1 = max_x;
	p->y1 = min_y;
	p->x2 = min_x;
	p->y2 = max_y;
	p->x3 = max_x;
	p->y3 = max_y;
	MyaddPrim( db.ot,OTSIZE-2,p );
	db.polyptr=(char*)(p+1);
}

sint32 scrnx,scrny;

int GetScreenPosRel(sint32 in_x, sint32 in_y, sint32 in_z, PHD_VECTOR *out)
{
	sint32	x,y,z, *mptr;


	mptr = phd_mxptr;

	x = (*(mptr+M00) * in_x) + (*(mptr+M01) * in_y) + (*(mptr+M02) * in_z) + *(mptr+M03);
	y = (*(mptr+M10) * in_x) + (*(mptr+M11) * in_y) + (*(mptr+M12) * in_z) + *(mptr+M13);
	z = (*(mptr+M20) * in_x) + (*(mptr+M21) * in_y) + (*(mptr+M22) * in_z) + *(mptr+M23);

	if (z>=phd_znear)
	{
		if (z>phd_zfar)
			z = phd_zfar;
		out->z = z = phd_persp/z;
		out->x = x*z+phd_centerx;
		out->y = y*z+phd_centery;
		return(0);
	}
	else
	{
		out->x = x;
		out->y = y;
		out->z = phd_znear;
		return(1);
	}
}
