new fps counter

This commit is contained in:
binarycat 2022-02-26 22:45:11 -05:00 committed by lustlion
parent fcac1abef1
commit e3a5ab0c42
5 changed files with 87 additions and 17 deletions

View File

@ -8,7 +8,7 @@ function DebugUI()
love.graphics.print(light.pos.x,light.pos.x,light.pos.y+40)
end
love.graphics.print("time: "..fps_total..", fps: "..fps_draw..", frametime: "..math.floor(current_dt* 1000).."ms", 10*textScale, 0*textScale, 0, textScale)
love.graphics.print("fps: "..fps_current, 10*textScale, 0*textScale, 0, textScale)
love.graphics.print(--[["CPUtime: "..checkCPUTime("total")..", CPU: "..(math.floor(checkCPUTime("get")*10000)/100).."%,]] "memoryUsage: "..memoryUsage.."kB", 10*textScale, 20*textScale, 0, textScale)
love.graphics.setColor(1,1,1)

View File

@ -220,7 +220,8 @@ function Player:DoPhysics()
if self.isHooked then
self.move_x = 0
local hook = Vector(self.pos.x, self.pos.y, self.hookAnchor.x, self.hookAnchor.y)
if GetVectorValue(hook) > self.hookDistance then
local dist = math.min(GetVectorValue(hook), self.hookDistance)
local hook_angle = GetAngleFromVector(hook[1],hook[2])-math.rad(180)
if Keybind:CheckDown(Keybind.move.right) then
@ -229,6 +230,7 @@ function Player:DoPhysics()
if Keybind:CheckDown(Keybind.move.left) then
hook_angle = hook_angle + self.hookSwingSpeed
end
local particle_data = {
@ -242,13 +244,12 @@ function Player:DoPhysics()
}
Particle:New(self.pos.x,self.pos.y,particle_data)
local pos_x = self.hookAnchor.x + self.hookDistance * math.cos(hook_angle)
local pos_y = self.hookAnchor.y + self.hookDistance * math.sin(hook_angle)
local pos_x = self.hookAnchor.x + dist * math.cos(hook_angle)
local pos_y = self.hookAnchor.y + dist * math.sin(hook_angle)
self.vel.x = self.vel.x + pos_x - self.pos.x
self.vel.y = self.vel.y + pos_y - self.pos.y
self.pos.x = pos_x
self.pos.y = pos_y
end
end

74
code/queue.lua Normal file
View File

@ -0,0 +1,74 @@
Queue = {}
function Queue:New()
local o = {head = nil, tail = nil}
setmetatable(o, self)
self.__index = self
return o
end
function Queue:Enqueue(item)
local elem = {item = item}
if self.head == nil then
self.head = elem
self.tail = elem
return
end
elem.next = self.head
elem.next.prev = elem
self.head = elem
return
end
function Queue:Empty()
return self.tail == nil
end
function Queue:Dequeue()
if self:Empty() then
return nil
end
local newtail = self.tail.prev
local item = self.tail.item
if newtail then
newtail.next = nil
end
self.tail = newtail
return item
end
local tq1 = Queue:New()
tq1:Enqueue(5)
assert(tq1.head.item == 5)
assert(tq1:Dequeue() == 5)
-- queue that keeps a rolling tally of its arguments
AvgQueue = {}
function AvgQueue:New(n, initial)
local o = {}
o.n = n
o.queue = Queue:New()
o.avg = initial
local x = initial / n
for _ = 1,n do
o.queue:Enqueue(x)
end
setmetatable(o, self)
self.__index = self
return o
end
function AvgQueue:Push(item)
local x = item/self.n
self.avg = self.avg + x - self.queue:Dequeue()
self.queue:Enqueue(x)
return self.avg
end

View File

@ -18,6 +18,7 @@ require "code/level"
require "code/camera"
require "code/animation"
require "code/audio"
require "code/queue"
-- objects
require "code/entity"

View File

@ -10,10 +10,7 @@ function love.load()
editor_mode = false
textScale = 1
fps_count = 0
fps_second = 0
fps_draw = 0
fps_total = 0
love.graphics.setColor(1,1,1)
love.keyboard.setKeyRepeat(true)
@ -28,6 +25,9 @@ function love.load()
require "code/require"
fps_history = AvgQueue:New(30,60)
logPrint(loveInitLog)
loveInitLog = nil
@ -65,14 +65,8 @@ function love.update(dt)
-- audio update
love.audio.update()
-- fps counter
if fps_second >= 1 then
fps_second = fps_second - 1
fps_draw = fps_count
fps_count = 0
fps_total = fps_total + 1
end
fps_second = fps_second + dt
fps_count = fps_count + 1
fps_current = fps_history:Push(1/dt)
current_dt = dt
if DemoRecording or DemoPlayback then Demo:Step() end