Advertisement
CrazedProgrammer

Surface API 1.4.0

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