program CGA2;

// Open this image, experiment with importing your own!
@export "images/image1.flf" "images/image1.bin" 0

var
	time:byte = 0;
	y: integer = 0;
	p1, p2,p3 : pointer;
	data : incbin("images/image1.bin");
	sine : incbin("data/sine.bin");
	

begin
	// Set CGA screen mode
	SetScreenMode(mode_cga_320x200);
	

	
	memcpy16(#data,screen_cga, 8000); // Display the original image. 
	//Wait a bit
	for y:=0 to 80 do waitforverticalblank();


	// Set p2 to point to the center of the screen somewhere	
	// Point to scanlines 0
	p2:=ptr($B850,0);
	// Point to scanlines 2
	p3:=ptr($BA40,0);
	while (true) do
	begin
		// Cycle the two palettes at various intervals
		if (time=  0) then setcgapalette($101);
		if (time=128) then setcgapalette($100);

		// Calculate an y sine				
		y:=sine[time&$FF]/8;
		// Multiply with scanlines
		y:=y*80;


		// Copy scanlines 0 data	
		p1:=#data +y;		
		// Copy image data (only every single scanline, )
		memcpy16(p1,p2, 40*70); // Copy the data, 70 lines in total (70*40 words = 70*40*2 bytes)


		// Calculate an y sine with shift
		y:=sine[(time+64)&$FF]/8;
		// Multiply with scanlines
		y:=y*80;


		// Copy scanlines 2 data			
		p1:=#data+70*80-y;		
		// Copy image data (only every single scanline, )
		memcpy16(p1,p3, 40*70); // Copy the data, 70 lines in total (70*40 words = 70*40*2 bytes)
		// Wait for vertical blank		
		waitforverticalblank();

		// Increase time counter		
		time:=time+4;

	end;
end.