Page 1 of 1

High cpu usage [solved]

Posted: Fri Sep 07, 2018 8:01 am
by xNick1
Hi guys,
My game has a high CPU usage: ~25%.
I think it's a little too high for a pixel art game.
I don't know what could cause it, the game is pretty big already.

What are the operations which could take a high amount of CPU?
I make a lot of calculations but it's simple math

Re: High cpu usage

Posted: Fri Sep 07, 2018 10:10 am
by Nelvin
Check your FPS ... maybe you're running your game without vsync and so with a ridiculously high framerate resulting in your logic running, like 500 times a second.

Re: High cpu usage

Posted: Fri Sep 07, 2018 11:05 am
by xNick1
Nelvin wrote:
Fri Sep 07, 2018 10:10 am
Check your FPS ... maybe you're running your game without vsync and so with a ridiculously high framerate resulting in your logic running, like 500 times a second.
Hmm I'm using vsync, I have 60 stable fps.
Does the audio impact much on the cpu?

Edit: It seems I'm messing something up with the audio.
In normal circumstances the game doesn't use over 4% of cpu, until I mess up with the audio.

I want to play some music when I'm near the castle.
The background music has to fade out, and the castle music has to fade in

castle:

Code: Select all

CastleSummer = Object:extend()
local imgPath = "assets/images/summer/"
local img = lg.newImage(imgPath .. "castle/castle.png")
img:setFilter('nearest', 'nearest')
local width = wWidth / 2 --wWidth / 1.5
local height = wHeight / 1.6 --wHeight / 1.3 
local sx = width / img:getWidth()
local sy = height / img:getHeight()
local y = wHeight - height
local music = la.newSource("assets/sounds/summer/castle.ogg") 

function CastleSummer:new(x)
    self.x = x
    self.y = y + wHeight / 70
    self.width = width
    self.height = height
    
    self.music = music
    self.music:setLooping(true)
    self.music:play()
    self.volume = 0
end

function CastleSummer:update(dt)
    self.music:setVolume(self.volume)
end

function CastleSummer:draw()
    lg.draw(img, self.x, self.y, 0, sx, sy)
end

background music:

Code: Select all

BackgroundMusicSummer = Object:extend()

function BackgroundMusicSummer:new()
    self.music = la.newSource("assets/sounds/summer/summer.ogg")
    self.music:setLooping(false)
    self.volume = 1
end

function BackgroundMusicSummer:update(dt)
    
    self.music:setVolume(self.volume)
end

function BackgroundMusicSummer:draw()
end

function BackgroundMusicSummer:stop()
    self.music:stop()
end

function BackgroundMusicSummer:play()
    self.music:play()
end
map:

Code: Select all

function spawnCastleSummer(dt)
    timePassedCastleSpawn = timePassedCastleSpawn + 1 * dt
    if timePassedCastleSpawn > castleSpawnTime then
        timePassedCastleSpawn = 0
        castleSpawnTime = love.math.random(30, 60)
        local r = playerSummer.x + wWidth
        local castleSummer = CastleSummer(r)
        table.insert(castles, castleSummer)
    end
    
    for i, c in ipairs(castles) do
        c:update(dt)
        
        if c.x < playerSummer.x - wWidth * 4 then
            table.remove(castles, i)
        end
    end
end

function collisionsCastleSummer(dt)
    for _, c in ipairs(castles) do
        if playerSummer.x >= c.x and playerSummer.x <= c.x + c.width then
            backgroundMusicSummer.volume = backgroundMusicSummer.volume - 0.2 * dt
            if backgroundMusicSummer.volume <= 0 then
                backgroundMusicSummer.volume = 0.1
            end
            
            c.volume = c.volume + 0.4 * dt
            if c.volume >= 1 then
                c.volume = 1
            end

        else
            c.volume = 0
            backgroundMusicSummer.volume = backgroundMusicSummer.volume + 0.2 * dt
            if backgroundMusicSummer.volume >= 1 then
                backgroundMusicSummer.volume = 1
            end
        end
    end
end

Re: High cpu usage [updated, problems with the Audio]

Posted: Fri Sep 07, 2018 3:17 pm
by Nelvin
That's strange - have you verified that removing these two background audio sources reduces the CPU load?

I just added to background songs to my game and changed their volume each frame with no measurable difference in cpu usage.

Re: High cpu usage [updated, problems with the Audio]

Posted: Fri Sep 07, 2018 3:24 pm
by xNick1
Wops, I feel dumb now.
The audio thing was one of the latest things I added, but the issue is still there after removing the music all together.
The cpu usage increases regularly as I play the game.
I spawn many objects while I play, but it's just simple sprites.
I'm definitely doing something wrong, but the project is big already and I have to figure out what I'm doing wrong.

Thanks for the help

The sprites are supposed to end up in the ram right?
What could even even use that much cpu?

Re: High cpu usage [updated, problems with the Audio]

Posted: Fri Sep 07, 2018 3:46 pm
by grump
xNick1 wrote:
Fri Sep 07, 2018 3:24 pm
I spawn many objects while I play, but it's just simple sprites.
How many is 'many'? Are you creating lots of (temporary) tables? Garbage collection is known to be a common bottleneck if the code makes poor use of tables.
I'm definitely doing something wrong, but the project is big already and I have to figure out what I'm doing wrong.
If you can't figure it out with the source code right before your eyes, you can't expect others to figure it out without looking at your code.
The sprites are supposed to end up in the ram right?
Textures are stored on the GPU. Anything else is stored in RAM.
What could even even use that much cpu?
Literally anything. Impossible to say without looking at the code.

Search the forums for "profiler". IIRC, ivan made a general purpose profiler that you could use to find bottlenecks in your code.

Re: High cpu usage [updated, problems with the Audio]

Posted: Fri Sep 07, 2018 5:52 pm
by pgimeno
grump wrote:
Fri Sep 07, 2018 3:46 pm
If you can't figure it out with the source code right before your eyes, you can't expect others to figure it out without looking at your code.
Spot-on. May I borrow that sentence? It can be useful in more than one occasion. :)

Re: High cpu usage

Posted: Sat Sep 08, 2018 6:25 am
by xNick1
I finally found the real problem.
I was running a twin with flux in an update function.
I didn't really need the other optimizations