Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------
- -- Name: Buffer --
- -- --
- -- Info: gl is a buffer for --
- -- smoother rendering. --
- -- --
- -- Made by: LeDark Lua --
- ----------------------------------
- local _scp=term.setCursorPos
- local _sbl=term.blit
- local _colb=colors.black
- local _colw=colors.white
- local _tbc=table.concat
- local _colors = {[1]="0";[2]="1";[4]="2";[8]="3";[16]="4";[32]="5";[64]="6";[128]="7";[256]="8";[512]="9";[1024]="a";[2048]="b";[4096]="c";[8192]="d";[16384]="e";[32768]="f";}
- local _invColors={["0"]=1;["1"]=2;["2"]=4;["3"]=8;["4"]=16;["5"]=32;["6"]=64;["7"]=128;["8"]=256;["9"]=512;["a"]=1024;["b"]=2048;["c"]=4096;["d"]=8192;["e"]=16384;["f"]=32768;}
- local function getColor(c)
- local tbl={}
- for i=1, #c do
- tbl[#tbl+1]=_invColors[c:sub(i,i)]
- end
- return tbl
- end
- function create(x,y,w,h)
- local buffer={
- changed=true;
- x=x;
- y=y;
- width=w;
- height=h;
- }
- function buffer:setPos(x,y)
- self.x=x
- self.y=y
- self.changed=true
- end
- function buffer:getPos()
- return self.x, self.y
- end
- function buffer:setSize(w,h)
- self.width=w
- self.height=h
- self.changed=true
- end
- function buffer:getSize()
- return self.width, self.height
- end
- function buffer:clearLine(n, col, txtCol)
- for x=1, self.width do
- self[x..":"..n]={col or _colb,txtCol or _colw," "}
- end
- self.changed=true
- end
- function buffer:clear(col, col2)
- for y=1, self.height do
- self:clearLine(y, col, col2)
- end
- end
- function buffer:drawChar(x,y,chr,bck,txt)
- if #chr>1 then return end
- self[x..":"..y]={bck,txt,chr or " "}
- self.changed=true
- end
- function buffer:drawText(x,y,chr,bck,txt)
- for i=1, #chr do
- self:drawChar(x+(i-1),y,chr:sub(i,i),bck,txt)
- end
- end
- function buffer:draw(disp)
- if self.changed then
- local display=disp or term.current()
- local data={}
- local chr,backC,textC={},{},{}
- for y=1, self.height do
- for x=1, self.width do
- local _cbp=self[x..":"..y]
- chr[#chr+1]=_cbp[3] or " "
- backC[#backC+1]=_colors[_cbp[1] or 32768]
- textC[#textC+1]=_colors[_cbp[2] or 1]
- end
- data[#data+1]=y
- data[#data+1]=_tbc(chr)
- data[#data+1]=_tbc(textC)
- data[#data+1]=_tbc(backC)
- chr,backC,textC={},{},{}
- end
- for i=1,#data,4 do
- display.setCursorPos(self.x,(data[i]-1)+self.y)
- display.blit(data[i+1],data[i+2],data[i+3])
- end
- self.changed=false
- end
- end
- function buffer:getTerm()
- local term={}
- local self = self
- self.currX=1
- self.currY=1
- self.currBack=colors.black
- self.currText=colors.white
- function term.clear()
- self:clear(self.currBack, self.currText)
- end
- function term.clearLine(n)
- self:clearLine(n, self.currBack, self.currText)
- end
- function term.getSize()
- return self:getSize()
- end
- function term.isColor()
- return true
- end
- function term.setCursorPos(x,y)
- self.currX=x
- self.currY=y
- end
- function term.getCursorPos()
- return self.currX, self.currY
- end
- function term.setBackgroundColor(c)
- self.currBack=c
- end
- function term.setTextColor(c)
- self.currText=c
- end
- function term.getBackgroundColor()
- return self.currBack
- end
- function term.getTextColor()
- return self.currText
- end
- term.isColour = term.isColor
- term.setBackgroundColour=term.setBackgroundColor
- term.setTextColour=term.setTextColor
- function term.write(txt)
- for i=1, #txt do
- self:drawChar((self.currX-1)+i, self.currY, txt:sub(i,i), self.currBack, self.currText)
- end
- self.currX=self.currX + (#txt-1)
- end
- function term.blit(txt,txtc,bckc)
- local txtC=getColor(txtc)
- local bckC=getColor(bckc)
- for i=1, #txt do
- self:drawChar((self.currX-1)+i,self.currY,txt:sub(i,i),bckC[i],txtC[i])
- end
- self.currX=self.currX+(#txt-1)
- end
- return term
- end
- for yy=1, h do
- for xx=1, w do
- buffer[xx..":"..yy]={colors.black,colors.white,chr or " "}
- end
- end
- return buffer
- end
Advertisement
Add Comment
Please, Sign In to add comment