From e867c238d60f402a85d2a8d3be5a1aecfbb53007 Mon Sep 17 00:00:00 2001 From: Nixola Date: Tue, 7 Jan 2020 22:43:25 +0100 Subject: [PATCH] Added fluctuating and random frametimes; modified keybindings --- conf.lua | 2 +- main.lua | 161 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 112 insertions(+), 51 deletions(-) diff --git a/conf.lua b/conf.lua index 400d160..6638c1e 100644 --- a/conf.lua +++ b/conf.lua @@ -36,7 +36,7 @@ function love.conf(t) t.modules.image = true -- Enable the image module (boolean) t.modules.joystick = false -- Enable the joystick module (boolean) t.modules.keyboard = true -- Enable the keyboard module (boolean) - t.modules.math = false -- Enable the math module (boolean) + t.modules.math = true -- Enable the math module (boolean) t.modules.mouse = true -- Enable the mouse module (boolean) t.modules.physics = false -- Enable the physics module (boolean) t.modules.sound = false -- Enable the sound module (boolean) diff --git a/main.lua b/main.lua index 6801a60..2b5422b 100644 --- a/main.lua +++ b/main.lua @@ -1,6 +1,9 @@ require "run" local fps, frameTime, lastUpdate +local fpsMax, fpsTimer, fluctuating, fpsSpeed + +local random, randomAmount, randomTime local speed, num, bars, width local WIDTH, HEIGHT @@ -14,9 +17,11 @@ target FPS: %d (change with up/down arrow) actual FPS: %d speed: %d (change with left/right arrow) number of bars: %d (change with -/+) -fullscreen: %s (toggle with f) +fullscreen: %s (toggle with ctrl+f) busy wait: %s (toggle with b) -vsync: %s (toggle with v+s) +vsync: %s (toggle with s) +fluctuating: %s (toggle with f, change max with ctrl + up/down arrow, change speed with ctrl + left/right arrow) +random stutter: %s [%dms] (toggle with r, change max amount with ctrl + +/-, shift to change faster) Freesync will only work when the application is fullscreen on Linux. Busy waiting is more precise, but much heavier on processor and battery. @@ -43,6 +48,16 @@ love.load = function() fps = flags.refreshrate - 5 fps = (fps > 0) and fps or 56 + + fpsMax = fps + fpsTimer = 0 + fluctuating = false + fpsSpeed = 10 + + random = false + randomTime = 0 + randomAmount = 0 + frameTime = 1 / fps lastUpdate = 0 speed = 10 @@ -59,34 +74,47 @@ love.load = function() end -sanitize = function() - fps = math.max(1, fps) - frameTime = 1/fps - - speed = math.max(1, speed) -end - -love.update = function() +love.update = function(dt) if love.busy then - while lastUpdate + frameTime > love.timer.getTime() do end + while lastUpdate + frameTime + randomTime > love.timer.getTime() do end else - while lastUpdate + frameTime > love.timer.getTime() do + while lastUpdate + frameTime + randomTime > love.timer.getTime() do love.timer.sleep(0.001) end end lastUpdate = love.timer.getTime() + fpsTimer = fpsTimer + dt * fpsSpeed / 10 + if fluctuating then + fpsCur = fps + (math.sin(fpsTimer)/2 + 0.5) * (fpsMax - fps) + frameTime = 1/fpsCur + end + + if random then + randomTime = (love.math.random() - 0.5 ) * randomAmount/1000 + end + for i = 1, num do - bars[i] = (bars[i] + speed / 10 * WIDTH * frameTime) % (WIDTH) + bars[i] = (bars[i] + speed * dt * WIDTH / 20) % (WIDTH) end end love.draw = function() - - local str = string.format(str, fps, love.timer.getFPS(), speed, num, tostring(fullscreen), tostring(love.busy), tostring(vsync)) + local fstr = fluctuating and ("true [max: %d, speed: %d, current: %d]"):format(fpsMax, fpsSpeed, fpsCur) or "false" + + local str = string.format(str, + fps, + love.timer.getFPS(), + speed, + num, + tostring(fullscreen), + tostring(love.busy), + tostring(vsync), + fstr, + tostring(random), randomAmount) for i = 1, num do love.graphics.rectangle("fill", bars[i], y, width, HEIGHT) @@ -103,44 +131,77 @@ love.draw = function() end +sanitize = function() + fps = math.max(1, fps) + frameTime = 1/fps + fpsMax = math.max(fpsMax, fps) + fpsSpeed = math.max(1, fpsSpeed) + + randomAmount = math.max(math.min(randomAmount, 1000), 0) + + speed = math.max(1, speed) +end + love.keypressed = function(key, keycode) - local s = love.keyboard.isDown("s") - local v = love.keyboard.isDown("v") - - if key == "up" then - fps = fps + 1 - elseif key == "down" then - fps = fps - 1 - elseif key == "left" then - speed = speed - 1 - elseif key == "right" then - 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) - love.window.setPosition(1, 1) - else - love.window.setFullscreen(true) - love.window.setPosition(0, 0) + local ctrl = love.keyboard.isDown("lctrl", "rctrl") + local shift = love.keyboard.isDown("lshift", "rshift") + + if ctrl then + if key == "up" then + fpsMax = fpsMax + 1 + elseif key == "down" then + fpsMax = fpsMax - 1 + elseif key == "left" then + fpsSpeed = fpsSpeed - 1 + elseif key == "right" then + fpsSpeed = fpsSpeed + 1 + elseif key == "+" then + randomAmount = randomAmount + (shift and 5 or 1) + elseif key == "-" then + randomAmount = randomAmount - (shift and 5 or 1) + elseif key == "f" then + if fullscreen then + love.window.setFullscreen(false) + love.window.setPosition(1, 1) + else + love.window.setFullscreen(true) + love.window.setPosition(0, 0) + end + fullscreen = not fullscreen + end + else + if key == "up" then + fps = fps + 1 + elseif key == "down" then + fps = fps - 1 + elseif key == "left" then + speed = speed - 1 + elseif key == "right" then + 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 + fluctuating = not fluctuating + fpsTimer = 0 + elseif key == "b" then + love.busy = not love.busy + elseif key == "s" then + local w, h, flags = love.window.getMode() + flags.vsync = (flags.vsync == 0) and 1 or 0 + love.window.setMode(w, h, flags) + flags = select(3, love.window.getMode()) + vsync = flags.vsync > 0 + elseif key == "r" then + random = not random + randomTime = 0 end - fullscreen = not fullscreen - elseif key == "b" then - love.busy = not love.busy - elseif (key == "v" and s) or (key == "s" and v) then - local w, h, flags = love.window.getMode() - flags.vsync = (flags.vsync == 0) and 1 or 0 - love.window.setMode(w, h, flags) - flags = select(3, love.window.getMode()) - vsync = flags.vsync > 0 end sanitize() end