#include "pch.hpp"
//===========================================================================//
// File:	 Clipping.cpp													 //
// Project:  GameOS					                                         //
// Contents: Functions to clip primitives									 //
//---------------------------------------------------------------------------//
// Date      Who    Modification                                             //
// 04/25/99  AG		Created								 					 //
//																			 //
//---------------------------------------------------------------------------//
// Copyright (C) 1999 Microsoft Corp.						                 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//
#include "3DRasterizer.hpp"
#include "DXRasterizer.hpp"
#include "DirectX.hpp"
#include "Debugger.hpp"
#include "Texture Manager.hpp"







//
// Clip this quad to the current screen size and draw (used internally to GameOS)
//
// 03
// 12
//
// Alters the vertices passed in
//
void gos_ClipDrawQuad( gos_VERTEX* v ) 
{
//
// Texel offset
//
		if( Compatibility3D&TextureOffset )
		{
			if( RenderStates[gos_State_Texture] )
			{
				float TexOffsetU=0.0625f/TEXHANDLE_TO_PTR(RenderStates[gos_State_Texture])->Width();
				float TexOffsetV=0.0625f/TEXHANDLE_TO_PTR(RenderStates[gos_State_Texture])->Height();
				v[0].u+=TexOffsetU;
				v[0].v+=TexOffsetV;
				v[1].u+=TexOffsetU;
				v[1].v+=TexOffsetV;
				v[2].u+=TexOffsetU;
				v[2].v+=TexOffsetV;
				v[3].u+=TexOffsetU;
				v[3].v+=TexOffsetV;
			}
		}
		else
		{
			v[0].x-=0.4375f;
			v[0].y-=0.4375f;
			v[1].x-=0.4375f;
			v[1].y-=0.4375f;
			v[2].x-=0.4375f;
			v[2].y-=0.4375f;
			v[3].x-=0.4375f;
			v[3].y-=0.4375f;
		}
//
// Cater for blade pixel doubled mode
//
	float maxX=(float)Environment.screenWidth;
	float maxY=(float)Environment.screenHeight;

	if( Environment.Renderer==3 && RenderStates[gos_State_MonoEnable]&256 )
	{
		maxX/=2.0f;
		maxY/=2.0f;
	}	
//
// Quick check for whole quad off screen
//
	if( v[1].x >= maxX ||
		v[3].x <= 0.0f ||
		v[3].y >= maxY ||
		v[1].y <= 0.0f )
		return;
//
// Flush any renderstates
//
	if( DirtyStates )
		FlushRenderStates();
//
// Quick check for whole quad on screen
//
	if(	v[1].x >= 0.0f &&
		v[3].x <= maxX &&
		v[3].y >= 0.0f &&
		v[1].y <= maxY )
	{
		gos_DrawQuads( v, 4 );
		return;
	}
//
// Update debugging information
//
	#ifdef LAB_ONLY
	PrimitivesRendered+=2;
	QuadsRendered++;
	#endif
//
// If hardware can guard band clip just render it.
//
	if( HasGuardBandClipping )
	{
		#ifdef LAB_ONLY
		NumGuardBandClipped++;
		#endif
		SafeFPU();
		wDrawPrimitive( d3dDevice7, D3DPT_TRIANGLEFAN, D3DFVF_TLVERTEX, &v[0], 4,  0 );
		GameOSFPU();
		return;
	}
//
// Otherwise let D3D clip it
//
	#ifdef LAB_ONLY
	NumClipped+=2;
	#endif
	wSetRenderState( d3dDevice7, D3DRENDERSTATE_CLIPPING, 1 );
	wDrawPrimitive( d3dDevice7, D3DPT_TRIANGLEFAN, D3DFVF_TLVERTEX, &v[0], 4, 0 );
	wSetRenderState( d3dDevice7, D3DRENDERSTATE_CLIPPING, 0 );
	return;
}


