program Tutorial1;
@use "screen"
@use "input"
var
// Sprites are alwyas 16-colour. Export them automatically!	
@export "resources/gfx/sprites.flf" "resources/gfx/sprites.bin" 1

// Sprite data
	sprites1 : incbin("resources/gfx/sprites.bin");
// Automatically exported palette data
	palette_sprites: incbin("resources/gfx/sprites.pal");
// A sine table
	sin256:array[256] of byte = BuildSineTable(127);
// Position of sprites
	
	sx,sy : integer;
	val, curSprite,time,moveTimer,flip : byte;
	
	moveset : array[] of byte = (
	0, 0, 5*2, 0, 0,6*2, 6*2,0,
	1*2,2*2,3*2,4*2,1*2,2*2,3*2,4*2);
	
	
	
begin
	Screen::Init();
		
	Screen::SetPalette(#palette_sprites,128,128);
	// Put sprites at VRAM = 0
	Screen::CopyToVRAM(#sprites1,$0000,128*128/2);
	// Turn on sprites
	Screen::EnableGFX(Screen::ENABLE_SPRITES);// 01 = BG1	
	// Turn on display
	Screen::EnableDisplay();
	sx:=100;
	sy:=100;
	moveTimer:=0;
 	while (true) offpage do
 	begin
	 	Screen::WaitForVerticalBlank();
		Screen::CopySprites(4); // copy 4 sprites
// Put the sprite on screen
		
		Input::Read();
		curSprite:=0;
		if (Input::Movement( Input::jpad1 )) then curSprite:=8;
		
		if (Input::Left( Input::jpad1 )) then flip:=1
		else 
		if (Input::Right( Input::jpad1 )) then flip:=0;
		
		Screen::PutSprite(0, // Sprite number (in VRAM)
				sx,sy,  // x,y
				2,2,  // width, height
				moveset[curSprite+moveTimer],    // Sprite sheet ID (will draw 2x2 sprites with this value +1,2,3 and 16,17,18 etc 
				%00000000 | Screen::SPRITE_PRIORITY3,  // Sprite attributes (including palette)
				flip);  // Sprite horizontal flip. Not implemented yet.
				
		sx += Input::Right( Input::jpad1 ) - Input::Left( Input::jpad1 );
		sy += Input::Up( Input::jpad1 ) - Input::Down( Input::jpad1 );
		
		//val :=(1+Input::Up( Input::jpad1 ) - Input::Down( Input::jpad1 ));
		time+=1;
		if (time&7=0) then 
		begin
			moveTimer := (moveTimer+1)&7;
		end;
 	end;
	
end.