Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Fire Effect Created by Bitmap

0.00/5 (No votes)
25 Dec 2009 1  
Shows you how to make a simple fire effect

Introduction

It's really hard to simulate fire, so I want to find a simple way to implement simulating the fire effect by bitmap.

Background

It will take a long time to become familiar with dib's API. Bitmaps and palettes are both potentially the most useful part and most confusing parts of the GDI subsystem to neophyte coders.

Principle

This is a simple application and the effect is not very good. I only want to find a better way to realize it. About this demo, I don't want to waste much of my time. So, sorry everyone. Now I'll have a short analysis about this app.

The main thinking of the demo is alert the bits of bitmap, the bits of DIB.

First I draw two lines at the bottom. Sorry that I use too many digits - it's easy for me, I am a special man that loves digits.

for(i=0;i<WIDTHBYTES(300)*7+20;i++)
   for(j=0;j<2;j++)
    {
    setPixel(pBits,i,j,RGB(255,max(rand()%200,80),0));
    setPixel(pBits,i,j,RGB(255,max(rand()%200,195),0)); 
    }

As we all see, the color of RED is assigned by a rand()%255, so its range is between 0~255. So that it could create many red points, we also need to create yellow points (I mean yellow color pixel) when the pixel has been created by a random number. Next, we want the pixel up. From bottom to top. Look at the snippet:

   for(i=0;i<300;i+=2)
   {
    for(j=rand()%300;j>1;j-=1)
    setPixel(pBits,i,j,getPixel(pBits,i,j-1));
   }

As the fire size is fixed, I use digit again. "300" means the height of bitmap is 300 pixels. To ensure each pixel has a different height, here use a random too. "j=rand()%300)" will create a value for j between 0~300.

Pixel could be from bottom to top now. But it doesn't fade. How do we do that? Of course, reduce the value of each pixel. FadeColor() is to do that.

void FadeColor(BYTE *pBits,int x,int y)
{
 pBits[y*300*3+x*3]=max(pBits[y*300*3+x*3]-6,0);
 pBits[y*300*3+x*3+1]=max(pBits[y*300*3+x*3+1]-6,0);
 pBits[y*300*3+x*3+2]=max(pBits[y*300*3+x*3+2]-3,0);
}

Digit "3" means the bitmap's bitcount is 24. (24/8=3). Every time reduce 6 of RGB value so that it will create a gradient. So easy!!

Want to know more? Please download the source code. I have a lot of things to do. Considering the time, I can't write too much. Thank you for all your support!!! Really! Hope to be your friend! Hope for your good vote!

History

  • 26th December, 2009: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here