Click here to Skip to main content
15,899,026 members
Articles / Programming Languages / C

Fire Effect Created by Bitmap

Rate me:
Please Sign up or sign in to vote.
4.03/5 (16 votes)
25 Dec 2009CPOL2 min read 31.5K   950   13   7
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.

C++
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:

C++
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.

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
China China
Secret..

Comments and Discussions

 
GeneralUse a Particle System Pin
sps-itsec465-Jan-10 1:02
sps-itsec465-Jan-10 1:02 
Hi Aric,

for effects like fire, smoke, etc. you should try to use a Particle System. You will get very nice results with rather few lines of code.

Some links for you:
* http://en.wikipedia.org/wiki/Particle_system[^]
* http://flintparticles.org/examples/fire-and-smoke[^]
* http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=19[^]

Have fun!

cheers,
Michael

OMM: "Let us be thankful we have an occupation to fill. Work hard, increase production, prevent accidents and be happy."

GeneralRe: Use a Particle System Pin
Aric Wang5-Jan-10 16:02
Aric Wang5-Jan-10 16:02 
GeneralMy vote of 1 Pin
gbelov26-Dec-09 1:01
gbelov26-Dec-09 1:01 
GeneralRe: My vote of 1 Pin
Aric Wang26-Dec-09 3:23
Aric Wang26-Dec-09 3:23 
GeneralRe: My vote of 1 PinPopular
delyk26-Dec-09 5:54
delyk26-Dec-09 5:54 
GeneralRe: My vote of 1 Pin
Aric Wang26-Dec-09 13:51
Aric Wang26-Dec-09 13:51 
GeneralRe: My vote of 1 Pin
Mishra Vikas29-Dec-09 2:17
Mishra Vikas29-Dec-09 2:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.