Advertisement
Xelostar

BufferAPI-2.0

Jul 14th, 2018
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.29 KB | None | 0 0
  1.  
  2. -- Made by Xelostar: https://www.youtube.com/channel/UCDE2STpSWJrIUyKtiYGeWxw
  3.  
  4. local screenBuffer = {}
  5.  
  6. local colorChar = {}
  7. for i = 1, 16 do
  8.     colorChar[2 ^ (i - 1)] = ("0123456789abcdef"):sub(i, i)
  9. end
  10.  
  11. local function round(number)
  12.     return math.floor(number + 0.5)
  13. end
  14.  
  15. local function linear(x1, y1, x2, y2)
  16.     local dy = y2 - y1
  17.     local dx = x2 - x1
  18.  
  19.     local a = math.pow(10, 99)
  20.     if (dx ~= 0) then
  21.         a = dy / dx
  22.     end
  23.     local b = y1 - a * x1
  24.  
  25.     return a, b
  26. end
  27.  
  28. function newBuffer(x1, y1, x2, y2)
  29.     local buffer = {x1 = x1, y1 = y1, x2 = x2, y2 = y2, screenBuffer = {{}}, blittleWindow = -1}
  30.  
  31.     function buffer:setBufferSize(x1, y1, x2, y2)
  32.         buffer.x1 = x1
  33.         buffer.y1 = y1
  34.  
  35.         buffer.x2 = x2
  36.         buffer.y2 = y2
  37.  
  38.         buffer:clear(colors.white)
  39.     end
  40.  
  41.     function buffer:clear(color)
  42.         buffer.screenBuffer.c1 = {}
  43.         buffer.screenBuffer.c2 = {}
  44.         buffer.screenBuffer.chars = {}
  45.         for y = 1, buffer.y2 - buffer.y1 + 1 do
  46.             buffer.screenBuffer.c1[y] = {}
  47.             buffer.screenBuffer.c2[y] = {}
  48.             buffer.screenBuffer.chars[y] = {}
  49.             for x = 1, buffer.x2 - buffer.x1 + 1 do
  50.                 buffer.screenBuffer.c1[y][x] = colorChar[color]
  51.                 buffer.screenBuffer.c2[y][x] = colorChar[color]
  52.                 buffer.screenBuffer.chars[y][x] = " "
  53.             end
  54.         end
  55.     end
  56.  
  57.     function buffer:setPixel(x, y, c1, c2, char)
  58.         local x = round(x)
  59.         local y = round(y)
  60.  
  61.         if (x >= 1 and x <= (buffer.x2 - buffer.x1 + 1)) then
  62.             if (y >= 1 and y <= (buffer.y2 - buffer.y1 + 1)) then
  63.                 buffer.screenBuffer.c1[y][x] = colorChar[c1]
  64.                 buffer.screenBuffer.c2[y][x] = colorChar[c2]
  65.                 buffer.screenBuffer.chars[y][x] = char
  66.             end
  67.         end
  68.     end
  69.  
  70.     function buffer:write(x, y, c1, c2, string)
  71.         local charNr = 0
  72.         for char in string:gmatch(".") do
  73.             buffer:setPixel(x + charNr, y, c1, c2, char)
  74.             charNr = charNr + 1
  75.         end
  76.     end
  77.  
  78.     function buffer:loadImage(dx, dy, image, useBlittle)
  79.         for y, row in pairs(image) do
  80.             for x, value in pairs(row) do
  81.                 if (value ~= nil and value > 0) then
  82.                     if (useBlittle == true) then
  83.                         buffer:setPixel(x + (dx - 1) * 2, y + (dy - 1) * 3, value, value, " ")
  84.                     else
  85.                         buffer:setPixel(x + dx - 1, y + dy - 1, value, value, " ")
  86.                     end
  87.                 end
  88.             end
  89.         end
  90.     end
  91.  
  92.     function buffer:loadBox(x1, y1, x2, y2, c1, c2, char)
  93.         for x = x1, x2 do
  94.             for y = y1, y2 do
  95.                 buffer:setPixel(x, y, c1, c2, char)
  96.             end
  97.         end
  98.     end
  99.  
  100.     function buffer:loadBorderBox(x1, y1, x2, y2, c1, c2, char)
  101.         for x = x1, x2 do
  102.             if (x == x1) then
  103.                 for y = y1, y2 do
  104.                     if (y == y1) then
  105.                         buffer:setPixel(x, y, c1, c2, string.char(151))
  106.                     elseif (y == y2) then
  107.                         buffer:setPixel(x, y, c2, c1, string.char(138))
  108.                     else
  109.                         buffer:setPixel(x, y, c1, c2, string.char(149))
  110.                     end
  111.                 end
  112.             elseif (x == x2) then
  113.                 for y = y1, y2 do
  114.                     if (y == y1) then
  115.                         buffer:setPixel(x, y, c2, c1, string.char(148))
  116.                     elseif (y == y2) then
  117.                         buffer:setPixel(x, y, c2, c1, string.char(133))
  118.                     else
  119.                         buffer:setPixel(x, y, c2, c1, string.char(149))
  120.                     end
  121.                 end
  122.             else
  123.                 for y = y1, y2 do
  124.                     if (y == y1) then
  125.                         buffer:setPixel(x, y, c1, c2, string.char(131))
  126.                     elseif (y == y2) then
  127.                         buffer:setPixel(x, y, c2, c1, string.char(143))
  128.                     else
  129.                         buffer:setPixel(x, y, c1, c2, char)
  130.                     end
  131.                 end
  132.             end
  133.         end
  134.     end
  135.  
  136.     function buffer:loadBorderBoxBlittle(x1, y1, x2, y2, c1, c2, char)
  137.         for x = x1, x2 do
  138.             for y = y1, y2 do
  139.                 if (x == x1 or x == x2 or y == y1 or y == y2) then
  140.                     buffer:setPixel(x, y, c2, c1, " ")
  141.                 else
  142.                     buffer:setPixel(x, y, c1, c2, " ")
  143.                 end
  144.             end
  145.         end
  146.     end
  147.  
  148.     function buffer:loadLine(x1, y1, x2, y2, c, char, charc)
  149.         local a, b = linear(x1, y1, x2, y2)
  150.  
  151.         if (x2 >= x1) then
  152.             local start = x1>1 and x1 or 1
  153.             for x = start, x2 do
  154.                 local y = a * x + b
  155.                 buffer:setPixel(x, y, charc, c, char)
  156.  
  157.                 if (x > buffer.x2 - buffer.x1 + 6) then
  158.                     break
  159.                 end
  160.             end
  161.         else
  162.             local start = x2>1 and x2 or 1
  163.             for x = start, x1 do
  164.                 local y = a * x + b
  165.                 buffer:setPixel(x, y, charc, c, char)
  166.  
  167.                 if (x > buffer.x2 - buffer.x1 + 6) then
  168.                     break
  169.                 end
  170.             end
  171.         end
  172.  
  173.         if (y2 >= y1) then
  174.             local start = y1>1 and y1 or 1
  175.             for y = start, y2 do
  176.                 local x = (y - b) / a
  177.                 buffer:setPixel(x, y, charc, c, char)
  178.  
  179.                 if (y > buffer.y2 - buffer.y1 + 6) then
  180.                     break
  181.                 end
  182.             end
  183.         else
  184.             local start = y2>1 and y2 or 1
  185.             for y = start, y1 do
  186.                 local x = (y - b) / a
  187.                 buffer:setPixel(x, y, charc, c, char)
  188.  
  189.                 if (y > buffer.y2 - buffer.y1 + 6) then
  190.                     break
  191.                 end
  192.             end
  193.         end
  194.     end
  195.  
  196.     function buffer:horLine(a1, b1, a2, b2, startY, endY, c, char, charc)
  197.         if (startY < 0) then startY = 0
  198.         elseif (startY > buffer.y2 - buffer.y1 + 2) then startY = buffer.y2 - buffer.y1 + 2 end
  199.         if (endY < 0) then endY = 0
  200.         elseif (endY > buffer.y2 - buffer.y1 + 2) then endY = buffer.y2 - buffer.y1 + 2 end
  201.  
  202.         for y = startY, endY do
  203.             local y2 = y
  204.             if (y ~= startY and y ~= endY) then
  205.                 y2 = round(y)
  206.             end
  207.  
  208.             local x1 = round((round(y2 - 0.5) - b1) / a1)
  209.             local x2 = round((round(y2 - 0.5) - b2) / a2)
  210.  
  211.             if (x1 < 0) then x1 = 0
  212.             elseif (x1 > buffer.x2 - buffer.x1 + 2) then x1 = buffer.x2 - buffer.x1 + 2 end
  213.             if (x2 < 0) then x2 = 0
  214.             elseif (x2 > buffer.x2 - buffer.x1 + 2) then x2 = buffer.x2 - buffer.x1 + 2 end
  215.  
  216.             if (x1 < x2) then
  217.                 for x = x1, x2 do
  218.                     buffer:setPixel(x, y2, charc, c, char)
  219.                 end
  220.             else
  221.                 for x = x2, x1 do
  222.                     buffer:setPixel(x, y2, charc, c, char)
  223.                 end
  224.             end
  225.         end
  226.     end
  227.  
  228.     function buffer:loadTriangle(x1, y1, x2, y2, x3, y3, c, char, charc)
  229.         char = char or " "
  230.         charc = charc or c
  231.  
  232.         if (x1 < 0 and x2 < 0 and x3 < 0 or x1 > buffer.x2 - buffer.x1 + 2 and x2 > buffer.x2 - buffer.x1 + 2 and x3 > buffer.x2 - buffer.x1 + 2 or y1 < 0 and y2 < 0 and y3 < 0 or y1 > buffer.y2 - buffer.y1 + 2 and y2 > buffer.y2 - buffer.y1 + 2 and y3 > buffer.y2 - buffer.y1 + 2) then
  233.             return
  234.         elseif (x1 == x2 and x2 == x3 and y1 == y2 and y2 == y3) then
  235.             buffer:setPixel(x1, y1, charc, c, char)
  236.         elseif (math.min(x1, x2, x3) >= math.max(x1, x2, x3) - 1 and math.min(y1, y2, y3) >= math.max(y1, y2, y3) - 1) then
  237.             buffer:setPixel(x1, y1, charc, c, char)
  238.             buffer:setPixel(x2, y2, charc, c, char)
  239.             buffer:setPixel(x3, y3, charc, c, char)
  240.         else
  241.             local a1, b1 = linear(x1, y1, x2, y2)
  242.             local a2, b2 = linear(x2, y2, x3, y3)
  243.             local a3, b3 = linear(x1, y1, x3, y3)
  244.  
  245.             buffer:loadLine(x1, y1, x2, y2, c, char, charc)
  246.             buffer:loadLine(x2, y2, x3, y3, c, char, charc)
  247.             buffer:loadLine(x3, y3, x1, y1, c, char, charc)
  248.  
  249.             if (y1 <= y2 and y1 <= y3) then
  250.                 if (y2 <= y3) then
  251.                     if (a1 ~= 0) then
  252.                         buffer:horLine(a1, b1, a3, b3, y1, y2, c, char, charc)
  253.                     end
  254.                     if (a2 ~= 0) then
  255.                         buffer:horLine(a2, b2, a3, b3, y2, y3, c, char, charc)
  256.                     end
  257.                 else
  258.                     if (a3 ~= 0) then
  259.                         buffer:horLine(a1, b1, a3, b3, y1, y3, c, char, charc)
  260.                     end
  261.                     if (a2 ~= 0) then
  262.                         buffer:horLine(a1, b1, a2, b2, y3, y2, c, char, charc)
  263.                     end
  264.                 end
  265.             elseif (y2 <= y1 and y2 <= y3) then
  266.                 if (y1 <= y3) then
  267.                     if (a1 ~= 0) then
  268.                         buffer:horLine(a1, b1, a2, b2, y2, y1, c, char, charc)
  269.                     end
  270.                     if (a3 ~= 0) then
  271.                         buffer:horLine(a2, b2, a3, b3, y1, y3, c, char, charc)
  272.                     end
  273.                 else
  274.                     if (a2 ~= 0) then
  275.                         buffer:horLine(a1, b1, a2, b2, y2, y3, c, char, charc)
  276.                     end
  277.                     if (a3 ~= 0) then
  278.                         buffer:horLine(a1, b1, a3, b3, y3, y1, c, char, charc)
  279.                     end
  280.                 end
  281.             else
  282.                 if (y1 <= y2) then
  283.                     if (a3 ~= 0) then
  284.                         buffer:horLine(a2, b2, a3, b3, y3, y1, c, char, charc)
  285.                     end
  286.                     if (a1 ~= 0) then
  287.                         buffer:horLine(a1, b1, a2, b2, y1, y2, c, char, charc)
  288.                     end
  289.                 else
  290.                     if (a2 ~= 0) then
  291.                         buffer:horLine(a2, b2, a3, b3, y3, y2, c, char, charc)
  292.                     end
  293.                     if (a1 ~= 0) then
  294.                         buffer:horLine(a1, b1, a3, b3, y2, y1, c, char, charc)
  295.                     end
  296.                 end
  297.             end
  298.         end
  299.     end
  300.  
  301.     function buffer:loadCircle(x, y, c1, c2, char, radius)
  302.         for loopX = buffer.x1, buffer.x2 do
  303.             for loopY = buffer.y1, buffer.y2 do
  304.                 local dx = loopX - x
  305.                 local dy = loopY - y
  306.                 local distance = math.sqrt(dx^2 + dy^2)
  307.  
  308.                 if (round(distance) <= radius) then
  309.                     buffer:setPixel(loopX, loopY, c1, c2, char)
  310.                 end
  311.             end
  312.         end
  313.     end
  314.  
  315.     function buffer:drawBuffer(blittleOn)
  316.         if (blittleOn == false) then
  317.             for y = 1, buffer.y2 - buffer.y1 + 1 do
  318.                 local chars = table.concat(buffer.screenBuffer.chars[y])
  319.                 local c1s = table.concat(buffer.screenBuffer.c1[y])
  320.                 local c2s = table.concat(buffer.screenBuffer.c2[y])
  321.  
  322.                 term.setCursorPos(buffer.x1, y + buffer.y1 - 1)
  323.                 term.blit(chars, c1s, c2s)
  324.             end
  325.         else
  326.             if (buffer.blittleWindow == -1) then
  327.                 buffer.blittleWindow = blittle.createWindow(term.current(), (buffer.x1-1)*0.5+1, (buffer.y1-1)*0.333333 + 1, (buffer.x2 - buffer.x1)*0.5+1, (buffer.y2 - buffer.y1)*0.333333, false)
  328.             end
  329.  
  330.             for y = 1, buffer.y2 - buffer.y1 - 2 do
  331.                 local chars = table.concat(buffer.screenBuffer.chars[y])
  332.                 local c1s = table.concat(buffer.screenBuffer.c1[y])
  333.                 local c2s = table.concat(buffer.screenBuffer.c2[y])
  334.  
  335.                 buffer.blittleWindow.setCursorPos(1, y)
  336.                 buffer.blittleWindow.blit(chars, c1s, c2s)
  337.             end
  338.             buffer.blittleWindow.setVisible(true)
  339.             buffer.blittleWindow.setVisible(false)
  340.         end
  341.     end
  342.  
  343.     return buffer
  344. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement