Difference between revisions of "love.timer.sleep"

m
m (Examples)
Line 21: Line 21:
 
Nothing.
 
Nothing.
 
== Examples ==
 
== Examples ==
=== Follow the the 0.8.0 API for love.timer.sleep but fix the older version ===
+
=== Use sleep to cap FPS at 30 ===
<source lang="lua">
 
local love_timer_sleep = love.timer.sleep -- in s (like >=0.8.0)
 
if love._version:find("^0%.[0-7]%.") then -- if version < 0.8.0
 
  -- love.timer.sleep in ms
 
  love_timer_sleep = function(s) love.timer.sleep(s*1000) end
 
end
 
</source>
 
=== Use sleep to cap FPS at 30 (in 0.8.0 or older) ===
 
 
<source lang="lua">
 
<source lang="lua">
 
function love.update(dt)
 
function love.update(dt)
 
   if dt < 1/30 then
 
   if dt < 1/30 then
       love_timer_sleep(1/30 - dt)
+
       love.timer.sleep(1/30 - dt)
 
   end
 
   end
 
end
 
end
 
</source>
 
</source>
=== More sophisticated way to cap FPS (in 0.8.0 or older) ===
+
=== More sophisticated way to cap FPS ===
 
This takes into account the time spent updating and drawing each frame.
 
This takes into account the time spent updating and drawing each frame.
 
<source lang="lua">
 
<source lang="lua">
 
function love.load()
 
function love.load()
 
   min_dt = 1/30
 
   min_dt = 1/30
   next_time = love.timer.getMicroTime()
+
   next_time = love.timer.getTime()
 
end
 
end
  
Line 54: Line 46:
 
   --rest of function here
 
   --rest of function here
  
   local cur_time = love.timer.getMicroTime()
+
   local cur_time = love.timer.getTime()
 
   if next_time <= cur_time then
 
   if next_time <= cur_time then
 
       next_time = cur_time
 
       next_time = cur_time
 
       return
 
       return
 
   end
 
   end
   love_timer_sleep(next_time - cur_time)
+
   love.timer.sleep(next_time - cur_time)
 
end
 
end
 
</source>
 
</source>
 +
 
== See Also ==
 
== See Also ==
 
* [[parent::love.timer]]
 
* [[parent::love.timer]]

Revision as of 08:50, 6 March 2014

Pauses the current thread for the specified amount of time.

Function

Available since LÖVE 0.8.0
This behaviour is not supported in earlier versions.

Synopsis

love.timer.sleep( s )

Arguments

number s
Seconds to sleep for.

Returns

Nothing.

Function

Removed in LÖVE 0.8.0
This behaviour is not supported in that and later versions.

Synopsis

love.timer.sleep( ms )

Arguments

number ms
Milliseconds to sleep for.

Returns

Nothing.

Examples

Use sleep to cap FPS at 30

function love.update(dt)
   if dt < 1/30 then
      love.timer.sleep(1/30 - dt)
   end
end

More sophisticated way to cap FPS

This takes into account the time spent updating and drawing each frame.

function love.load()
   min_dt = 1/30
   next_time = love.timer.getTime()
end

function love.update(dt)
   next_time = next_time + min_dt

   --rest of function here
end

function love.draw()
   --rest of function here

   local cur_time = love.timer.getTime()
   if next_time <= cur_time then
      next_time = cur_time
      return
   end
   love.timer.sleep(next_time - cur_time)
end

See Also


Other Languages