Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local gpu = component.gpu
- local computer = require("computer")
- local event = require("event")
- -- Screen dimensions
- local w, h = gpu.getResolution()
- -- Optimization: Precalculate constants
- local pi2 = math.pi * 2
- local hw, hh = w / 2, h / 2
- -- Plasma parameters
- local time = 0
- local speed = 0.05
- -- Color palette (optimized for performance)
- local colors = {
- 0x000000, 0x0000FF, 0x00FFFF, 0x00FF00,
- 0xFFFF00, 0xFF0000, 0xFF00FF, 0xFFFFFF
- }
- -- Initialize buffers
- local frontBuffer = {}
- local backBuffer = {}
- for y = 1, h do
- frontBuffer[y] = {}
- backBuffer[y] = {}
- for x = 1, w do
- frontBuffer[y][x] = 0
- backBuffer[y][x] = 0
- end
- end
- -- Precalculate sine lookup table
- local sinLookup = {}
- for i = 0, 255 do
- sinLookup[i] = math.sin(i * pi2 / 256)
- end
- -- Fast sine approximation
- local function fastSin(x)
- return sinLookup[math.floor(x * 40.743) % 256]
- end
- -- Efficient drawing function
- local function drawChanges(changes)
- local lastColor = nil
- for i = 1, #changes do
- local change = changes[i]
- if change[3] ~= lastColor then
- gpu.setBackground(change[3])
- lastColor = change[3]
- end
- gpu.set(change[1], change[2], " ")
- end
- end
- -- Swap buffers and update screen (minimized GPU calls)
- local function swapBuffers()
- local changes = {}
- for y = 1, h do
- for x = 1, w do
- if backBuffer[y][x] ~= frontBuffer[y][x] then
- table.insert(changes, {x, y, colors[backBuffer[y][x]]})
- frontBuffer[y][x] = backBuffer[y][x]
- end
- end
- end
- -- Batch update GPU
- drawChanges(changes)
- end
- -- FPS counter variables
- local fpsCount = 0
- local fpsTimer = 0
- local currentFPS = 0
- -- Function to update FPS
- local function updateFPS()
- fpsCount = fpsCount + 1
- local currentTime = computer.uptime()
- if currentTime - fpsTimer >= 1 then
- currentFPS = fpsCount
- fpsCount = 0
- fpsTimer = currentTime
- end
- end
- -- Function to display FPS
- local function displayFPS()
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- gpu.set(1, 1, string.format("FPS: %d", currentFPS))
- end
- -- Main plasma update function
- local function updatePlasma()
- for y = 1, h do
- for x = 1, w do
- -- Optimized plasma calculation using fast sine
- local v = fastSin(x / 16 + time)
- + fastSin(y / 8 + time)
- + fastSin((x + y) / 16 + time)
- + fastSin(math.sqrt((x-hw)^2 + (y-hh)^2) / 8)
- -- Map value to color index
- local index = math.floor((v + 4) * (#colors / 8)) % #colors + 1
- -- Update back buffer
- backBuffer[y][x] = index
- end
- end
- -- Swap buffers and update screen
- swapBuffers()
- -- Update time (with wrapping to prevent overflow)
- time = (time + speed) % pi2
- end
- -- Main loop
- local running = true
- event.timer(0, function()
- while running do
- local startTime = computer.uptime()
- updatePlasma()
- updateFPS()
- displayFPS()
- local endTime = computer.uptime()
- local frameTime = endTime - startTime
- local sleepTime = math.max(0, 1/60 - frameTime) -- Aim for 60 FPS
- os.sleep(sleepTime)
- end
- end, math.huge)
- -- Event handler to stop the program
- event.listen("key_down", function(_, _, _, code)
- if code == 16 then -- 'Q' key
- running = false
- event.push("interrupt")
- end
- end)
- -- Wait for interrupt
- event.pull("interrupt")
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- gpu.fill(1, 1, w, h, " ")
- print("Plasma effect stopped.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement