Page 1 of 2

[SOLVED]Draw order affecting speed

Posted: Tue Jul 07, 2015 3:18 am
by Featzen
First of all, sorry my bad english, i'm from Brazil...
Well, my problem is:
when i draw multiple images with a loop(like from zero to 600), my game still slow, the character moves slow.
Are there some solution for it?

See the video(very low quality)

Re: Draw order affecting speed

Posted: Tue Jul 07, 2015 5:42 am
by micha
Hi and welcome to the forum!

You are correct: Drawing many images can make a game slow, but there are some methods to speed it up. As a rule of thumb, try to minimize the number of "love.graphics.draw"-calls. Every time you call it, LÖVE has to communicate with the graphics card and that takes some time.

For speeding your game up, try to use a spriteBatch. With it you can draw many images with only one draw-call. Have a look at this tutorial for an introduction.

Re: Draw order affecting speed

Posted: Tue Jul 07, 2015 5:53 am
by Positive07
Other enhancements:

1- If you need to refresh every single frame (for example the background is randomly generated/animated and made of tiles), try to lower the number of draw calls by just drawing what is inside of the screen and not drawing what is outside of it.

2- If the level is completely static (the tiles dont change) you can use a [wiki]Canvas[/wiki] as an alternative to Sprite Batches (or a combination of both if you are clever enough)

3- Reduce the amount of RAM consumed by your images, for example by using some compressed formats (there are a bunch, some of them lack some channels, like the alpha one, or have a limited amount of bits for each channel, say 6bits instead of the standard 8bits), another way to reduce the size of an image is by reducing the width and height.

4- This is linked to the above, many images consume more RAM than a single image containing all the sprites, this basically means you can use a sprite sheet and then use [wiki]Quads[/wiki] to draw each sprite to the screen (Also Quads work really nice with Sprite Batches, check [wiki]Spritebatch:add[/wiki] for more info)

5- Whenever you need something pretty standard like squares or circles try to use the primitive shapes LÖVE provides instead of an image of it (Unless the shape is part of a more complex shape and you can simple draw it in the same image)

And I guess that is it!

Re: Draw order affecting speed

Posted: Tue Jul 07, 2015 9:48 am
by Featzen
Thank you both, i will read about spritebatch.

Love2d is beautiful :p

Re: Draw order affecting speed

Posted: Tue Jul 07, 2015 7:18 pm
by Nixola
Positive07 wrote:5- Whenever you need something pretty standard like squares or circles try to use the primitive shapes LÖVE provides instead of an image of it (Unless the shape is part of a more complex shape and you can simple draw it in the same image)
Primitive shapes are shamefully slow though. You can use meshes to easily draw filled primitives like this:

Code: Select all

local segments = 50
local t = {{0,0,0,0}}
for i = 0, segments do
  t[i+2] = {math.cos(math.pi*2/(segments)*i), math.sin(math.pi*2/(segments)*i),0,0}
end
t[#t+1] = t[2]
segments = segments+1
circle = love.graphics.newMesh(t)
circle:setVertexColors(false)
And then just draw it like this:

Code: Select all

love.graphics.draw(circle, x, y, 0, radius) -- 0 is the rotation, pretty useless on a circle, don't you think?
If you do this, be sure to create the mesh just once and store the result. You can draw a filled rectangle the same way:

Code: Select all

rectangle = love.graphics.newMesh{0,0, 1,0, 1,1, 0,1, 0,0}
rectangle:setVertexColors(false)

--draw it like this:
love.graphics.draw(rectangle, x, y, rotation, width, height)

Re: Draw order affecting speed

Posted: Wed Jul 08, 2015 3:11 am
by Positive07
Nixola wrote:-snip-
You are right! In the case that you need an static shape (vertices dont change) you can use [wiki]Mesh[/wiki]es, they have the added benefit that you can place them wherever you want! You can even texture them :awesome:

But creating them is expensive so in that case primitive shapes may be better.

NOTE: If you are doing a simple shape and you just need to change the size you could scale it (rotate it, sheer it) though since those transformations dont affect the Mesh

Re: Draw order affecting speed

Posted: Wed Jul 08, 2015 6:39 am
by SiENcE
@Nixola There is a small typo: "setVertexColors" instead of "setVertexColor"

Re: Draw order affecting speed

Posted: Wed Jul 08, 2015 11:17 am
by Nixola
SiENcE wrote:@Nixola There is a small typo: "setVertexColors" instead of "setVertexColor"
Right, I keep forgetting to update the repo where I got the snippets from, thanks!

Re: Draw order affecting speed

Posted: Thu Jul 09, 2015 10:53 pm
by Featzen
I'm drawing images, now with spritebatch.
But i'm doing something wrong, what is?
Here, still 6 fps :s

Re: Draw order affecting speed

Posted: Fri Jul 10, 2015 3:24 am
by Ethan-Taylor
Featzen wrote:I'm drawing images, now with spritebatch.
But i'm doing something wrong, what is?
Here, still 6 fps :s
Mate,
We're running at 60 over here! I think there might be a problem over your side ...