Skip to content

Commit

Permalink
Merge pull request #1 from tleydxdy/patch-1
Browse files Browse the repository at this point in the history
smooth, refresh rate independent animation
  • Loading branch information
Nixola authored Dec 18, 2019
2 parents bbd8810 + 1a5a72a commit 228dd0b
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "run"

local fps, frameTime, lastUpdate

local steps, current, width
local speed, num, bars, width
local WIDTH, HEIGHT

local fullscreen
Expand All @@ -12,7 +12,8 @@ local vsync
local str = [[
target FPS: %d (change with up/down arrow)
actual FPS: %d
steps: %d (change with left/right arrow)
speed: %d (change with left/right arrow)
number of bars: %d (change with -/+)
fullscreen: %s (toggle with f)
busy wait: %s (toggle with b)
vsync: %s (toggle with v+s)
Expand All @@ -24,6 +25,13 @@ Vsync should eliminate tearing, but increases input lag and adds no smoothness.]
local y


newBars = function()
width = WIDTH / (num * 3)
for i = 1, num do
bars[i] = WIDTH / num * (i - 1)
end
end

love.load = function()

love.busy = false
Expand All @@ -37,10 +45,10 @@ love.load = function()
fps = (fps > 0) and fps or 56
frameTime = 1 / fps
lastUpdate = 0
steps = 20
width = WIDTH / steps
current = 0

speed = 10
num = 3
bars = {}
newBars()

love.graphics.setBackgroundColor(3/8, 3/8, 3/8)
love.graphics.setColor(5/8, 5/8, 5/8)
Expand All @@ -55,14 +63,13 @@ sanitize = function()
fps = math.max(1, fps)
frameTime = 1/fps

steps = math.max(1, steps)
width = WIDTH / steps
speed = math.max(1, speed)
end


love.update = function()

if love.busy then
if love.busy then
while lastUpdate + frameTime > love.timer.getTime() do end
else
while lastUpdate + frameTime > love.timer.getTime() do
Expand All @@ -71,20 +78,26 @@ love.update = function()
end
lastUpdate = love.timer.getTime()

current = (current + 1) % (steps )

for i = 1, num do
bars[i] = (bars[i] + speed / 10 * WIDTH * frameTime) % (WIDTH)
end
end


love.draw = function()

local str = string.format(str, fps, love.timer.getFPS(), steps, tostring(fullscreen), tostring(love.busy), tostring(vsync))
local str = string.format(str, fps, love.timer.getFPS(), speed, num, tostring(fullscreen), tostring(love.busy), tostring(vsync))

love.graphics.rectangle("fill", width * current, y, width, HEIGHT)
for i = 1, num do
love.graphics.rectangle("fill", bars[i], y, width, HEIGHT)
if bars[i] > WIDTH - width then
love.graphics.rectangle("fill", bars[i] - WIDTH, y, width, HEIGHT)
end
end
love.graphics.print(str, 8, 8)
--[[
love.graphics.print(fps, 0, 0)
love.graphics.print(steps, 0, 16)
love.graphics.print(speed, 0, 16)
love.graphics.print(tostring(fullscreen), 0, 32)--]]

end
Expand All @@ -100,9 +113,17 @@ love.keypressed = function(key, keycode)
elseif key == "down" then
fps = fps - 1
elseif key == "left" then
steps = steps - 1
speed = speed - 1
elseif key == "right" then
steps = steps + 1
speed = speed + 1
elseif key == "-" or key == "_" then
num = num - 1
num = math.max(1, num)
newBars()
elseif key == "=" or key == "+" then
num = num + 1
num = math.max(1, num)
newBars()
elseif key == "f" then
if fullscreen then
love.window.setFullscreen(false)
Expand All @@ -122,4 +143,4 @@ love.keypressed = function(key, keycode)
vsync = flags.vsync > 0
end
sanitize()
end
end

0 comments on commit 228dd0b

Please sign in to comment.