delayTime = 10;

function delay(delayTimeParam)
	delayTime = delayTimeParam;
end

function pause(pauseTime)
	local totalTime = 0;
	local elapsedTime = 0;
	
	while true do
		elapsedTime = coroutine.yield(0);
		
		totalTime = totalTime + elapsedTime;
		
		if totalTime >= pauseTime then
			break;
		end
	end
end

-- will loop over a table which contains the frame indicices to show
-- c++ will yield from each call to showFrame
function frames(framelist)
	local totalTime = 0;
	local maxFrames = table.getn(framelist);
	local maxTime = maxFrames * delayTime;
	local elapsedTime = 0;
	
	while true do
		local currentFrame = math.floor(totalTime / delayTime) + 1;
		setCurrentFrame(framelist[currentFrame]);
		
		elapsedTime = coroutine.yield(0);
		
		totalTime = totalTime + elapsedTime;
		
		if totalTime >= maxTime then
			setCurrentFrame(framelist[maxFrames]);
			break;
		end
	end
end

-- Loop the given framelist.
function loop (times, framelist)
	local remaining = 0;
	
	if times <= 0 then
		while true do
			 frames(framelist);
		end
	else
		for i = 1, times do
			 frames(framelist);
		end
	end
end