Advertisement
CrazedProgrammer

Surface API

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