Skip to content

Instantly share code, notes, and snippets.

@nagolove
Created May 22, 2020 16:43
Show Gist options
  • Select an option

  • Save nagolove/b6d6f714bd736a6d894746934b06f026 to your computer and use it in GitHub Desktop.

Select an option

Save nagolove/b6d6f714bd736a6d894746934b06f026 to your computer and use it in GitHub Desktop.
function love.conf(t)
t.window.fullscreen = true
t.window.vsync = false
end
local lk = love.keyboard
local time = love.timer.getTime()
local ticksPerSecond, tick = 0, 0
local xpos, ypos = 0, 0
local lastdt
love.draw = function()
local gr = love.graphics
local y = 0
gr.print(string.format("FPS %d", love.timer.getFPS()), 0, y)
y = y + gr.getFont():getHeight()
gr.print(string.format("dt = %f", lastdt), 0, y)
y = y + gr.getFont():getHeight()
gr.print(string.format("updates per second %d", ticksPerSecond), 0, y)
gr.rectangle("fill", xpos, ypos, 64, 54)
end
love.update = function(dt)
local now = love.timer.getTime()
tick = tick + 1
if now - time >= 1 then
time = now
ticksPerSecond = tick
tick = 0
end
lastdt = dt
if lk.isDown("left") then
xpos = xpos - 1
elseif lk.isDown("right") then
xpos = xpos + 1
elseif lk.isDown("up") then
ypos = ypos - 1
elseif lk.isDown("down") then
ypos = ypos + 1
end
end
function love.run()
if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
-- We don't want the first frame's dt to include time taken by love.load.
if love.timer then love.timer.step() end
local dt = 0
local time = love.timer.getTime()
local interval60hz = 60 / 1000
-- Main loop time.
return function()
-- Process events.
if love.event then
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then
if not love.quit or not love.quit() then
return a or 0
end
end
love.handlers[name](a,b,c,d,e,f)
end
end
-- Update dt, as we'll be passing it to update
if love.timer then dt = love.timer.step() end
-- Call update and draw
if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
if love.graphics and love.graphics.isActive() then
local now = love.timer.getTime()
if now - time > interval60hz then
time = now
love.graphics.origin()
love.graphics.clear(love.graphics.getBackgroundColor())
if love.draw then love.draw() end
love.graphics.present()
end
end
if love.timer then love.timer.sleep(0.001) end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment