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
High cpu usage [solved]
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
High cpu usage [solved]
Last edited by xNick1 on Sun Sep 09, 2018 5:34 pm, edited 3 times in total.
Re: High cpu usage
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
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
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
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]
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.
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]
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?
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]
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.
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.I'm definitely doing something wrong, but the project is big already and I have to figure out what I'm doing wrong.
Textures are stored on the GPU. Anything else is stored in RAM.The sprites are supposed to end up in the ram right?
Literally anything. Impossible to say without looking at the code.What could even even use that much cpu?
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]
Spot-on. May I borrow that sentence? It can be useful in more than one occasion.
Re: High cpu usage
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
I was running a twin with flux in an update function.
I didn't really need the other optimizations
Who is online
Users browsing this forum: Google [Bot] and 198 guests