SHARE
TWEET

Surface API 1.1

CrazedProgrammer Mar 18th, 2015 (edited) 207 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Surface API version 1.1 by CrazedProgrammer
  2. -- You can find info and documentation on these pages:
  3. -- http://www.computercraft.info/forums2/index.php?/topic/22397-surface-api/
  4. -- You may use this in your ComputerCraft programs and modify it without asking.
  5. -- However, you may not publish this API under your name without asking me.
  6. -- If you have any suggestions, bug reports or questions then please send an email to:
  7. -- crazedprogrammer@gmail.com
  8.  
  9. function create(width, height, char, backcolor, textcolor)
  10.   local surf = { }
  11.   surf.width = width
  12.   surf.height = height
  13.   surf.x1 = 1
  14.   surf.y1 = 1
  15.   surf.x2 = width
  16.   surf.y2 = height
  17.   surf.overwrite = false
  18.   surf.buffer = { }
  19.   surf.setBounds = setBounds
  20.   surf.save = save
  21.   surf.render = render
  22.   surf.clear = clear
  23.   surf.drawPixel = drawPixel
  24.   surf.getPixel = getPixel
  25.   surf.drawText = drawText
  26.   surf.drawLine = drawLine
  27.   surf.drawRect = drawRect
  28.   surf.fillRect = fillRect
  29.   surf.drawTriangle = drawTriangle
  30.   surf.fillTriangle = fillTriangle
  31.   surf.drawEllipse = drawEllipse
  32.   surf.fillEllipse = fillEllipse
  33.   surf.floodFill = floodFill
  34.   surf.drawSurface = drawSurface
  35.   surf.drawSurfacePart = drawSurfacePart
  36.   surf.drawSurfaceScaled = drawSurfaceScaled
  37.   for i=1,width*height,1 do
  38.     surf.buffer[(i - 1) * 3 + 1] = char
  39.     surf.buffer[(i - 1) * 3 + 2] = backcolor
  40.     surf.buffer[(i - 1) * 3 + 3] = textcolor
  41.   end
  42.   return surf
  43. end
  44.  
  45. function load(path)
  46.   local lines = { }
  47.   local f = fs.open(path, "r")
  48.   local line = f.readLine()
  49.   while line do
  50.     table.insert(lines, line)
  51.     line = f.readLine()
  52.   end
  53.   f.close()
  54.   local height = #lines
  55.   if lines[1]:byte(1) == 30 then
  56.     local width = 0
  57.     local i = 1
  58.     while i <= #lines[1] do
  59.       local char = lines[1]:byte(i)
  60.       if char == 30 or char == 31 then
  61.         i = i + 1
  62.       else
  63.         width = width + 1
  64.       end
  65.       i = i + 1
  66.     end
  67.     local surf = surface.create(width, height)
  68.     local backcolor = nil
  69.     local testcolor = nil
  70.     for j=1,height,1 do
  71.       local i = 1
  72.       local px = 1
  73.       while i <= #lines[j] do
  74.         local char = lines[j]:byte(i)
  75.         if char == 30 then
  76.           i = i + 1
  77.           char = lines[j]:byte(i)
  78.           local color = tonumber(lines[j]:sub(i, i), 16)
  79.           if color then
  80.             backcolor = 2^color
  81.           else
  82.             backcolor = nil
  83.           end
  84.         elseif char == 31 then
  85.           i = i + 1
  86.           char = lines[j]:byte(i)
  87.           local color = tonumber(lines[j]:sub(i, i), 16)
  88.           if color then
  89.             textcolor = 2^color
  90.           else
  91.             textcolor = nil
  92.           end
  93.         else
  94.           surf.buffer[((j - 1) * surf.width + px - 1) * 3 + 1] = lines[j]:sub(i, i)
  95.           surf.buffer[((j - 1) * surf.width + px - 1) * 3 + 2] = backcolor
  96.           surf.buffer[((j - 1) * surf.width + px - 1) * 3 + 3] = textcolor
  97.           px = px + 1
  98.         end
  99.         i = i + 1
  100.       end
  101.     end
  102.     return surf
  103.   else
  104.     local width = #lines[1]
  105.     local surf = surface.create(width, height)
  106.     for j=1,height,1 do
  107.       for i=1,width,1 do
  108.         local color = tonumber(lines[j]:sub(i, i), 16)
  109.         if color then
  110.           surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 2] = 2 ^ color
  111.         end
  112.       end
  113.     end
  114.     return surf
  115.   end
  116. end
  117.  
  118. function setBounds(surf, x1, y1, x2, y2)
  119.   if x2 < x1 then
  120.     local temp = x1
  121.     x1 = x2
  122.     x2 = temp
  123.   end
  124.   if y2 < y1 then
  125.     local temp = y1
  126.     y1 = y2
  127.     y2 = temp
  128.   end
  129.   if x2 < 1 then return end
  130.   if x1 > surf.width then return end
  131.   if y2 < 1 then return end
  132.   if y1 > surf.height then return end
  133.   if x1 < 1 then x1 = 1 end
  134.   if x2 > surf.width then x2 = surf.width end
  135.   if y1 < 1 then y1 = 1 end
  136.   if y2 > surf.height then y2 = surf.height end
  137.   surf.x1 = x1
  138.   surf.y1 = y1
  139.   surf.x2 = x2
  140.   surf.y2 = y2
  141. end
  142.  
  143. function save(surf, path, type)
  144.   type = type or "nft"
  145.   local f = fs.open(path, "w")
  146.   if type == "nfp" then
  147.     for j=1,surf.height,1 do
  148.       if j > 1 then f.write("\n") end
  149.       for i=1,surf.width,1 do
  150.         f.write(colorToHex(surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 2]))
  151.       end
  152.     end
  153.   elseif type == "nft" then
  154.     for j=1,surf.height,1 do
  155.       backcolor = -1
  156.       textcolor = -1
  157.       if j > 1 then f.write("\n") end
  158.       for i=1,surf.width,1 do
  159.         if backcolor ~= surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 2] then
  160.           f.write(string.char(30))
  161.           backcolor = surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 2]
  162.           f.write(_colorToHex(backcolor))
  163.         end
  164.         if textcolor ~= surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 3] then
  165.           f.write(string.char(31))
  166.           textcolor = surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 3]
  167.           f.write(_colorToHex(textcolor))
  168.         end
  169.         if surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 1] then
  170.           f.write(surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 1])
  171.         else
  172.           f.write(" ")
  173.         end
  174.       end
  175.     end
  176.   end
  177.   f.close()
  178. end
  179.  
  180. function render(surf, display, x, y, sx1, sy1, sx2, sy2)
  181.   x = x or 1
  182.   y = y or 1
  183.   sx1 = sx1 or 1
  184.   sy1 = sy1 or 1
  185.   sx2 = sx2 or surf.width
  186.   sy2 = sy2 or surf.height
  187.   local cmd = { }
  188.   local str = ""
  189.   local backcolor = 0
  190.   local textcolor = 0
  191.  
  192.   for j=sy1,sy2,1 do
  193.     cmd[#cmd + 1] = 1
  194.     cmd[#cmd + 1] = y + j - sy1
  195.     for i=sx1,sx2,1 do
  196.       if surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 2] ~= backcolor then
  197.         backcolor = surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 2]
  198.         if str ~= "" then
  199.           cmd[#cmd + 1] = 4
  200.           cmd[#cmd + 1] = str
  201.           str = ""
  202.         end
  203.         cmd[#cmd + 1] = 2
  204.         cmd[#cmd + 1] = backcolor
  205.       end
  206.       if surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 3] ~= textcolor then
  207.         textcolor = surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 3]
  208.         if str ~= "" then
  209.           cmd[#cmd + 1] = 4
  210.           cmd[#cmd + 1] = str
  211.           str = ""
  212.         end
  213.         cmd[#cmd + 1] = 3
  214.         cmd[#cmd + 1] = textcolor
  215.       end
  216.       str = str..surf.buffer[((j - 1) * surf.width + i - 1) * 3 + 1]
  217.     end
  218.     cmd[#cmd + 1] = 4
  219.     cmd[#cmd + 1] = str
  220.     str = ""
  221.   end
  222.  
  223.   for i=1,#cmd/2,1 do
  224.     local c = cmd[(i - 1) * 2 + 1]
  225.     local a = cmd[(i - 1) * 2 + 2]
  226.     if c == 1 then
  227.       display.setCursorPos(x, a)
  228.     elseif c == 2 then
  229.       display.setBackgroundColor(a)
  230.     elseif c == 3 then
  231.       display.setTextColor(a)
  232.     elseif c == 4 then
  233.       display.write(a)
  234.     end
  235.   end
  236. end
  237.  
  238. function clear(surf, char, backcolor, textcolor)
  239.   local overwrite = surf.overwrite
  240.   surf.overwrite = true
  241.   surf:fillRect(surf.x1, surf.y1, surf.x2, surf.y2, char, backcolor, textcolor)
  242.   surf.overwrite = overwrite
  243. end
  244.  
  245. function drawPixel(surf, x, y, char, backcolor, textcolor)
  246.   if x < surf.x1 or y < surf.y1 or x > surf.x2 or y > surf.y2 then return end
  247.   if char or surf.overwrite then
  248.     surf.buffer[((y - 1) * surf.width + x - 1) * 3 + 1] = char
  249.   end
  250.   if backcolor or surf.overwrite then
  251.     surf.buffer[((y - 1) * surf.width + x - 1) * 3 + 2] = backcolor
  252.   end
  253.   if textcolor or surf.overwrite then
  254.     surf.buffer[((y - 1) * surf.width + x - 1) * 3 + 3] = textcolor
  255.   end
  256. end
  257.  
  258. function getPixel(surf, x, y)
  259.   if x < surf.x1 or y < surf.y1 or x > surf.x2 or y > surf.y2 then return end
  260.   return surf.buffer[((y - 1) * surf.width + x - 1) * 3 + 1], surf.buffer[((y - 1) * surf.width + x - 1) * 3 + 2], surf.buffer[((y - 1) * surf.width + x - 1) * 3 + 3]
  261. end
  262.  
  263. function drawText(surf, x, y, text, backcolor, textcolor)
  264.   local px = x
  265.   for i=1,#text,1 do
  266.     if text:sub(i, i) ~= "\n" then
  267.       surf:drawPixel(x, y, text:sub(i, i), backcolor, textcolor)
  268.     else
  269.       x = px - 1
  270.       y = y + 1
  271.     end
  272.     x = x + 1
  273.   end
  274. end
  275.  
  276. function drawLine(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  277.   local delta_x = x2 - x1
  278.   local ix = delta_x > 0 and 1 or -1
  279.   delta_x = 2 * math.abs(delta_x)
  280.   local delta_y = y2 - y1
  281.   local iy = delta_y > 0 and 1 or -1
  282.   delta_y = 2 * math.abs(delta_y)
  283.   surf:drawPixel(x1, y1, char, backcolor, textcolor)
  284.   if delta_x >= delta_y then
  285.     local error = delta_y - delta_x / 2
  286.     while x1 ~= x2 do
  287.       if (error >= 0) and ((error ~= 0) or (ix > 0)) then
  288.         error = error - delta_x
  289.         y1 = y1 + iy
  290.       end
  291.       error = error + delta_y
  292.       x1 = x1 + ix
  293.       surf:drawPixel(x1, y1, char, backcolor, textcolor)
  294.     end
  295.   else
  296.     local error = delta_x - delta_y / 2
  297.     while y1 ~= y2 do
  298.       if (error >= 0) and ((error ~= 0) or (iy > 0)) then
  299.         error = error - delta_y
  300.         x1 = x1 + ix
  301.       end
  302.       error = error + delta_x
  303.       y1 = y1 + iy
  304.       surf:drawPixel(x1, y1, char, backcolor, textcolor)
  305.     end
  306.   end
  307. end
  308.  
  309. function drawRect(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  310.   surf:drawLine(x1, y1, x2, y1, char, backcolor, textcolor)
  311.   surf:drawLine(x1, y1, x1, y2, char, backcolor, textcolor)
  312.   surf:drawLine(x1, y2, x2, y2, char, backcolor, textcolor)
  313.   surf:drawLine(x2, y1, x2, y2, char, backcolor, textcolor)
  314. end
  315.  
  316. function fillRect(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  317.   if x2 < x1 then
  318.     local temp = x1
  319.     x1 = x2
  320.     x2 = temp
  321.   end
  322.   if y2 < y1 then
  323.     local temp = y1
  324.     y1 = y2
  325.     y2 = temp
  326.   end
  327.   if x2 < surf.x1 then return end
  328.   if x1 > surf.x2 then return end
  329.   if y2 < surf.y1 then return end
  330.   if y1 > surf.y2 then return end
  331.   if x1 < surf.x1 then x1 = surf.x1 end
  332.   if x2 > surf.x2 then x2 = surf.x2 end
  333.   if y1 < surf.y1 then y1 = surf.y1 end
  334.   if y2 > surf.y2 then y2 = surf.y2 end
  335.  
  336.   if char or surf.overwrite then
  337.     for y=y1,y2,1 do
  338.       for x=x1,x2,1 do
  339.         surf.buffer[((y-1) * surf.width + (x-1)) * 3 + 1] = char
  340.       end
  341.     end
  342.   end
  343.   if backcolor or surf.overwrite then
  344.     for y=y1,y2,1 do
  345.       for x=x1,x2,1 do
  346.         surf.buffer[((y-1) * surf.width + (x-1)) * 3 + 2] = backcolor
  347.       end
  348.     end
  349.   end
  350.   if textcolor or surf.overwrite then
  351.     for y=y1,y2,1 do
  352.       for x=x1,x2,1 do
  353.         surf.buffer[((y-1) * surf.width + (x-1)) * 3 + 3] = textcolor
  354.       end
  355.     end
  356.   end
  357. end
  358.  
  359. function drawTriangle(surf, x1, y1, x2, y2, x3, y3, char, backcolor, textcolor)
  360.   surf:drawLine(x1, y1, x2, y2, char, backcolor, textcolor)
  361.   surf:drawLine(x2, y2, x3, y3, char, backcolor, textcolor)
  362.   surf:drawLine(x3, y3, x1, y1, char, backcolor, textcolor)
  363. end
  364.  
  365. function fillTriangle(surf, x1, y1, x2, y2, x3, y3, char, backcolor, textcolor)
  366.   local minX = x1
  367.   local minY = y1
  368.   local maxX = x1
  369.   local maxY = y1
  370.   if x2 < minX then minX = x2 end
  371.   if x3 < minX then minX = x3 end
  372.   if y2 < minY then minY = y2 end
  373.   if y3 < minY then minY = y3 end
  374.   if x2 > maxX then maxX = x2 end
  375.   if x3 > maxX then maxX = x3 end
  376.   if y2 > maxY then maxY = y2 end
  377.   if y3 > maxY then maxY = y3 end
  378.   local width = maxX - minX + 1
  379.   local height = maxY - minY + 1
  380.   local buffer = { }
  381.   _bufferLine(buffer, width, x1 - minX + 1, y1 - minY + 1, x2 - minX + 1, y2 - minY + 1)
  382.   _bufferLine(buffer, width, x2 - minX + 1, y2 - minY + 1, x3 - minX + 1, y3 - minY + 1)
  383.   _bufferLine(buffer, width, x3 - minX + 1, y3 - minY + 1, x1 - minX + 1, y1 - minY + 1)
  384.   _bufferFill(buffer, width, height)
  385.   for i=1,width,1 do
  386.     for j=1,height,1 do
  387.       if buffer[(j - 1) * width + i] then
  388.         surf:drawPixel(minX + i - 1, minY + j - 1, char, backcolor, textcolor)
  389.       end
  390.     end
  391.   end
  392. end
  393.  
  394. function drawEllipse(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  395.   if x2 < x1 then
  396.     local temp = x1
  397.     x1 = x2
  398.     x2 = temp
  399.   end
  400.   if y2 < y1 then
  401.     local temp = y1
  402.     y1 = y2
  403.     y2 = temp
  404.   end
  405.   local resolution = 12
  406.   local step = (math.pi * 2) / resolution
  407.   local midX = (x1 + x2) / 2
  408.   local midY = (y1 + y2) / 2
  409.   local width = (x2 - x1) / 2
  410.   local height = (y2 - y1) / 2
  411.   local lastX = 1
  412.   local lastY = 1
  413.   for i=1,resolution+1,1 do
  414.     local x = math.floor((midX + math.cos(step * i) * width) + 0.5)
  415.     local y = math.floor((midY + math.sin(step * i) * height) + 0.5)
  416.     if i > 1 then
  417.       surf:drawLine(lastX, lastY, x, y, char, backcolor, textcolor)
  418.     end
  419.     lastX = x
  420.     lastY = y
  421.   end
  422. end
  423.  
  424. function fillEllipse(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  425.   if x2 < x1 then
  426.     local temp = x1
  427.     x1 = x2
  428.     x2 = temp
  429.   end
  430.   if y2 < y1 then
  431.     local temp = y1
  432.     y1 = y2
  433.     y2 = temp
  434.   end
  435.   local resolution = 12
  436.   local step = (math.pi * 2) / resolution
  437.   local midX = (x1 + x2) / 2
  438.   local midY = (y1 + y2) / 2
  439.   local width = (x2 - x1) / 2
  440.   local height = (y2 - y1) / 2
  441.   local lastX = 1
  442.   local lastY = 1
  443.   local minX = x1
  444.   local minY = y1
  445.   local maxX = x2
  446.   local maxY = y2
  447.   local bwidth = maxX - minX + 1
  448.   local bheight = maxY - minY + 1
  449.   local buffer = { }
  450.   for i=1,resolution+1,1 do
  451.     local x = math.floor((midX + math.cos(step * i) * width) + 0.5)
  452.     local y = math.floor((midY + math.sin(step * i) * height) + 0.5)
  453.     if i > 1 then
  454.       _bufferLine(buffer, bwidth, lastX - minX + 1, lastY - minY + 1, x - minX + 1, y - minY + 1)
  455.     end
  456.     lastX = x
  457.     lastY = y
  458.   end
  459.   _bufferFill(buffer, bwidth, bheight)
  460.   for i=1,bwidth,1 do
  461.     for j=1,bheight,1 do
  462.       if buffer[(j - 1) * bwidth + i] then
  463.         surf:drawPixel(minX + i - 1, minY + j - 1, char, backcolor, textcolor)
  464.       end
  465.     end
  466.   end
  467. end
  468.  
  469. function floodFill(surf, x, y, char, backcolor, textcolor)
  470.   local stack = { x, y }
  471.   local tchar, tbackcolor, ttextcolor = surf:getPixel(x, y)
  472.   if (tchar == char) and (tbackcolor == backcolor) and (ttextcolor == textcolor) then return end
  473.   while #stack > 0 do
  474.     local cx = stack[1]
  475.     table.remove(stack, 1)
  476.     local cy = stack[1]
  477.     table.remove(stack, 1)
  478.     if not (cx < surf.x1 or cy < surf.y1 or cx > surf.x2 or cy > surf.y2) then
  479.       local cchar, cbackcolor, ctextcolor = surf:getPixel(cx, cy)
  480.       if (tchar == cchar) and (tbackcolor == cbackcolor) and (ttextcolor == ctextcolor) then
  481.         surf:drawPixel(cx, cy, char, backcolor, textcolor)
  482.         table.insert(stack, cx - 1)
  483.         table.insert(stack, cy)
  484.         table.insert(stack, cx + 1)
  485.         table.insert(stack, cy)
  486.         table.insert(stack, cx)
  487.         table.insert(stack, cy - 1)
  488.         table.insert(stack, cx)
  489.         table.insert(stack, cy + 1)
  490.       end
  491.     end
  492.   end
  493. end
  494.  
  495. function drawSurface(surf, x, y, surf2)
  496.   for j=1,surf2.height,1 do
  497.     for i=1,surf2.width,1 do
  498.       surf:drawPixel(i + x - 1, j + y - 1, surf2.buffer[((j-1) * surf2.width + (i-1)) * 3 + 1], surf2.buffer[((j-1) * surf2.width + (i-1)) * 3 + 2], surf2.buffer[((j-1) * surf2.width + (i-1)) * 3 + 3])
  499.     end
  500.   end
  501. end
  502.  
  503. function drawSurfacePart(surf, x, y, sx1, sy1, sx2, sy2, surf2)
  504.   if sx2 < sx1 then
  505.     local temp = sx1
  506.     sx1 = sx2
  507.     sx2 = temp
  508.   end
  509.   if sy2 < sy1 then
  510.     local temp = sy1
  511.     sy1 = sy2
  512.     sy2 = temp
  513.   end
  514.   if sx2 < 1 then return end
  515.   if sx1 > surface.width then return end
  516.   if sy2 < 1 then return end
  517.   if sy1 > surface.width then return end
  518.   if sx1 < 1 then sx1 = 1 end
  519.   if sx2 > surface.width then sx2 = surface.width end
  520.   if sy1 < 1 then sy1 = 1 end
  521.   if sy2 > surface.width then sy2 = surface.height end
  522.  
  523.   for j=sy1,sy2,1 do
  524.     for i=sx1,sx2,1 do
  525.       surf:drawPixel(x + i - sx1, y + j - sy1, surf2.buffer[((j-1) * surf2.width + (i-1)) * 3 + 1], surf2.buffer[((j-1) * surf2.width + (i-1)) * 3 + 2], surf2.buffer[((j-1) * surf2.width + (i-1)) * 3 + 3])
  526.     end
  527.   end
  528. end
  529.  
  530. function drawSurfaceScaled(surf, x1, y1, x2, y2, surf2)
  531.   local x = 0
  532.   local width = 0
  533.   local xinv = false
  534.   local y = 0
  535.   local height = 0
  536.   local yinv = false
  537.   if x2 >= x1 then
  538.     x = x1
  539.     width = x2 - x1 + 1
  540.   else
  541.     x = x2
  542.     width = x1 - x2 + 1
  543.     xinv = true
  544.   end
  545.   if y2 >= y1 then
  546.     y = y1
  547.     height = y2 - y1 + 1
  548.   else
  549.     y = y2
  550.     height = y1 - y2 + 1
  551.     yinv = true
  552.   end
  553.   local xscale = width / surf2.width
  554.   local yscale = height / surf2.height
  555.  
  556.   for j=1,height,1 do
  557.     for i=1,width,1 do
  558.       local ii = i
  559.       local jj = j
  560.       if xinv then ii = width - ii + 1 end
  561.       if yinv then jj = height - jj + 1 end
  562.       local px = math.floor((ii - 0.5) / xscale) + 1
  563.       local py = math.floor((jj - 0.5) / yscale) + 1
  564.       surf:drawPixel(x + i - 1, y + j - 1, surf2.buffer[((py-1) * surf2.width + (px-1)) * 3 + 1], surf2.buffer[((py-1) * surf2.width + (px-1)) * 3 + 2], surf2.buffer[((py-1) * surf2.width + (px-1)) * 3 + 3])
  565.     end
  566.   end
  567. end
  568.  
  569. function _colorToHex(color)
  570.   if color then
  571.     return string.format("%x", math.log(color)/math.log(2))
  572.   else
  573.     return " "
  574.   end
  575. end
  576.  
  577. function _bufferLine(buffer, width, x1, y1, x2, y2)
  578.   local delta_x = x2 - x1
  579.   local ix = delta_x > 0 and 1 or -1
  580.   delta_x = 2 * math.abs(delta_x)
  581.   local delta_y = y2 - y1
  582.   local iy = delta_y > 0 and 1 or -1
  583.   delta_y = 2 * math.abs(delta_y)
  584.   buffer[(y1 - 1) * width + x1] = true
  585.   if delta_x >= delta_y then
  586.     local error = delta_y - delta_x / 2
  587.     while x1 ~= x2 do
  588.       if (error >= 0) and ((error ~= 0) or (ix > 0)) then
  589.         error = error - delta_x
  590.         y1 = y1 + iy
  591.       end
  592.       error = error + delta_y
  593.       x1 = x1 + ix
  594.       buffer[(y1 - 1) * width + x1] = true
  595.     end
  596.   else
  597.     local error = delta_x - delta_y / 2
  598.     while y1 ~= y2 do
  599.       if (error >= 0) and ((error ~= 0) or (iy > 0)) then
  600.         error = error - delta_y
  601.         x1 = x1 + ix
  602.       end
  603.       error = error + delta_x
  604.       y1 = y1 + iy
  605.       buffer[(y1 - 1) * width + x1] = true
  606.     end
  607.   end
  608. end
  609.  
  610. function _bufferFill(buffer, width, height)
  611.   for j=1,height,1 do
  612.     local lines = 0
  613.     local wait = false
  614.     for i=1,width,1 do
  615.       local cur = buffer[(j - 1) * width + i]
  616.       if cur and not wait then
  617.         lines = lines + 1
  618.         wait = true
  619.       end
  620.       if not cur and wait then
  621.         wait = false
  622.       end
  623.     end
  624.     if lines > 1 then
  625.       local write = false
  626.       local wait = false
  627.       for i=1,width,1 do
  628.         local cur = buffer[(j - 1) * width + i]
  629.         if cur then
  630.           wait = true
  631.         else
  632.           if wait then
  633.             wait = false
  634.             write = not write
  635.           end
  636.           if write then
  637.             cur = true
  638.           end
  639.         end
  640.         buffer[(j - 1) * width + i] = cur
  641.       end
  642.     end
  643.   end
  644. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top