Rename shell scripts and implement better frame timing

This commit is contained in:
nightmareci 2022-04-28 11:47:31 -07:00
parent 23a8c400ba
commit e5892c0fae
3 changed files with 35 additions and 15 deletions

View File

@ -81,7 +81,7 @@ function love.draw()
love.graphics.setFont(font_3x5_2) love.graphics.setFont(font_3x5_2)
love.graphics.setColor(1, 1, 1, 1) love.graphics.setColor(1, 1, 1, 1)
love.graphics.printf( love.graphics.printf(
string.format("%.2f", 1 / love.timer.getAverageDelta()) .. string.format("%.2f", 1.0 / love.timer.getAverageDelta()) ..
"fps - " .. version, 0, 460, 635, "right" "fps - " .. version, 0, 460, 635, "right"
) )
end end
@ -290,6 +290,7 @@ function love.resize(w, h)
end end
local TARGET_FPS = 60 local TARGET_FPS = 60
local FRAME_DURATION = 1.0 / TARGET_FPS
function love.run() function love.run()
if love.load then love.load(love.arg.parseGameArguments(arg), arg) end if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
@ -299,7 +300,7 @@ function love.run()
local dt = 0 local dt = 0
local last_time = love.timer.getTime() local last_time = love.timer.getTime()
local time_accumulator = 0 local time_accumulator = 0.0
return function() return function()
if love.event then if love.event then
love.event.pump() love.event.pump()
@ -320,24 +321,39 @@ function love.run()
if scene and scene.update and love.timer then if scene and scene.update and love.timer then
scene:update() scene:update()
local frame_duration = 1.0 / TARGET_FPS if time_accumulator < FRAME_DURATION then
if time_accumulator < frame_duration then
if love.graphics and love.graphics.isActive() and love.draw then if love.graphics and love.graphics.isActive() and love.draw then
love.graphics.origin() love.graphics.origin()
love.graphics.clear(love.graphics.getBackgroundColor()) love.graphics.clear(love.graphics.getBackgroundColor())
love.draw() love.draw()
love.graphics.present() love.graphics.present()
end end
local end_time = last_time + frame_duration
local time = love.timer.getTime() -- request 1ms delays first but stop short of overshooting, then do "0ms" delays without overshooting (0ms requests generally do a delay of some nonzero amount of time, but maybe less than 1ms)
while time < end_time do for milliseconds=0.001,0.000,-0.001 do
love.timer.sleep(0.001) local max_delay = 0.0
time = love.timer.getTime() while max_delay < FRAME_DURATION do
local delay_start_time = love.timer.getTime()
if delay_start_time - last_time < FRAME_DURATION - max_delay then
love.timer.sleep(milliseconds)
local last_delay = love.timer.getTime() - delay_start_time
if last_delay > max_delay then
max_delay = last_delay
end
else
break
end
end
end
while love.timer.getTime() - last_time < FRAME_DURATION do
-- busy loop, do nothing here until delay is finished; delays above stop short of finishing, so this part can finish it off precisely
end
end
local finish_delay_time = love.timer.getTime()
local real_frame_duration = finish_delay_time - last_time
time_accumulator = time_accumulator + real_frame_duration - FRAME_DURATION
last_time = finish_delay_time
end end
time_accumulator = time_accumulator + time - last_time
end
time_accumulator = time_accumulator - frame_duration
end
last_time = love.timer.getTime()
end end
end end

View File

@ -1 +1,3 @@
#!/bin/sh
zip -r cambridge.love libs load res scene tetris conf.lua main.lua scene.lua funcs.lua zip -r cambridge.love libs load res scene tetris conf.lua main.lua scene.lua funcs.lua

View File

@ -1,4 +1,6 @@
./package #!/bin/sh
./package-love.sh
mkdir dist mkdir dist
mkdir dist/windows mkdir dist/windows
mkdir dist/win32 mkdir dist/win32