
procedure DebugRaster(dbc:byte);
begin
@ifdef debug_raster
	if (show_raster=1) then 
		SCREEN_BG_COL:=dbc;
@endif
end;




// Forward declare RasterWaiter
interrupt RasterWaiter();

procedure IncTime();
begin
	inc(time);
	if (time&1=0) then inc(time2);
	if (time&63=0) then time4+=1;


end;


/*
	ProgressTracker is called in every frame of every scene.
	Draws debug rasters
	Plays music	
	Checks whether transitions should occur
	Progresses to new frame

*/

procedure ProgressTracker();
var
	curTimeStamp : byte;
begin
// Play music, display music raster time
@ifdef play_music
@ifdef debug_raster
	DebugRaster(YELLOW);
@endif
	call(SIDFILE_1_PLAY);
@ifdef debug_raster
	DebugRaster(BLACK);
@endif

@endif

	transitionMode:=0;
	// Find timestamp of current scene 
	curTimeStamp:=timeStamps[currentPart*4+1];
	// Is it time to advance to the next scene?
	if (time4=curTimeStamp) then begin
		// Start pointing to the next part
		inc(currentPart);
		// Reset time and time2, but not time 4 (which is used for global timing)
		time:=0;
		time2:=0;
		// new scene is not yet initialized
		initComplete:=0;
		// Point to next scene 
		nextScene:=timeStamps[currentPart*4];
		// Load the raster waiter.
		RasterIRQ(RasterWaiter(),0,@useKernal);
		// Since initComplete is zero and nextScene is set, the main loop will instantly start initializing
		// the next scene.
	end;
	// However, if we are at the final timestep before a change, set transitioning flag to 1
	if (time4=curTimeStamp-1) then transitionMode:=1;

// Toggle raster debugging if raster is on and key is pressed
@ifdef debug_raster
	if (keypressed(@rasterDebugKey)=1) then begin
		show_raster:=(show_raster+1)&1;
	end;
@endif
	// Increase time, time2 and time4
	IncTime();	

end;

// Include a scene
//@include "include/scene1.ras"


/*
	Main raster routine 
	Initializes new scene basics
	Waits and play music with black screenoff();
	Makes sure we don't do stuff without turning on $D000 etc

*/
interrupt RasterWaiter();
begin
	// This replaces startirq by always turning ON the vic 
	// in case we were in the middle of a scene initializing decrunch. Remember to restore
	// the original value of $01 at the end of the raster!
	Asm("
		pha
		txa
		pha
		tya
		pha

		lda $01
		sta currentConfig
		lda keep01
		sta $01

		asl $D019
		");

	// Only advance when main loop gives the flag that scene init is complete!
	if (initComplete=1) then begin
		InitSceneRaster();

	end;

@ifdef play_music
	call(SIDFILE_1_PLAY);
@endif
	// Set original config
	poke(^$01, 0, currentConfig);
	CloseIRQ();

end;

/*
  Empty break interrupt
*/
interrupt NMI();
begin

end;


/*
	The main loop updater: runs through all 

*/

procedure UpdateMainloop();
begin
	while (true) do begin
		if initComplete=0 then begin
			// Make sure that everything is black
			SPRITE_BITMASK:=0;
			screenoff();

			MainLoopInitScene();

			screenon();
			initComplete:=1;
		end;

	end;

end;

procedure DemoMakerMainLoop();
begin
	PreventIRQ();
	DisableCIAInterrupts();
	// Empty NMI irq
	nmiirq(NMI());
	// Set current memory config
	SetMemoryConfig(1,@useKernal,0);
	// Make sure we keep that config stored 
	keep01:=peek(^$01,0);
	currentPart := 0;
	nextScene:=timeStamps[currentPart*4];

	initsid(SIDFILE_1_INIT);
	initComplete:=0;
	RasterIRQ(RasterWaiter(),0,@useKernal);

	EnableRasterIRQ();
	enableirq();
	UpdateMainloop();
end;

