每秒帧数

多少才够?

越快并不总是越好

多少才够?

我要做的这类游戏并不需要很高的 FPS。不过先回到开头:它是什么?它是1秒内连续显示的帧(屏幕画面)的数量。

30 FPS 就是在1秒内连续显示30张画面。如果 FPS 太低,你会看到画面卡顿、不流畅(并不总是这样)。

目前我的目标是至少 60 FPS,而如你所见,确实做到了 😄

游戏内 FPS

游戏内 FPS

保持掌控

我读了好几篇文章,讨论什么方式最好、最简单。结论是取决于游戏类型和平台。因为我更看重稳定可靠,而且这是一款简单的2D游戏,所以我选择把帧率固定为60。

为此我使用了 https://github.com/bjornbytes/tick,并在上面做了一些自己的修改,目前是这样的。

local tick = {
  rate = 1 / 60
}

local timeAccumulator = 0

function love.run()
  love.load(love.arg.parseGameArguments(arg), arg)
  love.update(0)
  love.timer.step()

  return function()
    -- Handle events.
    love.event.pump()
    for name, a, b, c, d, e, f in love.event.poll() do
      if name == "quit" and not (love.quit and love.quit()) then
        return a or 0
      end
      love.handlers[name](a, b, c, d, e, f)
    end

    -- Get delta time.
    local _, _, flags = love.window.getMode()
    local dt = love.timer.step()

    if flags.vsync > 0 and flags.refreshrate > 0 then
      local perfectDt = 1 / flags.refreshrate

      if dt < perfectDt * 1.3 and dt > perfectDt / 1.3 then -- If dt is somewhat near perfect, force it to be actually perfect.
        dt = perfectDt
      else
        dt = math.min(dt, 1 / 20) -- In case the game is lagging, let it lag (but let the game think it's at least 20 FPS). Not clamping dt may put us in a situation where we can't do all required ticks in time and the game will more or less freeze.
      end
    end

    -- Update game.
    timeAccumulator = timeAccumulator + dt
    local didUpdate = false

    while timeAccumulator >= tick.rate do
      timeAccumulator = timeAccumulator - tick.rate
      didUpdate = true

      -- local updateStartTime = love.timer.getTime()
      love.update(tick.rate)
      -- love.timer.sleep(20*(love.timer.getTime()-updateStartTime)) -- DEBUG: Emulate a slower computer.
    end

    -- Draw game.
    if didUpdate then
      love.graphics.origin()
      love.graphics.clear(love.graphics.getBackgroundColor())
      love.draw()
      love.graphics.present()
    end

    if not (flags.vsync > 0 and didUpdate) then
      love.timer.sleep(.001) -- Not needed if vsync is on and we called present(), as present() will have slept for us.
    end
  end
end

我这样做,是为了可以默认游戏始终以 60 FPS 运行,并且即使有人的显卡能跑到 10000 FPS,也不会出现奇怪的问题。用这种方法,它“始终”保持稳定。

Deltatime

几乎所有会动的东西我都用了 deltatime,以免动作不同步。

---Move point to destination
---@param dt number DeltaTime
---@return integer? x,integer? y,boolean? stopped
function Move:updateToDestination(dt)
    if self.vDestination == nil then
        return
    end

    local dist = self.vPosition:dist(self.vDestination)
    local step = self.speed * dt

Vsync

大多数显示器也有 vsync 选项,我在 Love2d 里反正已经把它打开了。

t.window.vsync = 1 -- Vertical sync mode (number)

最后

我还不能百分之百确定我在这里设想的方案在实践中是否可行,只有在不同的电脑和设备上测试过之后才会知道。我觉得不可能对所有系统都做到完全正确。当然也有一些电脑太慢,游戏在上面无法流畅运行。目前我正在尝试在这种情况下显示一条提示。

    local checkFpsSeconds = .1
    local minimalFpsNeeded = 55
    local warningAfterSeconds = 4.0
    local startLoadingTime = love.timer.getTime()
    self.tick =
        Tick.recur(
        function()
            local fps = love.timer.getFPS()

            if tonumber(love.timer.getTime() - startLoadingTime) > warningAfterSeconds then
                loadingScreen.textObj:setText("You need " .. tostring(minimalFpsNeeded - fps) .. " FPS more.")
            end

            if fps > minimalFpsNeeded then
                self.tick:stop()

                -- everything is fast
            end
        end,
        checkFpsSeconds
    )