Advertisement
br1wr2el3

Code 14c - Images

May 29th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1.  
  2. --# Main
  3. -- Code 14c - Images
  4.  
  5. -- Bruce Elliott
  6. -- May 2013
  7.  
  8. -- Coroutine draw multiple images to screen
  9.  
  10. supportedOrientations(LANDSCAPE_RIGHT)
  11. displayMode(FULLSCREEN)
  12.  
  13. function setup()
  14.  
  15. print("Coroutine Example")
  16. useTouch = false
  17.  
  18. obj = coroutine.create(object)
  19. coroutine.resume(obj)
  20. -- Remove comment to draw an object
  21. -- each time screen is touched--
  22. -- useTouch = true
  23. end
  24.  
  25. -- This function gets called once every frame
  26. function draw()
  27.  
  28. -- This sets the line thickness
  29. strokeWidth(2)
  30.  
  31. if useTouch == true then
  32. -- wait
  33. else
  34. drawObject()
  35. end
  36.  
  37. -- Do your drawing here
  38.  
  39. end
  40.  
  41. function object()
  42. -- img contains the nodes of an object
  43. -- to be drawn on the screen
  44. img ={}
  45.  
  46. -- scale is used to change the length of img
  47. -- lines
  48. scale = 0
  49. while scale < 50 do
  50. -- img can have 2 to 15 nodes
  51. -- two nodes are used to draw a line
  52. n = math.random(2, 15)
  53. for i = 1, n do
  54. img[i] = buildNode(scale)
  55. end
  56.  
  57. -- Move nodes on screen attempt
  58. -- to get even distribution for objects
  59. deltaX = math.random(1, WIDTH)
  60. deltaY = math.random(1, HEIGHT)
  61.  
  62. -- Adjust nodes so they are on screen
  63. -- using math.min and WIDTH or HEIGHT
  64. -- This method simply forces nodes onto
  65. -- the screen which changes original
  66. -- image shape. Better might be to move
  67. -- all nodes enough so they are all on the
  68. -- screen and the image shape is maintained.
  69.  
  70. for i = 1, n do
  71. img[i].x = math.min(img[i].x + deltaX, WIDTH)
  72. img[i].y = math.min(img[i].y + deltaY, HEIGHT)
  73. end
  74.  
  75. -- stop coroutine and wait for drawObject
  76. -- to resume it
  77. coroutine.yield(n, img)
  78.  
  79. -- Increment scale (length of img lines)
  80. scale = scale + 1
  81.  
  82. end
  83.  
  84. end
  85.  
  86. function drawObject()
  87. -- resume the coroutine return:
  88. -- status - coroutine active
  89. -- drawN - number of nodes in object
  90. -- thisObj - table of node values
  91.  
  92. -- "Done" will display in upper left corner after all
  93. -- objects have been drawn
  94. status, drawN, thisObj = coroutine.resume(obj)
  95.  
  96. -- Set line color for this image
  97. setStroke()
  98.  
  99. -- Determine if coroutine is active
  100. if status ~= false and thisObj ~= nil then
  101. -- Draw all but the last line in an object
  102. for i = 1, drawN - 1 do
  103. line(thisObj[i].x, thisObj[i].y, thisObj[i+1].x, thisObj[i+1].y)
  104. end
  105. -- Draw last line fron last node to first node
  106. line(thisObj[drawN].x, thisObj[drawN].y, thisObj[1].x, thisObj[1].y)
  107. else
  108. text("Done", 70, HEIGHT -50)
  109. end
  110. end
  111.  
  112. function setStroke()
  113. -- Determine a random color for an
  114. -- object lines
  115. strokeRed = math.random(0, 255)
  116. strokeGreen = math.random(0, 255)
  117. strokeBlue = math.random(0, 255)
  118. stroke(strokeRed, strokeGreen, strokeBlue, 255)
  119. end
  120.  
  121. function buildNode(scale)
  122. -- Create a node as a vec2
  123. p = vec2()
  124.  
  125. -- Random x, y values
  126. p.x, p.y = getRan()
  127.  
  128. -- Add scale to x, y
  129. p.x = p.x + scale
  130. p.y = p.y + scale
  131. -- print("node", p.x, p.y)
  132.  
  133. -- return the node
  134. return p
  135. end
  136.  
  137. function getRan()
  138. -- This determines the base x and y position
  139. -- of the object used by buildNode
  140. x = math.random(10,150)
  141. y = math.random(10,150)
  142.  
  143. -- Return x, y
  144. return x, y
  145. end
  146.  
  147. function touched(touch)
  148. if touch.state == ENDED and useTouch == true then
  149. drawObject()
  150. end
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement