Advertisement
CrazedProgrammer

Surface API

Mar 15th, 2015
1,565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 34.31 KB | None | 0 0
  1. -- Surface API version 1.6.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. version = "1.6.2"
  9.  
  10. local math_floor, math_cos, math_sin, table_concat, _colors = math.floor, math.cos, math.sin, table.concat, {[1] = "0", [2] = "1", [4] = "2", [8] = "3", [16] = "4", [32] = "5", [64] = "6", [128] = "7", [256] = "8", [512] = "9", [1024] = "a", [2048] = "b", [4096] = "c", [8192] = "d", [16384] = "e", [32768] = "f"}
  11.  
  12. local function _bufferLine(buffer, width, x1, y1, x2, y2)
  13.     local delta_x = x2 - x1
  14.     local ix = delta_x > 0 and 1 or -1
  15.     delta_x = 2 * math.abs(delta_x)
  16.     local delta_y = y2 - y1
  17.     local iy = delta_y > 0 and 1 or -1
  18.     delta_y = 2 * math.abs(delta_y)
  19.     buffer[(y1 - 1) * width + x1] = true
  20.     if delta_x >= delta_y then
  21.         local error = delta_y - delta_x / 2
  22.         while x1 ~= x2 do
  23.             if (error >= 0) and ((error ~= 0) or (ix > 0)) then
  24.                 error = error - delta_x
  25.                 y1 = y1 + iy
  26.             end
  27.             error = error + delta_y
  28.             x1 = x1 + ix
  29.             buffer[(y1 - 1) * width + x1] = true
  30.         end
  31.     else
  32.         local error = delta_x - delta_y / 2
  33.         while y1 ~= y2 do
  34.             if (error >= 0) and ((error ~= 0) or (iy > 0)) then
  35.                 error = error - delta_y
  36.                 x1 = x1 + ix
  37.             end
  38.             error = error + delta_x
  39.             y1 = y1 + iy
  40.             buffer[(y1 - 1) * width + x1] = true
  41.         end
  42.     end
  43. end
  44.  
  45. local _functions = {
  46. setBounds = function(surf, x1, y1, x2, y2)
  47.     if x1 > x2 then
  48.         local temp = x1
  49.         x1, x2 = x2, temp
  50.     end
  51.     if y1 > y2 then
  52.         local temp = y1
  53.         y1, y2 = y2, temp
  54.     end
  55.     if x2 < 1 or x1 > surf.width or y2 < 1 or y1 > surf.height then return end
  56.     if x1 < 1 then x1 = 1 end
  57.     if x2 > surf.width then x2 = surf.width end
  58.     if y1 < 1 then y1 = 1 end
  59.     if y2 > surf.height then y2 = surf.height end
  60.     surf.x1, surf.y1, surf.x2, surf.y2 = x1, y1, x2, y2
  61. end,
  62.  
  63. getBounds = function(surf)
  64.     return surf.x1, surf.y1, surf.x2, surf.y2
  65. end,
  66.  
  67. copy = function(surf)
  68.     local surf2 = create(surf.width, surf.height)
  69.     surf2.x1, surf2.y1, surf2.x2, surf2.y2, surf2.blink, surf2.curX, surf2.curY, surf2.overwrite = surf.x1, surf.y1, surf.x2, surf.y2, surf.blink, surf.curX, surf.curY, surf.overwrite
  70.     for i=1,surf.width * surf.height * 3 do
  71.         surf2.buffer[i] = surf.buffer[i]
  72.     end
  73.     return surf2
  74. end,
  75.  
  76. save = function(surf, path, type)
  77.     type = type or "srf"
  78.     local f = fs.open(path, "w")
  79.     if type == "nfp" then
  80.         local color = nil
  81.         for j=1,surf.height do
  82.             if j > 1 then f.write("\n") end
  83.             for i=1,surf.width do
  84.                 color = surf.buffer[((j - 1) * surf.width + i) * 3 - 1]
  85.                 if color then
  86.                     f.write(_colors[color])
  87.                 else
  88.                     f.write(" ")
  89.                 end
  90.             end
  91.         end
  92.     elseif type == "nft" then
  93.         local backcolor, textcolor, char = nil
  94.         for j=1,surf.height do
  95.             if j > 1 then f.write("\n") end
  96.             backcolor, textcolor = nil
  97.             for i=1,surf.width do
  98.                 if backcolor ~= surf.buffer[((j - 1) * surf.width + i) * 3 - 1] then
  99.                     f.write(string.char(30))
  100.                     backcolor = surf.buffer[((j - 1) * surf.width + i) * 3 - 1]
  101.                     if backcolor then
  102.                         f.write(_colors[backcolor])
  103.                     else
  104.                         f.write(" ")
  105.                     end
  106.                 end
  107.                 if textcolor ~= surf.buffer[((j - 1) * surf.width + i) * 3] then
  108.                     f.write(string.char(31))
  109.                     textcolor = surf.buffer[((j - 1) * surf.width + i) * 3]
  110.                     if textcolor then
  111.                         f.write(_colors[textcolor])
  112.                     else
  113.                         f.write(" ")
  114.                     end
  115.                 end
  116.                 char = surf.buffer[((j - 1) * surf.width + i) * 3 - 2]
  117.                 if char then
  118.                     f.write(char)
  119.                 else
  120.                     f.write(" ")
  121.                 end
  122.             end
  123.         end
  124.     elseif type == "srf" then
  125.         f.write(surf:saveString())
  126.     end
  127.     f.close()
  128. end,
  129.  
  130. saveString = function(surf)
  131.     local str = {"_"..string.format("%04x", surf.width)..string.format("%04x", surf.height)}
  132.     for j=1,surf.height do
  133.         for i=1,surf.width do
  134.             if surf.buffer[((j - 1) * surf.width + i) * 3 - 2] then
  135.                 str[#str + 1] = string.format("%02x", surf.buffer[((j - 1) * surf.width + i) * 3 - 2]:byte(1))
  136.             else
  137.                 str[#str + 1] = "__"
  138.             end
  139.             if surf.buffer[((j - 1) * surf.width + i) * 3 - 1] then
  140.                 str[#str + 1] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3 - 1]]
  141.             else
  142.                 str[#str + 1] = "_"
  143.             end
  144.             if surf.buffer[((j - 1) * surf.width + i) * 3] then
  145.                 str[#str + 1] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3]]
  146.             else
  147.                 str[#str + 1] = "_"
  148.             end
  149.         end
  150.     end
  151.     return table_concat(str)
  152. end,
  153.  
  154. getTerm = function(surf)
  155.     local term, backcolor, textcolor = { }, colors.black, colors.white
  156.     function term.write(str)
  157.         surf:drawText(surf.curX, surf.curY, tostring(str), backcolor, textcolor)
  158.         surf.curX = surf.curX + #tostring(str)
  159.     end
  160.     function term.blit(str, text, back)
  161.         for i=1,#str do
  162.             if surf.curX >= surf.x1 and surf.curY >= surf.y1 and surf.curX <= surf.x2 and surf.curY <= surf.y2 then
  163.                 surf.buffer[((surf.curY - 1) * surf.width + surf.curX) * 3 - 2] = str:sub(i, i)
  164.                 surf.buffer[((surf.curY - 1) * surf.width + surf.curX) * 3 - 1] = 2 ^ tonumber(back:sub(i, i), 16)
  165.                 surf.buffer[((surf.curY - 1) * surf.width + surf.curX) * 3] = 2 ^ tonumber(text:sub(i, i), 16)
  166.             end
  167.             surf.curX = surf.curX + 1
  168.         end
  169.     end
  170.     function term.clear()
  171.         surf:clear(" ", backcolor, textcolor)
  172.     end
  173.     function term.clearLine(n)
  174.         surf:drawHLine(surf.x1, surf.x2, surf.curY, " ", backcolor, textcolor)
  175.     end
  176.     function term.getCursorPos()
  177.         return surf.curX, surf.curY
  178.     end
  179.     function term.setCursorPos(x, y)
  180.         surf.curX, surf.curY = math_floor(x), math_floor(y)
  181.     end
  182.     function term.setCursorBlink(blink)
  183.         surf.blink = blink
  184.     end
  185.     function term.isColor()
  186.         return true
  187.     end
  188.     term.isColour = term.isColor
  189.     function term.setTextColor(color)
  190.         textcolor = color
  191.     end
  192.     term.setTextColour = term.setTextColor
  193.     function term.setBackgroundColor(color)
  194.         backcolor = color
  195.     end
  196.     term.setBackgroundColour = term.setBackgroundColor
  197.     function term.getSize()
  198.         return surf.width, surf.height
  199.     end
  200.     function term.scroll(n)
  201.         surf:shift(0, -n)
  202.     end
  203.     function term.getTextColor()
  204.         return textcolor
  205.     end
  206.     term.getTextColour = term.getTextColor
  207.     function term.getBackgroundColor()
  208.         return backcolor
  209.     end
  210.     term.getBackgroundColour = term.getBackgroundColor
  211.     return term
  212. end,
  213.  
  214. render = function(surf, display, x, y, sx1, sy1, sx2, sy2)
  215.     display, x, y, sx1, sy1, sx2, sy2 = display or term, x or 1, y or 1, sx1 or 1, sy1 or 1, sx2 or surf.width, sy2 or surf.height
  216.     if sx1 > sx2 then
  217.         local temp = sx1
  218.         sx1, sx2 = sx2, temp
  219.     end
  220.     if sy1 > sy2 then
  221.         local temp = sy1
  222.         sy1, sy2 = sy2, temp
  223.     end
  224.     if sx2 < 1 or sx1 > surf.width or sy2 < 1 or sy1 > surf.height then return end
  225.     if sx1 < 1 then sx1 = 1 end
  226.     if sx2 > surf.width then sx2 = surf.width end
  227.     if sy1 < 1 then sy1 = 1 end
  228.     if sy2 > surf.height then sy2 = surf.height end
  229.     local cmd = { }
  230.     if display.blit then
  231.         local str, back, text = { }, { }, { }
  232.         for j=sy1,sy2 do
  233.             for i=sx1,sx2 do
  234.                 str[i - sx1 + 1] = surf.buffer[((j - 1) * surf.width + i) * 3 - 2] or " "
  235.                 back[i - sx1 + 1] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3 - 1] or 32768]
  236.                 text[i - sx1 + 1] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3] or 1]
  237.             end
  238.             cmd[#cmd + 1] = y + j - sy1
  239.             cmd[#cmd + 1] = table_concat(str)
  240.             cmd[#cmd + 1] = table_concat(text)
  241.             cmd[#cmd + 1] = table_concat(back)
  242.         end
  243.         for i=1,#cmd,4 do
  244.             display.setCursorPos(x, cmd[i])
  245.             display.blit(cmd[i + 1], cmd[i + 2], cmd[i + 3])
  246.         end
  247.     else
  248.         local str, backcolor, textcolor, backc, textc = "", 0, 0
  249.         for j=sy1,sy2 do
  250.             cmd[#cmd + 1] = 1
  251.             cmd[#cmd + 1] = y + j - sy1
  252.             for i=sx1,sx2 do
  253.                 backc, textc = (surf.buffer[((j - 1) * surf.width + i) * 3 - 1] or 32768), (surf.buffer[((j - 1) * surf.width + i) * 3] or 1)
  254.                 if backc ~= backcolor then
  255.                     backcolor = backc
  256.                     if str ~= "" then
  257.                         cmd[#cmd + 1] = 4
  258.                         cmd[#cmd + 1] = str
  259.                         str = ""
  260.                     end
  261.                     cmd[#cmd + 1] = 2
  262.                     cmd[#cmd + 1] = backcolor
  263.                 end
  264.                 if textc ~= textcolor then
  265.                     textcolor = textc
  266.                     if str ~= "" then
  267.                         cmd[#cmd + 1] = 4
  268.                         cmd[#cmd + 1] = str
  269.                         str = ""
  270.                     end
  271.                     cmd[#cmd + 1] = 3
  272.                     cmd[#cmd + 1] = textcolor
  273.                 end
  274.                 str = str..(surf.buffer[((j - 1) * surf.width + i) * 3 - 2] or " ")
  275.             end
  276.             cmd[#cmd + 1] = 4
  277.             cmd[#cmd + 1] = str
  278.             str = ""
  279.         end
  280.         local c, a = nil
  281.         for i=1,#cmd,2 do
  282.             c, a = cmd[i], cmd[i + 1]
  283.             if c == 1 then
  284.                 display.setCursorPos(x, a)
  285.             elseif c == 2 then
  286.                 display.setBackgroundColor(a)
  287.             elseif c == 3 then
  288.                 display.setTextColor(a)
  289.             else
  290.                 display.write(a)
  291.             end
  292.         end
  293.     end
  294.     if surf.blink and surf.curX >= 1 and surf.curY >= 1 and surf.curX <= surf.width and surf.curY <= surf.height then
  295.         display.setCursorPos(x + surf.curX - sx1, y + surf.curY - sy1)
  296.         display.setCursorBlink(true)
  297.     elseif surf.blink == false then
  298.         display.setCursorBlink(false)
  299.         surf.blink = nil
  300.     end
  301.     return #cmd / 2
  302. end,
  303.  
  304. clear = function(surf, char, backcolor, textcolor)
  305.     local overwrite = surf.overwrite
  306.     surf.overwrite = true
  307.     surf:fillRect(surf.x1, surf.y1, surf.x2, surf.y2, char, backcolor, textcolor)
  308.     surf.overwrite = overwrite
  309. end,
  310.  
  311. drawPixel = function(surf, x, y, char, backcolor, textcolor)
  312.     if x < surf.x1 or y < surf.y1 or x > surf.x2 or y > surf.y2 then return end
  313.     if char or surf.overwrite then
  314.         surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = char
  315.     end
  316.     if backcolor or surf.overwrite then
  317.         surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  318.     end
  319.     if textcolor or surf.overwrite then
  320.         surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  321.     end
  322. end,
  323.  
  324. getPixel = function(surf, x, y)
  325.     if x < 1 or y < 1 or x > surf.width or y > surf.height then return end
  326.     return surf.buffer[((y - 1) * surf.width + x) * 3 - 2], surf.buffer[((y - 1) * surf.width + x) * 3 - 1], surf.buffer[((y - 1) * surf.width + x) * 3]
  327. end,
  328.  
  329. drawText = function(surf, x, y, text, backcolor, textcolor)
  330.     local px = x
  331.     for i=1,#text do
  332.         if text:sub(i, i) ~= "\n" then
  333.             if x >= surf.x1 and y >= surf.y1 and x <= surf.x2 and y <= surf.y2 then
  334.                 surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = text:sub(i, i)
  335.                 if backcolor or surf.overwrite then
  336.                     surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  337.                 end
  338.                 if textcolor or surf.overwrite then
  339.                     surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  340.                 end
  341.             end
  342.         else
  343.             x = px - 1
  344.             y = y + 1
  345.         end
  346.         x = x + 1
  347.     end
  348. end,
  349.  
  350. drawLine = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  351.     local delta_x = x2 - x1
  352.     local ix = delta_x > 0 and 1 or -1
  353.     delta_x = 2 * math.abs(delta_x)
  354.     local delta_y = y2 - y1
  355.     local iy = delta_y > 0 and 1 or -1
  356.     delta_y = 2 * math.abs(delta_y)
  357.     surf:drawPixel(x1, y1, char, backcolor, textcolor)
  358.     if delta_x >= delta_y then
  359.         local error = delta_y - delta_x / 2
  360.         while x1 ~= x2 do
  361.             if (error >= 0) and ((error ~= 0) or (ix > 0)) then
  362.                 error = error - delta_x
  363.                 y1 = y1 + iy
  364.             end
  365.             error = error + delta_y
  366.             x1 = x1 + ix
  367.             surf:drawPixel(x1, y1, char, backcolor, textcolor)
  368.         end
  369.     else
  370.         local error = delta_x - delta_y / 2
  371.         while y1 ~= y2 do
  372.             if (error >= 0) and ((error ~= 0) or (iy > 0)) then
  373.                 error = error - delta_y
  374.                 x1 = x1 + ix
  375.             end
  376.             error = error + delta_x
  377.             y1 = y1 + iy
  378.             surf:drawPixel(x1, y1, char, backcolor, textcolor)
  379.         end
  380.     end
  381. end,
  382.  
  383. drawLines = function(surf, points, mode, char, backcolor, textcolor)
  384.     mode = mode or 1
  385.     if mode == 1 then
  386.         for i=1,#points,4 do
  387.             surf:drawLine(points[i], points[i+1], points[i+2], points[i+3], char, backcolor, textcolor)
  388.         end
  389.     elseif mode == 2 then
  390.         local lastx, lasty = points[1], points[2]
  391.         for i=3,#points,2 do
  392.             local curx, cury = points[i], points[i+1]
  393.             surf:drawLine(lastx, lasty, curx, cury, char, backcolor, textcolor)
  394.             lastx = curx
  395.             lasty = cury
  396.         end
  397.     elseif mode == 3 then
  398.         local midx, midy = points[1], points[2]
  399.         for i=3,#points,2 do
  400.             surf:drawLine(midx, midy, points[i], points[i+1], char, backcolor, textcolor)
  401.         end
  402.     end
  403. end,
  404.  
  405. drawHLine = function(surf, x1, x2, y, char, backcolor, textcolor)
  406.     if x1 > x2 then
  407.         local temp = x1
  408.         x1, x2 = x2, temp
  409.     end
  410.     if y < surf.y1 or y > surf.y2 or x2 < surf.x1 or x1 > surf.x2 then return end
  411.     if x1 < surf.x1 then x1 = surf.x1 end
  412.     if x2 > surf.x2 then x2 = surf.x2 end
  413.     if char or surf.overwrite then
  414.         for x=x1,x2 do
  415.             surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = char
  416.         end
  417.     end
  418.     if backcolor or surf.overwrite then
  419.         for x=x1,x2 do
  420.             surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  421.         end
  422.     end
  423.     if textcolor or surf.overwrite then
  424.         for x=x1,x2 do
  425.             surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  426.         end
  427.     end
  428. end,
  429.  
  430. drawVLine = function(surf, y1, y2, x, char, backcolor, textcolor)
  431.     if y1 > y2 then
  432.         local temp = y1
  433.         y1, y2 = y2, temp
  434.     end
  435.     if x < surf.x1 or x > surf.x2 or y2 < surf.y1 or y1 > surf.y2 then return end
  436.     if y1 < surf.y1 then y1 = surf.y1 end
  437.     if y2 > surf.y2 then y2 = surf.y2 end
  438.     if char or surf.overwrite then
  439.         for y=y1,y2 do
  440.             surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = char
  441.         end
  442.     end
  443.     if backcolor or surf.overwrite then
  444.         for y=y1,y2 do
  445.             surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  446.         end
  447.     end
  448.     if textcolor or surf.overwrite then
  449.         for y=y1,y2 do
  450.             surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  451.         end
  452.     end
  453. end,
  454.  
  455. drawRect = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  456.     surf:drawHLine(x1, x2, y1, char, backcolor, textcolor)
  457.     surf:drawHLine(x1, x2, y2, char, backcolor, textcolor)
  458.     surf:drawVLine(y1, y2, x1, char, backcolor, textcolor)
  459.     surf:drawVLine(y1, y2, x2, char, backcolor, textcolor)
  460. end,
  461.  
  462. drawRoundRect = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  463.     surf:drawHLine(x1 + 1, x2 - 1, y1, char, backcolor, textcolor)
  464.     surf:drawHLine(x1 + 1, x2 - 1, y2, char, backcolor, textcolor)
  465.     surf:drawVLine(y1 + 1, y2 - 1, x1, char, backcolor, textcolor)
  466.     surf:drawVLine(y1 + 1, y2 - 1, x2, char, backcolor, textcolor)
  467. end,
  468.  
  469. drawRoundedRect = function(surf, x1, y1, x2, y2, radius, char, backcolor, textcolor)
  470.     surf:drawHLine(x1 + radius, x2 - radius, y1, char, backcolor, textcolor)
  471.     surf:drawHLine(x1 + radius, x2 - radius, y2, char, backcolor, textcolor)
  472.     surf:drawVLine(y1 + radius, y2 - radius, x1, char, backcolor, textcolor)
  473.     surf:drawVLine(y1 + radius, y2 - radius, x2, char, backcolor, textcolor)
  474.     surf:drawArc(x1, y1, x1 + radius * 2 + 2, y1 + radius * 2 + 2, -math.pi, -math.pi / 2, char, backcolor, textcolor)
  475.     surf:drawArc(x2, y1, x2 - radius * 2 - 2, y1 + radius * 2 + 2, 0, -math.pi / 2, char, backcolor, textcolor)
  476.     surf:drawArc(x1, y2, x1 + radius * 2 + 2, y2 - radius * 2 - 2, math.pi, math.pi / 2, char, backcolor, textcolor)
  477.     surf:drawArc(x2, y2, x2 - radius * 2 - 2, y2 - radius * 2 - 2, 0, math.pi / 2, char, backcolor, textcolor)
  478. end,
  479.  
  480. fillRect = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  481.     if x1 > x2 then
  482.         local temp = x1
  483.         x1, x2 = x2, temp
  484.     end
  485.     if y1 > y2 then
  486.         local temp = y1
  487.         y1, y2 = y2, temp
  488.     end
  489.     if x2 < surf.x1 or x1 > surf.x2 or y2 < surf.y1 or y1 > surf.y2 then return end
  490.     if x1 < surf.x1 then x1 = surf.x1 end
  491.     if x2 > surf.x2 then x2 = surf.x2 end
  492.     if y1 < surf.y1 then y1 = surf.y1 end
  493.     if y2 > surf.y2 then y2 = surf.y2 end
  494.     if char or surf.overwrite then
  495.         for y=y1,y2 do
  496.             for x=x1,x2 do
  497.                 surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = char
  498.             end
  499.         end
  500.     end
  501.     if backcolor or surf.overwrite then
  502.         for y=y1,y2 do
  503.             for x=x1,x2 do
  504.                 surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  505.             end
  506.         end
  507.     end
  508.     if textcolor or surf.overwrite then
  509.         for y=y1,y2 do
  510.             for x=x1,x2 do
  511.                 surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  512.             end
  513.         end
  514.     end
  515. end,
  516.  
  517. fillRoundRect = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  518.     surf:drawHLine(x1 + 1, x2 - 1, y1, char, backcolor, textcolor)
  519.     surf:drawHLine(x1 + 1, x2 - 1, y2, char, backcolor, textcolor)
  520.     surf:drawVLine(y1 + 1, y2 - 1, x1, char, backcolor, textcolor)
  521.     surf:drawVLine(y1 + 1, y2 - 1, x2, char, backcolor, textcolor)
  522.     surf:fillRect(x1 + 1, y1 + 1, x2 - 1, y2 - 1, char, backcolor, textcolor)
  523. end,
  524.  
  525. fillRoundedRect = function(surf, x1, y1, x2, y2, radius, char, backcolor, textcolor)
  526.     surf:fillRect(x1 + radius, y1, x2 - radius, y2, char, backcolor, textcolor)
  527.     surf:fillRect(x1, y1 + radius, x1 + radius, y2 - radius, char, backcolor, textcolor)
  528.     surf:fillRect(x2 - radius, y1 + radius, x2, y2 - radius, char, backcolor, textcolor)
  529.     surf:fillPie(x1, y1, x1 + radius * 2 + 2, y1 + radius * 2 + 2, -math.pi, -math.pi / 2, char, backcolor, textcolor)
  530.     surf:fillPie(x2, y1, x2 - radius * 2 - 2, y1 + radius * 2 + 2, 0, -math.pi / 2, char, backcolor, textcolor)
  531.     surf:fillPie(x1, y2, x1 + radius * 2 + 2, y2 - radius * 2 - 2, math.pi, math.pi / 2, char, backcolor, textcolor)
  532.     surf:fillPie(x2, y2, x2 - radius * 2 - 2, y2 - radius * 2 - 2, 0, math.pi / 2, char, backcolor, textcolor)
  533. end,
  534.  
  535. drawTriangle = function(surf, x1, y1, x2, y2, x3, y3, char, backcolor, textcolor)
  536.     surf:drawLine(x1, y1, x2, y2, char, backcolor, textcolor)
  537.     surf:drawLine(x2, y2, x3, y3, char, backcolor, textcolor)
  538.     surf:drawLine(x3, y3, x1, y1, char, backcolor, textcolor)
  539. end,
  540.  
  541. fillTriangle = function(surf, x1, y1, x2, y2, x3, y3, char, backcolor, textcolor)
  542.     local minX, minY, maxX, maxY = x1, y1, x1, y1
  543.     if x2 < minX then minX = x2 end
  544.     if x3 < minX then minX = x3 end
  545.     if y2 < minY then minY = y2 end
  546.     if y3 < minY then minY = y3 end
  547.     if x2 > maxX then maxX = x2 end
  548.     if x3 > maxX then maxX = x3 end
  549.     if y2 > maxY then maxY = y2 end
  550.     if y3 > maxY then maxY = y3 end
  551.     local width, height, buffer, min, max = maxX - minX + 1, maxY - minY + 1, { }, 0, 0
  552.     _bufferLine(buffer, width, x1 - minX + 1, y1 - minY + 1, x2 - minX + 1, y2 - minY + 1)
  553.     _bufferLine(buffer, width, x2 - minX + 1, y2 - minY + 1, x3 - minX + 1, y3 - minY + 1)
  554.     _bufferLine(buffer, width, x3 - minX + 1, y3 - minY + 1, x1 - minX + 1, y1 - minY + 1)
  555.     for j=1,height do
  556.         min, max = nil
  557.         for i=1,width do
  558.             if buffer[(j - 1) * width + i] then
  559.                 if not min then min = i end
  560.                 max = i
  561.             end
  562.         end
  563.         surf:drawHLine(min + minX - 1, max + minX - 1, j + minY - 1, char, backcolor, textcolor)
  564.     end
  565. end,
  566.  
  567. drawTriangles = function(surf, points, mode, char, backcolor, textcolor)
  568.     mode = mode or 1
  569.     if mode == 1 then
  570.         for i=1,#points,6 do
  571.             surf:drawTriangle(points[i], points[i+1], points[i+2], points[i+3], points[i+4], points[i+5], char, backcolor, textcolor)
  572.         end
  573.     elseif mode == 2 then
  574.         local lastx, lasty, prevx, prevy, curx, cury = points[1], points[2], points[3], points[4]
  575.         for i=5,#points,2 do
  576.             curx, cury = points[i], points[i+1]
  577.             surf:drawTriangle(lastx, lasty, prevx, prevy, curx, cury, char, backcolor, textcolor)
  578.             lastx, lasty, prevx, prevy = prevx, prevy, curx, cury
  579.         end
  580.     elseif mode == 3 then
  581.         local midx, midy, lastx, lasty, curx, cury = points[1], points[2], points[3], points[4]
  582.         for i=5,#points,2 do
  583.             curx, cury = points[i], points[i+1]
  584.             surf:drawTriangle(midx, midy, lastx, lasty, curx, cury, char, backcolor, textcolor)
  585.             lastx, lasty = curx, cury
  586.         end
  587.     end
  588. end,
  589.  
  590. fillTriangles = function(surf, points, mode, char, backcolor, textcolor)
  591.     mode = mode or 1
  592.     if mode == 1 then
  593.         for i=1,#points,6 do
  594.             surf:fillTriangle(points[i], points[i+1], points[i+2], points[i+3], points[i+4], points[i+5], char, backcolor, textcolor)
  595.         end
  596.     elseif mode == 2 then
  597.         local lastx, lasty, prevx, prevy, curx, cury = points[1], points[2], points[3], points[4]
  598.         for i=5,#points,2 do
  599.             curx, cury = points[i], points[i+1]
  600.             surf:fillTriangle(lastx, lasty, prevx, prevy, curx, cury, char, backcolor, textcolor)
  601.             lastx, lasty, prevx, prevy = prevx, prevy, curx, cury
  602.         end
  603.     elseif mode == 3 then
  604.         local midx, midy, lastx, lasty, curx, cury = points[1], points[2], points[3], points[4]
  605.         for i=5,#points,2 do
  606.             curx, cury = points[i], points[i+1]
  607.             surf:fillTriangle(midx, midy, lastx, lasty, curx, cury, char, backcolor, textcolor)
  608.             lastx, lasty = curx, cury
  609.         end
  610.     end
  611. end,
  612.  
  613. drawEllipse = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  614.     if x1 > x2 then
  615.         local temp = x1
  616.         x1, x2 = x2, temp
  617.     end
  618.     if y1 > y2 then
  619.         local temp = y1
  620.         y1, y2 = y2, temp
  621.     end
  622.     local step, midX, midY, width, height, lastX, lastY = (math.pi * 2) / 16, (x1 + x2) / 2, (y1 + y2) / 2, (x2 - x1) / 2, (y2 - y1) / 2, 1, 1
  623.     for i=1,17 do
  624.         local x, y = math_floor((midX + math_cos(step * i) * width) + 0.5), math_floor((midY + math_sin(step * i) * height) + 0.5)
  625.         if i > 1 then
  626.             surf:drawLine(lastX, lastY, x, y, char, backcolor, textcolor)
  627.         end
  628.         lastX, lastY = x, y
  629.     end
  630. end,
  631.  
  632. fillEllipse = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  633.     if x1 > x2 then
  634.         local temp = x1
  635.         x1, x2 = x2, temp
  636.     end
  637.     if y1 > y2 then
  638.         local temp = y1
  639.         y1, y2 = y2, temp
  640.     end
  641.     local resolution, step, midX, midY, width, height, lastX, lastY, bwidth, bheight, buffer = 16, (math.pi * 2) / 16, (x1 + x2) / 2, (y1 + y2) / 2, (x2 - x1) / 2, (y2 - y1) / 2, 1, 1, x2 - x1 + 1, y2 - y1 + 1, { }
  642.     for i=1,resolution+1 do
  643.         local x, y = math_floor((midX + math_cos(step * i) * width) + 0.5), math_floor((midY + math_sin(step * i) * height) + 0.5)
  644.         if i > 1 then
  645.             _bufferLine(buffer, bwidth, lastX - x1 + 1, lastY - y1 + 1, x - x1 + 1, y - y1 + 1)
  646.         end
  647.         lastX, lastY = x, y
  648.     end
  649.     for j=1,bheight do
  650.         min, max = nil
  651.         for i=1,bwidth do
  652.             if buffer[(j - 1) * bwidth + i] then
  653.                 if not min then min = i end
  654.                 max = i
  655.             end
  656.         end
  657.         surf:drawHLine(min + x1 - 1, max + x1 - 1, j + y1 - 1, char, backcolor, textcolor)
  658.     end
  659. end,
  660.  
  661. drawArc = function(surf, x1, y1, x2, y2, a1, a2, char, backcolor, textcolor)
  662.     if x1 > x2 then
  663.         local temp = x1
  664.         x1, x2 = x2, temp
  665.     end
  666.     if y1 > y2 then
  667.         local temp = y1
  668.         y1, y2 = y2, temp
  669.     end
  670.     if a1 > a2 then
  671.         local temp = a1
  672.         a1, a2 = a2, temp
  673.     end
  674.     local step, midX, midY, width, height, lastX, lastY = (a2 - a1) / 16, (x1 + x2) / 2, (y1 + y2) / 2, (x2 - x1) / 2, (y2 - y1) / 2, 1, 1
  675.     for i=1,17 do
  676.         local x, y = math_floor((midX + math_cos(step * (i - 1) + a1) * width) + 0.5), math_floor((midY - math_sin(step * (i - 1) + a1) * height) + 0.5)
  677.         if i > 1 then
  678.             surf:drawLine(lastX, lastY, x, y, char, backcolor, textcolor)
  679.         end
  680.         lastX, lastY = x, y
  681.     end
  682. end,
  683.  
  684. drawPie = function(surf, x1, y1, x2, y2, a1, a2, char, backcolor, textcolor)
  685.     if x1 > x2 then
  686.         local temp = x1
  687.         x1, x2 = x2, temp
  688.     end
  689.     if y1 > y2 then
  690.         local temp = y1
  691.         y1, y2 = y2, temp
  692.     end
  693.     if a1 > a2 then
  694.         local temp = a1
  695.         a1, a2 = a2, temp
  696.     end
  697.     local step, midX, midY, width, height, lastX, lastY = (a2 - a1) / 16, (x1 + x2) / 2, (y1 + y2) / 2, (x2 - x1) / 2, (y2 - y1) / 2, 1, 1
  698.     for i=1,17 do
  699.         local x, y = math_floor((midX + math_cos(step * (i - 1) + a1) * width) + 0.5), math_floor((midY - math_sin(step * (i - 1) + a1) * height) + 0.5)
  700.         if i > 1 then
  701.             surf:drawLine(lastX, lastY, x, y, char, backcolor, textcolor)
  702.         end
  703.         lastX, lastY = x, y
  704.     end
  705.     surf:drawLine(math_floor(midX + 0.5), math_floor(midY + 0.5), math_floor((midX + math_cos(a1) * width) + 0.5), math_floor((midY - math_sin(a1) * height) + 0.5), char, backcolor, textcolor)
  706.     surf:drawLine(math_floor(midX + 0.5), math_floor(midY + 0.5), math_floor((midX + math_cos(a2) * width) + 0.5), math_floor((midY - math_sin(a2) * height) + 0.5), char, backcolor, textcolor)
  707. end,
  708.  
  709. fillPie = function(surf, x1, y1, x2, y2, a1, a2, char, backcolor, textcolor)
  710.     if x1 > x2 then
  711.         local temp = x1
  712.         x1, x2 = x2, temp
  713.     end
  714.     if y1 > y2 then
  715.         local temp = y1
  716.         y1, y2 = y2, temp
  717.     end
  718.     if a1 > a2 then
  719.         local temp = a1
  720.         a1, a2 = a2, temp
  721.     end
  722.     local step, midX, midY, width, height, lastX, lastY, bwidth, bheight, buffer = (a2 - a1) / 16, (x1 + x2) / 2, (y1 + y2) / 2, (x2 - x1) / 2, (y2 - y1) / 2, 1, 1, x2 - x1 + 1, y2 - y1 + 1, { }
  723.     for i=1,17 do
  724.         local x, y = math_floor((midX + math_cos(step * (i - 1) + a1) * width) + 0.5), math_floor((midY - math_sin(step * (i - 1) + a1) * height) + 0.5)
  725.         if i > 1 then
  726.             _bufferLine(buffer, bwidth, lastX - x1 + 1, lastY - y1 + 1, x - x1 + 1, y - y1 + 1)
  727.         end
  728.         lastX, lastY = x, y
  729.     end
  730.     _bufferLine(buffer, bwidth, math_floor(midX + 0.5) - x1 + 1, math_floor(midY + 0.5) - y1 + 1, math_floor((midX + math_cos(a1) * width) + 0.5) - x1 + 1, math_floor((midY - math_sin(a1) * height) + 0.5) - y1 + 1)
  731.     _bufferLine(buffer, bwidth, math_floor(midX + 0.5) - x1 + 1, math_floor(midY + 0.5) - y1 + 1, math_floor((midX + math_cos(a2) * width) + 0.5) - x1 + 1, math_floor((midY - math_sin(a2) * height) + 0.5) - y1 + 1)
  732.     for j=1,bheight do
  733.         min, max = nil
  734.         for i=1,bwidth do
  735.             if buffer[(j - 1) * bwidth + i] then
  736.                 if not min then min = i end
  737.                 max = i
  738.             end
  739.         end
  740.         if min then
  741.             surf:drawHLine(min + x1 - 1, max + x1 - 1, j + y1 - 1, char, backcolor, textcolor)
  742.         end
  743.     end
  744. end,
  745.  
  746. floodFill = function(surf, x, y, char, backcolor, textcolor)
  747.     if x < surf.x1 or y < surf.y1 or x > surf.x2 or y > surf.y2 then return end
  748.     local stack, tchar, tbackcolor, ttextcolor = { x, y }, surf.buffer[((y - 1) * surf.width + x) * 3 - 2], surf.buffer[((y - 1) * surf.width + x) * 3 - 1], surf.buffer[((y - 1) * surf.width + x) * 3]
  749.     if (tchar == char) and (tbackcolor == backcolor) and (ttextcolor == textcolor) then return end
  750.     while #stack > 0 do
  751.         local cx, cy = stack[#stack - 1], stack[#stack]
  752.         stack[#stack] = nil
  753.         stack[#stack] = nil
  754.         if cx >= surf.x1 and cy >= surf.y1 and cx <= surf.x2 and cy <= surf.y2 then
  755.             local cchar, cbackcolor, ctextcolor = surf.buffer[((cy - 1) * surf.width + cx) * 3 - 2], surf.buffer[((cy - 1) * surf.width + cx) * 3 - 1], surf.buffer[((cy - 1) * surf.width + cx) * 3]
  756.             if (tchar == cchar) and (tbackcolor == cbackcolor) and (ttextcolor == ctextcolor) then
  757.                 if char or surf.overwrite then
  758.                     surf.buffer[((cy - 1) * surf.width + cx) * 3 - 2] = char
  759.                 end
  760.                 if backcolor or surf.overwrite then
  761.                     surf.buffer[((cy - 1) * surf.width + cx) * 3 - 1] = backcolor
  762.                 end
  763.                 if textcolor or surf.overwrite then
  764.                     surf.buffer[((cy - 1) * surf.width + cx) * 3] = textcolor
  765.                 end
  766.                 stack[#stack + 1] = cx - 1
  767.                 stack[#stack + 1] = cy
  768.                 stack[#stack + 1] = cx + 1
  769.                 stack[#stack + 1] = cy
  770.                 stack[#stack + 1] = cx
  771.                 stack[#stack + 1] = cy - 1
  772.                 stack[#stack + 1] = cx
  773.                 stack[#stack + 1] = cy + 1
  774.             end
  775.         end
  776.     end
  777. end,
  778.  
  779. drawSurface = function(surf, x, y, surf2)
  780.     for j=1,surf2.height do
  781.         for i=1,surf2.width do
  782.             surf:drawPixel(i + x - 1, j + y - 1, surf2.buffer[((j - 1) * surf2.width + i) * 3 - 2], surf2.buffer[((j - 1) * surf2.width + i) * 3 - 1], surf2.buffer[((j - 1) * surf2.width + i) * 3])
  783.         end
  784.     end
  785. end,
  786.  
  787. drawSurfacePart = function(surf, x, y, sx1, sy1, sx2, sy2, surf2)
  788.     if sx1 > sx2 then
  789.         local temp = sx1
  790.         sx1, sx2 = sx2, temp
  791.     end
  792.     if sy1 > sy2 then
  793.         local temp = sy1
  794.         sy1, sy2 = sy2, temp
  795.     end
  796.     if sx2 < 1 or sx1 > surf2.width or sy2 < 1 or sy1 > surf2.height then return end
  797.     if sx1 < 1 then sx1 = 1 end
  798.     if sx2 > surf2.width then sx2 = surf2.width end
  799.     if sy1 < 1 then sy1 = 1 end
  800.     if sy2 > surf2.height then sy2 = surf2.height end
  801.     for j=sy1,sy2 do
  802.         for i=sx1,sx2 do
  803.             surf:drawPixel(x + i - sx1, y + j - sy1, surf2.buffer[((j - 1) * surf2.width + i) * 3 - 2], surf2.buffer[((j - 1) * surf2.width + i) * 3 - 1], surf2.buffer[((j - 1) * surf2.width + i) * 3])
  804.         end
  805.     end
  806. end,
  807.  
  808. drawSurfaceScaled = function(surf, x1, y1, x2, y2, surf2)
  809.     local x, width, xinv, y, height, yinv = 0, 0, false, 0, 0, false
  810.     if x1 <= x2 then
  811.         x = x1
  812.         width = x2 - x1 + 1
  813.     else
  814.         x = x2
  815.         width = x1 - x2 + 1
  816.         xinv = true
  817.     end
  818.     if y1 <= y2 then
  819.         y = y1
  820.         height = y2 - y1 + 1
  821.     else
  822.         y = y2
  823.         height = y1 - y2 + 1
  824.         yinv = true
  825.     end
  826.     local xscale, yscale, px, py = width / surf2.width, height / surf2.height
  827.     for j=1,height do
  828.         for i=1,width do
  829.             if xinv then
  830.                 px = math_floor((width - i + 0.5) / xscale) + 1
  831.             else
  832.                 px = math_floor((i - 0.5) / xscale) + 1
  833.             end
  834.             if yinv then
  835.                 py = math_floor((height - j + 0.5) / yscale) + 1
  836.             else
  837.                 py = math_floor((j - 0.5) / yscale) + 1
  838.             end
  839.             surf:drawPixel(x + i - 1, y + j - 1, surf2.buffer[((py - 1) * surf2.width + px) * 3 - 2], surf2.buffer[((py - 1) * surf2.width + px) * 3 - 1], surf2.buffer[((py - 1) * surf2.width + px) * 3])
  840.         end
  841.     end
  842. end,
  843.  
  844. drawSurfaceRotated = function(surf, x, y, ox, oy, angle, surf2)
  845.     local cos, sin, range = math_cos(angle), math_sin(angle), math_floor(math.sqrt(surf2.width * surf2.width + surf2.height * surf2.height))
  846.     x, y = x - math_floor(cos * (ox - 1) + sin * (oy - 1) + 0.5), y - math_floor(cos * (oy - 1) - sin * (ox - 1) + 0.5)
  847.     for j=-range,range do
  848.         for i=-range,range do
  849.             local sx, sy = math_floor(i * cos - j * sin), math_floor(i * sin + j * cos)
  850.             if sx >= 0 and sx < surf2.width and sy >= 0 and sy < surf2.height then
  851.                 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])
  852.             end
  853.         end
  854.     end
  855. end,
  856.  
  857. shader = function(surf, f, x1, y1, x2, y2)
  858.     x1, y1, x2, y2 = x1 or surf.x1, y1 or surf.y1, x2 or surf.x2, y2 or surf.y2
  859.     if x1 > x2 then
  860.         local temp = x1
  861.         x1, x2 = x2, temp
  862.     end
  863.     if y1 > y2 then
  864.         local temp = y1
  865.         y1, y2 = y2, temp
  866.     end
  867.     if x2 < surf.x1 or x1 > surf.x2 or y2 < surf.y1 or y1 > surf.y2 then return end
  868.     if x1 < surf.x1 then x1 = surf.x1 end
  869.     if x2 > surf.x2 then x2 = surf.x2 end
  870.     if y1 < surf.y1 then y1 = surf.y1 end
  871.     if y2 > surf.y2 then y2 = surf.y2 end
  872.     local width, buffer = x2 - x1 + 1, { }
  873.     for j=y1,y2 do
  874.         for i=x1,x2 do
  875.             buffer[((j - y1) * width + i - x1) * 3 + 1], buffer[((j - y1) * width + i - x1) * 3 + 2], buffer[((j - y1) * width + i - x1) * 3 + 3] = f(surf.buffer[((j - 1) * surf.width + i) * 3 - 2], surf.buffer[((j - 1) * surf.width + i) * 3 - 1], surf.buffer[((j - 1) * surf.width + i) * 3], i, j)
  876.         end
  877.     end
  878.     for j=y1,y2 do
  879.         for i=x1,x2 do
  880.             surf.buffer[((j - 1) * surf.width + i) * 3 - 2], surf.buffer[((j - 1) * surf.width + i) * 3 - 1], surf.buffer[((j - 1) * surf.width + i) * 3] = buffer[((j - y1) * width + i - x1) * 3 + 1], buffer[((j - y1) * width + i - x1) * 3 + 2], buffer[((j - y1) * width + i - x1) * 3 + 3]
  881.         end
  882.     end
  883. end,
  884.  
  885. shift = function(surf, x, y, x1, y1, x2, y2)
  886.     x1, y1, x2, y2 = x1 or surf.x1, y1 or surf.y1, x2 or surf.x2, y2 or surf.y2
  887.     if x1 > x2 then
  888.         local temp = x1
  889.         x1, x2 = x2, temp
  890.     end
  891.     if y1 > y2 then
  892.         local temp = y1
  893.         y1, y2 = y2, temp
  894.     end
  895.     if x2 < surf.x1 or x1 > surf.x2 or y2 < surf.y1 or y1 > surf.y2 then return end
  896.     if x1 < surf.x1 then x1 = surf.x1 end
  897.     if x2 > surf.x2 then x2 = surf.x2 end
  898.     if y1 < surf.y1 then y1 = surf.y1 end
  899.     if y2 > surf.y2 then y2 = surf.y2 end
  900.     local width, buffer = x2 - x1 + 1, { }
  901.     for j=y1,y2 do
  902.         for i=x1,x2 do
  903.             if i - x >= 1 and j - y >= 1 and i - x <= surf.width and j - y <= surf.height then
  904.                 buffer[((j - y1) * width + i - x1) * 3 + 1], buffer[((j - y1) * width + i - x1) * 3 + 2], buffer[((j - y1) * width + i - x1) * 3 + 3] = surf.buffer[((j - y - 1) * surf.width + i - x) * 3 - 2], surf.buffer[((j - y - 1) * surf.width + i - x) * 3 - 1], surf.buffer[((j - y - 1) * surf.width + i - x) * 3]
  905.             end
  906.         end
  907.     end
  908.     for j=y1,y2 do
  909.         for i=x1,x2 do
  910.             surf.buffer[((j - 1) * surf.width + i) * 3 - 2], surf.buffer[((j - 1) * surf.width + i) * 3 - 1], surf.buffer[((j - 1) * surf.width + i) * 3] = buffer[((j - y1) * width + i - x1) * 3 + 1], buffer[((j - y1) * width + i - x1) * 3 + 2], buffer[((j - y1) * width + i - x1) * 3 + 3]
  911.         end
  912.     end
  913. end
  914. }
  915.  
  916. function create(width, height, char, backcolor, textcolor)
  917.     local surf = { }
  918.     for k,v in pairs(_functions) do
  919.         surf[k] = v
  920.     end
  921.     surf.width, surf.height, surf.x1, surf.y1, surf.x2, surf.y2, surf.curX, surf.curY, surf.overwrite, surf.buffer = width, height, 1, 1, width, height, 1, 1, false, { }
  922.     if char then
  923.         for i=1,width * height do
  924.             surf.buffer[i * 3 - 2] = char
  925.         end
  926.     end
  927.     if backcolor then
  928.         for i=1,width * height do
  929.             surf.buffer[i * 3 - 1] = backcolor
  930.         end
  931.     end
  932.     if textcolor then
  933.         for i=1,width * height do
  934.             surf.buffer[i * 3] = textcolor
  935.         end
  936.     end
  937.     return surf
  938. end
  939.  
  940. function load(path)
  941.     local lines, f = { }, fs.open(path, "r")
  942.     for line in f.readLine do
  943.         lines[#lines + 1] = line
  944.     end
  945.     f.close()
  946.     local height = #lines
  947.     if lines[1]:byte(1) == 30 then
  948.         local width, i = 0, 1
  949.         while i <= #lines[1] do
  950.             local char = lines[1]:byte(i)
  951.             if char == 30 or char == 31 then
  952.                 i = i + 1
  953.             else
  954.                 width = width + 1
  955.             end
  956.             i = i + 1
  957.         end
  958.         local surf, backcolor, textcolor, i, px, char, color = create(width, height)
  959.         for j=1,height do
  960.             i = 1
  961.             px = 1
  962.             while i <= #lines[j] do
  963.                 char = lines[j]:byte(i)
  964.                 if char == 30 then
  965.                     i = i + 1
  966.                     char = lines[j]:byte(i)
  967.                     color = tonumber(lines[j]:sub(i, i), 16)
  968.                     if color then
  969.                         backcolor = 2^color
  970.                     else
  971.                         backcolor = nil
  972.                     end
  973.                 elseif char == 31 then
  974.                     i = i + 1
  975.                     char = lines[j]:byte(i)
  976.                     color = tonumber(lines[j]:sub(i, i), 16)
  977.                     if color then
  978.                         textcolor = 2^color
  979.                     else
  980.                         textcolor = nil
  981.                     end
  982.                 else
  983.                     surf.buffer[((j - 1) * surf.width + px) * 3 - 2] = lines[j]:sub(i, i)
  984.                     surf.buffer[((j - 1) * surf.width + px) * 3 - 1] = backcolor
  985.                     surf.buffer[((j - 1) * surf.width + px) * 3] = textcolor
  986.                     px = px + 1
  987.                 end
  988.                 i = i + 1
  989.             end
  990.         end
  991.         return surf
  992.     elseif lines[1]:byte(1) == 95 then
  993.         return loadString(lines[1])
  994.     else
  995.         local width = 0
  996.         for i=1,#lines do
  997.             if #lines[i] > width then
  998.                 width = #lines[i]
  999.             end
  1000.         end
  1001.         local surf, color = create(width, height)
  1002.         for j=1,height do
  1003.             for i=1,width do
  1004.                 color = tonumber(lines[j]:sub(i, i), 16)
  1005.                 if color then
  1006.                     surf.buffer[((j - 1) * surf.width + i) * 3 - 1] = 2 ^ color
  1007.                 end
  1008.             end
  1009.         end
  1010.         return surf
  1011.     end
  1012. end
  1013.  
  1014. function loadString(str)
  1015.     local width, height, n = tonumber(str:sub(2, 5), 16), tonumber(str:sub(6, 9), 16), 10
  1016.     local surf = create(width, height)
  1017.     for j=1,height do
  1018.         for i=1,width do
  1019.             if str:byte(n) ~= 95 then
  1020.                 surf.buffer[((j - 1) * surf.width + i) * 3 - 2] = string.char(tonumber(str:sub(n, n + 1), 16))
  1021.             end
  1022.             if str:byte(n + 2) ~= 95 then
  1023.                 surf.buffer[((j - 1) * surf.width + i) * 3 - 1] = 2 ^ tonumber(str:sub(n + 2, n + 2), 16)
  1024.             end
  1025.             if str:byte(n + 3) ~= 95 then
  1026.                 surf.buffer[((j - 1) * surf.width + i) * 3] = 2 ^ tonumber(str:sub(n + 3, n + 3), 16)
  1027.             end
  1028.             n = n + 4
  1029.         end
  1030.     end
  1031.     return surf
  1032. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement