Usage
This should be used for simple cases like basic pixel noise in small areas or in small amounts.
What this function does
It gives you a higher level wrapper for PICO-8s 4-bit framebuffer (0x6000-0x7fff) (lower 4-bits: left pixel, upper 4-bits: right pixel), and does some bitwise math and multiplication to put a single pixel on screen.
Code Snippet
NOTE: This function should be only used sparingly, it's expensive to use and not fully optimized (e.g. batching and minimizing the code to only required calculations) (using it heavily can cause severe lag).
function putpixel(x,y,col) -- floor x & y to stop rounding errors in pixel position calculations x, y = flr(x), flr(y) -- prevent pixel from wrapping around the screen horizontally if x > 127 or x < 0 then return end -- get 1 dimensional bit offset, multiplying y by the screen width to iterate by line local pixel_pos = x + (y*128) -- divide the pixel position in half to fit into the 4-bit offset to a 2 pixel pair in memory local offset = 0x6000 + pixel_pos\2 -- do not draw pixels if they are outside the framebuffer if offset < 0x6000 or offset > 0x7fff then return end -- get the 2 pixel pair of colors we are writing to local pixel = peek(offset) -- get which nybble (4-bit) offset the color resides on local nybble_shift = (pixel_pos&1) * 4 -- mask and set the pixel color from `nybble_shift` offset pixel &= ~(0x0f << nybble_shift) pixel |= col << nybble_shift -- write pixel to framebuffer poke(offset, pixel) end |
Example Cartridge
This example shows how the putpixel function only overrides pixels at their specified positions without completely removing other unrelated pixels from the screen.
Interesting as a demo of the pico-8 video memory layout, but I don't see why you'd use the function instead of pset(x,y,c).
Maybe for the independence from the current camera offset and draw palette ? but these are usually beneficial features.
Thanks. I honestly didn't think PICO-8 had that function. I'll consider editing and renaming it to something like "Direct Video Memory Example" later.
I'm just starting out so this is helpful. I go by example/research with trial and error when learning, later on reading docs when really trying something advanced.
[Please log in to post a comment]




