program MyProgram;
@use "system/screen"

var  
   i,j,b,k,l,time,k2: byte; 
   in,ia,ib,ix,iy : integer;
	zp:pointer;   
	const spriteLoc:address=$8000;
	curFrame : byte = 0;
	test:array[10] of byte;
// Emulator only supports 16 sprites

@define noSprites 127

@requirefile "data/example7_sprite.bin" "Please run the ray-tracer file 'example7_sprites.fjo' before compiling this program"
	sprites : incbin("data/example7_sprite.bin",spriteLoc);
	
// Set som funky palette
procedure SetPalette(aa,bb,cc:byte);
var
  ia,ib,ic: byte;
begin
	for i:=0 to 64 do 
	begin
		ia:=i/aa;
		ib:=i/bb;
		ic:=i/cc;
		SetColor(ia,ib,ic);
	end;

end;

procedure UpdateSprites();
begin

//	if (time&1=1) then 
	curFrame:=(curFrame +1)&15;
	j:=curFrame;
	// base ix, iy sprite position
	ix:=sine[time];
	iy:=0+sine[time+34]/2;
	// Set timers
	k:=time;
	k2:=time*2;
	
	for i:=0 to @noSprites do 
	begin
		// Point to sprite i on VERA
		Screen::PointToSprite(i);
		// Set current frame
		j:=(j+2)&15;
		in:=$8200 + j*8;
		// Set sprite position = $200 = $4000/32 + frame*256 (each frame is 256 byes)
//		verapokedual($FC,00,hi(in),lo(in));
	//	setspriteloc(in);
		Screen::SetSpriteLoc(in);
		// Set x / y position of individuals
		ia:=100 + ix+sine[k];
		ib:=sine[k2]*1+iy;
		// Set position
		Screen::SetSpritePos(ia,ib);
	//	spritepos(ia, ib);
		// Advance counters with some primes
		k:=k+11;
		k2:=k2+7;
	end;	

end;

procedure InitSprites();
begin
	Screen::EnableSprites(1);
	// Emulator still only supports 16 sprites	
	for i:=0 to @noSprites offpage  do 
	begin
		Screen::PointToSprite(i);	
		
	    // Set 16x16 (0 = 8, 1=16, 2=32, 3=64)		
		spritesize(1,1);// 16x16
		spritedepth(3); // In front ofall
		
		j:=((i*11)&3)*4; // 64 colour shift RGB
		
		SpritePaletteOffset(j);	
	end;

end;


begin

	CopyDataToVera(#spriteLoc, $00,$40, $00, 16);  

	Initpalette(0);
	SetPalette(8,6,4);
	SetPalette(8,4,8);
	SetPalette(4,12,12);
	SetPalette(4,16,4);
	
	InitSprites();

	while (true) do 
	begin
		UpdateSprites();
		for i:=0 to 255 do wait(50);
		time:=time+1;
	end;

end.
