/* Fire av PEK & Arctic */

#include <stdlib.h>
#include <conio.h>
#include <mem.h>
#include <dos.h>

#include <iostream.h>
#include <time.h>


#include "f:\program\c\egna\include\GFX_Asm.c"


void setpal(void);


void 	main()
{
	unsigned int frames = 0, sp = 0;
	register unsigned int x;
	register unsigned int color;
	clock_t start, stop;

	unsigned char *array = new unsigned char[320*200], *darray = array+320*198, *zarray = array+32000;
	unsigned char *slump = new unsigned char[5000];

	setmode(0x013);

	setpal();

	memset(array,0,64000);

	randomize();


	for(x = 0; x<5000; x++)
		slump[x] = random(191);

	start = clock();

	while(!kbhit())
	{
		frames++;

		for(x = 0; x<640; x++, sp+=sp<5000 ? 1 : -5000)
			darray[x] = slump[sp];

		for(x = 32000; x<63360; x++)
		{
			color = array[x+319] + array[x+320] + array[x+321] + array[x+640];

			color >>= 2;

			if(color)
				array[x] = --color;
		}

		memcpy(MK_FP(0xA000,32000),zarray,30400);
	}

	stop = clock();


	delete[] array;
	delete[] slump;

	setmode(3);


	cout << "Quite optimized fire routine by ARCTiC & PEk!!" << endl;

	cout << "RunTime : " << (stop-start)/CLK_TCK << " secs" << endl;
	cout << "Frames : " << frames << endl;
	cout << "Average FrameRate : " << frames / ((stop-start)/CLK_TCK) << " Frames/Sec." << endl;

	cin.get();
}


void setpal(void)
{
	int c;

	outp(0x3C8, 0);

	for(c = 0; c<64; c++)
	{
		outp(0x3C9, c);
		outp(0x3C9, 0);
		outp(0x3C9, 0);
	}

	for(c = 0; c<64; c++)
	{
		outp(0x3C9, 63);
		outp(0x3C9, c);
		outp(0x3C9, 0);
	}

	for(c = 0; c<64; c++)
	{
		outp(0x3C9, 63);
		outp(0x3C9, 63);
		outp(0x3C9, c);
	}
}
