resource sharing

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
User avatar
Igmon
Prole
Posts: 9
Joined: Wed Mar 11, 2009 6:11 pm

resource sharing

Post by Igmon »

Hi,

I just have a quick question. When one creates an image in love like this:

var = love.graphics.newImage( "smiley.png" )
var2 = love.graphics.newImage( "smiley.png" )

Will both variables share the same memory or are both independent from each other? I'm assuming the lower level image loader will know when to share memory with the same filename, but I'm not sure. I could try it out real quick...but I figure I ask here too for confirmation.

The same goes for other resources such as sound.

It's not a problem to build a resource management to handle the shared memory. Would be awesome to have some of those resource management handled at low level to a certain degree though...

Thanks.
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: resource sharing

Post by bmelts »

I could be wrong, but I think it loads the image twice into two separate chunks of memory. That way, you can manipulate each instance of it separately. I know I ended up doing this in a project I worked on in löve, and it seemed to work alright that way, whereas when I pointed both variables to the same love.graphics.newImage(), I couldn't manipulate them separately.

Then again I could be completely wrong :P Paging rude to this thread!
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: resource sharing

Post by rude »

@Igmon: it will not share memory between them. This is by design. You should not need to create two Image objects from the same image.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: resource sharing

Post by Robin »

I think anjo is right. If you look at it from the LÖVE-perspective, caching images doesn't make much sense. If you really want that behavior, you could insert this (untested) code in your main.lua:

Code: Select all

local oldNewImage = love.graphics.newImage
local imageCache = {}
function love.graphics.newImage(name)
    if not imageCache[name] then
        imageCache[name] = oldNewImage(name)
    end
    return imageCache[name]
end
(note: it will probably fail hard anyway)
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: MrFariator and 70 guests