A question on displaying an image multiple times

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.
Post Reply
ZBoyer1000
Prole
Posts: 39
Joined: Sat Nov 28, 2015 10:13 am

A question on displaying an image multiple times

Post by ZBoyer1000 » Wed Aug 17, 2016 2:56 pm

Is it better to display the same image at multiple positions than to create new images with the same texture?

User avatar
Nixola
Inner party member
Posts: 1940
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: A question on displaying an image multiple times

Post by Nixola » Wed Aug 17, 2016 3:32 pm

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

User avatar
AnRu
Citizen
Posts: 69
Joined: Sun Oct 27, 2013 1:33 pm
Contact:

Re: A question on displaying an image multiple times

Post by AnRu » Wed Aug 17, 2016 5:14 pm

Also a good point is using spritebatches. They can produce performance with equal images.
Also take a look at AutoBatch

User avatar
Jasoco
Inner party member
Posts: 3637
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: A question on displaying an image multiple times

Post by Jasoco » Thu Aug 18, 2016 4:55 am

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.
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?

User avatar
zorg
Party member
Posts: 2527
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: A question on displaying an image multiple times

Post by zorg » Thu Aug 18, 2016 5:33 am

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 :3True 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.

User avatar
Positive07
Party member
Posts: 1005
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: A question on displaying an image multiple times

Post by Positive07 » Thu Aug 18, 2016 5:40 am

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.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

User avatar
pgimeno
Party member
Posts: 1530
Joined: Sun Oct 18, 2015 2:58 pm

Re: A question on displaying an image multiple times

Post by pgimeno » Thu Aug 18, 2016 5:58 am

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:

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
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories - portland (mobile orientation)
The MS-Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

User avatar
Jasoco
Inner party member
Posts: 3637
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: A question on displaying an image multiple times

Post by Jasoco » Thu Aug 18, 2016 6:56 am

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests