Difference between revisions of "love.timer.sleep"

(what is this i dont even)
(new way to cap FPS)
Line 16: Line 16:
 
       love.timer.sleep(1000 * (1/30 - dt))
 
       love.timer.sleep(1000 * (1/30 - dt))
 
   end
 
   end
 +
end
 +
</source>
 +
=== More sophisticated way to cap FPS ===
 +
This takes into account the time spent updating and drawing each frame.
 +
<source lang="lua">
 +
function love.load()
 +
  min_dt = 1/30
 +
  next_time = love.timer.getMicroTime()
 +
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.getMicroTime()
 +
  if next_time <= cur_time then
 +
      next_time = cur_time
 +
      return
 +
  end
 +
  love.timer.sleep(1000*(next_time - cur_time))
 
end
 
end
 
</source>
 
</source>

Revision as of 04:07, 5 October 2011

Sleeps the program for the specified amount of time.

Function

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(1000 * (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.getMicroTime()
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.getMicroTime()
   if next_time <= cur_time then
      next_time = cur_time
      return
   end
   love.timer.sleep(1000*(next_time - cur_time))
end

See Also


Other Languages