Advertisement
CrazedProgrammer

Surface API 1.1

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