[
  {
    "path": ".gitignore",
    "content": "build/\n\n"
  },
  {
    "path": "LICENSE",
    "content": "Copyright (C) 2019-2021 Nicola Orlando\n\nThis software is provided 'as-is', without any express or implied\nwarranty.  In no event will the authors be held liable for any damages\narising from the use of this software.\n\nPermission is granted to anyone to use this software for any purpose,\nincluding commercial applications, and to alter it and redistribute it\nfreely, subject to the following restrictions:\n\n1. The origin of this software must not be misrepresented; you must not\n   claim that you wrote the original software. If you use this software\n   in a product, an acknowledgment in the product documentation would be\n   appreciated but is not required.\n2. Altered source versions must be plainly marked as such, and must not be\n   misrepresented as being the original software.\n3. This notice may not be removed or altered from any source distribution.\n"
  },
  {
    "path": "README.md",
    "content": "# VRRTest\nA very small utility I wrote to test variable refresh rate on Linux. Should work on all major OSes.\n\n## Usage\nJust run the executable. Builds are provided for Windows and Linux (64-bit). You can download the LÖVE [https://love2d.org] runtime to run the .love file on any supported OS on any supported architecture.  \nAssuming the runtime is installed and in PATH, you can run it with `love <dir>`, where `<dir>` is the directory where this repo is cloned/extracted.  \n* Up and down arrows will change the target FPS of the tool.  \n* `Ctrl+f`  toggles fullscreen.\n* `b` toggles busy waiting. Having it on makes the framerate more precise, at the cost of a ton of battery and CPU utilization. Off by default.  \n* `s` toggles VSync.  \n* `f` toggles fluctuating framerate; `Ctrl+↑/↓` changes the maximum framerate, `Ctrl+←/→` changes the fluctuation speed.  \n* `r` toggles random stutter; `Alt+↑/↓` changes the amount of stuttering. Hold Shift as well to change faster.\n* `Alt+←/→` changes the monitor the tool will be displayed on.\n* `l` increases the amount of information shown on the screen, from nothing, to GPU-related information, to a list of frametimes. Wraps around.\n* Number keys will select a scene to be displayed. Each scene has additional controls, shown on the right.\n\n## Scenes\nAs of version 2.0.0, VRRTest supports different scenes, of which there are currently two. (A 100% increase over previous versions!)\n\n### Bars\nThe first and default scene, Bars, is easy on the eyes (I'm not claiming it looks good; you'll see what I mean in the screenshots section) and easily allows the user to detect screen tearing, by displaying vertical bars moving towards the right. The number and speed of said bars are tunable by the user.\nAdditional controls are as follows:\n* Left and right arrows will change the speed of the columns moving across the screen.  \n* `+` and `-` will change the amount of columns.\n\n### Squares\nThe second scene, Squares, adopts a higher-contrast color scheme (pure white on pure black) due to one of its functions. It displays a grid of squares, lighting up one (or more; see further) square per frame, each frame switching to the next one. The size of the squares can be changed by the user.\nOptionally, a trail can be set to light up more than one square per frame (or to have squares stay lit up for more than one frame; the end result is the same) to achieve two different functions:\n* Having no trail and a very high contrast allows the user to easily take a video, to later check frame-by-frame, or a long-exposure picture (assuming your phone or camera can do it) to check for duplicate or dropped frames. The maximum exposure length is the tool's period, which is displayed on the top-right. Examples of long-exposure pictures will be provided in the Screenshots section, even though they aren't screenshots. Guess I might just call it \"Screenshots and pictures\".\n* Having a trail might be useful if you need or want to check the latency difference of two mirrored monitors (or any other way to mirror a monitor, such as [Looking Glass](https://looking-glass.io) if, like me, you're one of those VFIO people), taking a picture of this tool running on the mirrored monitors with a trail makes counting the difference in frames easier, by just counting how many squares ahead (or behind) each output is. Not sure why that is, though; it might just be me.\n\nAdditional controls are as follows:\n* Left and right arrows will decrease or increase the trail length.\n* `+` and `-` will increase or decrease, respectively, the size of the squares.\n\n## Screenshots and pictures\nSome of these can't be screenshots. I apologize in advance for the quality of the pictures I took, as I don't own a camera or a tripod, both of which would prove useful in taking long-exposure pictures.\n### Scene 1 - Bars\n![Scene 1 screenshot without tearing](https://nixo.la/img/vrrtest_scene1.png)\n*How the Bars scene is supposed to look like, without any screen tearing. Ignore the visible portion of the cursor, please.*  \n\n\n![Scene 1 screenshot with tearing](https://nixo.la/img/vrrtest_scene1_tearing.png)\n*A screenshot of how the scene looks like with screen tearing.*  \n\n\n### Scene 2 - Squares\n![Scene 2 screenshot](https://nixo.la/img/vrrtest_scene2.png)\n*How a still frame of the Squares scene looks like. Its usefulness can't easily be conveyed by still screenshots.*  \n\n\n![Scene 2 low framerate without VRR](https://nixo.la/img/vrrtest_scene2_novrr_low.png)\n*Long-exposure picture of a monitor with Freesync disabled with lower framerate than its refresh frequency. Notice how some squares are brighter than others, caused by duplicated frames.*  \n\n\n![Scene 2 high framerate without VRR](https://nixo.la/img/vrrtest_scene2_novrr_high.png)\n*Long-exposure picture of a monitor with Freesync disabled with higher framerate than its refresh frequency. The empty squared are caused by dropped frames. Note that this might happen on a Freesync monitor too, when above its frequency range.*  \n\n\n![Scene 2 low framerate with VRR](https://nixo.la/img/vrrtest_scene2_vrr_low.png)\n*Finally, a long-exposure picture of a Freesync monitor with refresh rate within its range. Everything looks like it should, with every lit square being approximately the same as the others.*  \n\n\n![Scene 2 high framerate with VRR](https://nixo.la/img/vrrtest_scene2_vrr_high.png)\n*Long-exposure picture of a Freesync monitor with higher framerate than its maximum refresh frequency. VRR can't do much in this case; either limit your framerate to below your monitor maximum refresh frequency or enable V-Sync in your software and/or driver.*\n"
  },
  {
    "path": "colorFade.lua",
    "content": "local color = {}\n\nlocal current = {}\ncurrent.fg = {}\ncurrent.bg = {}\n\nlocal target = {}\ntarget.fg = {}\ntarget.bg = {}\n\nlocal speed = {}\nspeed.fg = {}\nspeed.bg = {}\n\ncolor.setColor = function(fg, bg)\n    current.fg = {unpack(fg)}\n    current.bg = {unpack(bg)}\n    target.fg = {unpack(fg)}\n    target.bg = {unpack(bg)}\n    speed.fg = {0, 0, 0}\n    speed.bg = {0, 0, 0}\nend\n\ncolor.setTarget = function(fg, bg)\n    target.fg = {unpack(fg)}\n    target.bg = {unpack(bg)}\n    speed.fg = {target.fg[1] - current.fg[1], target.fg[2] - current.fg[2], target.fg[3] - current.fg[3]}\n    speed.bg = {target.bg[1] - current.bg[1], target.bg[2] - current.bg[2], target.bg[3] - current.bg[3]}\nend\n\n\ncolor.update = function(dt)\n    for i = 1, 3 do\n        local new_fg = current.fg[i] + speed.fg[i] * dt\n        if new_fg <= target.fg[i] and current.fg[i] >= target.fg[i] or\n          new_fg >= target.fg[i] and current.fg[i] <= target.fg[i] then\n            new_fg = target.fg[i]\n        end\n        current.fg[i] = new_fg\n\n        local new_bg = current.bg[i] + speed.bg[i] * dt\n        if new_bg <= target.bg[i] and current.bg[i] >= target.bg[i] or\n          new_bg >= target.bg[i] and current.bg[i] <= target.bg[i] then\n            new_bg = target.bg[i]\n        end\n        current.bg[i] = new_bg\n    end\n    love.graphics.setColor(current.fg)\n    love.graphics.setBackgroundColor(current.bg)\nend\n\ncolor.bg = function()\n    return current.bg\nend\n\ncolor.fg = function()\n    return current.fg\nend\n\nreturn color"
  },
  {
    "path": "conf.lua",
    "content": "function love.conf(t)\n    t.identity = \"freesynctest\"         -- The name of the save directory (string)\n    t.appendidentity = false            -- Search files in source directory before save directory (boolean)\n    t.version = \"11.0\"                  -- The LÖVE version this game was made for (string)\n    t.console = false                   -- Attach a console (boolean, Windows only)\n    t.accelerometerjoystick = false     -- Enable the accelerometer on iOS and Android by exposing it as a Joystick (boolean)\n    t.externalstorage = false           -- True to save files (and read from the save directory) in external storage on Android (boolean) \n    t.gammacorrect = false              -- Enable gamma-correct rendering, when supported by the system (boolean)\n \n    t.audio.mixwithsystem = true        -- Keep background music playing when opening LOVE (boolean, iOS and Android only)\n \n    t.window.title = \"Freesync test\"    -- The window title (string)\n    t.window.icon = nil                 -- Filepath to an image to use as the window's icon (string)\n    t.window.width = 0                  -- The window width (number)\n    t.window.height = 0                 -- The window height (number)\n    t.window.borderless = true          -- Remove all border visuals from the window (boolean)\n    t.window.resizable = false          -- Let the window be user-resizable (boolean)\n    t.window.minwidth = 1               -- Minimum window width if the window is resizable (number)\n    t.window.minheight = 1              -- Minimum window height if the window is resizable (number)\n    t.window.fullscreen = true          -- Enable fullscreen (boolean)\n    t.window.fullscreentype = \"desktop\" -- Choose between \"desktop\" fullscreen or \"exclusive\" fullscreen mode (string)\n    t.window.vsync = 0                  -- Vertical sync mode (number)\n    t.window.msaa = 0                   -- The number of samples to use with multi-sampled antialiasing (number)\n    t.window.depth = nil                -- The number of bits per sample in the depth buffer\n    t.window.stencil = nil              -- The number of bits per sample in the stencil buffer\n    t.window.display = 1                -- Index of the monitor to show the window in (number)\n    t.window.highdpi = false            -- Enable high-dpi mode for the window on a Retina display (boolean)\n    t.window.x = nil                    -- The x-coordinate of the window's position in the specified display (number)\n    t.window.y = nil                    -- The y-coordinate of the window's position in the specified display (number)\n \n    t.modules.audio = false             -- Enable the audio module (boolean)\n    t.modules.data = false              -- Enable the data module (boolean)\n    t.modules.event = true              -- Enable the event module (boolean)\n    t.modules.font = true               -- Enable the font module (boolean)\n    t.modules.graphics = true           -- Enable the graphics module (boolean)\n    t.modules.image = true              -- Enable the image module (boolean)\n    t.modules.joystick = false          -- Enable the joystick module (boolean)\n    t.modules.keyboard = true           -- Enable the keyboard module (boolean)\n    t.modules.math = true               -- Enable the math module (boolean)\n    t.modules.mouse = true              -- Enable the mouse module (boolean)\n    t.modules.physics = false           -- Enable the physics module (boolean)\n    t.modules.sound = false             -- Enable the sound module (boolean)\n    t.modules.system = false            -- Enable the system module (boolean)\n    t.modules.thread = false            -- Enable the thread module (boolean)\n    t.modules.timer = true              -- Enable the timer module (boolean), Disabling it will result 0 delta time in love.update\n    t.modules.touch = false             -- Enable the touch module (boolean)\n    t.modules.video = false             -- Enable the video module (boolean)\n    t.modules.window = true             -- Enable the window module (boolean)\nend "
  },
  {
    "path": "main.lua",
    "content": "require \"run\"\n\nlocal color = require \"colorFade\"\n\nlocal fps, frameTime, lastUpdate\nlocal fpsMax, fpsTimer, fluctuating, fpsSpeed\n\nlocal random, randomAmount, randomTime\n\nlocal displays, display\n\nlocal fullscreen\n\nlocal vsync\n\nlocal logLevel, logLevels\nlocal deltaTimes, logLines, logWidth\n\nlocal WIDTH, HEIGHT\n\nlocal scenes = {}\nscenes.x = 0\nscenes.y = 0\nlocal scene = 1\nlocal loadScenes = function(width, height)\n    for i, file in ipairs(love.filesystem.getDirectoryItems(\"scenes\")) do\n        local f, err = love.filesystem.load(\"scenes/\" .. file)\n        if not f then\n            print(\"Could not load\", file..\":\", err)\n        else\n            f, err = pcall(f)\n            if not f then\n                print(\"Could not run\", file..\":\", err)\n            else\n                scenes[i] = err\n                err.load(width, height)\n                print(i, err)\n            end\n        end\n    end\nend\n\nlocal setDisplay = function(n)\n    local new_displays = love.window.getDisplayCount()\n    n = (n-1) % new_displays + 1\n    if n == display and new_displays == displays then return end\n    WIDTH, HEIGHT = love.window.getDesktopDimensions(n)\n    love.window.setMode(WIDTH, HEIGHT, {display = n, fullscreen = fullscreen, vsync = vsync and 1 or 0})\n    display = n\n    displays = new_displays\n    for i, scene in ipairs(scenes) do\n        scene.resize(WIDTH - scenes.x, HEIGHT - scenes.y)\n    end\nend\n\n\n\nlocal str = [[\nactual FPS: %d\ntarget FPS: %d (change with up/down arrow)\nfullscreen: %s (toggle with ctrl+f)\nbusy wait: %s (toggle with b)\nvsync: %s (toggle with s)\nfluctuating: %s (toggle with f, change max with ctrl + up/down arrow, change speed with ctrl + left/right arrow)\nrandom stutter: %s [%dms] (toggle with r, change max amount with alt + up/down arrow, shift to change faster)\ndisplay: %d/%d (switch with alt + left/right arrow)\nlog level: %d/%d (increase with l, wraps around)\nselected scene: %d (%s)\n\nFreesync will only work when the application is fullscreen on Linux.\nBusy waiting is more precise, but much heavier on processor and battery.\nVsync should eliminate tearing, but increases input lag and adds no smoothness.\nYou can quit this program with the Escape or Q keys.]]\nlocal sceneStr\n\nlove.load = function()\n\n    love.busy = false\n\n    WIDTH, HEIGHT = love.graphics.getDimensions()\n\n    local flags = select(3, love.window.getMode())\n\n    scenes.y = (#select(2, love.graphics.getFont():getWrap(str, WIDTH)) + 1) * love.graphics.getFont():getHeight() + 8\n\n    fps = flags.refreshrate - 5\n    fps = (fps > 0) and fps or 56\n\n    fpsMax = fps\n    fpsTimer = 0\n    fluctuating = false\n    fpsSpeed = 10\n\n    random = false\n    randomTime = 0\n    randomAmount = 0\n\n    displays = love.window.getDisplayCount()\n    display = 1\n\n    frameTime = 1 / fps\n    lastUpdate = 0\n\n    logLevel = 0\n    logLevels = 3 -- 0-2\n    deltaTimes = {}\n    logLines = math.floor((HEIGHT - scenes.y) / love.graphics.getFont():getHeight())\n    logWidth = love.graphics.getFont():getWidth(\"00000 µs\") + 16\n\n    fullscreen = flags.fullscreen\n    vsync = flags.vsync > 0\n    love.keyboard.setKeyRepeat(true)\n\n    loadScenes(WIDTH - scenes.x, HEIGHT - scenes.y)\n\n    color.setColor(scenes[scene].color.fg, scenes[scene].color.bg)\nend\n\n\nlove.update = function(dt)\n\n    if love.busy then\n        while lastUpdate + frameTime + randomTime > love.timer.getTime() do end\n    else\n        while lastUpdate + frameTime + randomTime > love.timer.getTime() do\n            love.timer.sleep(0)\n        end\n    end\n    lastUpdate = love.timer.getTime()\n\n    fpsTimer = fpsTimer + dt * fpsSpeed / 10\n    if fluctuating then\n        fpsCur = fps + (math.sin(fpsTimer)/2 + 0.5) * (fpsMax - fps)\n        frameTime = 1/fpsCur\n    end\n\n    if random then\n        randomTime = (love.math.random() - 0.5 ) * randomAmount/1000\n    end\n\n    scenes[scene].update(dt, fps)\n    color.update(dt)\n    if logLevel > 1 then\n        table.insert(deltaTimes, 1, string.format(\"%d µs\", dt * 1000000))\n        deltaTimes[logLines + 1] = nil\n    end\nend\n\n\nlove.draw = function()\n    local fstr = fluctuating and (\"true [max: %d, speed: %d, current: %d]\"):format(fpsMax, fpsSpeed, fpsCur) or \"false\"\n\n    local str = string.format(str, \n        love.timer.getFPS(),\n        fps,\n        tostring(fullscreen),\n        tostring(love.busy),\n        tostring(vsync),\n        fstr,\n        tostring(random), randomAmount,\n        display, displays,\n        logLevel, logLevels - 1,\n        scene,\n        scenes[scene].name)\n\n    love.graphics.print(str, 8, 8)\n    love.graphics.print(scenes[scene].str, WIDTH - scenes[scene].strWidth - 8, 8)\n    if logLevel > 0 then\n        love.graphics.printf(table.concat({love.graphics.getRendererInfo()}, \"\\n\"), 0, scenes.y - love.graphics.getFont():getHeight() * 5, WIDTH - 8, \"right\")\n    end\n\n    scenes[scene].draw(scenes.x, scenes.y)\n\n    if logLevel > 1 then\n        --love.graphics.setColor(.75, 0, 0)\n        love.graphics.setColor(color.bg())\n        love.graphics.rectangle(\"fill\", WIDTH - logWidth, scenes.y, logWidth + 1, HEIGHT)\n        love.graphics.setColor(color.fg())\n        for i, ms in ipairs(deltaTimes) do\n            love.graphics.printf(ms, 0, scenes.y + (i - 1) * love.graphics.getFont():getHeight(), WIDTH - 8, \"right\")\n        end\n    end\n\nend\n\n\nsanitize = function()\n    fps = math.max(1, fps)\n    frameTime = 1/fps\n    fpsMax = math.max(fpsMax, fps)\n    fpsSpeed = math.max(1, fpsSpeed)\n\n    randomAmount = math.max(math.min(randomAmount, 1000), 0)\nend\n\nlove.keypressed = function(key, keycode)\n\n    local ctrl = love.keyboard.isDown(\"lctrl\", \"rctrl\")\n    local shift = love.keyboard.isDown(\"lshift\", \"rshift\")\n    local alt = love.keyboard.isDown(\"ralt\", \"lalt\")\n\n    if ctrl then\n        if key == \"up\" then\n            fpsMax = fpsMax + 1\n        elseif key == \"down\" then\n            fpsMax = fpsMax - 1\n        elseif key == \"left\" then\n            fpsSpeed = fpsSpeed - 1\n        elseif key == \"right\" then\n            fpsSpeed = fpsSpeed + 1\n        elseif key == \"f\" then\n            if fullscreen then\n                love.window.setFullscreen(false)\n                love.window.setPosition(1, 1)\n            else\n                love.window.setFullscreen(true)\n                love.window.setPosition(0, 0)\n            end\n            fullscreen = not fullscreen\n        end\n    elseif alt then\n        if key == \"up\" then\n            randomAmount = randomAmount + (shift and 5 or 1)\n        elseif key == \"down\" then\n            randomAmount = randomAmount - (shift and 5 or 1)\n        elseif key == \"left\" then\n            setDisplay(display - 1)\n            return\n        elseif key == \"right\" then\n            setDisplay(display + 1)\n            return\n        end\n    else\n        if key == \"up\" then\n            fps = fps + 1\n        elseif key == \"down\" then\n            fps = fps - 1\n        elseif key == \"f\" then\n            fluctuating = not fluctuating\n            fpsTimer = 0\n        elseif key == \"b\" then\n            love.busy = not love.busy\n        elseif key == \"s\" then\n            local w, h, flags = love.window.getMode()\n            flags.vsync = (flags.vsync == 0) and 1 or 0\n            love.window.setMode(w, h, flags)\n            flags = select(3, love.window.getMode())\n            vsync = flags.vsync > 0\n        elseif key == \"r\" then\n            random = not random\n            randomTime = 0\n        elseif key == \"escape\" or key == \"q\" then\n            love.event.quit()\n        elseif key == \"l\" then\n            logLevel = (logLevel + 1) % logLevels\n        end\n    end\n    if tonumber(key) and scenes[tonumber(key)] then\n        scene = tonumber(key)\n        color.setTarget(scenes[scene].color.fg, scenes[scene].color.bg)\n        return\n    end\n    scenes[scene].keypressed(key, keycode)\n    sanitize()\nend\n\nlove.textinput = function(str)\n    scenes[scene].textinput(str)\n    sanitize()\nend\n"
  },
  {
    "path": "run.lua",
    "content": "function love.run()\n    if love.load then love.load(love.arg.parseGameArguments(arg), arg) end\n \n    -- We don't want the first frame's dt to include time taken by love.load.\n    if love.timer then love.timer.step() end\n \n    local dt = 0\n \n    -- Main loop time.\n    return function()\n        -- Process events.\n        if love.event then\n            love.event.pump()\n            for name, a,b,c,d,e,f in love.event.poll() do\n                if name == \"quit\" then\n                    if not love.quit or not love.quit() then\n                        return a or 0\n                    end\n                end\n                love.handlers[name](a,b,c,d,e,f)\n            end\n        end\n \n        -- Update dt, as we'll be passing it to update\n        if love.timer then dt = love.timer.step() end\n \n        -- Call update and draw\n        if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled\n \n        if love.graphics and love.graphics.isActive() then\n            love.graphics.origin()\n            love.graphics.clear(love.graphics.getBackgroundColor())\n \n            if love.draw then love.draw() end\n \n            love.graphics.present()\n        end\n\n        if not love.busy then\n            love.timer.sleep(0.001)\n        end\n    end\nend"
  },
  {
    "path": "scenes/1.lua",
    "content": "local scene = {}\nscene.name = \"Bars\"\nscene.color = {}\nscene.color.fg = {5/8, 5/8, 5/8}\nscene.color.bg = {3/8, 3/8, 3/8}\n\nlocal speed, num, bars, barWidth\n\nlocal WIDTH, HEIGHT\n\nlocal str = [[\nspeed: %d (change with left/right arrow)\nnumber of bars: %d (change with -/+)]]\n\nnewBars = function()\n    barWidth = WIDTH / (num * 3)\n    for i = 1, num do\n        bars[i] = WIDTH / num * (i - 1)\n    end\nend\n\nscene.load = function(w, h)\n    WIDTH, HEIGHT = w, h\n    speed = 10\n    num = 3\n    bars = {}\n    newBars()\n    scene.strWidth = love.graphics.getFont():getWidth(str:format(1000, 1000))\nend\n\nscene.resize = function(w, h)\n    WIDTH, HEIGHT = w, h\nend\n\nscene.update = function(dt, fps)\n    for i = 1, num do\n        bars[i] = (bars[i] + speed * dt * WIDTH / 20) % (WIDTH)\n    end\n    scene.str = str:format(speed, num)\nend\n\nscene.draw = function(x, y)\n    for i = 1, num do\n        love.graphics.rectangle(\"fill\", bars[i] + x , y, barWidth, HEIGHT)\n        if bars[i] > WIDTH - barWidth then\n            love.graphics.rectangle(\"fill\", bars[i] - WIDTH + x, y, barWidth, HEIGHT)\n        end\n    end\nend\n\nscene.keypressed = function(key, keycode, isRepeat)\n    local ctrl = love.keyboard.isDown(\"lctrl\", \"rctrl\")\n    local shift = love.keyboard.isDown(\"lshift\", \"rshift\")\n    local alt = love.keyboard.isDown(\"ralt\", \"lalt\")\n\n    if ctrl or shift or alt then return end\n\n    if key == \"left\" then\n        speed = speed - 1\n    elseif key == \"right\" then\n        speed = speed + 1\n    end\n    speed = math.max(1, speed)\nend\n\nscene.textinput = function(str)\n    local ctrl = love.keyboard.isDown(\"lctrl\", \"rctrl\")\n    local shift = love.keyboard.isDown(\"lshift\", \"rshift\")\n    local alt = love.keyboard.isDown(\"ralt\", \"lalt\")\n\n    if str == \"-\" then\n        num = num - 1\n        num = math.max(1, num)\n        newBars()\n    elseif str == \"+\" then\n        num = num + 1\n        num = math.max(1, num)\n        newBars()\n    end\nend\n\nreturn scene\n"
  },
  {
    "path": "scenes/2.lua",
    "content": "local scene = {}\nscene.name = \"Squares\"\nscene.color = {}\nscene.color.fg = {1, 1, 1}\nscene.color.bg = {0, 0, 0}\n\nlocal frame, size, width, height, frames, trail, gcd\nlocal WIDTH, HEIGHT\n\nlocal str = [[\ntrail (frames): %d (change with left/right arrow)\nsquare size (px): %d (change with +/-)\nperiod (seconds): ~%.2f (results from size)\n\ntrail=0 makes it easier to use a video or a\nlong-exposure picture (lasting up to the shown period)\nto see repeated or dropped frames. Higher values can\nhelp show latency between monitors or other ways of\nmirroring a screen, when the same istance of this\nprogram is displayed on all of them.]]\n\ngcd = function(n1, n2)\n    if n1 % n2 == 0 then\n        return n2\n    elseif n1 < n2 then\n        n1, n2 = n2, n1\n    end\n    return gcd(n1 % n2, n2)\nend\n\nlocal wrap = function(n, limit)\n    return n % limit\nend\n\nlocal sanitize = function()\n    size = math.min(math.max(3, size), WIDTH, HEIGHT)\n    width = math.ceil(WIDTH / size)\n    height = math.ceil(HEIGHT / size)\n    frames = width * height\n    trail = math.min(math.max(trail, 0), frames - 1)\nend\n\nscene.load = function(w, h)\n    WIDTH, HEIGHT = w, h\n    frame = 0\n    size = math.max(math.min(gcd(WIDTH, HEIGHT), WIDTH/4, HEIGHT/3), WIDTH/16, HEIGHT/9)\n    width = math.ceil(WIDTH / size)\n    height = math.ceil(HEIGHT / size)\n    frames = width * height\n    trail = 0\n    scene.strWidth = love.graphics.getFont():getWidth(str:format(1000, 1000, 10.99))\nend\n\nscene.resize = function(w, h)\n    WIDTH, HEIGHT = w, h\n    sanitize()\nend\n\nscene.update = function(dt, fps)\n    frame = wrap(frame + 1, frames)\n    scene.str = str:format(trail, size, frames / fps)\nend\n\nscene.draw = function(x, y)\n    for lx = 0, width do\n        love.graphics.line(lx *  size + x, y, lx * size + x, HEIGHT + y)\n    end\n    for ly = 0, height do\n        love.graphics.line(x, ly * size + y, WIDTH + x, ly * size + y)\n    end\n\n    for f = frame - trail,  frame do\n        local f = wrap(f, frames)\n        local rx = f % width\n        local ry = math.floor(f / width)\n\n        love.graphics.rectangle(\"fill\", x + rx * size, y + ry * size, size, size)\n    end\n\nend\n\nscene.keypressed = function(key, keycode, isRepeat)\n    local ctrl = love.keyboard.isDown(\"lctrl\", \"rctrl\")\n    local shift = love.keyboard.isDown(\"lshift\", \"rshift\")\n    local alt = love.keyboard.isDown(\"ralt\", \"lalt\")\n\n    if ctrl or shift or alt then return end\n\n    if key == \"left\" then\n        trail = trail - 1\n    elseif key == \"right\" then\n        trail = trail + 1\n    end\n    sanitize()\nend\n\nscene.textinput = function(str)\n    local ctrl = love.keyboard.isDown(\"lctrl\", \"rctrl\")\n    local shift = love.keyboard.isDown(\"lshift\", \"rshift\")\n    local alt = love.keyboard.isDown(\"ralt\", \"lalt\")\n\n    if str == \"+\" then\n        size = size + 1\n    elseif str == \"-\" then\n        size = size - 1\n    end\n    sanitize()\nend\n\nreturn scene\n"
  }
]