SHARE
TWEET

NFT API (ComputerCraft)

BombBloke Oct 20th, 2016 (edited) 154 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- A simple ComputerCraft API to load and draw NFT files generated by NPaintPro.
  2. -- http://www.computercraft.info/forums2/index.php?/topic/5029-145-npaintpro/
  3.  
  4. -- Two exposed functions:
  5.  
  6. -- NFT.load(<String> file)
  7. -- Returns: <table> image
  8.  
  9. -- NFT.draw(<table> image, <number> xPos, <number> yPos, [<table> terminal object])
  10. -- Draws specified image at specified location.
  11. -- Uses the current terminal if you don't provide a different one.
  12.  
  13. local blit
  14.  
  15. if term.blit then
  16.     blit = term.blit
  17. else
  18.     local colNums = {}
  19.     for i = 1, 16 do colNums[("0123456789abcdef"):sub(i, i)] = 2 ^ (i - 1) end
  20.    
  21.     blit = function(text, fg, bg)
  22.         local first, curText, curFG, curBG = true, {}
  23.        
  24.         for i = 1, #text do
  25.             local thisChar, thisFG, thisBG = text:sub(i, i), fg:sub(i, i), bg:sub(i, i)
  26.            
  27.             if thisFG ~= curFG or thisBG ~= curBG then
  28.                 if first then first = false else term.write(table.concat(curText)) end
  29.                
  30.                 curText = {}
  31.                
  32.                 if thisFG ~= curFG then
  33.                     curFG = thisFG
  34.                     term.setTextColour(colNums[curFG])
  35.                 end
  36.                
  37.                 if thisBG ~= curBG then
  38.                     curBG = thisBG
  39.                     term.setBackgroundColour(colNums[curBG])
  40.                 end
  41.             end
  42.            
  43.             curText[#curText + 1] = thisChar
  44.         end
  45.        
  46.         term.write(table.concat(curText))
  47.     end
  48. end
  49.  
  50. function load(file)
  51.     local results = {}
  52.    
  53.     for line in io.lines(file) do
  54.         local curSkip, curLine, curText, curFG, curBG, readPos, writePos, linePos, fg, bg = 0, {}, {}, {}, {}, 1, 1, 0, "0", " "
  55.        
  56.         while readPos <= #line do
  57.             local char = line:sub(readPos, readPos)
  58.            
  59.             if char == "\031" then
  60.                 readPos = readPos + 1
  61.                 fg = line:sub(readPos, readPos)
  62.                 if fg == " " then fg = "f" end
  63.             elseif char == "\030" then
  64.                 readPos = readPos + 1
  65.                 bg = line:sub(readPos, readPos)
  66.             else
  67.                 if bg == " " then
  68.                     curSkip = curSkip + 1
  69.                    
  70.                     if #curText > 0 then
  71.                         curLine[#curLine + 1] = {table.concat(curText), table.concat(curFG), table.concat(curBG)}
  72.                         curText, curFG, curBG, writePos = {}, {}, {}, 1
  73.                     end
  74.                 else
  75.                     curText[writePos], curFG[writePos], curBG[writePos] = char, fg, bg
  76.                     writePos = writePos + 1
  77.                    
  78.                     if curSkip > 0 then
  79.                         curLine[#curLine + 1] = linePos
  80.                         curSkip = 0
  81.                     end
  82.                 end
  83.                
  84.                 linePos = linePos + 1
  85.             end
  86.            
  87.             readPos = readPos + 1
  88.         end
  89.        
  90.         if #curText > 0 then curLine[#curLine + 1] = {table.concat(curText), table.concat(curFG), table.concat(curBG)} end
  91.        
  92.         results[#results + 1] = curLine
  93.     end
  94.    
  95.     return results
  96. end
  97.  
  98. function draw(image, x, y, terminal)
  99.     local oldTerm = terminal and term.redirect(terminal)
  100.     y = y - 1
  101.    
  102.     for yy = 1, #image do
  103.         local thisLine = image[yy]
  104.         term.setCursorPos(x, y + yy)
  105.        
  106.         for xx = 1, #thisLine do
  107.             local thisVal = thisLine[xx]
  108.            
  109.             if type(thisVal) == "number" then
  110.                 term.setCursorPos(x + thisVal, y + yy)
  111.             else
  112.                 blit(thisVal[1], thisVal[2], thisVal[3])
  113.             end
  114.         end
  115.     end
  116.    
  117.     if oldTerm then term.redirect(oldTerm) end
  118. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top