ah, sorry yeah - i missed the context. i now grok that t is supposed to be sparse. each t[z] contains all the points that need to be drawn at a given brightness, yeah?
so, if we are really gonna be extreme about this (we’ve profiled the heck out of this, and we know that optimizing px_to_drawtable is crucial and worth burning some MB of RAM… (we only have one of these)
-- a brightness table shall have: a count of pixels, and a table of pixel vectors
local function level_table_new(max_pixels)
if max_pixels == nil then max_pixels = 128 * 64
local t = {}
t.count = 0
for i=1,max_pixels do
t{i} = {x=0, y=0}
end
return t
end
local function level_table_clear(level_tab)
level_tab.count = 0
end
local function level_table_add_px(level_tab, x, y)
level_tab.count = level_tab.count + 1
level_tab[level_tab.count].x = x
level_tab[level_tab.count].y = y
end
function clear_drawtable()
for i=1,64 do
level_table_clear(drawtable[i])
end
end
local drawtable = {}
function drawtable_init()
for i=1,64 do drawtable[i] = level_table_new() end
end
drawtable_init()
function bitmap_to_drawtable(bmp)
clear_drawtable()
for i=1,8 do for j=1,8 do
level_table_add_px(drawtable[bmp[i][j]], i, j)
end end
end
now, i have no real clue if that will be faster or not, it’s just something i would try - no allocations and no conditionals in the hot function.
but really, the answer to these things would be to have more drawing routines available. e.g. being able to make a cairo surface from a pixel array, persisted in RAM.