program Hello_IBM_XT_CGA;
var  
    time: byte = 0;
	i:integer; 
   	x,y,centerx,centery : integer = 0;
   	ka,kb : byte = 0;
   	
	sine : array[256] of byte = buildsinetable(255);

	cols : array[4] of byte=(cga_color0, cga_color1, cga_color2, cga_color3);

	
/*

	4.77 mhz IBM XT!

	First DOS example. Compiler still extremely inefficient, no optimizations yet. Binary
	expression will fail at will. Have fun! 
	
	Setup DOXBOX 
	
*/


procedure SetupBackgroundScreen();
begin
	for x:=0 to 160 do
	begin
		for y:=0 to 100 do
		begin
			setpixelcga(screen_cga,x,y,cga_color2);
			setpixelcga(screen_cga,x+160,y,cga_color1);
			setpixelcga(screen_cga,x,y+100,cga_color3);
		end;
	end;
end;


procedure RenderEffect();
begin
	ka:=ka+1;
	kb:=kb+2;
	time:=time+1;
		
	if (time=10) then 
	begin
		kb:=kb+1;
		WaitForVerticalBlank();
		i+=1;
	end;

	centerx:=sine[kb] + 50;
	centery:=sine[(kb+64)&$FF]/3;

	x:=sine[ka&$FF];
	y:=sine[(ka+64)&$FF]/3;
	setpixelcga(screen_cga, x+centerx,y+centery, cols[i]);
		
	i:=(i+1)&3;
end;


	
begin
	init_cga_scanlines(); // Calculate CGA screen tables for setpixelcga
	SetScreenMode(mode_cga_320x200); // CGA mode

	SetupBackgroundScreen();
	
	kb:=ka+64;
	while (true) do
	begin
		RenderEffect();			
	end;
	Loop();
end.
