#include <proto/intuition.h>
#include <proto/graphics.h>
#include <intuition/intuition.h>
#include <exec/memory.h>
#include "skeleton.h"

int dispmode = ALL;

static struct Border border={ 0, 0, 1, 0, JAM1, -1, NULL, NULL};

void DrawFrame(obj_ptr frame)
{
	obj_ptr object;
	
	if(frame==NULL) return;

	object = frame->framedown;
	while(object!=NULL)
	{
	    if(((dispmode&IMAGE)!=0) ||
	      ((dispmode==ENTRY) && ((object->entry&IMAGE)!=0)))
		DrawObject(object->image,object->offset[0],object->offset[1]);
	    object = object->framedown;
	}

	object = frame->framedown;
	while(object!=NULL)
	{
	    if(((dispmode&SKELETON)!=0) ||
	      ((dispmode==ENTRY) && ((object->entry&SKELETON)!=0)))
		DrawObject(object->skeleton,object->offset[0],object->offset[1]);
	    object = object->framedown;
	}
	object = frame->framedown;
	while(object!=NULL)
	{
	    if(((dispmode&XOUTLINE)!=0) ||
	      ((dispmode==ENTRY) && ((object->entry&XOUTLINE)!=0)))
		DrawOutline(object->outline,object->skeleton,object->offset[0],object->offset[1]);
	    object = object->framedown;
	}
}

void FillObject(line_ptr line,int xoff, int yoff)
{
	SHORT *newbuff;
	int i;

	SetAPen(RP,line->fillc);
	
	if((xoff+line->box[0][0]<0)   || (yoff+line->box[0][1]<0) ||
	   (xoff+line->box[1][0]>ScrnWidth) || (yoff+line->box[1][1]>ScrnHeight))
	   	return;	/* put in clipping later */
	if(line->number>=AreaSize)
	{
		/* Added 1 incase AreaEnd adds a point */
		i = line->number + 1 + MEM_BLOCKSIZE - ((line->number+1)%MEM_BLOCKSIZE);
		newbuff = (SHORT *)malloc(5*i);
  		if(newbuff==NULL)
			return;
		free(AreaBuffer,5*AreaSize);
		AreaSize = i;
		AreaBuffer = newbuff;
		InitArea(RP->AreaInfo,AreaBuffer,AreaSize);
	}
	
	if(line->number<3)
		return;
			
	AreaMove(RP,line->pts->p[0][0],line->pts->p[0][1]);
	for(i=1;i<line->number;i++)
	{
		AreaDraw(RP,line->pts->p[i][0],line->pts->p[i][1]);
	}
	AreaEnd(RP);
		
}

void DrawObject(line_ptr line,int xoff, int yoff)
{
	int i;
	
	while(line!=NULL)
	{
	   if(line->fillc!=0) FillObject(line,xoff,yoff);
	   border.FrontPen = line->linec;
	   i  = 0;
	   while(line->number>i)
	   {
		border.XY = (SHORT *)line->pts->p[i];

		if(line->number-i<=101)
		{
			border.Count = line->number-i;
			i = line->number;
		}
		else
		{
			border.Count = 101;
			i = i + 100;
		}
		
		if(border.Count==1)
			border.Count = 2;
		if(border.Count>1)
		{
			DrawBorder(RP,&border,xoff,yoff);
		}
	   }
		line = line->next;
	}
}

void DrawOutline(line_ptr out,line_ptr skel,int xoff, int yoff)
{
    int i;
    SHORT points[3][2];
    
    if(out==NULL) return;

    border.FrontPen = out->linec;    
    border.Count = 3;
    border.XY = points[0];
    
    for(i=0;i<out->number;i++)
    {
	points[0][0]=points[1][0]=points[2][0] = out->pts->p[i][0];
	points[0][1]=points[1][1]=points[2][1] = out->pts->p[i][1];
	
	if((skel!=NULL) && (i/2<skel->number))
	{
		points[0][0] = skel->pts->p[i/2][0];
		points[0][1] = skel->pts->p[i/2][1];
	}
	if(i<out->number-2)
	{
		points[2][0] = out->pts->p[i+2][0];
		points[2][1] = out->pts->p[i+2][1];		
	}
	DrawBorder(RP,&border,xoff,yoff);
    }
}

void EraseFrame(obj_ptr frame)
{
	SetRast(RP,0);
}

