Per-pixel plasma / copperbars
Posted: Thu Mar 03, 2011 10:53 pm
Branching off my previous topic http://love2d.org/forums/viewtopic.php?f=4&t=2547 about per pixel manipulation speed; after taking into consideration that framebuffers won't work on all computers, I looked into the newImageData function to use that as a sort of buffer.
It's not fast, but at least it seems faster than love.graphics.point.
Please excuse the code, I've only learned the bare minimum of love to create this, if that..
MAIN.LUA
[edit] the plasma code is heavily based on a freebasic plasma a guy name Shockwave @ dbfinteractive.com wrote [/edit]
It's not fast, but at least it seems faster than love.graphics.point.
Please excuse the code, I've only learned the bare minimum of love to create this, if that..
MAIN.LUA
Code: Select all
initScreen = love.graphics.setMode( 800, 600, false, true, 0 )
pixBuf = love.image.newImageData( 512, 256 ) -- initialize PO2 buffer
pix = love.graphics.newImage( pixBuf )
pixUpdate = 0; pixOffsetX = 0; pixOffsetY = 0
f1 = 0; f2 = 0; f3 = 0
av = 0; av2 = 0; av3 = 0
l1 = 0; l2 = 0; b1 = 0
pm = 3.14 / 180
-- ---------------------------------------------------------------------
function love.update( dt )
pixUpdate = pixUpdate + 1
if pixUpdate > 1 then -- limit render FPS
plasma()
pixUpdate = 0
end
love.timer.sleep( 1 )
end
-- ---------------------------------------------------------------------
function love.draw()
love.graphics.draw( pix, pixOffsetX, pixOffsetY, 0, 2, 2, 0, 0 )
love.graphics.print("FPS: " .. love.timer.getFPS(), 1, 1 )
end
-- ---------------------------------------------------------------------
function preparePix()
collectgarbage('collect')
pixBuf = love.image.newImageData( 512, 256 )
end
-- ---------------------------------------------------------------------
function updatePix( pOX, pOY )
pixOffsetX = pOX; pixOffsetY = pOY
pix = love.graphics.newImage( pixBuf )
pix:setFilter( "nearest", "nearest" )
end
-- ---------------------------------------------------------------------
function plasma()
preparePix()
f1 = f1 + 1
f2 = f2 - 7
f3 = f3 + 3
for l1 = 0, 180 do -- plasma size 320 x 180 ( rendered 640 x 360 )
av = 50 * math.sin(( l1 + f1 ) * pm )
av2 = 60 + av * math.cos(( f2 + l1 ) * pm )
av3 = 100 + 10 * math.sin(( f1 + l1 ) / 10 )
b1 = 100 + 70 * ( math.cos( av2 + l1 )) / 8
for l2 = 0, 320 do
r = 140 + av2 * math.sin(( l2 + av2 + l1 ) / av3 )
pixBuf:setPixel(l2, l1, r, av2, b1, 255 )
end
end
updatePix( 80, 120 )
end