1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-02-12 00:50:05 +00:00

Update paintutils.lua

Resolve a few potential bugs.
This commit is contained in:
Bomb Bloke 2017-06-15 00:23:40 +10:00 committed by GitHub
parent 34ac28066e
commit 221143e767

View File

@ -1,5 +1,15 @@
local setPos, write, setCol, blit = term.setCursorPos, term.write, term.setBackgroundColour, term.blit
local maxn = table.maxn or function( tTable )
local maxn = 0
for n in pairs( tTable ) do
if type( n ) == "number" and n > max then
maxn = n
end
end
return maxn
end
local tColourLookup = {}
for n=1,16 do
tColourLookup[ string.sub( "0123456789abcdef",n,n ) ] = 2^(n-1)
@ -33,15 +43,22 @@ function saveImage( tImage, sPath )
end
local file = fs.open(sPath, "w" )
if not file then return false end
for y=1,#tImage do
local tOld, tNew = tImage[y], {}
for x=1,#tOld do
tNew[x] = tColourLookup[ tOld[x] ] or " "
end
file.writeLine( table.concat( tNew ) )
for y=1,maxn( tImage ) do
local tOld, tNew, last = tImage[y], {}, 0
if tOld then for x=1,maxn( tOld ) do
local thisCol = tColourLookup[ tOld[x] ]
if thisCol then
tNew[x], last = thisCol, x
else
tNew[x] = " "
end
end end
file.writeLine( table.concat( tNew, "", 1, last ) )
end
file.close()
return true
end
function drawPixel( xPos, yPos, nColour )
@ -93,9 +110,8 @@ function drawLine( startX, startY, endX, endY, nColour )
local yDiff = maxY - minY
if minY == maxY then
local sStr = string.rep( " ", xDiff + 1 )
setPos( minX, minY )
write( sStr )
write( string.rep( " ", xDiff + 1 ) )
return
end
@ -193,9 +209,9 @@ function drawImage( tImage, xPos, yPos )
if type( tImage ) ~= "table" or type( xPos ) ~= "number" or type( yPos ) ~= "number" then
error( "Expected image, x, y", 2 )
end
for y=1,#tImage do
for y=1,maxn( tImage ) do
local tLine, sBG, counter = tImage[y], {}, 0
for x=1,#tLine+1 do
if tLine then for x=1,maxn( tLine )+1 do
local px = tLine[x] or 0
if px > 0 then
counter = counter + 1
@ -206,6 +222,6 @@ function drawImage( tImage, xPos, yPos )
blit( sT, sT, table.concat( sBG ) )
sBG, counter = {}, 0
end
end
end end
end
end