/* Copyright 2008 Jeffrey Lee
   This file is part of GnarlPlot.
   GnarlPlot is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   GnarlPlot is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with GnarlPlot.  If not, see <http://www.gnu.org/licenses/>. */

#include <stdio.h>
#include <stdlib.h>
#include "kernel.h"
#include "swis.h"

#include "screen.h"
#include "spr.h"

/* Simple GnarlPlot test program. Link it with libgp and libwoum to get it to work. */

int doody(int c, int v)
{
	int r,g,b;
	v+=v >> 7;
	r = c & 255;
	g = (c & 0xFF00) >> 8;
	b = (c & 0xFF0000) >> 16;
	r*=v;
	g*=v;
	b*=v;
	r = r >> 8;
	g = g >> 8;
	b = b >> 8;
	return r + (g << 8) + (b << 16);
}

int main(int argc,char **argv)
{
	gp_screen *scr;
	gp_spr *spr;
	gp_sprfmt *f;
	int x,y,l;
	_kernel_swi_regs regs;
	scr = gp_screen_new_fromvdu();
	if (scr == 0)
		printf("scr fail\n");
	scr->bank1 = scr->bank0;
	f = &gp_spr_00;
	spr = (f->fromscreen)(f,scr,0,0,scr->width/2,scr->height/2,0,0);
/*	scr->mt = GP_MT_GREY;*/
/*	spr = (gp_spr_20.create)(&gp_spr_20,256,256,0,0);*/
	if (spr == 0)
		printf("spr fail\n");
/*	printf("%dx%d, ps %d, mt %d\n",(spr->f->getw)(spr),(spr->f->geth)(spr),(spr->f->getps)(spr),(spr->f->getmt)(spr));*/
	for (x=0;x<256;x++)
		for (y=0;y<(spr->f->geth)(spr);y++)
		{
			(spr->f->setmask)(spr,x,y,GP_MT_GREY,2,255-x);
			(spr->f->setpix)(spr,x,y,2,doody((spr->f->getpix)(spr,x,y,2),x));
		}
/*	for (x=0;x<256;x++)
		for (y=0;y<256;y++)
			if ((spr->f->setpix)(spr,x,y,2,x+256*y))
				printf("write fail\n");*/
	do {
		_kernel_swi(OS_Mouse,&regs,&regs);
		x = (f1616_sin(regs.r[0] << 15)*regs.r[1]) >> 8;
		y = (f1616_cos(regs.r[0] << 15)*regs.r[1]) >> 8;
		l = (spr->f->geth)(spr);
/*		x = regs.r[0] << 8;
		y = regs.r[1] << 8;
		l = f1616_div((spr->f->geth)(spr) << 16,y) >> 16;*/
#if 0
		while (l-- > 0) {
			(spr->f->tplot)(spr,scr->width/4,scr->height/4+l,scr,(spr->f->getw)(spr),-y*l,x*l,x,y);
/*			(spr->f->tplot)(spr,scr->width/4,scr->height/4+l,scr,(spr->f->getw)(spr),0,y*l,x,0);*/
		}
#else
		gp_spr_plot_rotscale(spr,scr->width/2,scr->height/2,scr,x,y);
#endif
	} while (1);
	return 0;
}
