Guest User

fire effect

a guest
May 6th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1.  
  2. // rand is a 16-bit integer
  3. u8 rand_bits = rand & 0b11;
  4.  
  5. // wptr is write pointer for the current row
  6. // apply a small random offset to make the fire move left or right
  7. u8* dst = wptr-rand_bits+1;
  8.  
  9.  
  10. // same here, wuptr is a write pointer for the row above   
  11. u8* udst = wuptr-rand_bits+1;
  12.  
  13.  
  14. // read a pair of pixels from framebuffer
  15. // rptr is a pointer to a couple rows down (to propagate the flames upward)
  16. u8 full_pix = *rptr;        
  17.  
  18. // if both pixels are completely black, just
  19. // short circuit the other work and write black pixels to the destination                  
  20. if(full_pix == 0) {    
  21.   // pixels are doubled vertically
  22.   *dst = 0x00;
  23.   *udst = 0x00;
  24.  } else {
  25.    // extract the right 4-bit pixel                
  26.   u8 pix_col = full_pix & 0b1111;
  27.  
  28.   // if black, write black pixels to the destination           
  29.   if(pix_col == 0) { *dst = 0x00; *udst = 0x00; }
  30.  
  31.  
  32.   // otherwise use a couple more random bits to select the color lookup table
  33.  
  34.   // fire_lut is a set of 4 16-color palettes that darken the index color you pass in
  35.   // which modify the left and right pixels in a byte differently
  36.   // increasing the apparent horizontal resolution
  37.  
  38.   // e.g.  
  39.   // fire_lut[0*16 + 0xF] => 0xEE
  40.   // fire_lut[1*16 + 0xF] => 0xFE
  41.   // fire_lut[2*16 + 0xF] => 0xEF
  42.   // fire_lut[3*16 + 0xF] => 0xFF ( no darkening 1/4 of the time)
  43.  
  44.   // to select the palette,
  45.   // grab random bits that are already in the right position
  46.   // (i.e. shifted left by 4, or multiplied by 16,
  47.   // because each palette is 16 entries)
  48.  
  49.   // we're using the same random bits multiple times over different iterations
  50.   // but it's ok as long as there are no obvious repeating patterns
  51.  
  52.   u8 dark_col = fire_lut[(rand&0b110000)+pix_col];
  53.  
  54.   *dst = dark_col;                         
  55.   *udst = dark_col;
  56.  }                                 
  57. // move all framebuffer pointers left by one pixel pair
  58. rptr--;                                
  59. wptr--;                                
  60. wuptr--;
  61.  
  62. // this used to be a shift by 2, to reuse less random bits
  63. // but shifting by 1 still looks as good and means we can call
  64. // random() less often
  65. rand >>= 1;
Add Comment
Please, Sign In to add comment