#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#include <clib/graphics_protos.h>
#include <clib/intuition_protos.h>
#include <clib/cybergraphics_protos.h>
#include <cybergraphx/cybergraphics.h>
#include <pragmas/cybergraphics_pragmas.h>
#include <libraries/mui.h>
#include <graphics/gfx.h>
#include <iostream.h>
#include <math.h>

#include "output/pp.h"
#define SQUARESIZE 4

PixelPlotOutput::PixelPlotOutput(){
	curX=0;
	curY=0;
	oa_SetName("PixelPlot Output");
	oa_Author="T.Miles";
	oa_Version="1.0";
	op_Type=MAKE_TYPE('P','P');
	op_Configured=TRUE;
}

PixelPlotOutput::~PixelPlotOutput(){
}

BOOL PixelPlotOutput::StartUp(){
	AddTrackName("Plot_Red");
	AddTrackName("Plot_Blue");
	AddTrackName("Plot_Green");
	AddTrackName("Plot_Magenta");
	AddTrackName("Plot_Cyan");
	AddTrackName("Plot_Yellow");
//	oa_SetStatus(APP_STATUS_REALTIME,TRUE);	// Say that we want to use the timer
	return OmegaOutput::StartUp();
}

void PixelPlotOutput::DrawNextFrame(UINT trackNo){ 
	UBYTE value=o_GetCurrentValue(trackNo);
	ULONG ARGB;
	UINT r=255,g=255,b=255;
	switch (trackNo){
	case 0:					// Shades of red
		g=b=value;
		break;
	case 1:					// Shades of blue
		r=g=value;
		break;
	case 2:					// Shades of green
		r=b=value;
		break;
	case 3:
		r=value;
		break;
	case 4:
		g=value;
		break;
	case 5:
		b=value;
		break;
	}
	ARGB=(r<<16)+(g<<8)+b;

	switch(direction){
	case 0:
		curX+=SQUARESIZE;
		curRadius+=SQUARESIZE;
		if (curRadius>=maxRadius){
			curRadius=0;
			direction++;
		}			
		break;
	case 1:
		curY-=SQUARESIZE;
		curRadius+=SQUARESIZE;
		if (curRadius>=maxRadius){
			maxRadius+=SQUARESIZE;
			curRadius=0;
			direction++;
		}
		break;
	case 2:
		curX-=SQUARESIZE;
		curRadius+=SQUARESIZE;
		if (curRadius>=maxRadius){
			curRadius=0;
			direction++;
		}		
		break;
	case 3:
		curY+=SQUARESIZE;
		curRadius+=SQUARESIZE;
		if (curRadius>=maxRadius){
			maxRadius+=SQUARESIZE;		
			curRadius=0;
			direction=0;
		}		
		break;
	}

	if (maxRadius>o_Height){
		maxRadius=SQUARESIZE;
		curX=o_Width/2;
		curY=o_Height/2;
	}
	
	for (UINT n=0;n<SQUARESIZE;n++){
		for (UINT f=0;f<SQUARESIZE;f++){
			WriteRGBPixel(&o_RPort,curX+n,curY+f,ARGB);
		}
	}
}

void PixelPlotOutput::OnStart(){
	direction=0;
	curX=o_Width/2;
	curY=o_Height/2;
	curRadius=0;
	maxRadius=SQUARESIZE;
}