From e5892c0fae770c682517f3d28026be555282a89d Mon Sep 17 00:00:00 2001 From: nightmareci Date: Thu, 28 Apr 2022 11:47:31 -0700 Subject: [PATCH] Rename shell scripts and implement better frame timing --- main.lua | 42 ++++++++++++++++++++++++----------- package => package.sh | 2 ++ release => release-windows.sh | 6 +++-- 3 files changed, 35 insertions(+), 15 deletions(-) rename package => package.sh (88%) rename release => release-windows.sh (89%) diff --git a/main.lua b/main.lua index 9c04823..52fecf9 100644 --- a/main.lua +++ b/main.lua @@ -81,7 +81,7 @@ function love.draw() love.graphics.setFont(font_3x5_2) love.graphics.setColor(1, 1, 1, 1) love.graphics.printf( - string.format("%.2f", 1 / love.timer.getAverageDelta()) .. + string.format("%.2f", 1.0 / love.timer.getAverageDelta()) .. "fps - " .. version, 0, 460, 635, "right" ) end @@ -290,6 +290,7 @@ function love.resize(w, h) end local TARGET_FPS = 60 +local FRAME_DURATION = 1.0 / TARGET_FPS function love.run() if love.load then love.load(love.arg.parseGameArguments(arg), arg) end @@ -299,7 +300,7 @@ function love.run() local dt = 0 local last_time = love.timer.getTime() - local time_accumulator = 0 + local time_accumulator = 0.0 return function() if love.event then love.event.pump() @@ -319,25 +320,40 @@ function love.run() if scene and scene.update and love.timer then 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 love.graphics.origin() love.graphics.clear(love.graphics.getBackgroundColor()) love.draw() love.graphics.present() end - local end_time = last_time + frame_duration - local time = love.timer.getTime() - while time < end_time do - love.timer.sleep(0.001) - 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) + for milliseconds=0.001,0.000,-0.001 do + local max_delay = 0.0 + 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 - time_accumulator = time_accumulator + time - last_time end - time_accumulator = time_accumulator - frame_duration + + 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 - last_time = love.timer.getTime() end end diff --git a/package b/package.sh similarity index 88% rename from package rename to package.sh index ed6f662..530ffe7 100755 --- a/package +++ b/package.sh @@ -1 +1,3 @@ +#!/bin/sh + zip -r cambridge.love libs load res scene tetris conf.lua main.lua scene.lua funcs.lua diff --git a/release b/release-windows.sh similarity index 89% rename from release rename to release-windows.sh index 5a31406..ad32024 100755 --- a/release +++ b/release-windows.sh @@ -1,4 +1,6 @@ -./package +#!/bin/sh + +./package-love.sh mkdir dist mkdir dist/windows mkdir dist/win32 @@ -8,4 +10,4 @@ zip dist/cambridge-windows.zip dist/windows/* SOURCES.md LICENSE.md cat dist/win32/love.exe cambridge.love > dist/win32/cambridge.exe zip dist/cambridge-win32.zip dist/win32/* SOURCES.md LICENSE.md cp cambridge.love dist/other/ -zip dist/cambridge-other.zip cambridge.love libs/discord-rpc.* SOURCES.md LICENSE.md \ No newline at end of file +zip dist/cambridge-other.zip cambridge.love libs/discord-rpc.* SOURCES.md LICENSE.md