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

sleep() in default love.run

Post by chrism » Wed Aug 24, 2011 9:12 pm

Can someone explain why this is in the default love.run() code?

Code: Select all

if love.timer then love.timer.sleep(1) end
Does something break if you don't do that?

User avatar
genericdave
Citizen
Posts: 53
Joined: Tue Dec 15, 2009 9:08 am

Re: sleep() in default love.run

Post by genericdave » Wed Aug 24, 2011 9:21 pm

It's probably there to keep love from using 100% cpu all the time, even if it isn't actually doing anything.

chrism
Prole
Posts: 21
Joined: Wed May 25, 2011 6:17 pm

Re: sleep() in default love.run

Post by chrism » Wed Aug 24, 2011 9:28 pm

If that's the case, then you probably don't need it if you limit your FPS somewhere else?

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: sleep() in default love.run

Post by Robin » Wed Aug 24, 2011 9:32 pm

Nope. It's there so people can have really simple love.updates, not change love.run and still don't have CPU monopoly.
Help us help you: attach a .love.

Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: sleep() in default love.run

Post by Rad3k » Thu Aug 25, 2011 12:45 am

I also wondered why default love.run sleeps for 1 milisecond each frame. It seems quite arbitrary.
Robin wrote:Nope. It's there so people can have really simple love.updates, not change love.run and still don't have CPU monopoly.
Wouldn't the OS handle this? Either way, most games that use default love.run eat as much CPU as they can (unless they use VSync), so it doesn't seem to help much.

Why not cap framerate to something reasonable by default? Is there any sense in squeezing more frames per second than your monitor can display? Most monitors I have used had refresh rate of 60Hz, although I know that some newer have 120Hz. I don't like it when really simple games make my laptop fans scream...

User avatar
genericdave
Citizen
Posts: 53
Joined: Tue Dec 15, 2009 9:08 am

Re: sleep() in default love.run

Post by genericdave » Thu Aug 25, 2011 5:10 am

In general, Love seems to be designed for maximum flexibility, rather than braindead ease of use. Maybe I want to update some logic faster than Vsync. Maybe not. I can set Vsync if I want. I can leave it off. It's up to me. You could even remove that sleep statement if it bothers you for some reason. Try removing it and see what happens. It's just there to be nice to your OS.

User avatar
bartbes
Sex machine
Posts: 4944
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: sleep() in default love.run

Post by bartbes » Thu Aug 25, 2011 8:52 am

It is a huge difference, and I have anecdotal evidence to back that up!
In all seriousness, 1ms might seem useless, it is not. It won't sleep for 1ms, because I doubt it has millisecond accuracy. What it does do, is it tells the OS "Oh hey, take your time to do something else, we can wait for a bit!".

Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: sleep() in default love.run

Post by Rad3k » Thu Aug 25, 2011 9:20 am

I get it. I tried running a game with this line commented out, and it resulted in game using even more cpu than normally. But still, even with this sleep, most games use much more cpu power than they need.

I understand the flexibility, and I like it that everyone can supply their own love.run tailored to their particular needs. I always do it myself, but that's because the default one doesn't seem to be good if you don't want to waste cpu cycles. That is, unless you use VSync, but I guess it's not wise to rely on it. This is love.run I use in all my projects:

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 dt = 0

	if timer then
		-- Set a reasonable FPS limit
		local FPS = 60
		dt = 1/FPS
	end

	-- Main loop
	while true do
		-- Update the timer
		if timer then timer.step() 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 love.update(dt) 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 time_work = timer.getDelta()
			if time_work < dt then
				timer.sleep((dt - time_work) * 1000)
			end
		end

		-- Show the frame
		if graphics then
			graphics.present()
		end
	end
end
If you add it to any game that doesn't supply its own love.run, it shouldn't break anything - you should only get constant framerate of 60 fps, regardless of VSync setting (EDIT: actually not - it works reliably only with VSync turned off). The dt is actually constant, which may be a good thing if you use love.physics - according to Box2D manual, it doesn't like variable timestep. Are there any cons for making something like this default?
Last edited by Rad3k on Thu Aug 25, 2011 2:01 pm, edited 1 time in total.

User avatar
genericdave
Citizen
Posts: 53
Joined: Tue Dec 15, 2009 9:08 am

Re: sleep() in default love.run

Post by genericdave » Thu Aug 25, 2011 9:40 am

Rad3k wrote:The dt is actually constant, which may be a good thing if you use love.physics - according to Box2D manual, it doesn't like variable timestep.
You need to read this.

Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: sleep() in default love.run

Post by Rad3k » Thu Aug 25, 2011 1:49 pm

genericdave wrote:You need to read this.
Yes, I considered doing something like this in case it proved necessary. I will look into it.

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests