/*
  Zkusebni program pro plasmu v AA-lib
  Pro int21h napsal BOby (<)2006
*/

#include <stdio.h>
#include <stdlib.h>
#include <aalib.h>
#include <math.h>

void res(aa_context *scr) {
	aa_resize(scr);
}

float radian(float x) {
	return x*M_PI/180;
}

int main(int argc, char **argv) {
	aa_context *scr;
	if (!aa_parseoptions(NULL,NULL,&argc,argv) || argc!=1) {
		printf("%s\nUssage:\n%s",argv[0],aa_help);
		exit(2);
	}
	scr=aa_autoinit(&aa_defparams);
	if (scr==NULL) {
		fprintf(stderr,"Auto-initialization failed.Try -driver <dr>\n");
        	exit(3);
	}
	aa_autoinitkbd(scr,AA_SENDRELEASE);
	aa_hidecursor(scr);
	aa_resizehandler(scr,res);

        aa_render(scr,&aa_defrenderparams,0, 0,
                       aa_scrwidth(scr),
                       aa_scrheight(scr));
	
	int integer,xhodnota,yhodnota,x,y;
	int WIDTH=aa_imgwidth(scr);
	int HEIGHT=aa_imgheight(scr);
	unsigned int t=0;
	int sins[513];
	int definice[WIDTH-1];

	for (x=0;x<=512;x++) {
		sins[x]=(int )(sin(radian(x)/512*360)*256);
	}
	
	while (!aa_getkey(scr,0)) {
		t++;
		for (x=0;x<=WIDTH-1-1;x++) {
			definice[x]=10*sins[(x*4+t) & 511]+20*sins[(x+t*4) & 511]+30*sins[(x*4+t / 4) & 511];
		}
		for (y=0;y<=HEIGHT-1;y++) {
			yhodnota=30*sins[(y*8+t) & 511]+20*sins[(y+t*8) & 511]+10*sins[(y+t / 8) & 511];
			for (x=0;x<=WIDTH-1;x++) {
				xhodnota=definice[x];
				aa_putpixel(scr,x,y,(xhodnota+yhodnota)/20);
			}
		}
		aa_render(scr,&aa_defrenderparams,0, 0,
			WIDTH/2,
			HEIGHT/2);
		aa_flush(scr);
	}
	aa_close(scr);
	return 0;
}
