program Tutorial6_Sprite_compiler;

@use compression
@use graphics

/*
	This example file compiles a sprite directly to assembly, and stores the function in then
	"draw_ball.ras" file. While this results in much faster rendering, the drawback is that
	a) the sprite can't be changed and b) it takes up way of more space


*/


var 
	done,next,time,k,t,cx,cy,i,x1,y1,x2,y2 : integer = 0;
	
	// Compiles the sprites_balls2.flf file to sprites_balls.ras defining a method called "draw_ball"
	@spritecompiler "images/sprites_balls2.flf" "images/sprites_balls.ras" 0 0 6 16 "draw_ball"
	
	@export "images/sprites_balls.flf" "images/sprites_balls.bin" 256
	pal : incbin("images/sprites_balls.pal");

	
	sin : array[256] of integer = buildsinetable(255);

/*

	
*/

@include "images/sprites_balls.ras"

procedure RenderBalls();
begin
	t:=time;
	k:=time/3;
	// center value
	cx:=sin[time&255]>>4+8;
	cy:=sin[(time*2)&255]>>3+16;
	for i:=0 to 16 do
	begin
		// x and y values for each of the 16 balls
		x1:=sin[t&255]+cx;
		y1:=sin[(t+64+k)&255]>>1+cy;
		
		// "draw_ball" is auto-generated in sprites_balls.ras 
		
		draw_ball(Graphics::curBuf, x1,y1);
		
		t:=t+10;
		k:=k+7;
	end;
end;

interrupt VBlank();
begin
	Graphics::vbl:=1;
end;

begin
	// Set to lores mode
	SetLoRes();
	
	Graphics::Init();

	setirq(VBlank(), irq_verticalblank);
	

	
	memcpy(#pal,0,#palette_start,0,15,2);
	while (done = false) do
	begin
		
		RenderBalls();
		
		Graphics::FlipBuffers();
		
		Graphics::WaitForVBL();
		Graphics::ClearScreenFast(Graphics::curBuf);
		done:=keypressed(sc_key_space_pressed);

		time+=1;

	end;	
//	setirq(VBLankEmpty(), irq_verticalblank);

end.


