Advertisement
CrazedProgrammer

Surface API 1.5.3

May 1st, 2015
9,370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 34.35 KB | None | 0 0
  1. -- Surface API version 1.5.3 by CrazedProgrammer
  2. -- You can find info and documentation on these pages:
  3. -- http://www.computercraft.info/forums2/index.php?/topic/22397-surface-api/
  4. -- You may use this in your ComputerCraft programs and modify it without asking.
  5. -- However, you may not publish this API under your name without asking me.
  6. -- If you have any suggestions, bug reports or questions then please send an email to:
  7. -- crazedprogrammer@gmail.com
  8. version = "1.5.3"
  9.  
  10. local math_floor, math_cos, math_sin, table_concat, _colors = math.floor, math.cos, math.sin, table.concat, {[1] = "0", [2] = "1", [4] = "2", [8] = "3", [16] = "4", [32] = "5", [64] = "6", [128] = "7", [256] = "8", [512] = "9", [1024] = "a", [2048] = "b", [4096] = "c", [8192] = "d", [16384] = "e", [32768] = "f"}
  11.  
  12. local _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(_colors[color])
  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(_colors[backcolor])
  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(_colors[textcolor])
  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] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3 - 1]]
  192.             else
  193.                 str[#str + 1] = "_"
  194.             end
  195.             if surf.buffer[((j - 1) * surf.width + i) * 3] then
  196.                 str[#str + 1] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3]]
  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] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3 - 1] or 32768]
  286.                 text[i - sx1 + 1] = _colors[surf.buffer[((j - 1) * surf.width + i) * 3] or 1]
  287.             end
  288.             cmd[#cmd + 1] = y + j - sy1
  289.             cmd[#cmd + 1] = table_concat(str)
  290.             cmd[#cmd + 1] = table_concat(text)
  291.             cmd[#cmd + 1] = table_concat(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.         return #cmd / 2
  298.     else
  299.         local cmd, str, backcolor, textcolor, backc, textc = { }, "", 0, 0
  300.         for j=sy1,sy2 do
  301.             cmd[#cmd + 1] = 1
  302.             cmd[#cmd + 1] = y + j - sy1
  303.             for i=sx1,sx2 do
  304.                 backc, textc = (surf.buffer[((j - 1) * surf.width + i) * 3 - 1] or 32768), (surf.buffer[((j - 1) * surf.width + i) * 3] or 1)
  305.                 if backc ~= backcolor then
  306.                     backcolor = backc
  307.                     if str ~= "" then
  308.                         cmd[#cmd + 1] = 4
  309.                         cmd[#cmd + 1] = str
  310.                         str = ""
  311.                     end
  312.                     cmd[#cmd + 1] = 2
  313.                     cmd[#cmd + 1] = backcolor
  314.                 end
  315.                 if textc ~= textcolor then
  316.                     textcolor = textc
  317.                     if str ~= "" then
  318.                         cmd[#cmd + 1] = 4
  319.                         cmd[#cmd + 1] = str
  320.                         str = ""
  321.                     end
  322.                     cmd[#cmd + 1] = 3
  323.                     cmd[#cmd + 1] = textcolor
  324.                 end
  325.                 str = str..(surf.buffer[((j - 1) * surf.width + i) * 3 - 2] or " ")
  326.             end
  327.             cmd[#cmd + 1] = 4
  328.             cmd[#cmd + 1] = str
  329.             str = ""
  330.         end
  331.         local c, a = nil, nil
  332.         for i=1,#cmd,2 do
  333.             c, a = cmd[i], cmd[i + 1]
  334.             if c == 1 then
  335.                 display.setCursorPos(x, a)
  336.             elseif c == 2 then
  337.                 display.setBackgroundColor(a)
  338.             elseif c == 3 then
  339.                 display.setTextColor(a)
  340.             else
  341.                 display.write(a)
  342.             end
  343.         end
  344.         return #cmd / 2
  345.     end
  346.     if surf.blink then
  347.         display.setCursorPos(x + surf.curX - sx1, y + surf.curY - sy1)
  348.         display.setCursorBlink(true)
  349.     elseif surf.blink == false then
  350.         display.setCursorBlink(false)
  351.         surf.blink = nil
  352.     end
  353. end,
  354.  
  355. clear = function(surf, char, backcolor, textcolor)
  356.     local overwrite = surf.overwrite
  357.     surf.overwrite = true
  358.     surf:fillRect(surf.x1, surf.y1, surf.x2, surf.y2, char, backcolor, textcolor)
  359.     surf.overwrite = overwrite
  360. end,
  361.  
  362. drawPixel = function(surf, x, y, char, backcolor, textcolor)
  363.     if x < surf.x1 or y < surf.y1 or x > surf.x2 or y > surf.y2 then return end
  364.     if char or surf.overwrite then
  365.         surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = char
  366.     end
  367.     if backcolor or surf.overwrite then
  368.         surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  369.     end
  370.     if textcolor or surf.overwrite then
  371.         surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  372.     end
  373. end,
  374.  
  375. getPixel = function(surf, x, y)
  376.     if x < 1 or y < 1 or x > surf.width or y > surf.height then return end
  377.     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]
  378. end,
  379.  
  380. drawText = function(surf, x, y, text, backcolor, textcolor)
  381.     local px = x
  382.     for i=1,#text,1 do
  383.         if text:sub(i, i) ~= "\n" then
  384.             if x >= surf.x1 and y >= surf.y1 and x <= surf.x2 and y <= surf.y2 then
  385.                 surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = text:sub(i, i)
  386.                 if backcolor or surf.overwrite then
  387.                     surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  388.                 end
  389.                 if textcolor or surf.overwrite then
  390.                     surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  391.                 end
  392.             end
  393.         else
  394.             x = px - 1
  395.             y = y + 1
  396.         end
  397.         x = x + 1
  398.     end
  399. end,
  400.  
  401. drawLine = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  402.     local delta_x = x2 - x1
  403.     local ix = delta_x > 0 and 1 or -1
  404.     delta_x = 2 * math.abs(delta_x)
  405.     local delta_y = y2 - y1
  406.     local iy = delta_y > 0 and 1 or -1
  407.     delta_y = 2 * math.abs(delta_y)
  408.     surf:drawPixel(x1, y1, char, backcolor, textcolor)
  409.     if delta_x >= delta_y then
  410.         local error = delta_y - delta_x / 2
  411.         while x1 ~= x2 do
  412.             if (error >= 0) and ((error ~= 0) or (ix > 0)) then
  413.                 error = error - delta_x
  414.                 y1 = y1 + iy
  415.             end
  416.             error = error + delta_y
  417.             x1 = x1 + ix
  418.             surf:drawPixel(x1, y1, char, backcolor, textcolor)
  419.         end
  420.     else
  421.         local error = delta_x - delta_y / 2
  422.         while y1 ~= y2 do
  423.             if (error >= 0) and ((error ~= 0) or (iy > 0)) then
  424.                 error = error - delta_y
  425.                 x1 = x1 + ix
  426.             end
  427.             error = error + delta_x
  428.             y1 = y1 + iy
  429.             surf:drawPixel(x1, y1, char, backcolor, textcolor)
  430.         end
  431.     end
  432. end,
  433.  
  434. drawLines = function(surf, points, mode, char, backcolor, textcolor)
  435.     mode = mode or 1
  436.     if mode == 1 then
  437.         for i=1,#points,4 do
  438.             surf:drawLine(points[i], points[i+1], points[i+2], points[i+3], char, backcolor, textcolor)
  439.         end
  440.     elseif mode == 2 then
  441.         local lastx, lasty = points[1], points[2]
  442.         for i=3,#points,2 do
  443.             local curx, cury = points[i], points[i+1]
  444.             surf:drawLine(lastx, lasty, curx, cury, char, backcolor, textcolor)
  445.             lastx = curx
  446.             lasty = cury
  447.         end
  448.     elseif mode == 3 then
  449.         local midx, midy = points[1], points[2]
  450.         for i=3,#points,2 do
  451.             surf:drawLine(midx, midy, points[i], points[i+1], char, backcolor, textcolor)
  452.         end
  453.     end
  454. end,
  455.  
  456. drawRect = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  457.     _drawHLine(surf, x1, x2, y1, char, backcolor, textcolor)
  458.     _drawHLine(surf, x1, x2, y2, char, backcolor, textcolor)
  459.     _drawVLine(surf, y1, y2, x1, char, backcolor, textcolor)
  460.     _drawVLine(surf, y1, y2, x2, char, backcolor, textcolor)
  461. end,
  462.  
  463. drawRoundRect = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  464.     _drawHLine(surf, x1 + 1, x2 - 1, y1, char, backcolor, textcolor)
  465.     _drawHLine(surf, x1 + 1, x2 - 1, y2, char, backcolor, textcolor)
  466.     _drawVLine(surf, y1 + 1, y2 - 1, x1, char, backcolor, textcolor)
  467.     _drawVLine(surf, y1 + 1, y2 - 1, x2, char, backcolor, textcolor)
  468. end,
  469.  
  470. drawRoundedRect = function(surf, x1, y1, x2, y2, radius, char, backcolor, textcolor)
  471.     _drawHLine(surf, x1 + radius, x2 - radius, y1, char, backcolor, textcolor)
  472.     _drawHLine(surf, x1 + radius, x2 - radius, y2, char, backcolor, textcolor)
  473.     _drawVLine(surf, y1 + radius, y2 - radius, x1, char, backcolor, textcolor)
  474.     _drawVLine(surf, y1 + radius, y2 - radius, x2, char, backcolor, textcolor)
  475.     surf:drawArc(x1, y1, x1 + radius * 2 + 2, y1 + radius * 2 + 2, -math.pi, -math.pi / 2, char, backcolor, textcolor)
  476.     surf:drawArc(x2, y1, x2 - radius * 2 - 2, y1 + radius * 2 + 2, 0, -math.pi / 2, char, backcolor, textcolor)
  477.     surf:drawArc(x1, y2, x1 + radius * 2 + 2, y2 - radius * 2 - 2, math.pi, math.pi / 2, char, backcolor, textcolor)
  478.     surf:drawArc(x2, y2, x2 - radius * 2 - 2, y2 - radius * 2 - 2, 0, math.pi / 2, char, backcolor, textcolor)
  479. end,
  480.  
  481. fillRect = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  482.     if x1 > x2 then
  483.         local temp = x1
  484.         x1, x2 = x2, temp
  485.     end
  486.     if y1 > y2 then
  487.         local temp = y1
  488.         y1, y2 = y2, temp
  489.     end
  490.     if x2 < surf.x1 or x1 > surf.x2 or y2 < surf.y1 or y1 > surf.y2 then return end
  491.     if x1 < surf.x1 then x1 = surf.x1 end
  492.     if x2 > surf.x2 then x2 = surf.x2 end
  493.     if y1 < surf.y1 then y1 = surf.y1 end
  494.     if y2 > surf.y2 then y2 = surf.y2 end
  495.     if char or surf.overwrite then
  496.         for y=y1,y2 do
  497.             for x=x1,x2 do
  498.                 surf.buffer[((y - 1) * surf.width + x) * 3 - 2] = char
  499.             end
  500.         end
  501.     end
  502.     if backcolor or surf.overwrite then
  503.         for y=y1,y2 do
  504.             for x=x1,x2 do
  505.                 surf.buffer[((y - 1) * surf.width + x) * 3 - 1] = backcolor
  506.             end
  507.         end
  508.     end
  509.     if textcolor or surf.overwrite then
  510.         for y=y1,y2 do
  511.             for x=x1,x2 do
  512.                 surf.buffer[((y - 1) * surf.width + x) * 3] = textcolor
  513.             end
  514.         end
  515.     end
  516. end,
  517.  
  518. fillRoundRect = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  519.     _drawHLine(surf, x1 + 1, x2 - 1, y1, char, backcolor, textcolor)
  520.     _drawHLine(surf, x1 + 1, x2 - 1, y2, char, backcolor, textcolor)
  521.     _drawVLine(surf, y1 + 1, y2 - 1, x1, char, backcolor, textcolor)
  522.     _drawVLine(surf, y1 + 1, y2 - 1, x2, char, backcolor, textcolor)
  523.     surf:fillRect(x1 + 1, y1 + 1, x2 - 1, y2 - 1, char, backcolor, textcolor)
  524. end,
  525.  
  526. fillRoundedRect = function(surf, x1, y1, x2, y2, radius, char, backcolor, textcolor)
  527.     surf:fillRect(x1 + radius, y1, x2 - radius, y2, char, backcolor, textcolor)
  528.     surf:fillRect(x1, y1 + radius, x1 + radius, y2 - radius, char, backcolor, textcolor)
  529.     surf:fillRect(x2 - radius, y1 + radius, x2, y2 - radius, char, backcolor, textcolor)
  530.     surf:fillPie(x1, y1, x1 + radius * 2 + 2, y1 + radius * 2 + 2, -math.pi, -math.pi / 2, char, backcolor, textcolor)
  531.     surf:fillPie(x2, y1, x2 - radius * 2 - 2, y1 + radius * 2 + 2, 0, -math.pi / 2, char, backcolor, textcolor)
  532.     surf:fillPie(x1, y2, x1 + radius * 2 + 2, y2 - radius * 2 - 2, math.pi, math.pi / 2, char, backcolor, textcolor)
  533.     surf:fillPie(x2, y2, x2 - radius * 2 - 2, y2 - radius * 2 - 2, 0, math.pi / 2, char, backcolor, textcolor)
  534. end,
  535.  
  536. drawTriangle = function(surf, x1, y1, x2, y2, x3, y3, char, backcolor, textcolor)
  537.     surf:drawLine(x1, y1, x2, y2, char, backcolor, textcolor)
  538.     surf:drawLine(x2, y2, x3, y3, char, backcolor, textcolor)
  539.     surf:drawLine(x3, y3, x1, y1, char, backcolor, textcolor)
  540. end,
  541.  
  542. fillTriangle = function(surf, x1, y1, x2, y2, x3, y3, char, backcolor, textcolor)
  543.     local minX, minY, maxX, maxY = x1, y1, x1, y1
  544.     if x2 < minX then minX = x2 end
  545.     if x3 < minX then minX = x3 end
  546.     if y2 < minY then minY = y2 end
  547.     if y3 < minY then minY = y3 end
  548.     if x2 > maxX then maxX = x2 end
  549.     if x3 > maxX then maxX = x3 end
  550.     if y2 > maxY then maxY = y2 end
  551.     if y3 > maxY then maxY = y3 end
  552.     local width, height, buffer, min, max = maxX - minX + 1, maxY - minY + 1, { }, 0, 0
  553.     _bufferLine(buffer, width, x1 - minX + 1, y1 - minY + 1, x2 - minX + 1, y2 - minY + 1)
  554.     _bufferLine(buffer, width, x2 - minX + 1, y2 - minY + 1, x3 - minX + 1, y3 - minY + 1)
  555.     _bufferLine(buffer, width, x3 - minX + 1, y3 - minY + 1, x1 - minX + 1, y1 - minY + 1)
  556.     for j=1,height do
  557.         min, max = nil, 0
  558.         for i=1,width,1 do
  559.             if buffer[(j - 1) * width + i] then
  560.                 if not min then min = i end
  561.                 if i > max then max = i end
  562.             end
  563.         end
  564.         _drawHLine(surf, min + minX - 1, max + minX - 1, j + minY - 1, char, backcolor, textcolor)
  565.     end
  566. end,
  567.  
  568. drawTriangles = function(surf, points, mode, char, backcolor, textcolor)
  569.     mode = mode or 1
  570.     if mode == 1 then
  571.         for i=1,#points,6 do
  572.             surf:drawTriangle(points[i], points[i+1], points[i+2], points[i+3], points[i+4], points[i+5], char, backcolor, textcolor)
  573.         end
  574.     elseif mode == 2 then
  575.         local lastx, lasty, prevx, prevy, curx, cury = points[1], points[2], points[3], points[4]
  576.         for i=5,#points,2 do
  577.             curx, cury = points[i], points[i+1]
  578.             surf:drawTriangle(lastx, lasty, prevx, prevy, curx, cury, char, backcolor, textcolor)
  579.             lastx, lasty, prevx, prevy = prevx, prevy, curx, cury
  580.         end
  581.     elseif mode == 3 then
  582.         local midx, midy, lastx, lasty, curx, cury = points[1], points[2], points[3], points[4]
  583.         for i=5,#points,2 do
  584.             curx, cury = points[i], points[i+1]
  585.             surf:drawTriangle(midx, midy, lastx, lasty, curx, cury, char, backcolor, textcolor)
  586.             lastx, lasty = curx, cury
  587.         end
  588.     end
  589. end,
  590.  
  591. fillTriangles = function(surf, points, mode, char, backcolor, textcolor)
  592.     mode = mode or 1
  593.     if mode == 1 then
  594.         for i=1,#points,6 do
  595.             surf:fillTriangle(points[i], points[i+1], points[i+2], points[i+3], points[i+4], points[i+5], char, backcolor, textcolor)
  596.         end
  597.     elseif mode == 2 then
  598.         local lastx, lasty, prevx, prevy, curx, cury = points[1], points[2], points[3], points[4]
  599.         for i=5,#points,2 do
  600.             curx, cury = points[i], points[i+1]
  601.             surf:fillTriangle(lastx, lasty, prevx, prevy, curx, cury, char, backcolor, textcolor)
  602.             lastx, lasty, prevx, prevy = prevx, prevy, curx, cury
  603.         end
  604.     elseif mode == 3 then
  605.         local midx, midy, lastx, lasty, curx, cury = points[1], points[2], points[3], points[4]
  606.         for i=5,#points,2 do
  607.             curx, cury = points[i], points[i+1]
  608.             surf:fillTriangle(midx, midy, lastx, lasty, curx, cury, char, backcolor, textcolor)
  609.             lastx, lasty = curx, cury
  610.         end
  611.     end
  612. end,
  613.  
  614. drawEllipse = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  615.     if x1 > x2 then
  616.         local temp = x1
  617.         x1, x2 = x2, temp
  618.     end
  619.     if y1 > y2 then
  620.         local temp = y1
  621.         y1, y2 = y2, temp
  622.     end
  623.     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
  624.     for i=1,17,1 do
  625.         local x, y = math_floor((midX + math_cos(step * i) * width) + 0.5), math_floor((midY + math_sin(step * i) * height) + 0.5)
  626.         if i > 1 then
  627.             surf:drawLine(lastX, lastY, x, y, char, backcolor, textcolor)
  628.         end
  629.         lastX, lastY = x, y
  630.     end
  631. end,
  632.  
  633. fillEllipse = function(surf, x1, y1, x2, y2, char, backcolor, textcolor)
  634.     if x1 > x2 then
  635.         local temp = x1
  636.         x1, x2 = x2, temp
  637.     end
  638.     if y1 > y2 then
  639.         local temp = y1
  640.         y1, y2 = y2, temp
  641.     end
  642.     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, { }
  643.     for i=1,resolution+1,1 do
  644.         local x, y = math_floor((midX + math_cos(step * i) * width) + 0.5), math_floor((midY + math_sin(step * i) * height) + 0.5)
  645.         if i > 1 then
  646.             _bufferLine(buffer, bwidth, lastX - x1 + 1, lastY - y1 + 1, x - x1 + 1, y - y1 + 1)
  647.         end
  648.         lastX, lastY = x, y
  649.     end
  650.     for j=1,bheight do
  651.         min, max = nil, 0
  652.         for i=1,bwidth,1 do
  653.             if buffer[(j - 1) * bwidth + i] then
  654.                 if not min then min = i end
  655.                 if i > max then max = i end
  656.             end
  657.         end
  658.         _drawHLine(surf, min + x1 - 1, max + x1 - 1, j + y1 - 1, char, backcolor, textcolor)
  659.     end
  660. end,
  661.  
  662. drawArc = function(surf, x1, y1, x2, y2, a1, a2, char, backcolor, textcolor)
  663.     if x1 > x2 then
  664.         local temp = x1
  665.         x1, x2 = x2, temp
  666.     end
  667.     if y1 > y2 then
  668.         local temp = y1
  669.         y1, y2 = y2, temp
  670.     end
  671.     if a1 > a2 then
  672.         local temp = a1
  673.         a1, a2 = a2, temp
  674.     end
  675.     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
  676.     for i=1,17,1 do
  677.         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)
  678.         if i > 1 then
  679.             surf:drawLine(lastX, lastY, x, y, char, backcolor, textcolor)
  680.         end
  681.         lastX, lastY = x, y
  682.     end
  683. end,
  684.  
  685. drawPie = function(surf, x1, y1, x2, y2, a1, a2, char, backcolor, textcolor)
  686.     if x1 > x2 then
  687.         local temp = x1
  688.         x1, x2 = x2, temp
  689.     end
  690.     if y1 > y2 then
  691.         local temp = y1
  692.         y1, y2 = y2, temp
  693.     end
  694.     if a1 > a2 then
  695.         local temp = a1
  696.         a1, a2 = a2, temp
  697.     end
  698.     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
  699.     for i=1,17,1 do
  700.         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)
  701.         if i > 1 then
  702.             surf:drawLine(lastX, lastY, x, y, char, backcolor, textcolor)
  703.         end
  704.         lastX, lastY = x, y
  705.     end
  706.     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)
  707.     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)
  708. end,
  709.  
  710. fillPie = function(surf, x1, y1, x2, y2, a1, a2, char, backcolor, textcolor)
  711.     if x1 > x2 then
  712.         local temp = x1
  713.         x1, x2 = x2, temp
  714.     end
  715.     if y1 > y2 then
  716.         local temp = y1
  717.         y1, y2 = y2, temp
  718.     end
  719.     if a1 > a2 then
  720.         local temp = a1
  721.         a1, a2 = a2, temp
  722.     end
  723.     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, { }
  724.     for i=1,17,1 do
  725.         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)
  726.         if i > 1 then
  727.             _bufferLine(buffer, bwidth, lastX - x1 + 1, lastY - y1 + 1, x - x1 + 1, y - y1 + 1)
  728.         end
  729.         lastX, lastY = x, y
  730.     end
  731.     _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)
  732.     _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)
  733.     for j=1,bheight do
  734.         min, max = nil, 0
  735.         for i=1,bwidth,1 do
  736.             if buffer[(j - 1) * bwidth + i] then
  737.                 if not min then min = i end
  738.                 if i > max then max = i end
  739.             end
  740.         end
  741.         if min then
  742.             _drawHLine(surf, min + x1 - 1, max + x1 - 1, j + y1 - 1, char, backcolor, textcolor)
  743.         end
  744.     end
  745. end,
  746.  
  747. floodFill = function(surf, x, y, char, backcolor, textcolor)
  748.     if x < surf.x1 or y < surf.y1 or x > surf.x2 or y > surf.y2 then return end
  749.     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]
  750.     if (tchar == char) and (tbackcolor == backcolor) and (ttextcolor == textcolor) then return end
  751.     while #stack > 0 do
  752.         local cx, cy = stack[#stack - 1], stack[#stack]
  753.         stack[#stack] = nil
  754.         stack[#stack] = nil
  755.         if cx >= surf.x1 and cy >= surf.y1 and cx <= surf.x2 and cy <= surf.y2 then
  756.             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]
  757.             if (tchar == cchar) and (tbackcolor == cbackcolor) and (ttextcolor == ctextcolor) then
  758.                 if char or surf.overwrite then
  759.                     surf.buffer[((cy - 1) * surf.width + cx) * 3 - 2] = char
  760.                 end
  761.                 if backcolor or surf.overwrite then
  762.                     surf.buffer[((cy - 1) * surf.width + cx) * 3 - 1] = backcolor
  763.                 end
  764.                 if textcolor or surf.overwrite then
  765.                     surf.buffer[((cy - 1) * surf.width + cx) * 3] = textcolor
  766.                 end
  767.                 stack[#stack + 1] = cx - 1
  768.                 stack[#stack + 1] = cy
  769.                 stack[#stack + 1] = cx + 1
  770.                 stack[#stack + 1] = cy
  771.                 stack[#stack + 1] = cx
  772.                 stack[#stack + 1] = cy - 1
  773.                 stack[#stack + 1] = cx
  774.                 stack[#stack + 1] = cy + 1
  775.             end
  776.         end
  777.     end
  778. end,
  779.  
  780. drawSurface = function(surf, x, y, surf2)
  781.     for j=1,surf2.height,1 do
  782.         for i=1,surf2.width,1 do
  783.             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])
  784.         end
  785.     end
  786. end,
  787.  
  788. drawSurfacePart = function(surf, x, y, sx1, sy1, sx2, sy2, surf2)
  789.     if sx1 > sx2 then
  790.         local temp = sx1
  791.         sx1, sx2 = sx2, temp
  792.     end
  793.     if sy1 > sy2 then
  794.         local temp = sy1
  795.         sy1, sy2 = sy2, temp
  796.     end
  797.     if sx2 < 1 or sx1 > surf2.width or sy2 < 1 or sy1 > surf2.height then return end
  798.     if sx1 < 1 then sx1 = 1 end
  799.     if sx2 > surf2.width then sx2 = surf2.width end
  800.     if sy1 < 1 then sy1 = 1 end
  801.     if sy2 > surf2.height then sy2 = surf2.height end
  802.     for j=sy1,sy2,1 do
  803.         for i=sx1,sx2,1 do
  804.             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])
  805.         end
  806.     end
  807. end,
  808.  
  809. drawSurfaceScaled = function(surf, x1, y1, x2, y2, surf2)
  810.     local x, width, xinv, y, height, yinv = 0, 0, false, 0, 0, false
  811.     if x1 <= x2 then
  812.         x = x1
  813.         width = x2 - x1 + 1
  814.     else
  815.         x = x2
  816.         width = x1 - x2 + 1
  817.         xinv = true
  818.     end
  819.     if y1 <= y2 then
  820.         y = y1
  821.         height = y2 - y1 + 1
  822.     else
  823.         y = y2
  824.         height = y1 - y2 + 1
  825.         yinv = true
  826.     end
  827.     local xscale, yscale, px, py = width / surf2.width, height / surf2.height
  828.     for j=1,height,1 do
  829.         for i=1,width,1 do
  830.             if xinv then
  831.                 px = math_floor((width - i + 0.5) / xscale) + 1
  832.             else
  833.                 px = math_floor((i - 0.5) / xscale) + 1
  834.             end
  835.             if yinv then
  836.                 py = math_floor((height - j + 0.5) / yscale) + 1
  837.             else
  838.                 py = math_floor((j - 0.5) / yscale) + 1
  839.             end
  840.             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])
  841.         end
  842.     end
  843. end,
  844.  
  845. drawSurfaceRotated = function(surf, x, y, ox, oy, angle, surf2)
  846.     local cos, sin, range = math_cos(angle), math_sin(angle), math_floor(math.sqrt(surf2.width * surf2.width + surf2.height * surf2.height))
  847.     x, y = x - math_floor(cos * (ox - 1) + sin * (oy - 1) + 0.5), y - math_floor(cos * (oy - 1) - sin * (ox - 1) + 0.5)
  848.     for j=-range,range,1 do
  849.         for i=-range,range,1 do
  850.             local sx, sy = math_floor(i * cos - j * sin), math_floor(i * sin + j * cos)
  851.             if sx >= 0 and sx < surf2.width and sy >= 0 and sy < surf2.height then
  852.                 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])
  853.             end
  854.         end
  855.     end
  856. end,
  857.  
  858. shader = function(surf, f, x1, y1, x2, y2)
  859.     x1, y1, x2, y2 = x1 or surf.x1, y1 or surf.y1, x2 or surf.x2, y2 or surf.y2
  860.     if x1 > x2 then
  861.         local temp = x1
  862.         x1, x2 = x2, temp
  863.     end
  864.     if y1 > y2 then
  865.         local temp = y1
  866.         y1, y2 = y2, temp
  867.     end
  868.     if x2 < surf.x1 or x1 > surf.x2 or y2 < surf.y1 or y1 > surf.y2 then return end
  869.     if x1 < surf.x1 then x1 = surf.x1 end
  870.     if x2 > surf.x2 then x2 = surf.x2 end
  871.     if y1 < surf.y1 then y1 = surf.y1 end
  872.     if y2 > surf.y2 then y2 = surf.y2 end
  873.     local width, buffer = x2 - x1 + 1, { }
  874.     for j=y1,y2 do
  875.         for i=x1,x2 do
  876.             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)
  877.         end
  878.     end
  879.     for j=y1,y2 do
  880.         for i=x1,x2 do
  881.             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]
  882.         end
  883.     end
  884. end,
  885.  
  886. shift = function(surf, x, y, x1, y1, x2, y2)
  887.     x1, y1, x2, y2 = x1 or surf.x1, y1 or surf.y1, x2 or surf.x2, y2 or surf.y2
  888.     if x1 > x2 then
  889.         local temp = x1
  890.         x1, x2 = x2, temp
  891.     end
  892.     if y1 > y2 then
  893.         local temp = y1
  894.         y1, y2 = y2, temp
  895.     end
  896.     if x2 < surf.x1 or x1 > surf.x2 or y2 < surf.y1 or y1 > surf.y2 then return end
  897.     if x1 < surf.x1 then x1 = surf.x1 end
  898.     if x2 > surf.x2 then x2 = surf.x2 end
  899.     if y1 < surf.y1 then y1 = surf.y1 end
  900.     if y2 > surf.y2 then y2 = surf.y2 end
  901.     local width, buffer = x2 - x1 + 1, { }
  902.     for j=y1,y2 do
  903.         for i=x1,x2 do
  904.             if i - x >= 1 and j - y >= 1 and i - x <= surf.width and j - y <= surf.height then
  905.                 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]
  906.             end
  907.         end
  908.     end
  909.     for j=y1,y2 do
  910.         for i=x1,x2 do
  911.             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]
  912.         end
  913.     end
  914. end
  915. }
  916.  
  917. function create(width, height, char, backcolor, textcolor)
  918.     local surf = { }
  919.     for k,v in pairs(_functions) do
  920.         surf[k] = v
  921.     end
  922.     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, { }
  923.     if char then
  924.         for i=1,width * height do
  925.             surf.buffer[i * 3 - 2] = char
  926.         end
  927.     end
  928.     if backcolor then
  929.         for i=1,width * height do
  930.             surf.buffer[i * 3 - 1] = backcolor
  931.         end
  932.     end
  933.     if textcolor then
  934.         for i=1,width * height do
  935.             surf.buffer[i * 3] = textcolor
  936.         end
  937.     end
  938.     return surf
  939. end
  940.  
  941. function load(path)
  942.     local lines, f = { }, fs.open(path, "r")
  943.     for line in f.readLine do
  944.         lines[#lines + 1] = line
  945.     end
  946.     f.close()
  947.     local height = #lines
  948.     if lines[1]:byte(1) == 30 then
  949.         local width, i = 0, 1
  950.         while i <= #lines[1] do
  951.             local char = lines[1]:byte(i)
  952.             if char == 30 or char == 31 then
  953.                 i = i + 1
  954.             else
  955.                 width = width + 1
  956.             end
  957.             i = i + 1
  958.         end
  959.         local surf, backcolor, textcolor, i, px, char, color = create(width, height)
  960.         for j=1,height do
  961.             i = 1
  962.             px = 1
  963.             while i <= #lines[j] do
  964.                 char = lines[j]:byte(i)
  965.                 if char == 30 then
  966.                     i = i + 1
  967.                     char = lines[j]:byte(i)
  968.                     color = tonumber(lines[j]:sub(i, i), 16)
  969.                     if color then
  970.                         backcolor = 2^color
  971.                     else
  972.                         backcolor = nil
  973.                     end
  974.                 elseif char == 31 then
  975.                     i = i + 1
  976.                     char = lines[j]:byte(i)
  977.                     color = tonumber(lines[j]:sub(i, i), 16)
  978.                     if color then
  979.                         textcolor = 2^color
  980.                     else
  981.                         textcolor = nil
  982.                     end
  983.                 else
  984.                     surf.buffer[((j - 1) * surf.width + px) * 3 - 2] = lines[j]:sub(i, i)
  985.                     surf.buffer[((j - 1) * surf.width + px) * 3 - 1] = backcolor
  986.                     surf.buffer[((j - 1) * surf.width + px) * 3] = textcolor
  987.                     px = px + 1
  988.                 end
  989.                 i = i + 1
  990.             end
  991.         end
  992.         return surf
  993.     elseif lines[1]:byte(1) == 95 then
  994.         return loadString(lines[1])
  995.     else
  996.         local width = 0
  997.         for i=1,#lines,1 do
  998.             if #lines[i] > width then
  999.                 width = #lines[i]
  1000.             end
  1001.         end
  1002.         local surf, color = create(width, height)
  1003.         for j=1,height do
  1004.             for i=1,width do
  1005.                 color = tonumber(lines[j]:sub(i, i), 16)
  1006.                 if color then
  1007.                     surf.buffer[((j - 1) * surf.width + i) * 3 - 1] = 2 ^ color
  1008.                 end
  1009.             end
  1010.         end
  1011.         return surf
  1012.     end
  1013. end
  1014.  
  1015. function loadString(str)
  1016.     local width, height, n = tonumber(str:sub(2, 5), 16), tonumber(str:sub(6, 9), 16), 10
  1017.     local surf = create(width, height)
  1018.     for j=1,height do
  1019.         for i=1,width do
  1020.             if str:byte(n) ~= 95 then
  1021.                 surf.buffer[((j - 1) * surf.width + i) * 3 - 2] = string.char(tonumber(str:sub(n, n + 1), 16))
  1022.             end
  1023.             if str:byte(n + 2) ~= 95 then
  1024.                 surf.buffer[((j - 1) * surf.width + i) * 3 - 1] = 2 ^ tonumber(str:sub(n + 2, n + 2), 16)
  1025.             end
  1026.             if str:byte(n + 3) ~= 95 then
  1027.                 surf.buffer[((j - 1) * surf.width + i) * 3] = 2 ^ tonumber(str:sub(n + 3, n + 3), 16)
  1028.             end
  1029.             n = n + 4
  1030.         end
  1031.     end
  1032.     return surf
  1033. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement