Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- HPWebcamAble Presents...
- Buffer
- --------------------------------
- /!\ Warning /!\
- The program is in BETA
- It is not officially stable yet!
- --------------------------------
- === Description ====
- This is an API that lets you efficiently draw to the screen
- ==== Documentation ====
- Coming soon
- ==== Installation and Use ====
- Pastebin Code: LVmgReu6
- pastebin get <code> buffer
- Then run 'buffer' (Or what you called it)
- ==== Update History ====
- Pastebin will always have the most recent version
- |0.1| <- This program
- First BETA release
- ]]
- local toHex = {
- [ colors.white ] = "0",
- [ colors.orange ] = "1",
- [ colors.magenta ] = "2",
- [ colors.lightBlue ] = "3",
- [ colors.yellow ] = "4",
- [ colors.lime ] = "5",
- [ colors.pink ] = "6",
- [ colors.gray ] = "7",
- [ colors.lightGray ] = "8",
- [ colors.cyan ] = "9",
- [ colors.purple ] = "a",
- [ colors.blue ] = "b",
- [ colors.brown ] = "c",
- [ colors.green ] = "d",
- [ colors.red ] = "e",
- [ colors.black ] = "f",
- }
- local function checkArgs(tCheckArgs,tExpectedArgs) -- This function isn't used. I know ;)
- local err = "expected "..tExpectedArgs[1]
- local got = ", got "..type(tCheckArgs[1])
- local shouldErr = type(tCheckArgs[1]) ~= tExpectedArgs[1]
- for i = 2, #tExpectedArgs do
- err = err..","..tExpectedArgs[i]
- got = got..","..type(tCheckArgs[i])
- shouldErr = (shouldErr or type(tCheckArgs[i]) ~= tExpectedArgs[i])
- end
- if shouldErr then
- error( err..got , 3 )
- end
- return true
- end
- function create()
- local string_rep = string.rep
- local string_sub = string.sub
- local function createLine(backgroundColor)
- local w,h = term.getSize()
- local line = {
- text = string_rep(" " , w),
- backColor = string_rep( toHex[backgroundColor] , w ),
- textColor = string_rep( toHex[colors.white] , w )
- }
- return line
- end
- local screen = {} -- Stores each line of the computer's screen
- local w,h = term.getSize()
- for i = 1, h do
- screen[i] = createLine(colors.black)
- end
- local buffer -- Stores which lines have been updated and need redrawing
- local api = {} -- Returned for the program implementing this API to use
- function api.buffer(text)
- if not buffer then buffer = {} end
- local curX,curY = term.getCursorPos()
- local maxX = curX + #text
- local minX = curX - 1
- screen[curY].text = screen[curY].text:sub(1,minX)..text..screen[curY].text:sub(maxX)
- screen[curY].textColor = screen[curY].textColor:sub(1,minX)..string_rep( toHex[term.getTextColor()] ,#text)..screen[curY].textColor:sub(maxX)
- screen[curY].backColor = screen[curY].backColor:sub(1,minX)..string_rep( toHex[term.getBackgroundColor()] ,#text)..screen[curY].backColor:sub(maxX)
- buffer[curY] = true
- term.setCursorPos(maxX-1,curY)
- end
- function api.writeToScreen()
- local startX,startY = term.getCursorPos()
- local w,h = term.getSize()
- if not buffer then return end
- for i = 1, h do
- if buffer[i] then
- term.setCursorPos(1,i)
- local line = screen[i]
- term.blit(line.text,line.textColor,line.backColor)
- end
- end
- buffer = nil
- term.setCursorPos(startX,startY)
- end
- function api.clearScreen()
- local w,h = term.getSize()
- local curBack = term.getBackgroundColor()
- if not buffer then buffer = {} end
- for i = 1, h do
- screen[i] = createLine(curBack)
- buffer[i] = true
- end
- end
- function api.focreRedraw()
- local startX,startY = term.getCursorPos()
- local w,h = term.getSize()
- for i = 1, h do
- term.setCursorPos(1,i)
- local line = screen[i]
- term.blit(line.text,line.textColor,line.backColor)
- end
- term.setCursorPos(startX,startY)
- end
- return api
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement