program Tutorial5;
@use "system/z80cpm"
@use "system/system"
@use "system/screen"
@use "system/tiles"
@use "system/graphics"

var
	
	@deletefile "images/tiles.bin"
	@exportsubregion "images/tiles.flf" "images/tiles.bin" 16  0 16 16	// pacman 1
	@exportsubregion "images/tiles.flf" "images/tiles.bin" 32  0 16 16	// pacman 2
	@exportsubregion "images/tiles.flf" "images/tiles.bin" 48  0 16 16	// pacman 3
	@exportsubregion "images/tiles.flf" "images/tiles.bin" 0  16 16 16	// manic
	@exportsubregion "images/tiles.flf" "images/tiles.bin" 16 16 16 16 // ball
	@exportsubregion "images/tiles.flf" "images/tiles.bin"  0 48 16 16 // gng 1
	@exportsubregion "images/tiles.flf" "images/tiles.bin" 16 48 16 16 // gng 2
	@exportsubregion "images/tiles.flf" "images/tiles.bin"  0 64 16 16 // gng 3
	@exportsubregion "images/tiles.flf" "images/tiles.bin" 16 64 16 16 // gng 4
	@exportsubregion "images/tiles.flf" "images/tiles.bin"  0 80 16 16 // dizzy 1
	@exportsubregion "images/tiles.flf" "images/tiles.bin" 16 80 16 16 // dizzy 2
	@exportsubregion "images/tiles.flf" "images/tiles.bin"  0 96 16 16 // dizzy 3
	@exportsubregion "images/tiles.flf" "images/tiles.bin" 16 96 16 16 // dizzy 4
	sprites: incbin("images/tiles.bin");	// include sprites
	
	font: incbin("zxchars.bin");

	i,j,col,ch,sprite,f: integer;

	@exportsubregion "images/tiles.flf" "images/temp1.bin" 16  0 16 16	// pacman 1
	@exportsubregion "images/tiles.flf" "images/temp2.bin" 16  0 16 16	// pacman 1
	@exportsubregion "images/tiles.flf" "images/temp3.bin" 16  0 16 16	// pacman 1
	@exportsubregion "images/tiles.flf" "images/temp4.bin" 16  0 16 16	// pacman 1

	@deletefile "images/temp1.bin"
	@deletefile "images/temp2.bin"
	@deletefile "images/temp3.bin"
	@deletefile "images/temp4.bin"

procedure PressAny();
begin
	Screen::GotoXY(10,0);
	Screen::PrintStr("Press any key...");
	ch := Z80CPM::GetChar();
	Screen::GotoXY(10,0);
	Screen::PrintStr("                ");
end;

begin
	Z80CPM::Startup();
	System::CursorOff();
	System::SetScreenPos(0);
	Screen::SetFont(#font);
	Screen::ClearBox(0, 0, 128, 64, 0);

	// set indexed tiles address	
	Tiles::SetIndexTiles(#sprites);

	// just draw some tiles/sprites...	
	// last parameter is tile index or address:
	//   - if value is 0-255, then indexed tiles are used (set with SetIndexTiles)
	//   - if address is passed, then that address is used as tile address
	Screen::ClearBox(0, 0, 128, 64, 0);

	// pacman is on indexes 0-2 in tiles.bin
	Tiles::PutTile(2,2,0);
	Tiles::PutTile(6,2,1);
	Tiles::PutTile(10,2,2);
	
	// a few more tiles...
	Tiles::PutTile(20,10,3);
	Tiles::PutTile(30,10,4);

	// sprite from Ghosts'n'goblins
	Tiles::PutTile(30,20,5);
	Tiles::PutTile(34,20,6);
	Tiles::PutTile(30,24,7);
	Tiles::PutTile(34,24,8);

	// sprite from Dizzy
	Tiles::PutTile(10,40,9);
	Tiles::PutTile(14,40,10);
	Tiles::PutTile(10,44,11);
	Tiles::PutTile(14,44,12);

    PressAny();

	// full screen of pacman tiles, flipped on X/Y each time
	sprite := 2;	// third pacman tile
	f := 0;
    for i := 0 to 128 step 4 do begin
        for j := 0 to 64 step 4 do begin
            Tiles::PutTile(i, j, sprite);
            if f = 1 then
				Tiles::FlipTileX(sprite)
            else
                Tiles::FlipTileY(sprite);
            f += 1; if f > 1 then f := 0;
        end;
		// different next column        
		if f = 1 then
			Tiles::FlipTileX(sprite)
		else
			Tiles::FlipTileY(sprite);
        f += 1; if f > 1 then f := 0;
    end;

    PressAny();

	Screen::ClearBox(0, 0, 128, 64, 0);
	System::CursorOn();
	Z80CPM::Exit();
end.
