Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // rand is a 16-bit integer
- u8 rand_bits = rand & 0b11;
- // wptr is write pointer for the current row
- // apply a small random offset to make the fire move left or right
- u8* dst = wptr-rand_bits+1;
- // same here, wuptr is a write pointer for the row above
- u8* udst = wuptr-rand_bits+1;
- // read a pair of pixels from framebuffer
- // rptr is a pointer to a couple rows down (to propagate the flames upward)
- u8 full_pix = *rptr;
- // if both pixels are completely black, just
- // short circuit the other work and write black pixels to the destination
- if(full_pix == 0) {
- // pixels are doubled vertically
- *dst = 0x00;
- *udst = 0x00;
- } else {
- // extract the right 4-bit pixel
- u8 pix_col = full_pix & 0b1111;
- // if black, write black pixels to the destination
- if(pix_col == 0) { *dst = 0x00; *udst = 0x00; }
- // otherwise use a couple more random bits to select the color lookup table
- // fire_lut is a set of 4 16-color palettes that darken the index color you pass in
- // which modify the left and right pixels in a byte differently
- // increasing the apparent horizontal resolution
- // e.g.
- // fire_lut[0*16 + 0xF] => 0xEE
- // fire_lut[1*16 + 0xF] => 0xFE
- // fire_lut[2*16 + 0xF] => 0xEF
- // fire_lut[3*16 + 0xF] => 0xFF ( no darkening 1/4 of the time)
- // to select the palette,
- // grab random bits that are already in the right position
- // (i.e. shifted left by 4, or multiplied by 16,
- // because each palette is 16 entries)
- // we're using the same random bits multiple times over different iterations
- // but it's ok as long as there are no obvious repeating patterns
- u8 dark_col = fire_lut[(rand&0b110000)+pix_col];
- *dst = dark_col;
- *udst = dark_col;
- }
- // move all framebuffer pointers left by one pixel pair
- rptr--;
- wptr--;
- wuptr--;
- // this used to be a shift by 2, to reuse less random bits
- // but shifting by 1 still looks as good and means we can call
- // random() less often
- rand >>= 1;
Add Comment
Please, Sign In to add comment