Advertisement
nonogamer9

Plasma Tier 3 GPU Buffering Demo For OpenComputers (Version 0.1)

Jul 23rd, 2024 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.54 KB | Software | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local computer = require("computer")
  4. local event = require("event")
  5.  
  6. -- Screen dimensions
  7. local w, h = gpu.getResolution()
  8.  
  9. -- Optimization: Precalculate constants
  10. local pi2 = math.pi * 2
  11. local hw, hh = w / 2, h / 2
  12.  
  13. -- Plasma parameters
  14. local time = 0
  15. local speed = 0.05
  16.  
  17. -- Color palette (optimized for performance)
  18. local colors = {
  19.   0x000000, 0x0000FF, 0x00FFFF, 0x00FF00,
  20.   0xFFFF00, 0xFF0000, 0xFF00FF, 0xFFFFFF
  21. }
  22.  
  23. -- Initialize buffers
  24. local frontBuffer = {}
  25. local backBuffer = {}
  26. for y = 1, h do
  27.   frontBuffer[y] = {}
  28.   backBuffer[y] = {}
  29.   for x = 1, w do
  30.     frontBuffer[y][x] = 0
  31.     backBuffer[y][x] = 0
  32.   end
  33. end
  34.  
  35. -- Precalculate sine lookup table
  36. local sinLookup = {}
  37. for i = 0, 255 do
  38.   sinLookup[i] = math.sin(i * pi2 / 256)
  39. end
  40.  
  41. -- Fast sine approximation
  42. local function fastSin(x)
  43.   return sinLookup[math.floor(x * 40.743) % 256]
  44. end
  45.  
  46. -- Efficient drawing function
  47. local function drawChanges(changes)
  48.   local lastColor = nil
  49.   for i = 1, #changes do
  50.     local change = changes[i]
  51.     if change[3] ~= lastColor then
  52.       gpu.setBackground(change[3])
  53.       lastColor = change[3]
  54.     end
  55.     gpu.set(change[1], change[2], " ")
  56.   end
  57. end
  58.  
  59. -- Swap buffers and update screen (minimized GPU calls)
  60. local function swapBuffers()
  61.   local changes = {}
  62.   for y = 1, h do
  63.     for x = 1, w do
  64.       if backBuffer[y][x] ~= frontBuffer[y][x] then
  65.         table.insert(changes, {x, y, colors[backBuffer[y][x]]})
  66.         frontBuffer[y][x] = backBuffer[y][x]
  67.       end
  68.     end
  69.   end
  70.  
  71.   -- Batch update GPU
  72.   drawChanges(changes)
  73. end
  74.  
  75. -- FPS counter variables
  76. local fpsCount = 0
  77. local fpsTimer = 0
  78. local currentFPS = 0
  79.  
  80. -- Function to update FPS
  81. local function updateFPS()
  82.   fpsCount = fpsCount + 1
  83.   local currentTime = computer.uptime()
  84.   if currentTime - fpsTimer >= 1 then
  85.     currentFPS = fpsCount
  86.     fpsCount = 0
  87.     fpsTimer = currentTime
  88.   end
  89. end
  90.  
  91. -- Function to display FPS
  92. local function displayFPS()
  93.   gpu.setBackground(0x000000)
  94.   gpu.setForeground(0xFFFFFF)
  95.   gpu.set(1, 1, string.format("FPS: %d", currentFPS))
  96. end
  97.  
  98. -- Main plasma update function
  99. local function updatePlasma()
  100.   for y = 1, h do
  101.     for x = 1, w do
  102.       -- Optimized plasma calculation using fast sine
  103.       local v = fastSin(x / 16 + time)
  104.         + fastSin(y / 8 + time)
  105.         + fastSin((x + y) / 16 + time)
  106.         + fastSin(math.sqrt((x-hw)^2 + (y-hh)^2) / 8)
  107.      
  108.       -- Map value to color index
  109.       local index = math.floor((v + 4) * (#colors / 8)) % #colors + 1
  110.      
  111.       -- Update back buffer
  112.       backBuffer[y][x] = index
  113.     end
  114.   end
  115.  
  116.   -- Swap buffers and update screen
  117.   swapBuffers()
  118.  
  119.   -- Update time (with wrapping to prevent overflow)
  120.   time = (time + speed) % pi2
  121. end
  122.  
  123. -- Main loop
  124. local running = true
  125. event.timer(0, function()
  126.   while running do
  127.     local startTime = computer.uptime()
  128.    
  129.     updatePlasma()
  130.     updateFPS()
  131.     displayFPS()
  132.    
  133.     local endTime = computer.uptime()
  134.     local frameTime = endTime - startTime
  135.     local sleepTime = math.max(0, 1/60 - frameTime)  -- Aim for 60 FPS
  136.     os.sleep(sleepTime)
  137.   end
  138. end, math.huge)
  139.  
  140. -- Event handler to stop the program
  141. event.listen("key_down", function(_, _, _, code)
  142.   if code == 16 then  -- 'Q' key
  143.     running = false
  144.     event.push("interrupt")
  145.   end
  146. end)
  147.  
  148. -- Wait for interrupt
  149. event.pull("interrupt")
  150. gpu.setBackground(0x000000)
  151. gpu.setForeground(0xFFFFFF)
  152. gpu.fill(1, 1, w, h, " ")
  153. print("Plasma effect stopped.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement