program InputMe;
// Use the CGA line renderer
@define LINE_PUTPIXEL "_putpixel_line_cga"

@use "gfx/gfx"
var  
    i,j, time,t: byte; 
    update,trigger: byte=0;
	ds_ddx,delta,xx,yy : integer;
	src, dst,zp : pointer;
	px, py,x,y,z : integer = 30;
	@donotremove ds_xx
	@donotremove ds_yy
	@donotremove ds_ddx
	@donotremove ds_ddy
	ds_xx, ds_yy : integer;
		sine : incbin("data/sine.bin");

		cols : array[4] of byte=(cga_color1, cga_color1, cga_color2, cga_color3);
		
	perlin : incbin("data/perlin64.dat");
	const landscapeWidth : byte = 16;
	const landscapeLength : integer = 1024;
	landscape : array[landscapeLength] of byte;
	curPos : integer = 0;

/*

	TEST with movement
	W A S D   to move around.

*/


@export "images/image2.flf" "images/image2.bin" 0
	data : incbin("images/image2.bin");


//@spritecompiler "images/sprites.flf" "cat" 0 0 5 3
@spritecompiler "images/sprites.flf" "plane" 0 5 4 3
// Spritecompiler outputs a method named drawsprite_cga_cat(source,dest, x,y)

procedure InitLandscape();
begin
	xx:=0;
	for x:=0 to landscapeLength do 
	begin
		
		yy:=perlin[x];
		y:=perlin[xx]/4;
		y:=yy-y;
		y:=260-y;
		if (y>190) then y:=190;
		landscape[x]:=y;
		xx:=xx+4
	end;

end;

procedure RenderLandscape(rl_color:byte);
begin
	if (update=0) then
		return();
	x:=landscapeWidth- delta;
	xx := 320 / landscapeWidth;
	z:=curPos;
		
	for i:=0 to landscapeWidth-1 do
	begin
		Gfx::DrawLine(x, landscape[z], x+xx,landscape[z+1] , rl_color, ptr(screen_cga,0));
		x:=x+xx;
		z:=z+1;
	end;
	update:=update-1;

end;

procedure RenderTriggerLandscape();
begin
	RenderLandscape(cga_color0);
	if (trigger=1) then
	begin
		delta:=delta +8;
		if (delta=landscapeWidth) then
		begin
			curPos:=curPos+1;
			delta:=0;
		end;
	end;
	if (trigger=2) then
	begin
		if (delta=0) then
		begin
			curPos:=curPos-1;
			delta:=landscapeWidth;
		end;
		delta:=delta -8;
	end;
	trigger:=0;
	RenderLandscape(cga_color1);

end;




begin
	init_cga_scanlines();
	init_drawline_cga();
	init_keyboard_irq();
	setscreenmode(mode_cga_320x200);
	InitLandscape();	

	fillw(#data,0,8000);

	px:=90;
	py:=30;
	update:=2;


	InstallKeyboardIRQ();	

	while (true) do
	begin
		waitforverticalblank();

		if (currentKey=key_d) then px:=px+2;
		if (currentKey=key_a) then px:=px-2;
		if (currentKey=key_s) then py:=py+2;
		if (currentKey=key_w) then py:=py-2;
		
		if (px>100) then
		begin
			px:=98;
			update:=2;
			trigger:=1;
		end;		
		if (px<80) then
		begin
			px:=82;
			update:=2;
			trigger:=2;
		end;		
		drawsprite_cga_plane(#data,screen_cga,px,py);
		RenderTriggerLandscape();	
	end;

end.
