Advertisement
electronic_steve

ESGraphics

Mar 14th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | None | 0 0
  1.  
  2.  
  3. r={}
  4. fps={}
  5. fps.old=os.time()
  6. fps.enable=true
  7. fps.count=0
  8. fps.fps=20
  9. c={1} --таблица цветов. просто для упрощения жизни.
  10. for i=1,15 do
  11. c[#c+1]=2^i
  12.  end
  13.  monitor = peripheral.wrap("right")
  14. r.s={} -- таблица для рендера.
  15. if monitor==nil then
  16. r.sx,r.sy=term.getSize() -- размер экрана
  17. else
  18. r.sx,r.sy=monitor.getSize()
  19. end
  20. r.d={} -- таблица для сравнения рендеров.
  21. r.se={} -- таблица несовпадений рендеров.
  22. r.to=os.time()
  23. r.renderspeed=0 -- чем больше число тем больше  сошибок делает рендер но fps будет выше. если поставить 0 то fps упадет до 10 до не будет глюков рендера.
  24. gh={}
  25. angle=0
  26. defbackcolor=16 -- цвет заднего фона.
  27. keypress=0
  28.  lcolor=1
  29.  
  30. function fps.update()
  31. local time=os.time()*1000*0.05
  32. if time>fps.old+1 then
  33. fps.old=time
  34. fps.fps=fps.count
  35. fps.count=0
  36.  
  37.  end
  38.  
  39.  
  40. return fps.fps
  41. end
  42.  
  43. function math.round(num, idp)
  44.  local mult = 10^(idp or 0)
  45.  return math.floor(num * mult + 0.5) / mult
  46. end
  47.  
  48. for y=1,r.sy do
  49. r.s[y]={}
  50. for x=1,r.sx do
  51. r.s[y][x]={16,16," "}
  52. end end
  53. for y=1,r.sy do
  54. r.d[y]={}
  55. for x=1,r.sx do
  56. r.d[y][x]={16,16,"X"}
  57. end end
  58.  
  59.  
  60. function gh.rectangle(mode,x,y,x2,y2,color,altcolor,text)
  61. local func={}
  62. func["line"]= function(x,y,x2,y2,color,altcolor,text)
  63. for nx=0,x2 do
  64. for ny=0,y2 do
  65. if nx==0 or nx==x2 or ny==0 or ny==y2 then
  66. gh.point(x+nx,y+ny,color,altcolor,text) end
  67.  
  68.  
  69. end end
  70.  end
  71. func["fill"]= function(x,y,x2,y2,color,altcolor,text)
  72. for nx=x,x2 do
  73. for ny=y,y2 do
  74.  
  75. gh.point(nx,ny,color,altcolor,text)
  76.  
  77.  
  78. end end
  79.  end
  80.  
  81.  func[mode](x,y,x2,y2,color,altcolor,text)
  82.  
  83. end
  84. function gh.point(x,y,color,altcolor,text)
  85. if altcolor==0 then
  86. r.s[y][x]={color,r.s[y][x][2],text} else
  87. r.s[y][x]={color,altcolor,text} end
  88. end
  89. function gh.text(x,y,color,altcolor,text)
  90. local X=0
  91.  
  92. for w in string.gmatch(text, ".") do
  93.  
  94. gh.point(x+X,y,color,altcolor,w)
  95. X=X+1
  96. end
  97. end
  98. function gh.centertext(y,color,altcolor,text)
  99. local X=0
  100.  
  101. for w in string.gmatch(text, ".") do
  102.  
  103. gh.point(X+math.floor((r.sx-gh.getTextWidth(text))/2),y,color,altcolor,w)
  104. X=X+1
  105. end
  106. end
  107. function gh.getTextWidth(text)
  108. local i=0
  109. for S in string.gmatch(text, ".") do
  110. i=i+1
  111. end
  112. return i
  113. end
  114. function gh.line(x2,y2,x3,y3,color,altcolor,text)
  115. local x=x3-x2
  116. local y=y3-y2
  117. local nx=x2
  118. local ny=y2
  119. local h=math.sqrt(x^2+y^2)
  120. local sina= y/h
  121. local cosa= x/h
  122.  
  123. for i=1,h do
  124. gh.point(math.floor(nx),math.floor(ny),color,altcolor,text)
  125.  
  126. nx=nx+cosa
  127. ny=ny+sina
  128.  
  129.  end
  130.  
  131. end
  132.  function r.draw()
  133.  
  134.  
  135. -- пререндер
  136.  
  137.  
  138.  
  139.  
  140. for y=1,r.sy do
  141. for x=1,r.sx do
  142. for i=1,3 do
  143. if  r.s[y][x][i]==r.d[y][x][i] then else
  144. r.se[#r.se+1]={y,x,r.s[y][x][1],r.s[y][x][2],r.s[y][x][3]}
  145. r.d[y][x][i]=r.s[y][x][i]
  146. r.s[y][x]={16,16," "}
  147. end
  148. end
  149. end
  150.  
  151.  
  152.  
  153.  
  154. -- рендер
  155. if #r.se>0 then
  156. for i=#r.se,1,-1 do
  157.  
  158.  if monitor==nil then
  159.  
  160.  term.setCursorPos(r.se[i][2],r.se[i][1])
  161. term.setTextColor(c[r.se[i][3] ])
  162. term.setBackgroundColor(c[r.se[i][4] ])
  163.  
  164. write(r.se[i][5])
  165.  
  166.  
  167.  else
  168.  monitor.setCursorPos(r.se[i][2],r.se[i][1])
  169. monitor.setTextColor(c[r.se[i][3] ])
  170. monitor.setBackgroundColor(c[r.se[i][4] ])
  171.  
  172. monitor.write(r.se[i][5])
  173.  
  174. r.se[i]=nil
  175.  end
  176.  
  177.  end
  178.  
  179.  
  180.  end
  181.  
  182. end
  183.  for y=1,r.sy do
  184. for x=1,r.sx do
  185. r.s[y][x]={16,16," "}
  186. end end
  187.  
  188.  end
  189.  function r.systemupdate()
  190.  
  191. update()
  192. fps.count=fps.count+1
  193. draw()
  194. r.draw()
  195. sleep(0)
  196.  end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement