Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local scw, sch = term.getSize()
- --[[ Graphics API 0.5.1 by AlMilliost
- With graphic/text buffer.
- Examples:
- os.loadAPI("graphics")
- var1 = graphics.addBuffer() -- new buffer
- graphics.clearScreen(var1,1) -- white screen
- graphics.drawBuffer(var1) -- draw all
- --]]
- function addBuffer()
- term.setBackgroundColor(colors.black)
- local buffer = {image = {}, text = {}}
- for y = 1, sch do
- buffer.image[y] = {}
- buffer.text[y] = {}
- for x = 1, scw do
- buffer.image[y][x] = colors.black
- buffer.text[y][x] = {[1]=" ",[2]=1}
- end
- end
- return buffer
- end
- function drawBuffer(tbuffer)
- for y=1,#tbuffer.image do
- local tLine = tbuffer.image[y]
- for x=1,#tLine do
- if tLine[x] > 0 then
- term.setCursorPos(x,y)
- term.setBackgroundColor(tbuffer["image"][y][x])
- term.setTextColor(tbuffer.text[y][x][2])
- if tbuffer.text[y][x][1] ~= 0 or nil then term.write(tbuffer.text[y][x][1]) end
- if tbuffer.text[y][x][1] == 0 or nil then term.write(" ") end
- end
- end
- end
- end
- function clearBuffer(buffer)
- for y = 1, sch do
- for x = 1, scw do
- buffer.image[y][x] = 1
- buffer.text[y][x][1] = 0
- buffer.text[y][x][2] = 1
- end
- end
- end
- function clearScreen(buffer,color)
- for y = 1, sch do
- for x = 1, scw do
- buffer.image[y][x] = color
- buffer.text[y][x][1] = 0
- buffer.text[y][x][2] = 1
- end
- end
- end
- function text(text, ... )
- term.setCursorPos(arg[1],arg[2])
- term.write(tostring(text))
- end
- function cText(text,c, ... )
- if #arg > 0 then term.setCursorPos(arg[1],arg[2]) end
- term.setTextColor(c)
- term.write(tostring(text))
- end
- function dText(buffer,text,x,y,color)
- for f = 1, #tostring(text) do
- if x+f-1 <= 51 and x+f-1 >= 1 and y <= 19 and y >= 1 then
- buffer.text[y][x+f-1][1] = string.sub(text,f,f)
- buffer.text[y][x+f-1][2] = 1
- if color then buffer.text[y][x+f-1][2] = color end
- end
- end
- end
- function drawPixel(buffer,x,y,color)
- if type(color) == "number" and x <= 51 and x >= 1 and y <= 19 and y >= 1 then
- buffer.image[y][x] = color
- buffer.text[y][x][1] = 0
- buffer.text[y][x][2] = 1
- end
- end
- function drawLine(buffer, startX, startY, endX, endY, color)
- startX = math.floor(startX)
- startY = math.floor(startY)
- endX = math.floor(endX)
- endY = math.floor(endY)
- if startX == endX and startY == endY then
- drawPixel(buffer,startX,startY,color)
- return
- end
- local minX = math.min( startX, endX )
- if minX == startX then
- minY = startY
- maxX = endX
- maxY = endY
- else
- minY = endY
- maxX = startX
- maxY = startY
- end
- local xDiff = maxX - minX
- local yDiff = maxY - minY
- if xDiff > math.abs(yDiff) then
- local y = minY
- local dy = yDiff / xDiff
- for x=minX,maxX do
- drawPixel(buffer,x,math.floor( y + 0.5 ),color)
- y = y + dy
- end
- else
- local x = minX
- local dx = xDiff / yDiff
- if maxY >= minY then
- for y=minY,maxY do
- drawPixel(buffer,math.floor( x + 0.5 ),y,color)
- x = x + dx
- end
- else
- for y=minY,maxY,-1 do
- drawPixel(buffer,math.floor( x + 0.5 ),y,color)
- x = x - dx
- end
- end
- end
- end
- function drawBox(buffer, x1, y1, w, h, c)
- for y = 1, h do
- for x = 1, w do
- drawPixel(buffer,x1+x-1,y1+y-1,c)
- end
- end
- end
- function addImage(buffer,image,x1,y1)
- for y = 1, #image do
- local tLine = image[y]
- for x = 1, #tLine do
- if image[y][x] ~= 0 then
- if x1+x-1 <= 51 and x1+x-1 >= 1 and y1+y-1 <= 19 and y1+y-1 >= 1 then
- buffer.image[y1+y-1][x1+x-1] = image[y][x]
- buffer.text[y1+y-1][x1+x-1][1] = 0
- buffer.text[y1+y-1][x1+x-1][2] = 1
- end
- end
- end
- end
- end
- function addMImage(buffer,image,x1,y1)
- for y = 1, #image do
- local tLine = image[y]
- for x = 1, #tLine do
- if image[y][#tLine-x+1] ~= 0 then
- if x1+x-1 <= 51 and x1+x-1 >= 1 and y1+y-1 <= 19 and y1+y-1 >= 1 then
- buffer.image[y1+y-1][x1+x-1] = image[y][#tLine-x+1]
- buffer.text[y1+y-1][x1+x-1][1] = 0
- buffer.text[y1+y-1][x1+x-1][2] = 1
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement