Guest User

[CC] Buffer GL

a guest
Jan 2nd, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. ----------------------------------
  2. -- Name: Buffer --
  3. -- --
  4. -- Info: gl is a buffer for --
  5. -- smoother rendering. --
  6. -- --
  7. -- Made by: LeDark Lua --
  8. ----------------------------------
  9.  
  10. local _scp=term.setCursorPos
  11. local _sbl=term.blit
  12. local _colb=colors.black
  13. local _colw=colors.white
  14. local _tbc=table.concat
  15. 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";}
  16. 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;}
  17. local function getColor(c)
  18. local tbl={}
  19. for i=1, #c do
  20. tbl[#tbl+1]=_invColors[c:sub(i,i)]
  21. end
  22. return tbl
  23. end
  24.  
  25. function create(x,y,w,h)
  26. local buffer={
  27. changed=true;
  28. x=x;
  29. y=y;
  30. width=w;
  31. height=h;
  32. }
  33.  
  34. function buffer:setPos(x,y)
  35. self.x=x
  36. self.y=y
  37. self.changed=true
  38. end
  39. function buffer:getPos()
  40. return self.x, self.y
  41. end
  42.  
  43. function buffer:setSize(w,h)
  44. self.width=w
  45. self.height=h
  46. self.changed=true
  47. end
  48. function buffer:getSize()
  49. return self.width, self.height
  50. end
  51. function buffer:clearLine(n, col, txtCol)
  52. for x=1, self.width do
  53. self[x..":"..n]={col or _colb,txtCol or _colw," "}
  54. end
  55. self.changed=true
  56. end
  57. function buffer:clear(col, col2)
  58. for y=1, self.height do
  59. self:clearLine(y, col, col2)
  60. end
  61. end
  62. function buffer:drawChar(x,y,chr,bck,txt)
  63. if #chr>1 then return end
  64. self[x..":"..y]={bck,txt,chr or " "}
  65. self.changed=true
  66. end
  67. function buffer:drawText(x,y,chr,bck,txt)
  68. for i=1, #chr do
  69. self:drawChar(x+(i-1),y,chr:sub(i,i),bck,txt)
  70. end
  71. end
  72. function buffer:draw(disp)
  73. if self.changed then
  74. local display=disp or term.current()
  75. local data={}
  76. local chr,backC,textC={},{},{}
  77. for y=1, self.height do
  78. for x=1, self.width do
  79. local _cbp=self[x..":"..y]
  80. chr[#chr+1]=_cbp[3] or " "
  81. backC[#backC+1]=_colors[_cbp[1] or 32768]
  82. textC[#textC+1]=_colors[_cbp[2] or 1]
  83. end
  84. data[#data+1]=y
  85. data[#data+1]=_tbc(chr)
  86. data[#data+1]=_tbc(textC)
  87. data[#data+1]=_tbc(backC)
  88. chr,backC,textC={},{},{}
  89. end
  90. for i=1,#data,4 do
  91. display.setCursorPos(self.x,(data[i]-1)+self.y)
  92. display.blit(data[i+1],data[i+2],data[i+3])
  93. end
  94. self.changed=false
  95. end
  96. end
  97.  
  98. function buffer:getTerm()
  99. local term={}
  100. local self = self
  101. self.currX=1
  102. self.currY=1
  103. self.currBack=colors.black
  104. self.currText=colors.white
  105. function term.clear()
  106. self:clear(self.currBack, self.currText)
  107. end
  108. function term.clearLine(n)
  109. self:clearLine(n, self.currBack, self.currText)
  110. end
  111. function term.getSize()
  112. return self:getSize()
  113. end
  114. function term.isColor()
  115. return true
  116. end
  117. function term.setCursorPos(x,y)
  118. self.currX=x
  119. self.currY=y
  120. end
  121. function term.getCursorPos()
  122. return self.currX, self.currY
  123. end
  124. function term.setBackgroundColor(c)
  125. self.currBack=c
  126. end
  127. function term.setTextColor(c)
  128. self.currText=c
  129. end
  130. function term.getBackgroundColor()
  131. return self.currBack
  132. end
  133. function term.getTextColor()
  134. return self.currText
  135. end
  136. term.isColour = term.isColor
  137. term.setBackgroundColour=term.setBackgroundColor
  138. term.setTextColour=term.setTextColor
  139. function term.write(txt)
  140. for i=1, #txt do
  141. self:drawChar((self.currX-1)+i, self.currY, txt:sub(i,i), self.currBack, self.currText)
  142. end
  143. self.currX=self.currX + (#txt-1)
  144. end
  145. function term.blit(txt,txtc,bckc)
  146. local txtC=getColor(txtc)
  147. local bckC=getColor(bckc)
  148. for i=1, #txt do
  149. self:drawChar((self.currX-1)+i,self.currY,txt:sub(i,i),bckC[i],txtC[i])
  150. end
  151. self.currX=self.currX+(#txt-1)
  152. end
  153. return term
  154. end
  155.  
  156. for yy=1, h do
  157. for xx=1, w do
  158. buffer[xx..":"..yy]={colors.black,colors.white,chr or " "}
  159. end
  160. end
  161. return buffer
  162. end
Advertisement
Add Comment
Please, Sign In to add comment