Skip to content

Instantly share code, notes, and snippets.

@treamology
Created August 3, 2016 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treamology/e6679b323e263169942bbc39e445efae to your computer and use it in GitHub Desktop.
Save treamology/e6679b323e263169942bbc39e445efae to your computer and use it in GitHub Desktop.
local autocache = {}
local new_image = love.graphics.newImage
local new_font = love.graphics.newFont
local new_image_font = love.graphics.newImageFont
local cachedImages = {}
local cachedFonts = {}
local function newImage(name, ...)
if type(name) ~= "string" then
return new_image(name, ...)
end
if cachedImages[name] == nil then
print("Loading image " .. name)
cachedImages[name] = new_image(name, ...)
end
return cachedImages[name]
end
local function newFont(name, ...)
if cachedFonts[name] == nil then
print("Loading font " .. name)
cachedFonts[name] = new_font(name, ...)
end
return cachedFonts[name]
end
local function newImageFont(name, ...)
if cachedFonts[name] == nil then
print("Loading font " .. name)
cachedFonts[name] = new_image_font(name, ...)
end
return cachedFonts[name]
end
love.graphics.newImage = newImage
love.graphics.newFont = newFont
love.graphics.newImageFont = newImageFont
autocache.cachedImages = cachedImages
autocache.cachedFonts = cachedFonts
return autocache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment