Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Last active Dec 17, 2015
Embed
What would you like to do?
imageloader
function setup()
LoadImages() --do this first, on its own
end
--put your setup code in here
function setup2()
print("Doing the rest of the setup....")
end
--this loads the images
function LoadImages()
imageStatus="Ready" --tells draw it's ok to draw the scene (will be turned off if we have to download images)
output.clear()
--pass through Codea name of image and internet url
--not in Codea, will be downloaded and saved
img1=LoadImage("Dropbox:3D-tree",
"http://i1303.photobucket.com/albums/ag142/ignatz_mouse/map-tree27c_zpsd56f4f5f.png")
img2=LoadImage("SpaceCute:Background")
img3=LoadImage("Dropbox:3D-wall",
"http://i1303.photobucket.com/albums/ag142/ignatz_mouse/map-stonewall1_zps875ba0f0.png")
if imageStatus=="Ready" then setup2() end
end
--downloads images one by one
function LoadImage(fileName,url)
local i=readImage(fileName)
if i~=nil then return i end
--not found, we need to download, add to queue (ie table)
if imageTable==nil then imageTable={} end
imageTable[#imageTable+1]={name=fileName,url=url}
print('Queueing',fileName)
imageStatus='Loading'
--if the first one, go ahead and download
if #imageTable==1 then
http.request(imageTable[1].url,ImageDownloaded)
print('loading',imageTable[1].name)
end
end
--saves downloaded images
function ImageDownloaded(img)
print(imageTable[1].name,'loaded')
saveImage(imageTable[1].name,img) --save
table.remove(imageTable,1)
--load next one if we have any more to do
if #imageTable>0 then
http.request(imageTable[1].url,ImageDownloaded)
print('loading',imageTable[1].name)
else
LoadImages()
end
end
function draw()
background(220)
if imageStatus=="Loading" then return end
sprite(img1,100,100,100)
sprite(img2,200,200,100)
sprite(img3,300,300,100)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment