## high resolution love.run function for 0.8.0

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### high resolution love.run function for 0.8.0

Copy and paste this function to the bottom of your main.lua file and you'll get high resolution timer driven main loop function. It assumes that you have both love.update and love.draw functions defined, and graphics, audio, timer and event modules loaded. If (in some case) not, just comment out it's call from the function.

Code: Select all

function love.run ( )
-- based on LÖVE 0.8.0 main loop code
math.randomseed ( os.time ( ) )
love.handlers.quit = function ( ) return love.quit and ( love.quit() ~= true ) or false end

local ot, nt = 0, 0
local lt_getMicroTime, lt_sleep = love.timer.getMicroTime, love.timer.sleep
local le_pump, le_poll = love.event.pump, love.event.poll
local lg_clear, lg_present = love.graphics.clear, love.graphics.present
local love_handlers = love.handlers

if love.init then love.init ( ) end
nt = lt_getMicroTime ( )
while true do
ot = nt
nt = lt_getMicroTime ( )
le_pump ( )
for e,a,b,c,d in le_poll ( ) do
if love_handlers[ e ] ( a, b, c, d ) == false then
love.audio.stop()
return
end
end
love.update ( nt - ot )
lg_clear ( )
love.draw ( )
lg_present ( )
lt_sleep ( 0 )
end
end
Also, you can cancel quit event by returning "true" from it, and you can instantly terminate the game by returning "false" from any event.
----
Thanks Robin for shorter version of quit event handler function, and for some clarifications.
Last edited by raidho36 on Tue Aug 27, 2013 4:56 pm, edited 1 time in total.

qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

### Re: high resolution love.run function for 0.8.0

can you explain what "high resolution timer driven main loop" is please
Lua is not an acronym.

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

### Re: high resolution love.run function for 0.8.0

Code: Select all

love.handlers.quit = function ( ) if love.quit then return love.quit ( ) ~= false and true or false else return true end end


First, it changes the behaviour of love.quit. In the default love.run, you return a truthy value from love.quit to stop the game from exiting, while in this version, you have to pass the value false for the same effect. This is something you need to realise if you intend to use this love.run with a love.quit callback.

Secondly: it is a bit long. This is logically equivalent:

Code: Select all

love.handlers.quit = function ( ) return not love.quit or love.quit() ~= false end

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: high resolution love.run function for 0.8.0

Thanks for the clarification, Robin. Also thanks for shorter version. I actually use version of it as simple as "love.handlers.quit = love.quit", so I didn't put too much effort into it.
can you explain what "high resolution timer driven main loop" is please
By default, update loop in LÖVE 0.8.0 (and earlier) is driven by low resolution timer, that is delta time (dt) value is computed using low resolution timer. It's resolution can be in order of tens of milliseconds, which is obviously not precise enough, especially if you deal with physics. This modified loop code uses high resolution timer (order of microseconds and less), and this provides, well, precise delta time compuation.

RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

### Re: high resolution love.run function for 0.8.0

The standard resolution is perfect for physics simulation. In programming you would need high-res timers for code profiling, where nano-seconds matter.

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: high resolution love.run function for 0.8.0

That's a big NO to you here. First, I saw whole tirades about how default resolution timer tampers physics simulation, and second, this is exactly the kind of thing where "the more the better". Hence 0.9.0 will use high resolution timer by default.

RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

### Re: high resolution love.run function for 0.8.0

You are confusing "the more the better" with "enough is enough". Smaller time step is only crucial for fast moving objects or "higly reactive". We are not simulating atoms for Christ sake.

The majority of physics libraries support a world metric system. A car is f.i 4 units long, as in 4 meters. Not 700000 or 0.32 (randomness). Body moving 1000km/h would only move ~4.64 meters in 1/60s ( 13 ms) time step. Collision detection is going to work, and everything will be smooth (no spring shooting stuff into space).

Sometimes people like to apply a physical time step a couple of times for each logical frame (say 5, but almost never 100) and you don't need a high res timer for that

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: high resolution love.run function for 0.8.0

You don't seem to get the idea of time resoltion. Let me explain:
Time resolution is not about frame rate. Time resoltion is about... is about the same thing as display resolution. The higher display resolution you have, the less pixellated picture you will get on it. Low timer resolution timer yields granulated, "pixellated" output, whereas high resolution timer is so precise that you can hardly notice such granulation even programmatically. Physics will run a lot more smoother, particularry.

Code: Select all

              1 second:     |------------------------------------------------------|
default time resoltion: ----|----------|----------|----------|----------|----------|---
high time resoltion: -|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-
If you ask a timer for current time, it will return one of the nearest marks, but not in between the marks. This result in timer return more or less time than actually passed. This is rough comparison of course, in reality high resolution timer is in order of several tens of magnitudes more precise than low resolution timer.

This is why "the more the better". Just as the more RAM the better, the more FLOPS the better, etc.

RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

### Re: high resolution love.run function for 0.8.0

You seem to have some real difficulties understanding other people. Try to read, not only see a post.

The reason why I am pointing out your mistakes is that you are spreading misconceptions and confusing other users. You have made no arguments so far (even invalid ones) and I have told you some already.

I will point it out for you once again:

1) AAA games, Indie games all work fine with ms precision .
2) Lower time step = more calculations. Why would you occupy your CPU with redundat calls if fewer are sufficient?
3) You are confusing other people trying to convince them they need the high-res timer for simulations. And you do it without using ANY arguments. If you would try to think it over you wouldn't post such irreflective theories. I'd suggest getting some hands-on experience and understanding the whole deal in the first place.

P.S Please maintain unified text font. We are not in the primary school anymore, try to express yourself by being concise and not putting BIG-ASS text.

slime
Solid Snayke
Posts: 2887
Joined: Mon Aug 23, 2010 6:45 am
Contact:

### Re: high resolution love.run function for 0.8.0

raidho36 wrote:Physics will run a lot more smoother, particularry.
In most cases, 0.8.0's delta time is not noticeably less 'smooth' than 0.9.0's.
raidho36 wrote:in reality high resolution timer is in order of several tens of magnitudes more precise than low resolution timer.
Microseconds (10^(-6) seconds) are only three orders of magnitude smaller than milliseconds (10^(-3) seconds).

I'm confused about why you chose to use local variables in the loop, because the code is a bit harder to understand now (partly due to the naming) and because if a few local instead of global variable accesses in the outermost run loop is causing measurable performance differences, it's likely there is little or no other code at all in the loop.

RedHot wrote:2) Lower time step = more calculations. Why would you occupy your CPU with redundat calls if fewer are sufficient?
Using a higher resolution timer has little to do with the amount of time steps.

### Who is online

Users browsing this forum: No registered users and 16 guests