Advertisement
br1wr2el3

App 07 - Image I/O http

May 5th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1.  
  2. --# Main
  3. -- App 07 - Image I/O http
  4.  
  5. -- Bruce Elliott
  6. -- May 2013
  7.  
  8. --Built from Image I/O example
  9.  
  10. function setup()
  11. ----------------------------------------------------
  12.  
  13. -- 2. Now we'll download our logo into an image
  14. logo = nil -- it's empty for now
  15. -- Start the request, didGetLogo is our callback function
  16. http.request( "http://twolivesleft.com/logo.png", didGetLogo )
  17. ---------------------------------------------------
  18. -- Once the image downloads we use makeCircleImage()
  19. -- to make a copy of the image then
  20. -- we save the image to Dropbox or Documents
  21. ---------------------------------------------------
  22. -- 4. Now let's list the images in Documents
  23. print("Sprites contained in 'Documents'")
  24. local list = spriteList( "Dropbox" )
  25. for _,v in pairs(list) do
  26. print(v)
  27. end
  28. print("")
  29. ---------------------------------------------------
  30. end
  31.  
  32. function makeCircleImage()
  33. -- This function makes a red circle
  34. -- Renders it into an image, and returns the image
  35. -- We exapanded the size of img to screen sizep
  36. local img = image(WIDTH, HEIGHT)
  37. ------------------------------------
  38. -- Use the image as a render target
  39. setContext(img)
  40. background(0,0,0,0) -- transparent background
  41. fill(255,0,0)
  42. -- We draw the sprite to img instead of the screen
  43. sprite( logo, WIDTH/2, HEIGHT/2, WIDTH )
  44. -- ellipse(200,200,200)
  45. -- Set render target back to screen
  46. setContext()
  47. ------------------------------------
  48. return img
  49. end
  50.  
  51. function didGetLogo( theLogo, status, headers )
  52. print( "Response Status: " .. status )
  53.  
  54. -- Store logo in our global variable
  55. logo = theLogo
  56.  
  57. -- Check if the status is OK (200)
  58. if status == 200 then
  59. print( "Downloaded logo OK" )
  60. print( " Image dimensions: " ..
  61. logo.width .. "x" .. logo.height )
  62. -- Call makeCircle once image was downloaded
  63. img = makeCircleImage()
  64. -- Save to Dropbox or Documents or both
  65. saveImage("Dropbox:Logo", img)
  66. saveImage("Documents:Logo", img)
  67. else
  68. print( "Error downloading logo" )
  69. end
  70. end
  71.  
  72. -- This function gets called once every frame
  73. function draw()
  74. -- This sets a dark background color
  75. background(40, 40, 50)
  76.  
  77. -- Draw the logo we downloaded (when it's ready!)
  78. if logo ~= nil then
  79. sprite(img, WIDTH/2, HEIGHT/2, WIDTH )
  80. end
  81.  
  82. -- Draw the circle image we saved and read from the sprite pack
  83. -- sprite( circleImage, WIDTH/2, 110 )
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement