Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- A simple ComputerCraft API to load and draw NFT files generated by NPaintPro.
- -- http://www.computercraft.info/forums2/index.php?/topic/5029-145-npaintpro/
- -- Two exposed functions:
- -- NFT.load(<String> file)
- -- Returns: <table> image
- -- NFT.draw(<table> image, <number> xPos, <number> yPos, [<table> terminal object])
- -- Draws specified image at specified location.
- -- Uses the current terminal if you don't provide a different one.
- local blit
- if term.blit then
- blit = term.blit
- else
- local colNums = {}
- for i = 1, 16 do colNums[("0123456789abcdef"):sub(i, i)] = 2 ^ (i - 1) end
- blit = function(text, fg, bg)
- local first, curText, curFG, curBG = true, {}
- for i = 1, #text do
- local thisChar, thisFG, thisBG = text:sub(i, i), fg:sub(i, i), bg:sub(i, i)
- if thisFG ~= curFG or thisBG ~= curBG then
- if first then first = false else term.write(table.concat(curText)) end
- curText = {}
- if thisFG ~= curFG then
- curFG = thisFG
- term.setTextColour(colNums[curFG])
- end
- if thisBG ~= curBG then
- curBG = thisBG
- term.setBackgroundColour(colNums[curBG])
- end
- end
- curText[#curText + 1] = thisChar
- end
- term.write(table.concat(curText))
- end
- end
- function load(file)
- local results = {}
- for line in io.lines(file) do
- local curSkip, curLine, curText, curFG, curBG, readPos, writePos, linePos, fg, bg = 0, {}, {}, {}, {}, 1, 1, 0, "0", " "
- while readPos <= #line do
- local char = line:sub(readPos, readPos)
- if char == "\031" then
- readPos = readPos + 1
- fg = line:sub(readPos, readPos)
- if fg == " " then fg = "f" end
- elseif char == "\030" then
- readPos = readPos + 1
- bg = line:sub(readPos, readPos)
- else
- if bg == " " then
- curSkip = curSkip + 1
- if #curText > 0 then
- curLine[#curLine + 1] = {table.concat(curText), table.concat(curFG), table.concat(curBG)}
- curText, curFG, curBG, writePos = {}, {}, {}, 1
- end
- else
- curText[writePos], curFG[writePos], curBG[writePos] = char, fg, bg
- writePos = writePos + 1
- if curSkip > 0 then
- curLine[#curLine + 1] = linePos
- curSkip = 0
- end
- end
- linePos = linePos + 1
- end
- readPos = readPos + 1
- end
- if #curText > 0 then curLine[#curLine + 1] = {table.concat(curText), table.concat(curFG), table.concat(curBG)} end
- results[#results + 1] = curLine
- end
- return results
- end
- function draw(image, x, y, terminal)
- local oldTerm = terminal and term.redirect(terminal)
- y = y - 1
- for yy = 1, #image do
- local thisLine = image[yy]
- term.setCursorPos(x, y + yy)
- for xx = 1, #thisLine do
- local thisVal = thisLine[xx]
- if type(thisVal) == "number" then
- term.setCursorPos(x + thisVal, y + yy)
- else
- blit(thisVal[1], thisVal[2], thisVal[3])
- end
- end
- end
- if oldTerm then term.redirect(oldTerm) end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement