I try to use coroutines to achieve cut scenes using Lua, and there are no problems with that except massive fps drop.
I really don't know why, but coroutine.resume slow down my program like from 5000 fps (without any rendering at all) to 300-350 fps while event couroutine is not dead (e.g resume constantly). Then event became dead fps returns to normal.
I think couroutines can't be so slow, and there are problems in my event.lua/eventManager.lua code, or i measure fps wrong way, or i doing everything completely horrible.
Event.lua
function event()
print("Event started")
--simply as it can be
for i = 1,1000 do
coroutine.yield()
end
--[[ wait
local wait = 0.0
print("Waiting 5 sec")
while wait < 5.0 do
wait = wait + coroutine.yield()
end
--]]
--[[ then play sound
local alarmSound = SoundsManager.getSound("sounds/alarm.ogg")
alarmSound:play()
while alarmSound:isPlaying() do
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 then
if FPS.fps ~= FPS.lastFPS then
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' then
coroutine.resume(EventsManager.event, frameDelta)
end
end
return EventsManager
Main.lua
EventsManager = require "EventsManager"
FPS = require "FPS"
EventsManager.init()
while true do
local frameDelta = getFrameDelta() --getting frameDelta somehow
EventsManager.update(frameDelta)-- comment this and fps will be ok
--render scene
FPS.render(frameDelta)
end