program Tutorial5_Fonts;
@use "music/adlibplayer"
@use "system/screen"
@use "system/memory"
@use "system/file"
@use "gfx/gfx"
@use "system/tables"
@use "system/compression"
@use "gfx/fonts"

var  
	song: incbin("music/song1.trt");
	i,x,x1,x2,y,j,j2,px,time: integer;
	p : pointer;
	
	cols : array[256*3] of byte;
	// External fils	
	filename1 : string="IMAGE2_C.BIN";
	palette1 : string="IMAGE2.PAL";
	font1_file : string="FONTS/FONT1.BIN";
	fontP : pointer of byte;
	// da cheesy text
	cheesy_text: string=("TU ERES",0,"MI HERMANO",0,"DEL ALMA",0,"REALMENTE",0,"EL AMIGO",0,0,255);	
	tp,tp0, tp1:pointer;
	f1 : File::File;

// Random image of Roberto Carlos doing his thing
	
@export "images/image2.flf" "images/image2.bin" 256
@compress "images/image2.bin" "image2_c.bin"

/*
	Font is generated with TRSE's "import" feature generating a bitmap font.
	This font is 20 columns wide, resulting in a 16x20 pixel wide font.
*/
@export "fonts/font1.flf" "fonts/font1.bin" 256
@compress "fonts/font1.bin" "font1_c.bin"



interrupt RasterIRQ();
begin
	StartIRQ();
	AdlibPlayer::Play();
	CloseIRQ();
end;

procedure Init();
begin
	Memory::Init();
	Screen::SetMode($13);
	Screen::InitVGABuffers(4);
	

	File::SetPaletteFromFile(#palette1);		
	// Load and decompress image
	File::ReadAllLarge(#filename1,Screen::buffer3);


	Compression::Decompress(Screen::buffer3, Screen::buffer2);
	
	// Load and decompress font
	fontP := Screen::buffer4;
	File::ReadAllLarge(#font1_file, Screen::buffer4);
	
//	Compression::Decompress(Screen::buffer3, Screen::buffer4);
	
end;


procedure AdvanceText(tp: global pointer);
begin
	while (tp[0]<>0) do tp+=1;
	tp:=tp+1;
	if (tp[0]=255) then tp:=#cheesy_text;
end;


begin
	Init();

	
	AdlibPlayer::Initialize(#song);
	Memory::SetTimer(Memory::Default_Timer);
	waitforverticalblank();
	SetInterrupt($8, RasterIRQ());
	// Initialise font to point to the font data, width 16, height 20, 20 columns, default shift (32)	
	Fonts::Init(fontP, 16,20,20,Fonts::DEFAULT_SHIFT);
	// Write font with mask >2
	Fonts::writeType := Fonts::WRITETYPE_MASK;
	Fonts::maskColor := 2;
	// Initialise text	
	tp0:=#cheesy_text;
	tp1:=#cheesy_text;
	AdvanceText(tp1);
	tp1:=tp;
	while (true) do
	begin
		waitforverticalblank();
		// Copy buffer to screen
		Screen::CopyVGABuffer32bit(Screen::buffer1, ptr(Screen::VGA,0));
		// Copy image to buffer (clear)

		Screen::CopyVGABuffer32bit(Screen::buffer2, Screen::buffer1);
     	// Draw that funky text!)		
		Fonts::addValue:=time; // text color
		Fonts::PrintString(tp0, Tables::sine256[(time*1)&255]/4+80,120,Screen::buffer1);		
		Fonts::addValue:=time+64;
		Fonts::PrintString(tp1, Tables::sine256[(time*2+64)&255]/4+80,160,Screen::buffer1);		
		// advance text pointers
		if (time&127=127) then
		begin
			AdvanceText(tp0);
			AdvanceText(tp);
			tp0:=tp;
			AdvanceText(tp1);
			AdvanceText(tp);
			tp1:=tp;
		end;
		time+=1;		
	end;
	AdlibPlayer::Stop();	
end.
