#ifndef _TPFSCREEN_C
#define _TPFSCREEN_C

#include <stdlib.h>
#include "screen.h"
#include "ang.h"
#include "vec.h"
#include "f1616.h"

tpfScreen *TpfScreen_New()
{
	tpfScreen *s;
	s = (tpfScreen *) malloc(sizeof(tpfScreen));
	s->zbuffer = 0;
	s->drawlist = 0;
	return s;
}

void TpfScreen_Delete(tpfScreen *s)
{
	if (s->zbuffer)
		free(s->zbuffer);
	if (s->drawlist) /* Assume it's empty */
		free(s->drawlist);
	free(s);
}

void TpfScreen_SetScreen(tpfScreen *s,int x,int y,int stride,int buffer)
{
	s->x = x;
	s->y = y;
	s->stride = stride;
	s->buffer = buffer;
	if (s->zbuffer)
		free(s->zbuffer);
	s->zbuffer = (f1616 *) malloc(sizeof(f1616)*x);
	if (s->drawlist)
		free(s->drawlist);
	s->drawlist = (tpfDrawObj **) malloc(sizeof(tpfDrawObj *)*x);
	for (y=0;y<x;y++)
	{
		s->drawlist[y] = 0;
		s->zbuffer[y] = 0x7FFFFFFF;
	}
}

void TpfScreen_SetViewPos(tpfScreen *s,tpfVec *p)
{
	s->pos = *p;
}

void TpfScreen_SetFov(tpfScreen *s,tpfAng left,tpfAng right,tpfAng top,tpfAng bottom)
{
	s->fovleft = left;
	s->fovright = right;
	s->fovtop = top;
	s->fovbottom = bottom;
}

void TpfScreen_ClearZBuffer(tpfScreen *s)
{
	int x;
	for (x=0;x<s->x;x++)
		s->zbuffer[x] = 0x7FFFFFFF; /* Largest positive number */
}

#endif
