program Tutorial4;
@use "system/z80cpm"
@use "system/system"
@use "system/screen"
@use "system/graphics"
@use "random"

var
	font: incbin("zxchars.bin");

	i,j,x,y,ch,col: integer;

@addemulatorparam "-nothrottle"

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);

	// plot some random pixels
	Screen::ClearBox(0, 0, 128, 64, 0);
	for i:= 0 to 100 do begin
		//x := Random::Random1() & 511;	// not supported by TRSE at the moment
		//y := Random::Random1() & 255;
		x := Random::Random1();
		y := Random::Random1();
		col := Random::Random1();
		asm("
			// limit x to 0-511
			ld hl,(x)
			ld a,h
			and 1
			ld h,a
			ld (x),hl
			// limit y to 0-255
			ld hl,(y)
			ld h,0
			ld (y),hl
			// limit col to 1-3
			ld hl,(col)
			ld a,h
			and 3
			jp nz, skip
			ld a, 1
		skip:
			ld h,a
			ld (col),hl
		");
		Graphics::Plot(x,y,col);
	end;
	PressAny();
	
	// plot some lines
	Screen::ClearBox(0, 0, 128, 64, 0);
    col := 1;
    j := 511;
    for i := 0 to 512 step 4 do begin
        Graphics::Line(i, 0, j, 255, col);
        col +=1;
        if col >= 4 then col := 1;
        j -= 4;
    end;
    j := 254;
    for i := 1 to 255 step 2 do begin
        Graphics::Line(0, j, 511, i, col);
        col +=1;
        if col >= 4 then col := 1;
        j -= 2;
    end;
	PressAny();

	// plot some circles and boxes
	Screen::ClearBox(0, 0, 128, 64, 0);
	for i:= 10 to 90 step 10 do begin
		Graphics::Box(i,i,512-i,255-i, 1);
		Graphics::Circle(i*4, 5+i*2, i, 2);
	end;
	PressAny();

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