
/*
   Renders a .flf level at map pos x,y
*/

procedure ReadHeader();
var
   m_rl_width, m_rl_height : byte;
   m_rl_sizex, m_rl_sizey : byte;
   m_rl_startx, m_rl_starty : byte;
   m_rl_dataSize, m_rl_pos : byte;
   m_rl_i, m_rl_j, m_rl_val, m_rl_idx: byte;
   m_rl_chunksize, m_rl_chunks : byte;

begin
	@levelpointer := @levelpointer +13;

	// Read level header info

	m_rl_sizex := @levelpointer[0];
	m_rl_sizey := @levelpointer[1];

	m_rl_width := @levelpointer[2];
	m_rl_height := @levelpointer[3];

	m_rl_startx := @levelpointer[4];
	m_rl_starty := @levelpointer[5];


	m_rl_chunks := @levelpointer[6];
	m_rl_chunksize := @levelpointer[7];

	m_rl_dataSize := @levelpointer[8];




end;

procedure RenderColorLevel(m_li, m_lj, m_screen: byte);
begin
	// Fluff files have 13 byte headers
	
	// Go past header
	@levelpointer := @levelpointer +32;

	// Calculate position in level layout

	m_rl_pos := m_rl_sizex*m_lj + m_li;
	
	m_rl_val := m_rl_width*m_rl_height;
//	m_rl_val := m_rl_val;// + m_width; 

	// Skip levels
	if m_rl_pos<>0 then
	for m_rl_j:=0 to m_rl_pos do begin
		@levelpointer := @levelpointer +m_rl_val*2;
		@levelpointer := @levelpointer +m_rl_dataSize;
	end;

	@levelpointer := @levelpointer +m_rl_val*2*m_rl_pos;
	@levelpointer := @levelpointer +m_rl_dataSize*m_rl_pos;

	// Set screen position 

	moveto(m_rl_startx, m_rl_starty, m_screen);
	
	// Fill screen with chars
	//call(SIDFILE_1_PLAY);
	
	for m_rl_j:=0 to m_rl_height do begin
		for m_rl_i:=0 to m_rl_width do begin
			m_rl_val := peek(@levelpointer,m_rl_i);
			m_rl_idx:=m_rl_i*2;

			screenmemory[m_rl_idx]:=m_rl_val;
			screenmemory[m_rl_idx+1]:=m_rl_val+1;
			screenmemory[m_rl_idx+40]:=m_rl_val+40;
			screenmemory[m_rl_idx+41]:=m_rl_val+41;
		 end;
		 screenmemory:=screenmemory+screen_width*2;
		 @levelpointer:=@levelpointer+m_rl_width;
//		inczp(levelpointer,m_rl_width);
	end;
	// Then colors
	moveto(m_rl_startx, m_rl_starty, $D8);
//	call(SIDFILE_1_PLAY);
	@colorpointer:=@levelpointer;
	for m_rl_j:=0 to m_rl_height do begin
		for m_rl_i:=0 to m_rl_width do begin
			m_rl_val := peek(@levelpointer,m_rl_i)&7 + 8;
			m_rl_idx:=m_rl_i*2;
	
			screenmemory[m_rl_idx]:=m_rl_val;
			screenmemory[m_rl_idx+1]:=m_rl_val;
			screenmemory[m_rl_idx+40]:=m_rl_val;
			screenmemory[m_rl_idx+41]:=m_rl_val;

	 	end;
	 	screenmemory:=screenmemory+screen_width*2;
		 @levelpointer:=@levelpointer+m_rl_width;

//		inczp(levelpointer, m_rl_width);
	
	end;
	// Set multicolor colors
	//call(SIDFILE_1_PLAY);
	poke(MULTICOLOR_CHAR_COL, 0, @levelpointer[0]);
	poke(MULTICOLOR_CHAR_COL, 2, @levelpointer[1]); 
	poke(MULTICOLOR_CHAR_COL, 1, @levelpointer[2]);

	@levelpointer:=@levelpointer+3;
	// levelpointer now points to following data cun!

end;

procedure RenderCharsetColorLevel(m_li2, m_lj2, m_screen2: byte);
var

begin
	// Fluff files have 13 byte headers
	@levelpointer:=@levelpointer+32+13;
//	inczp(@levelpointer, 32+13);

	m_rl_pos := m_rl_sizex*m_lj2 + m_li2;
	
	m_rl_val := m_rl_width*m_rl_height;
//	m_rl_val := m_rl_val;// + m_width; 

	// Skip levels
	if m_rl_pos<>0 then
	for m_rl_j:=0 to m_rl_pos do begin
		@levelpointer:=@levelpointer+m_rl_val;
		@levelpointer:=@levelpointer+m_rl_dataSize;
	end;

	moveto(m_rl_startx, m_rl_starty, $D8);
	colormemory:=screenmemory;
	moveto(m_rl_startx, m_rl_starty, m_screen2);
	
	// Fill screen with chars
	//if current_game_mode=@gameModeMap then
	//call(SIDFILE_1_PLAY);

	for m_rl_j:=0 to m_rl_height do begin
		m_rl_idx:=0;
	
		for m_rl_i:=0 to m_rl_width do begin
			m_rl_val := @levelpointer[m_rl_i];
			screenmemory[m_rl_idx+40]:=m_rl_val+40;
			screenmemory[m_rl_idx]:=m_rl_val;
			screenmemory[m_rl_idx+41]:=m_rl_val+41;
			screenmemory[m_rl_idx+1]:=m_rl_val+1;

			colormemory[m_rl_idx] := colors[m_rl_val];
			colormemory[m_rl_idx+1] := colors[m_rl_val+1];
			colormemory[m_rl_idx+41] := colors[m_rl_val+41];
			colormemory[m_rl_idx+40] := colors[m_rl_val+40];

			m_rl_idx:=m_rl_idx+2;
		 end;
		 screenmemory:=screenmemory+screen_width*2;
		 colormemory:=colormemory+screen_width*2;
		 @levelpointer:=@levelpointer+m_rl_width;
	end;

	poke(MULTICOLOR_CHAR_COL, 0, @levelpointer[0]);
	poke(MULTICOLOR_CHAR_COL, 2, @levelpointer[1]); 
	poke(MULTICOLOR_CHAR_COL, 1, @levelpointer[2]);
	@levelpointer:=@levelpointer+3;

end;
