Advertisement
Guest User

LitPix.lua

a guest
Jul 21st, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. _G.pix = {}
  2. _G.litpix = {}
  3. _G.epix = {}
  4.  
  5. litpix.pushed = true
  6. litpix.bgc = colors.black
  7. local function isOdd(x)
  8. return (x % 2) > 0
  9. end
  10.  
  11. local function exists(y, x)
  12. if pix[y] ~= nil then
  13. if pix[y][x] ~= nil then
  14. return true
  15. end
  16. end
  17. return false
  18. end
  19.  
  20. local function render()
  21. term.current().setVisible(false)
  22. term.clear()
  23. y = 0
  24. k = 0
  25. for z = 1, 20 do
  26. if isOdd(y) then k = k + 1 end
  27. for x = 1, 51 do
  28. if isOdd(y) then
  29. term.setTextColor(litpix.bgc); term.setBackgroundColor(litpix.bgc)
  30. if exists(k, x) then term.setTextColor(pix[k][x]) end
  31. if exists(k+1, x) then term.setBackgroundColor(pix[k+1][x]) end
  32. term.setCursorPos(x, z)
  33. term.write("\143")
  34. term.setTextColor(litpix.bgc); term.setBackgroundColor(litpix.bgc)
  35. else
  36. term.setTextColor(litpix.bgc); term.setBackgroundColor(litpix.bgc)
  37. if exists(k, x) then term.setTextColor(pix[k][x]) end
  38. if exists(k+1, x) then term.setBackgroundColor(pix[k+1][x]) end
  39. term.setCursorPos(x, z)
  40. term.write("\131")
  41. term.setTextColor(litpix.bgc); term.setBackgroundColor(litpix.bgc)
  42. end
  43. end
  44. y = y + 1
  45. k = k + 1
  46. end
  47. term.current().setVisible(true)
  48. end
  49.  
  50. litpix.setBackgroundColor = function(c)
  51. litpix.bgc = c
  52. end
  53.  
  54. litpix.createSymbol = function(s) return s end
  55. litpix.createPallete = function(s) return s end
  56.  
  57. litpix.drawSymbol = function(sx, sy, s, p)
  58. for y = 1, #s do
  59. for x = 1, #s[y] do
  60. if s[y][x] ~= 0 then
  61. if pix[y+sy-1] == nil then pix[y+sy-1] = {} end
  62. pix[y+sy-1][x+sx-1] = p[s[y][x]]
  63. end
  64. end
  65. end
  66. end
  67.  
  68. litpix.push = function()
  69. litpix.pushed = false
  70. end
  71.  
  72. litpix.pop = function()
  73. litpix.pushed = true
  74. render()
  75. end
  76.  
  77. litpix.clear = function()
  78. for i, v in pairs(pix) do
  79. pix[i] = {}
  80. end
  81. if litpix.pushed then render() end
  82. end
  83.  
  84. litpix.addPixel = function(x, y, c)
  85. if pix[y] == nil then pix[y] = {} end
  86. pix[y][x] = c
  87. end
  88.  
  89. litpix.drawPixel = function(x, y, c)
  90. litpix.addPixel(x, y, c)
  91. if litpix.pushed then render() end
  92. end
  93.  
  94. litpix.addLine = function(x1, y1, x2, y2, c)
  95. delta_x = x2 - x1
  96. ix = delta_x > 0 and 1 or -1
  97. delta_x = 2 * math.abs(delta_x)
  98.  
  99. delta_y = y2 - y1
  100. iy = delta_y > 0 and 1 or -1
  101. delta_y = 2 * math.abs(delta_y)
  102.  
  103. litpix.addPixel(x1, y1, c)
  104.  
  105. if delta_x >= delta_y then
  106. error = delta_y - delta_x / 2
  107.  
  108. while x1 ~= x2 do
  109. if (error >= 0) and ((error ~= 0) or (ix > 0)) then
  110. error = error - delta_x
  111. y1 = y1 + iy
  112. end
  113.  
  114. error = error + delta_y
  115. x1 = x1 + ix
  116.  
  117. litpix.addPixel(x1, y1, c)
  118. end
  119. else
  120. error = delta_x - delta_y / 2
  121.  
  122. while y1 ~= y2 do
  123. if (error >= 0) and ((error ~= 0) or (iy > 0)) then
  124. error = error - delta_y
  125. x1 = x1 + ix
  126. end
  127.  
  128. error = error + delta_x
  129. y1 = y1 + iy
  130.  
  131. litpix.addPixel(x1, y1, c)
  132. end
  133. end
  134. if litpix.pushed then render() end
  135. end
  136.  
  137. litpix.drawRect = function(x1, y1, x2, y2, co, recurse)
  138. recurse = recurse or false
  139. if recurse then
  140. for x = x1, x2 do
  141. for y = y1, y2 do
  142. litpix.addPixel(x, y, co)
  143. end
  144. end
  145. else
  146. for x = x1, x2 do
  147. litpix.addPixel(x, y1, co)
  148. litpix.addPixel(x, y2, co)
  149. end
  150. for y = y1, y2 do
  151. litpix.addPixel(x1, y, co)
  152. litpix.addPixel(x2, y, co)
  153. end
  154. end
  155. end
  156.  
  157. litpix.drawLine = function(x1, y1, x2, y2, c)
  158. litpix.addLine(x1, y1, x2, y2, c)
  159. if litpix.pushed then render() end
  160. end
  161.  
  162. litpix.drawPoly = function(...)
  163. local a = {...}
  164. local k = (#a-1)/2
  165. local j = 1
  166. for i = 1, k do
  167. if i < k then litpix.addLine(a[j], a[j+1], a[j+2], a[j+3], a[(k*2)+1])
  168. else litpix.addLine(a[j], a[j+1], a[1], a[2], a[(k*2)+1]) end
  169. j = j + 2
  170. end
  171. if litpix.pushed then render() end
  172. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement