Lua cut-scene - coroutine.resume leads to fps drop (or i doing it wrong) -
i try use coroutines achieve cut scenes using lua, , there no problems except massive fps drop.
i don't know why, coroutine.resume slow down program 5000 fps (without rendering @ all) 300-350 fps while event couroutine not dead (e.g resume constantly). event became dead fps returns normal.
i think couroutines can't slow, , there problems in event.lua/eventmanager.lua code, or measure fps wrong way, or doing horrible.
event.lua
function event() print("event started") --simply can = 1,1000 coroutine.yield() end --[[ wait local wait = 0.0 print("waiting 5 sec") while wait < 5.0 wait = wait + coroutine.yield() end --]] --[[ play sound local alarmsound = soundsmanager.getsound("sounds/alarm.ogg") alarmsound:play() while alarmsound:isplaying() coroutine.yield() end --]] print("event ended") end
fps.lua
local fps = { fps = 0, lastfps = 0, framestime = 0.0 } function fps.render(framedelta) fps.fps = fps.fps + 1 fps.framestime = fps.framestime + framedelta if fps.framestime >= 1.0 if fps.fps ~= fps.lastfps print("[fps] ".. fps.fps) fps.lastfps = fps.fps end fps.framestime = 0.0 fps.fps = 0 end end return fps
eventsmanager.lua
require "event" local eventsmanager = {} function eventsmanager.init() eventsmanager.event = coroutine.create(event) end function eventsmanager.update(framedelta) if coroutine.status(eventsmanager.event) ~= 'dead' coroutine.resume(eventsmanager.event, framedelta) end end return eventsmanager
main.lua
eventsmanager = require "eventsmanager" fps = require "fps" eventsmanager.init() while true local framedelta = getframedelta() --getting framedelta somehow eventsmanager.update(framedelta)-- comment , fps ok --render scene fps.render(framedelta) end
i tried code, getframedelta
returns time gap between previous , current calls, without scene render. changed wait time 10 seconds.
local prevtime = os.clock() local getframedelta = function() local curtime = os.clock() local framedelta = curtime - prevtime prevtime = curtime return framedelta end
here's output:
d:\dev>lua5.1 luafps.lua
event started waiting .. 10[fps] 879171 [fps] 882366 [fps] 880471 [fps] 882018 [fps] 880513 [fps] 881368 [fps] 879623 [fps] 881938 [fps] 880498
event ended
[fps] 882053 [fps] 1279909 [fps] 1279631 [fps] 1279899 [fps] 1277089 [fps] 1278399 [fps] 1279005 [fps] 1280125
so, yes co-routines take toll. every time co-routine yields
has keep tab of function left off, can resume
@ point next time call it. assume accounts discrepancy see : 800kfps v 1200kfps
.
that said, don't understand why need co-routines
calculating fps. have code calculates fps in fps.render
. should suffice call after render scene, doing now, skip event manager part calls co-routine
.
Comments
Post a Comment