Advertisement
nonogamer9

Plasma ASCII GPU Buffering Demo For OpenComputers (Version 1.0)

Jul 23rd, 2024 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.36 KB | Software | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local computer = require("computer")
  4.  
  5. local width, height = gpu.getResolution()
  6. local bufferIndex = gpu.allocateBuffer(width, height)
  7.  
  8. local colorMap = {}
  9. local chars = " .:-=+*#%@"
  10. for i = 0, 255 do
  11.     local value = (i / 255) * 2 - 1
  12.     local index = math.floor((value + 1) * (#chars - 1) / 2) + 1
  13.     colorMap[i] = chars:sub(index, index)
  14. end
  15.  
  16. local function plasma(x, y, t)
  17.     local value = math.sin(x * 0.1 + t) + math.sin(y * 0.1 + t) +
  18.                   math.sin((x + y) * 0.1 + t) + math.sin(math.sqrt(x * x + y * y) * 0.1 + t)
  19.     return math.floor((value / 4 + 1) * 127.5)
  20. end
  21.  
  22. local t = 0
  23. local lastTime = computer.uptime()
  24. local frameCount = 0
  25.  
  26. while true do
  27.     gpu.setActiveBuffer(bufferIndex)
  28.    
  29.     for y = 1, height do
  30.         for x = 1, width do
  31.             local value = plasma(x, y, t)
  32.             gpu.set(x, y, colorMap[value])
  33.         end
  34.     end
  35.    
  36.     gpu.setActiveBuffer(0)
  37.     gpu.bitblt(0, 1, 1, width, height, bufferIndex)
  38.    
  39.     t = t + 0.1
  40.     frameCount = frameCount + 1
  41.    
  42.     local currentTime = computer.uptime()
  43.     if currentTime - lastTime >= 1 then
  44.         local fps = frameCount / (currentTime - lastTime)
  45.         gpu.set(1, 1, string.format("FPS: %.2f", fps))
  46.         frameCount = 0
  47.         lastTime = currentTime
  48.     end
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement