Advertisement
CrazedProgrammer

Surface API 1.5.3

May 1st, 2015
9,801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 34.35 KB | None | 0 0
  1. -- Surface API version 1.5.3 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. version = "1.5.3"
  8.  
  9. 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"}
  10.  
  11. local _bufferLine = function(buffer, width, x1, y1, x2, y2)
  12.     local delta_x = x2 - x1
  13.     local ix = delta_x > 0 and 1 or -1
  14.     delta_x = 2 * math.abs(delta_x)
  15.     local delta_y = y2 - y1
  16.     local iy = delta_y > 0 and 1 or -1
  17.     delta_y = 2 * math.abs(delta_y)
  18.     buffer[(y1 - 1) * width + x1] = true
  19.     if delta_x >= delta_y then
  20.         local error = delta_y - delta_x / 2
  21.         while x1 ~= x2 do
  22.             if (error >= 0) and ((error ~= 0) or (ix > 0)) then
  23.                 error = error - delta_x
  24.                 y1 = y1 + iy
  25.             end
  26.             error = error + delta_y
  27.             x1 = x1 + ix
  28.             buffer[(y1 - 1) * width + x1] = true
  29.         end
  30.     else
  31.         local error = delta_x - delta_y / 2
  32.         while y1 ~= y2 do
  33.             if (error >= 0) and ((error ~= 0) or (iy > 0)) then
  34.                 error = error - delta_y
  35.                 x1 = x1 + ix
  36.             end
  37.             error = error + delta_x
  38.             y1 = y1 + iy
  39.             buffer[(y1 - 1) * width + x1] = true
  40.         end
  41.     end
  42. end
  43.  
  44. local _drawHLine = function(surf, x1, x2, y, char, backcolor, textcolor)
  45.     if x1 > x2 then
  46.         local temp = x1
  47.         x1, x2 = x2, temp
  48.     end
  49.     if y < surf.y1 or y > surf.y2 or x2 < surf.x1 or x1 > surf.x2 then return end
  50.     if x1 < surf.x1 then x1 = surf.x1 end
  51.     if x2 > surf.x2 then x2 = surf.x2 end
  52.     if char or surf.overwrite then
  53.         for x=x1,x2 do
  54.             surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = char
  55.         end
  56.     end
  57.     if backcolor or surf.overwrite then
  58.         for x=x1,x2 do
  59.             surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  60.         end
  61.     end
  62.     if textcolor or surf.overwrite then
  63.         for x=x1,x2 do
  64.             surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  65.         end
  66.     end
  67. end
  68.  
  69. local _drawVLine = function(surf, y1, y2, x, char, backcolor, textcolor)
  70.     if y1 > y2 then
  71.         local temp = y1
  72.         y1, y2 = y2, temp
  73.     end
  74.     if x < surf.x1 or x > surf.x2 or y2 < surf.y1 or y1 > surf.y2 then return end
  75.     if y1 < surf.y1 then y1 = surf.y1 end
  76.     if y2 > surf.y2 then y2 = surf.y2 end
  77.     if char or surf.overwrite then
  78.         for y=y1,y2 do
  79.             surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = char
  80.         end
  81.     end
  82.     if backcolor or surf.overwrite then
  83.         for y=y1,y2 do
  84.             surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  85.         end
  86.     end
  87.     if textcolor or surf.overwrite then
  88.         for y=y1,y2 do
  89.             surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  90.         end
  91.     end
  92. end
  93.  
  94. local _functions = {
  95. setBounds = function(surf, x1, y1, x2, y2)
  96.     if x1 > x2 then
  97.         local temp = x1
  98.         x1, x2 = x2, temp
  99.     end
  100.     if y1 > y2 then
  101.         local temp = y1
  102.         y1, y2 = y2, temp
  103.     end
  104.     if x2 < 1 or x1 > surf.width or y2 < 1 or y1 > surf.height then return end
  105.     if x1 < 1 then x1 = 1 end
  106.     if x2 > surf.width then x2 = surf.width end
  107.     if y1 < 1 then y1 = 1 end
  108.     if y2 > surf.height then y2 = surf.height end
  109.     surf.x1, surf.y1, surf.x2, surf.y2 = x1, y1, x2, y2
  110. end,
  111.  
  112. getBounds = function(surf)
  113.     return surf.x1, surf.y1, surf.x2, surf.y2
  114. end,
  115.  
  116. copy = function(surf)
  117.     local surf2 = surface.create(surf.width, surf.height)
  118.     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
  119.     for i=1,surf.width * surf.height * 3 do
  120.         surf2.buffer[i] = surf.buffer[i]
  121.     end
  122.     return surf2
  123. end,
  124.  
  125. save = function(surf, path, type)
  126.     type = type or "srf"
  127.     local f = fs.open(path, "w")
  128.     if type == "nfp" then
  129.         local color = nil
  130.         for j=1,surf.height do
  131.             if j > 1 then f.write("\n") end
  132.             for i=1,surf.width do
  133.                 color = surf.buffer[((j - 1) * surf.width + i) * 3 - 1]
  134.                 if color then
  135.                     f.write(_colors[color])
  136.                 else
  137.                     f.write(" ")
  138.                 end
  139.             end
  140.         end
  141.     elseif type == "nft" then
  142.         local backcolor, textcolor, char = nil
  143.         for j=1,surf.height,1 do
  144.             if j > 1 then f.write("\n") end
  145.             backcolor = nil
  146.             textcolor = nil
  147.             for i=1,surf.width do
  148.                 if backcolor ~= surf.buffer[((j - 1) * surf.width + i) * 3 - 1] then
  149.                     f.write(string.char(30))
  150.                     backcolor = surf.buffer[((j - 1) * surf.width + i) * 3 - 1]
  151.                     if backcolor then
  152.                         f.write(_colors[backcolor])
  153.                     else
  154.                         f.write(" ")
  155.                     end
  156.                 end
  157.                 if textcolor ~= surf.buffer[((j - 1) * surf.width + i) * 3] then
  158.                     f.write(string.char(31))
  159.                     textcolor = surf.buffer[((j - 1) * surf.width + i) * 3]
  160.                     if textcolor then
  161.                         f.write(_colors[textcolor])
  162.                     else
  163.                         f.write(" ")
  164.                     end
  165.                 end
  166.                 char = surf.buffer[((j - 1) * surf.width + i) * 3 - 2]
  167.                 if char then
  168.                     f.write(char)
  169.                 else
  170.                     f.write(" ")
  171.                 end
  172.             end
  173.         end
  174.     elseif type == "srf" then
  175.         f.write(surf:saveString())
  176.     end
  177.     f.close()
  178. end,
  179.  
  180. saveString = function(surf)
  181.     local str = { "_"..string.format("%04x", surf.width)..string.format("%04x", surf.height) }
  182.     for j=1,surf.height do
  183.         for i=1,surf.width do
  184.             if surf.buffer[((j - 1) * surf.width + i) * 3 - 2] then
  185.                 str[#str + 1] = string.format("%02x", surf.buffer[((j - 1) * surf.width + i) * 3 - 2]:byte(1))
  186.             else
  187.                 str[#str + 1] = "__"
  188.             end
  189.             if surf.buffer[((j - 1) * surf.width + i) * 3 - 1] then
  190.                 str[#str + 1] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3 - 1]]
  191.             else
  192.                 str[#str + 1] = "_"
  193.             end
  194.             if surf.buffer[((j - 1) * surf.width + i) * 3] then
  195.                 str[#str + 1] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3]]
  196.             else
  197.                 str[#str + 1] = "_"
  198.             end
  199.         end
  200.     end
  201.     return table_concat(str)
  202. end,
  203.  
  204. getTerm = function(surf)
  205.     local term, textcolor, backcolor = { }
  206.     function term.write(str)
  207.         surf:drawText(surf.curX, surf.curY, str, backcolor, textcolor)
  208.         surf.curX = surf.curX + #str
  209.     end
  210.     function term.blit(str, text, back)
  211.         for i=1,#str do
  212.             if surf.curX >= surf.x1 and surf.curY >= surf.y1 and surf.curX <= surf.x2 and surf.curY <= surf.y2 then
  213.                 surf.buffer[((surf.curY - 1) * surf.width + surf.curX) * 3 - 2] = str:sub(i, i)
  214.                 surf.buffer[((surf.curY - 1) * surf.width + surf.curX) * 3 - 1] = 2 ^ tonumber(back:sub(i, i), 16)
  215.                 surf.buffer[((surf.curY - 1) * surf.width + surf.curX) * 3] = 2 ^ tonumber(text:sub(i, i), 16)
  216.             end
  217.             surf.curX = surf.curX + 1
  218.         end
  219.     end
  220.     function term.clear()
  221.         surf:clear(" ", backcolor, textcolor)
  222.     end
  223.     function term.clearLine(n)
  224.         _drawHLine(surf, surf.x1, surf.x2, n)
  225.     end
  226.     function term.getCursorPos()
  227.         return surf.curX, surf.curY
  228.     end
  229.     function term.setCursorPos(x, y)
  230.         surf.curX, surf.curY = math_floor(x), math_floor(y)
  231.     end
  232.     function term.setCursorBlink(blink)
  233.         surf.blink = blink
  234.     end
  235.     function term.isColor()
  236.         return true
  237.     end
  238.     term.isColour = term.isColor
  239.     function term.setTextColor(color)
  240.         textcolor = color
  241.     end
  242.     term.setTextColour = term.setTextColor
  243.     function term.setBackgroundColor(color)
  244.         backcolor = color
  245.     end
  246.     term.setBackgroundColour = term.setBackgroundColor
  247.     function term.getSize()
  248.         return surf.width, surf.height
  249.     end
  250.     function term.scroll(n)
  251.         surf:shift(0, n)
  252.     end
  253.     function term.getTextColor()
  254.         return textcolor
  255.     end
  256.     term.getTextColour = term.getTextColor
  257.     function term.getBackgroundColor()
  258.         return backcolor
  259.     end
  260.     term.getBackgroundColour = term.getBackgroundColor
  261.     return term
  262. end,
  263.  
  264. render = function(surf, display, x, y, sx1, sy1, sx2, sy2)
  265.     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
  266.     if sx1 > sx2 then
  267.         local temp = sx1
  268.         sx1, sx2 = sx2, temp
  269.     end
  270.     if sy1 > sy2 then
  271.         local temp = sy1
  272.         sy1, sy2 = sy2, temp
  273.     end
  274.     if sx2 < 1 or sx1 > surf.width or sy2 < 1 or sy1 > surf.height then return end
  275.     if sx1 < 1 then sx1 = 1 end
  276.     if sx2 > surf.width then sx2 = surf.width end
  277.     if sy1 < 1 then sy1 = 1 end
  278.     if sy2 > surf.height then sy2 = surf.height end
  279.     if display.blit then
  280.         local cmd, str, back, text = { }, { }, { }, { }
  281.         for j=sy1,sy2 do
  282.             for i=sx1,sx2 do
  283.                 str[i - sx1 + 1] = surf.buffer[((j - 1) * surf.width + i) * 3 - 2] or " "
  284.                 back[i - sx1 + 1] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3 - 1] or 32768]
  285.                 text[i - sx1 + 1] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3] or 1]
  286.             end
  287.             cmd[#cmd + 1] = y + j - sy1
  288.             cmd[#cmd + 1] = table_concat(str)
  289.             cmd[#cmd + 1] = table_concat(text)
  290.             cmd[#cmd + 1] = table_concat(back)
  291.         end
  292.         for i=1,#cmd,4 do
  293.             display.setCursorPos(x, cmd[i])
  294.             display.blit(cmd[i + 1], cmd[i + 2], cmd[i + 3])
  295.         end
  296.         return #cmd / 2
  297.     else
  298.         local cmd, str, backcolor, textcolor, backc, textc = { }, "", 0, 0
  299.         for j=sy1,sy2 do
  300.             cmd[#cmd + 1] = 1
  301.             cmd[#cmd + 1] = y + j - sy1
  302.             for i=sx1,sx2 do
  303.                 backc, textc = (surf.buffer[((j - 1) * surf.width + i) * 3 - 1] or 32768), (surf.buffer[((j - 1) * surf.width + i) * 3] or 1)
  304.                 if backc ~= backcolor then
  305.                     backcolor = backc
  306.                     if str ~= "" then
  307.                         cmd[#cmd + 1] = 4
  308.                         cmd[#cmd + 1] = str
  309.                         str = ""
  310.                     end
  311.                     cmd[#cmd + 1] = 2
  312.                     cmd[#cmd + 1] = backcolor
  313.                 end
  314.                 if textc ~= textcolor then
  315.                     textcolor = textc
  316.                     if str ~= "" then
  317.                         cmd[#cmd + 1] = 4
  318.                         cmd[#cmd + 1] = str
  319.                         str = ""
  320.                     end
  321.                     cmd[#cmd + 1] = 3
  322.                     cmd[#cmd + 1] = textcolor
  323.                 end
  324.                 str = str..(surf.buffer[((j - 1) * surf.width + i) * 3 - 2] or " ")
  325.             end
  326.             cmd[#cmd + 1] = 4
  327.             cmd[#cmd + 1] = str
  328.             str = ""
  329.         end
  330.         local c, a = nil, nil
  331.         for i=1,#cmd,2 do
  332.             c, a = cmd[i], cmd[i + 1]
  333.             if c == 1 then
  334.                 display.setCursorPos(x, a)
  335.             elseif c == 2 then
  336.                 display.setBackgroundColor(a)
  337.             elseif c == 3 then
  338.                 display.setTextColor(a)
  339.             else
  340.                 display.write(a)
  341.             end
  342.         end
  343.         return #cmd / 2
  344.     end
  345.     if surf.blink then
  346.         display.setCursorPos(x + surf.curX - sx1, y + surf.curY - sy1)
  347.         display.setCursorBlink(true)
  348.     elseif surf.blink == false then
  349.         display.setCursorBlink(false)
  350.         surf.blink = nil
  351.     end
  352. end,
  353.  
  354. clear = function(surf, char, backcolor, textcolor)
  355.     local overwrite = surf.overwrite
  356.     surf.overwrite = true
  357.     surf:fillRect(surf.x1, surf.y1, surf.x2, surf.y2, char, backcolor, textcolor)
  358.     surf.overwrite = overwrite
  359. end,
  360.  
  361. drawPixel = function(surf, x, y, char, backcolor, textcolor)
  362.     if x < surf.x1 or y < surf.y1 or x > surf.x2 or y > surf.y2 then return end
  363.     if char or surf.overwrite then
  364.         surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = char
  365.     end
  366.     if backcolor or surf.overwrite then
  367.         surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  368.     end
  369.     if textcolor or surf.overwrite then
  370.         surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  371.     end
  372. end,
  373.  
  374. getPixel = function(surf, x, y)
  375.     if x < 1 or y < 1 or x > surf.width or y > surf.height then return end
  376.     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]
  377. end,
  378.  
  379. drawText = function(surf, x, y, text, backcolor, textcolor)
  380.     local px = x
  381.     for i=1,#text,1 do
  382.         if text:sub(i, i) ~= "\n" then
  383.             if x >= surf.x1 and y >= surf.y1 and x <= surf.x2 and y <= surf.y2 then
  384.                 surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = text:sub(i, i)
  385.                 if backcolor or surf.overwrite then
  386.                     surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  387.                 end
  388.                 if textcolor or surf.overwrite then
  389.                     surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  390.                 end
  391.             end
  392.         else
  393.             x = px - 1
  394.             y = y + 1
  395.         end
  396.         x = x + 1
  397.     end
  398. end,
  399.  
  400. drawLine = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  401.     local delta_x = x2 - x1
  402.     local ix = delta_x > 0 and 1 or -1
  403.     delta_x = 2 * math.abs(delta_x)
  404.     local delta_y = y2 - y1
  405.     local iy = delta_y > 0 and 1 or -1
  406.     delta_y = 2 * math.abs(delta_y)
  407.     surf:drawPixel(x1, y1, char, backcolor, textcolor)
  408.     if delta_x >= delta_y then
  409.         local error = delta_y - delta_x / 2
  410.         while x1 ~= x2 do
  411.             if (error >= 0) and ((error ~= 0) or (ix > 0)) then
  412.                 error = error - delta_x
  413.                 y1 = y1 + iy
  414.             end
  415.             error = error + delta_y
  416.             x1 = x1 + ix
  417.             surf:drawPixel(x1, y1, char, backcolor, textcolor)
  418.         end
  419.     else
  420.         local error = delta_x - delta_y / 2
  421.         while y1 ~= y2 do
  422.             if (error >= 0) and ((error ~= 0) or (iy > 0)) then
  423.                 error = error - delta_y
  424.                 x1 = x1 + ix
  425.             end
  426.             error = error + delta_x
  427.             y1 = y1 + iy
  428.             surf:drawPixel(x1, y1, char, backcolor, textcolor)
  429.         end
  430.     end
  431. end,
  432.  
  433. drawLines = function(surf, points, mode, char, backcolor, textcolor)
  434.     mode = mode or 1
  435.     if mode == 1 then
  436.         for i=1,#points,4 do
  437.             surf:drawLine(points[i], points[i+1], points[i+2], points[i+3], char, backcolor, textcolor)
  438.         end
  439.     elseif mode == 2 then
  440.         local lastx, lasty = points[1], points[2]
  441.         for i=3,#points,2 do
  442.             local curx, cury = points[i], points[i+1]
  443.             surf:drawLine(lastx, lasty, curx, cury, char, backcolor, textcolor)
  444.             lastx = curx
  445.             lasty = cury
  446.         end
  447.     elseif mode == 3 then
  448.         local midx, midy = points[1], points[2]
  449.         for i=3,#points,2 do
  450.             surf:drawLine(midx, midy, points[i], points[i+1], char, backcolor, textcolor)
  451.         end
  452.     end
  453. end,
  454.  
  455. drawRect = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  456.     _drawHLine(surf, x1, x2, y1, char, backcolor, textcolor)
  457.     _drawHLine(surf, x1, x2, y2, char, backcolor, textcolor)
  458.     _drawVLine(surf, y1, y2, x1, char, backcolor, textcolor)
  459.     _drawVLine(surf, y1, y2, x2, char, backcolor, textcolor)
  460. end,
  461.  
  462. drawRoundRect = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  463.     _drawHLine(surf, x1 + 1, x2 - 1, y1, char, backcolor, textcolor)
  464.     _drawHLine(surf, x1 + 1, x2 - 1, y2, char, backcolor, textcolor)
  465.     _drawVLine(surf, y1 + 1, y2 - 1, x1, char, backcolor, textcolor)
  466.     _drawVLine(surf, y1 + 1, y2 - 1, x2, char, backcolor, textcolor)
  467. end,
  468.  
  469. drawRoundedRect = function(surf, x1, y1, x2, y2, radius, char, backcolor, textcolor)
  470.     _drawHLine(surf, x1 + radius, x2 - radius, y1, char, backcolor, textcolor)
  471.     _drawHLine(surf, x1 + radius, x2 - radius, y2, char, backcolor, textcolor)
  472.     _drawVLine(surf, y1 + radius, y2 - radius, x1, char, backcolor, textcolor)
  473.     _drawVLine(surf, 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.     _drawHLine(surf, x1 + 1, x2 - 1, y1, char, backcolor, textcolor)
  519.     _drawHLine(surf, x1 + 1, x2 - 1, y2, char, backcolor, textcolor)
  520.     _drawVLine(surf, y1 + 1, y2 - 1, x1, char, backcolor, textcolor)
  521.     _drawVLine(surf, 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, 0
  557.         for i=1,width,1 do
  558.             if buffer[(j - 1) * width + i] then
  559.                 if not min then min = i end
  560.                 if i > max then max = i end
  561.             end
  562.         end
  563.         _drawHLine(surf, 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,1 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,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, 0
  651.         for i=1,bwidth,1 do
  652.             if buffer[(j - 1) * bwidth + i] then
  653.                 if not min then min = i end
  654.                 if i > max then max = i end
  655.             end
  656.         end
  657.         _drawHLine(surf, 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,1 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,1 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,1 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, 0
  734.         for i=1,bwidth,1 do
  735.             if buffer[(j - 1) * bwidth + i] then
  736.                 if not min then min = i end
  737.                 if i > max then max = i end
  738.             end
  739.         end
  740.         if min then
  741.             _drawHLine(surf, 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,1 do
  781.         for i=1,surf2.width,1 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,1 do
  802.         for i=sx1,sx2,1 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,1 do
  828.         for i=1,width,1 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,1 do
  848.         for i=-range,range,1 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.blink, surf.curX, surf.curY, surf.overwrite, surf.buffer = width, height, 1, 1, width, height, nil, 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,1 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