/*
   Copyright 2008 Jeffrey Lee
   This file is part of WOUM.
   WOUM 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.
   WOUM 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 WOUM.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "kernel.h"
#include "swis.h"

#include "span.h"

#include "screen.h"
/*#include "horzline.h"*/

extern void dumptree(int n);

void linedraw(int col,void *ptr,int len)
{
	int x,y;
	char *c;
	c = (char *) ptr;
	x = ptr-span_buf;
	y = x / ((span_width << span_ps) + span_gap);
	x = x % ((span_width << span_ps) + span_gap);
/*	if ((col) || (x))
		printf("L: c%08x x%d y%d l%d\n",col,x,y,len);*/
	while (len-- > 0)
	{
		*(c++) = col;
		if (span_ps > 0)
			*(c++) = col >> 8;
		if (span_ps > 1)
		{
			*(c++) = col >> 16;
			*(c++) = col >> 24;
		}
	}
}

int main(int argc,char **argv)
{
	FILE *log;
	int line;
	int xs,xe;
	invz zs,zg;
	int col,y;
	int c;
	int _xs,_y;
	if (argc < 5)
	{
		printf("Usage: sss <width> <height> <ps> <log file>\n");
		return 1;
	}
	screen_rpcmode(atoi(argv[1]),atoi(argv[2]),atoi(argv[3])+3);
	screen_getdata();
	span_lines = screen.height+1;
	span_width = screen.width+1;
	span_buf = (void *) screen.vdu;
	if ((screen.colours == MODE_8B) || (screen.colours == MODE_8BP))
		span_ps = 0;
	else if (screen.colours == MODE_16B)
		span_ps = 1;
	else
		span_ps = 2;
	span_init();
	log = fopen(argv[4],"r");
	line=0;
	while (!feof(log))
	{
		fscanf(log,"%d\t%d\t%08x\t%08x\t%08x\t%d\n",&xs,&xe,&zs,&zg,&col,&y);
		do {
			printf("Line %d: %d\t%d\t%08x\t%08x\t%08x\t%d\n",line,xs,xe,zs,zg,col,y);
			printf("(D)raw screen, (S)kip line, (A)dd line, (R)ewind, d(U)mp row, r(E)curse dump?");
			do {c = getchar();} while (c < 32);
			if ((c == 'D') || (c == 'd'))
			{
				screen_getdata();
				screen_cls();
				span_buf = (void *) screen.vdu;
				span_do2(linedraw); /* Slow! */
			}
			else if ((c == 'A') || (c == 'a'))
			{
				span_add(xs,xe,zs,zg,col,y);
				line++;
				break;
			}
			else if ((c == 'S') || (c == 's'))
			{
				line++;
				break;
			}
			else if ((c == 'R') || (c == 'r'))
			{
				span_free(0);
				span_init();
				screen_cls();
				line = 0;
				fseek(log,0,SEEK_SET);
				break;
			}
			else if ((c == 'U') || (c == 'u'))
			{
				printf("Enter start pos: ");
				scanf("%d",&_xs);
				scanf("%d",&_y);
				_xs = span_readstart(0,_xs,_y);
				printf("Rounding down to %d\n",_xs);
				while (_xs < span_width)
				{
					printf("%d\t%d\t%.2f\t%08x\t%d\n",_xs,span_readlen(0,_xs,_y),f1616_ToFloat(span_readdepth(0,_xs,_y)),span_readcol(0,_xs,_y),span_readstart(0,_xs,_y));
					_xs += span_readlen(0,_xs,_y);
				}
			}
			else if ((c == 'E') || (c == 'e'))
			{
				printf("Enter start pos: ");
				scanf("%d",&_y);
				dumptree(_y);
			}
		} while (1);
	}
	fclose(log);
	return 0;
}

