mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-04-29 06:03:12 +00:00
Update paintutils.lua
Resolve a few potential bugs.
This commit is contained in:
parent
34ac28066e
commit
221143e767
@ -1,5 +1,15 @@
|
|||||||
local setPos, write, setCol, blit = term.setCursorPos, term.write, term.setBackgroundColour, term.blit
|
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 = {}
|
local tColourLookup = {}
|
||||||
for n=1,16 do
|
for n=1,16 do
|
||||||
tColourLookup[ string.sub( "0123456789abcdef",n,n ) ] = 2^(n-1)
|
tColourLookup[ string.sub( "0123456789abcdef",n,n ) ] = 2^(n-1)
|
||||||
@ -33,15 +43,22 @@ function saveImage( tImage, sPath )
|
|||||||
end
|
end
|
||||||
|
|
||||||
local file = fs.open(sPath, "w" )
|
local file = fs.open(sPath, "w" )
|
||||||
|
if not file then return false end
|
||||||
|
|
||||||
for y=1,#tImage do
|
for y=1,maxn( tImage ) do
|
||||||
local tOld, tNew = tImage[y], {}
|
local tOld, tNew, last = tImage[y], {}, 0
|
||||||
for x=1,#tOld do
|
if tOld then for x=1,maxn( tOld ) do
|
||||||
tNew[x] = tColourLookup[ tOld[x] ] or " "
|
local thisCol = tColourLookup[ tOld[x] ]
|
||||||
|
if thisCol then
|
||||||
|
tNew[x], last = thisCol, x
|
||||||
|
else
|
||||||
|
tNew[x] = " "
|
||||||
end
|
end
|
||||||
file.writeLine( table.concat( tNew ) )
|
end end
|
||||||
|
file.writeLine( table.concat( tNew, "", 1, last ) )
|
||||||
end
|
end
|
||||||
file.close()
|
file.close()
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function drawPixel( xPos, yPos, nColour )
|
function drawPixel( xPos, yPos, nColour )
|
||||||
@ -93,9 +110,8 @@ function drawLine( startX, startY, endX, endY, nColour )
|
|||||||
local yDiff = maxY - minY
|
local yDiff = maxY - minY
|
||||||
|
|
||||||
if minY == maxY then
|
if minY == maxY then
|
||||||
local sStr = string.rep( " ", xDiff + 1 )
|
|
||||||
setPos( minX, minY )
|
setPos( minX, minY )
|
||||||
write( sStr )
|
write( string.rep( " ", xDiff + 1 ) )
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -193,9 +209,9 @@ function drawImage( tImage, xPos, yPos )
|
|||||||
if type( tImage ) ~= "table" or type( xPos ) ~= "number" or type( yPos ) ~= "number" then
|
if type( tImage ) ~= "table" or type( xPos ) ~= "number" or type( yPos ) ~= "number" then
|
||||||
error( "Expected image, x, y", 2 )
|
error( "Expected image, x, y", 2 )
|
||||||
end
|
end
|
||||||
for y=1,#tImage do
|
for y=1,maxn( tImage ) do
|
||||||
local tLine, sBG, counter = tImage[y], {}, 0
|
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
|
local px = tLine[x] or 0
|
||||||
if px > 0 then
|
if px > 0 then
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
@ -206,6 +222,6 @@ function drawImage( tImage, xPos, yPos )
|
|||||||
blit( sT, sT, table.concat( sBG ) )
|
blit( sT, sT, table.concat( sBG ) )
|
||||||
sBG, counter = {}, 0
|
sBG, counter = {}, 0
|
||||||
end
|
end
|
||||||
end
|
end end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user