Advertisement
Guest User

Graphics api

a guest
May 2nd, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.19 KB | None | 0 0
  1. local scw, sch = term.getSize()
  2.  
  3. --[[ Graphics API 0.5.1 by AlMilliost
  4. With graphic/text buffer.
  5. Examples:
  6. os.loadAPI("graphics")
  7. var1 = graphics.addBuffer() -- new buffer
  8. graphics.clearScreen(var1,1) -- white screen
  9.  
  10. graphics.drawBuffer(var1) -- draw all
  11. --]]
  12.  
  13. function addBuffer()
  14. term.setBackgroundColor(colors.black)
  15. local buffer = {image = {}, text = {}}
  16.  for y = 1, sch do
  17.   buffer.image[y] = {}
  18.   buffer.text[y] = {}
  19.   for x = 1, scw do
  20.   buffer.image[y][x] = colors.black
  21.   buffer.text[y][x] = {[1]=" ",[2]=1}
  22.   end
  23.  end
  24. return buffer
  25. end
  26.  
  27. function drawBuffer(tbuffer)
  28.  for y=1,#tbuffer.image do
  29.  local tLine = tbuffer.image[y]
  30.   for x=1,#tLine do
  31.    if tLine[x] > 0 then
  32.    term.setCursorPos(x,y)
  33.    term.setBackgroundColor(tbuffer["image"][y][x])
  34.    term.setTextColor(tbuffer.text[y][x][2])
  35.    if tbuffer.text[y][x][1] ~= 0 or nil then term.write(tbuffer.text[y][x][1]) end
  36.    if tbuffer.text[y][x][1] == 0 or nil then term.write(" ") end
  37.    end
  38.   end
  39.  end
  40. end
  41.  
  42. function clearBuffer(buffer)
  43.  for y = 1, sch do
  44.   for x = 1, scw do
  45.   buffer.image[y][x] = 1
  46.   buffer.text[y][x][1] = 0
  47.   buffer.text[y][x][2] = 1
  48.   end
  49.  end
  50. end
  51.  
  52. function clearScreen(buffer,color)
  53.  for y = 1, sch do
  54.   for x = 1, scw do
  55.   buffer.image[y][x] = color
  56.   buffer.text[y][x][1] = 0
  57.   buffer.text[y][x][2] = 1
  58.   end
  59.  end
  60. end
  61.  
  62. function text(text, ... )
  63. term.setCursorPos(arg[1],arg[2])
  64. term.write(tostring(text))
  65. end
  66.  
  67. function cText(text,c, ... )
  68. if #arg > 0 then term.setCursorPos(arg[1],arg[2]) end
  69. term.setTextColor(c)
  70. term.write(tostring(text))
  71. end
  72.  
  73. function dText(buffer,text,x,y,color)
  74.  for f = 1, #tostring(text) do
  75.   if x+f-1 <= 51 and x+f-1 >= 1 and y <= 19 and y >= 1 then
  76.   buffer.text[y][x+f-1][1] = string.sub(text,f,f)
  77.   buffer.text[y][x+f-1][2] = 1
  78.   if color then buffer.text[y][x+f-1][2] = color end
  79.   end
  80.  end
  81. end
  82.  
  83. function drawPixel(buffer,x,y,color)
  84.  if type(color) == "number" and x <= 51 and x >= 1 and y <= 19 and y >= 1 then
  85.  buffer.image[y][x] = color
  86.  buffer.text[y][x][1] = 0
  87.  buffer.text[y][x][2] = 1
  88.  end
  89. end
  90.  
  91. function drawLine(buffer, startX, startY, endX, endY, color)    
  92.     startX = math.floor(startX)
  93.     startY = math.floor(startY)
  94.     endX = math.floor(endX)
  95.     endY = math.floor(endY)
  96.     if startX == endX and startY == endY then
  97.         drawPixel(buffer,startX,startY,color)
  98.         return
  99.     end    
  100.     local minX = math.min( startX, endX )
  101.     if minX == startX then
  102.         minY = startY
  103.         maxX = endX
  104.         maxY = endY
  105.     else
  106.         minY = endY
  107.         maxX = startX
  108.         maxY = startY
  109.     end  
  110.     local xDiff = maxX - minX
  111.     local yDiff = maxY - minY          
  112.     if xDiff > math.abs(yDiff) then
  113.         local y = minY
  114.         local dy = yDiff / xDiff
  115.         for x=minX,maxX do
  116.             drawPixel(buffer,x,math.floor( y + 0.5 ),color)
  117.             y = y + dy
  118.         end
  119.     else
  120.         local x = minX
  121.         local dx = xDiff / yDiff
  122.         if maxY >= minY then
  123.             for y=minY,maxY do
  124.                 drawPixel(buffer,math.floor( x + 0.5 ),y,color)
  125.                 x = x + dx
  126.             end
  127.         else
  128.             for y=minY,maxY,-1 do
  129.                 drawPixel(buffer,math.floor( x + 0.5 ),y,color)
  130.                 x = x - dx
  131.             end
  132.         end
  133.     end
  134. end
  135.  
  136. function drawBox(buffer, x1, y1, w, h, c)
  137.  for y = 1, h do
  138.   for x = 1, w do
  139.   drawPixel(buffer,x1+x-1,y1+y-1,c)
  140.   end
  141.  end
  142. end
  143.  
  144. function addImage(buffer,image,x1,y1)
  145.  for y = 1, #image do
  146.  local tLine = image[y]
  147.   for x = 1, #tLine do
  148.    if image[y][x] ~= 0 then
  149.     if x1+x-1 <= 51 and x1+x-1 >= 1 and y1+y-1 <= 19 and y1+y-1 >= 1 then
  150.     buffer.image[y1+y-1][x1+x-1] = image[y][x]
  151.     buffer.text[y1+y-1][x1+x-1][1] = 0
  152.     buffer.text[y1+y-1][x1+x-1][2] = 1
  153.     end
  154.    end
  155.   end
  156.  end
  157. end
  158.  
  159. function addMImage(buffer,image,x1,y1)
  160.  for y = 1, #image do
  161.  local tLine = image[y]
  162.   for x = 1, #tLine do
  163.    if image[y][#tLine-x+1] ~= 0 then
  164.     if x1+x-1 <= 51 and x1+x-1 >= 1 and y1+y-1 <= 19 and y1+y-1 >= 1 then
  165.     buffer.image[y1+y-1][x1+x-1] = image[y][#tLine-x+1]
  166.     buffer.text[y1+y-1][x1+x-1][1] = 0
  167.     buffer.text[y1+y-1][x1+x-1][2] = 1
  168.     end
  169.    end
  170.   end
  171.  end
  172. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement