A question on displaying an image multiple times
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 39
- Joined: Sat Nov 28, 2015 10:13 am
A question on displaying an image multiple times
Is it better to display the same image at multiple positions than to create new images with the same texture?
Re: A question on displaying an image multiple times
Yes. Creating new images with the same texture is literally just a waste of video memory, you can just get one image and draw it in different locations.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: A question on displaying an image multiple times
Also a good point is using spritebatches. They can produce performance with equal images.
Also take a look at AutoBatch
Also take a look at AutoBatch
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: A question on displaying an image multiple times
Is it? Actually I was wondering this myself because I thought that Lua was smart in that any duplicate data just gets referenced. So you create a variable with some data, then create more variables with duplicate data, but Lua would instead just make those subsequent variables references to the original. So wouldn't the same thing happen with images? You create an image, then create more with the same file, won't it know this and just reference it?Nixola wrote:Yes. Creating new images with the same texture is literally just a waste of video memory, you can just get one image and draw it in different locations.
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: A question on displaying an image multiple times
Well, for images, lua won't, but i haven't checked löve's source whether that checks previously loaded images or not.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: A question on displaying an image multiple times
Nope! When you call love.graphics.newImage, LÖVE grabs some memory, reads the file, loads the image, stores it in memory, creates Lua userdata, pass it to the Lua code, the userdata is unique always, the memory alocation is also unique because LÖVE doesn't memoize which images you have loaded or not and can't check if two images look the same because the comparison would imply checking every pixel, its red, green, blue and alpha component.
So creating new images, fonts or whatever, is slow, takes memory and is not something you should do continuosly but once
Drawing is less silly, the data is already in memory so drawing is just telling the GPU, put this here in the screen, but GPU calls can be a bottleneck when you are drawing thousands of stuff to the screen, so that is where spritebatches come in, they tell the GPU, put this image here and here and here and ... in the screen.
So creating new images, fonts or whatever, is slow, takes memory and is not something you should do continuosly but once
Drawing is less silly, the data is already in memory so drawing is just telling the GPU, put this here in the screen, but GPU calls can be a bottleneck when you are drawing thousands of stuff to the screen, so that is where spritebatches come in, they tell the GPU, put this image here and here and here and ... in the screen.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: A question on displaying an image multiple times
Depends on how you use it. If you do a=love.graphics.newImage('image'), then b=a, then yes, you've got two references to the same image. But if you do a=love.graphics.newImage('image'), then b=love.graphics.newImage('image'), you will end up with two distinct copies. Same if you do a={ }; b={ }. Lua only checks if two things are equal (in order to store references rather than copies) if they are strings: doing a='string'; b='string' is equivalent to doing a='string'; b=a. Keeping track of whether a string has been already used is probably a significant part of the reason why strings are slow to manipulate in Lua (but fast to compare, because only the references are compared).
Easy enough to check with tables and images:
Easy enough to check with tables and images:
Code: Select all
local lgNewImage = love.graphics.newImage
print(lgNewImage('image.png') == lgNewImage('image.png')) -- prints false
a = {}
b = a
print(a == b) -- prints true
a[1] = 3
print(b[1]) -- prints 3
c = {}
d = {}
print(c == d) -- prints false
c[1] = 3
print(d[1]) -- prints nil
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: A question on displaying an image multiple times
Interesting. I learned something. Not that I ever tried to do it the way I said, but I'm glad to know that it definitely wouldn't conserve memory.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests