program part6_RenderLevel;
var  

/*
In this tutorial, we use actual level data to render a screen with 
our character set
*/

// Memory position where we include the level data
@define levelPosition $5000
// Memory position (on VIC 0) for character data
@define charsetPosition $2000
// Don't use kernal
@define useKernal 0



   	charset1: incbin("charsets/charset1.bin", @charsetPosition);
	levels: incbin("levels/level1.flf", @levelPosition);
	// Define some pointers
	colors, levelpointer, colormemory : pointer;
	i,j:byte = 0;
	// Current level screen x,y
	curLevel_x, curLevel_y : byte=0;

// Include RasLib level renderer
@define colorpointer colormemory
@include "../../RasLib/levels.ras"

// Initializes charsets etc
procedure Initialize();
begin
	clearscreen(LIGHT_BLUE, ^$D800);
	clearscreen($20, ^$0400);
	SCREEN_BG_COL:=BLACK;
	SCREEN_FG_COL:=BLACK;

	colors:=$2800;

	MULTICOLOR_CHAR_COL[1]:=GREY;
	MULTICOLOR_CHAR_COL[2]:=BROWN;

	setmulticolormode();

	setcharsetlocation(@charsetPosition);
	// Make sure that the level pointer points to level data location
	levelpointer:=@levelPosition;
	// Read header data
	ReadHeader();
end;


/*
	Main raster function that mainly just does nothing except when the joystick is moved. If so,
	a new level screen is rendered.

*/
interrupt RasterRenderLevels();
begin
	StartIRQ(@useKernal);
	if (i=0) then begin
		joystick(2);
		if (joystickdown=1) then begin inc(curLevel_y); i:=10; end;
		if (joystickup=1) then begin dec(curLevel_y); i:=10; end;
		if (joystickleft=1) then begin dec(curLevel_x); i:=10; end;
		if (joystickright=1) then begin inc(curLevel_x); i:=10; end;
	end;

	if (i=10) then begin
		levelpointer:=@levelPosition;
		screenoff();
		RenderCharsetColorLevel(curLevel_x,curLevel_y,$04);
		screenon();
	end;
	if i<>0 then dec(i); // i acts as a counter

	CloseIRQ();
end;

// Main block
begin
	Initialize();
	// Remember to set level location!
	levelpointer:=@levelPosition;
	// Render the level to $0400
	RenderCharsetColorLevel(curLevel_x,curLevel_y,$04);

	DisableCIAInterrupts();
	SetMemoryConfig(1,@useKernal,0);
	RasterIRQ(RasterRenderLevels(),$00,@useKernal);
	EnableRasterIRQ();
	enableirq();
	Loop(); 
end.
