This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
H4X0RZ's profile picture

TmpImg [draw Images from a table]

Started by H4X0RZ, 18 May 2013 - 04:12 AM
H4X0RZ #1
Posted 18 May 2013 - 06:12 AM
Hello all,
I started developing this little API today.

What it does:
Spoiler

1. It creates a "TMPIMAGE" folder
2.Saves all images from the given table in files called like this: Temp0
3.draws the file
4. Deletes the folder(with the content)

Functions:
Spoiler


TmpImg.startTmp() 
--Starts a TmpImg Session (only one can be created an have to be created)

TmpImg.creatTmp(<table>, <lines>)
--creates a new TmpImg with the content of the given table upto to the line you give it
--It returns the Tmp ID

TmpImg.drawTmp(<id>, sX, sY)
--draws the Image at the given position (if you don't entered a position, it draws at 1,1)

TmpImg.endTmp()
--deletes the TMPIMAGES folder

The code:
Spoiler

if not fs.exists("lastTmp") then
local file_handle = fs.open("lastTmp", "w")
file_handle.writeLine("0")
file_handle.close()
end

local lastTmp = fs.open("lastTmp", "r")
lastTmpnum = tonumber(lastTmp.readAll())
lastTmp.close()
end
function reoadTmp()
lastTmp = fs.open("lastTmp", "r")
lastTmpnum = tonumber(lastTmp.readAll())
lastTmp.close()
end

function startTmp()
fs.makeDir("TMPIMAGE")
end

function createTmp(table, sY)
if not type(table) == "table" the
return "Not a table"
else
local file = fs.open("TMPIMAGE/Temp"..tostring(lastTmpnum)+1, "w")
for i = i, sY do
file.writeLine(table[i])
end
file.close()
local editTmp = fs.open("lastTmp", "w")
editTmp.writeLine(lastTmpnum + 1)
editTmp.close()
reloadTmp()
return lastTmpnum
end
end

function drawTmp(id, sX, sY)
local sX = sX or 1
local sY = sY or 1
if not fs.exists("TMPIMAGE/Temp"..tostring(id)) then

else
local id = id
local tmpname = ("TMPIMAGE/Temp"..tostring(id))
tmpIMG = paintutils.loadImage(tmpname)
paintutils.drawImage(tmpIMG, sX, sY)
end
end

function endTmp()
fs.delete("TMPIMAGE")
local lastTmp = fs.open("lastTmp", "w")
lastTmp.writeLine("0")
lastTmp.close()
end
Example code:
Spoiler


local img = {
[1] = "15346533566433563" --Random color crap
}

os.loadAPI("TmpImg")
TmpImg.startTmp()

local randcol = TmpImg.createTmp(img, 1)

TmpImg.drawTmp(randcol, 1,1)

TmpImg.endTmp()

thx for reading
-Freack100-
H4X0RZ #2
Posted 18 May 2013 - 08:28 AM
Code is now online!
Molinko #3
Posted 22 May 2013 - 10:36 PM
function createTmp(table, sY)
if not type(table) == "table" the
return "Not a table"

Your function looks nice however 'table' is a keyword used in the table library in the lua language. When you have the chance you should change this so it wont interfere with people using the table library.. Even a simple 'Table' with a capital 'T' will do better because lua is case sensitive and all lua standard lib api are in lower case i.e the string api, etc…
hope this helps