sleep() in default love.run

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
chrism
Prole
Posts: 21
Joined: Wed May 25, 2011 6:17 pm

Re: sleep() in default love.run

Post by chrism »

Thanks for the run() example, much better than the way I was limiting FPS.
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: sleep() in default love.run

Post by Rad3k »

Ok, here is updated version of my love.run:

Code: Select all

function love.run ()
	-- these don't change, so we can make them local for faster access
	local audio, graphics = love.audio, love.graphics
	local event, timer = love.event, love.timer

	-- Prepare stuff
	if love.load then love.load(arg) end

	local timestep = 1/240
	local accumulator = 0
	-- Set a reasonable FPS limit
	local min_frame_time = 1/60

	-- Main loop
	while true do
		-- Update the timer
		if timer then
			timer.step()
			accumulator = accumulator + timer.getDelta()
		end

		-- Process events
		if event then
			for e, a, b, c in event.poll() do
				if e == "q" then
					if not love.quit or not love.quit() then
						if audio then audio.stop() end
					end
					return
				end
				love.handlers[e](a, b, c)
			end
		end
		
		-- Do the game mechanic
		if love.update then
			if timer then
				while accumulator >= timestep do
					love.update(timestep)
					accumulator = accumulator - timestep
				end
			else
				love.update(0)
			end
		end

		-- Draw the graphics
		if graphics then
			graphics.clear()
			if love.draw then love.draw() end
		end

		-- Wait for the end of this frame
		if timer then
			timer.step()
			local work_time = timer.getDelta()
			if work_time < min_frame_time then
				timer.sleep((min_frame_time - work_time) * 1000)
			end
			accumulator = accumulator + work_time
		end

		-- Show the frame
		if graphics then
			graphics.present()
		end
	end
end
I haven't implemented the interpolation, because I felt this would make stuff more complicated on the user side. But to some extent you can avoid the problems by making the timestep few times smaller than graphics frame time.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 135 guests