SHARE
TWEET

Surface API 1.2

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