It’s like 20 lines of code to implement the memory bitmap and see it works as expected, also it’s for sure multiple orders of magnitudes more efficient (not only in code executed on average but also wrt to resources used) and it’s also much much simpler code than to try to manually optimize/handle offscreen pixels etc.
And no, memory bitmap does not create rectangles - all it does is, it makes the bitmap used for a texture available for modifications and update/reupload/reswizzle on demand (by the invalidate method) it’s contents as required for the GPU.
Below’s a simple sample with 250.000 pixels, drawing 500 random pixels during touches which runs pretty smooth on my 4 year old 50€ Android tablet.
Now if you want to change 5000 pixels smoothly each frame it’s going to be slow on lowspec devices using Corona as it’s not a natively compiled language - a memory bitmap will still be the only valid/best available choice (besides creating a native c++ extension).
[lua]
–
– main.lua
–
local memoryBitmap = require( “plugin.memoryBitmap” )
local tex = memoryBitmap.newTexture(
{
width = 500,
height = 500,
– format = “rgba” – (default)
– format = “rgb”
– format = “mask”
})
– Create image using the bitmap texture
local bitmap = display.newImageRect( tex.filename, tex.baseDir, 500, 500 )
bitmap.x = display.contentCenterX
bitmap.y = display.contentCenterY
– place some random pixels
for i = 1, 1000 do
tex:setPixel( math.random(0, 1000), math.random(0,1000), 1, 0, 0, 1 ) – Set pixel at (10,10) to be red
end
– Submit texture to be updated
tex:invalidate()
local r, g, b = 1, 1, 1
function touchHandler( event )
if event.phase == “began” then
r, g, b = math.random(), math.random(), math.random()
end
local bmp = event.target
local x, y = bmp:contentToLocal( event.x, event.y )
x = (x + bmp.width/2)
y = (y + bmp.height/2)
for i = 1, 500 do
local xx, yy = math.random(x-50, x+50), math.random(y-50, y+50)
tex:setPixel( xx, yy, r, g, b, 1 )
end
tex:invalidate()
end
bitmap:addEventListener( “touch”, touchHandler )
[/lua]