[SOLVED]Draw order affecting speed

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Featzen
Prole
Posts: 7
Joined: Tue Jul 07, 2015 2:14 am

[SOLVED]Draw order affecting speed

Post 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)
Attachments
RpgProject.love
Solved
(495.58 KiB) Downloaded 205 times
Last edited by Featzen on Fri Jul 10, 2015 5:05 am, edited 1 time in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Draw order affecting speed

Post 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.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Draw order affecting speed

Post 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!
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Featzen
Prole
Posts: 7
Joined: Tue Jul 07, 2015 2:14 am

Re: Draw order affecting speed

Post by Featzen »

Thank you both, i will read about spritebatch.

Love2d is beautiful :p
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Draw order affecting speed

Post 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)
Last edited by Nixola on Wed Jul 08, 2015 11:17 am, edited 1 time in total.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Draw order affecting speed

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
SiENcE
Party member
Posts: 805
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: Draw order affecting speed

Post by SiENcE »

@Nixola There is a small typo: "setVertexColors" instead of "setVertexColor"
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Draw order affecting speed

Post 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!
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Featzen
Prole
Posts: 7
Joined: Tue Jul 07, 2015 2:14 am

Re: Draw order affecting speed

Post by Featzen »

I'm drawing images, now with spritebatch.
But i'm doing something wrong, what is?
Here, still 6 fps :s
Attachments
RpgProject.love
(495.53 KiB) Downloaded 168 times
Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

Re: Draw order affecting speed

Post 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 ...
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests