Advertisement
Xelostar

BufferAPI

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