Skip to content

Instantly share code, notes, and snippets.

@oeloeloel
Created March 26, 2023 16:27
Show Gist options
  • Save oeloeloel/9b695c07d5b205df39d0204cd781aab5 to your computer and use it in GitHub Desktop.
Save oeloeloel/9b695c07d5b205df39d0204cd781aab5 to your computer and use it in GitHub Desktop.
buffer pixel array data to persist across ticks
# buffer pixel array data to persist across ticks
def tick args
dimension = 100 # keep it small and let the GPU scale it when rendering the sprite.
if args.tick_count.zero?
# Set up our "parray" pixel array and fill it with black pixels.
args.pixel_array(:parray).width = dimension
args.pixel_array(:parray).height = dimension
args.pixel_array(:parray).pixels.fill(0xFFFFFFFF, 0, dimension * dimension) # black, full alpha
# This variable persists the pixel data
# You can create it directly - you don't need to make the pixel array above
# I'm just demonstrating that, if you already have the array created, keep a copy
# Note that .dup doesn't seem to be needed
$pixel_buffer = args.pixel_array(:parray).pixels
end
# Modify the pixel buffer
random_pixel = rand(dimension * dimension)
random_color = rand(0xFFFFFFFF)
$pixel_buffer[random_pixel] = random_color
# effectively make a new pixel array with the same name each tick
# and fill it with your pixel buffer data
args.pixel_array(:parray).width = dimension
args.pixel_array(:parray).height = dimension
args.pixel_array(:parray).pixels = $pixel_buffer
# output the pixel array
args.outputs.primitives << [590, 310, dimension * 4, dimension * 4, :parray].sprite
args.outputs.primitives << args.gtk.current_framerate_primitives
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment