Code: Select all
local aliasNewImage = love.graphics.newImage
local imageCache = {}
function love.graphics.newImage(arg)
if imageCache[arg] == nil then
if type(arg) == "string" then -- if it's the filename
imageCache[arg] = aliasNewImage(arg)
return imageCache[arg]
else
return imageCache[arg]
end
else
return imageCache[arg]
end
end
As far as I know, I'm aliasing the method love.graphics.newImage, and then changing it so if the image is already in the table 'cache', it returns the already loaded image, rather than loading it again, and if the image is *not* in the table 'cache', it puts it in there. Of course, if the parameter isn't the filename, it acts as normal.