PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Programming Fire effects

First i want to thank Phantom cause i'm really having great fun using easy CE !!! done

I'm coding some fire effects - works well, very very nice ! (tell me if you are interested by sources then i put it on ftp) i found explainations on

http://logarno.planet-d.net/djgfxfr/

the problem in that source is that i set a palet of colours, then for each pix i have to find the average of 3 or 4 other pixs. when i use getpixel, i get the real value of the pix, but not a palet index so i have to test all the values of my palet to retrieve the index wich makes it a little slow :(.

so here is my question: how can i quickly find the palet[index] of a pixel without testing all the palet values ???
Lewil · 11 Feb 2002, 10:51 UTC — oh oh
i realise that the url i gave you is in french :/
just look at the bottom page fire sample which looks like what i get on my ipaq.
Perhaps you can do the fire effect in a 240x320 byte buffer; then you can update the physical display each frame by converting this byte buffer to a 16-bit buffer using your palette. Advantage of this approach is that you never have to do a reverse palette operation; as you found out, going from a palette entry to 16bit is easy, the other way is terribly slow and should be avoided at all times.
Lewil · 11 Feb 2002, 15:28 UTC — yes !
this is what i've done, following your advice, this is faster but doing it on the full screen is too slow
anyway, that's a wonderful effect !!!

THX

Code:

#include "EasyCE.h"
unsigned short* buff;
unsigned short dummy[240*320];

unsigned short pal[257];

unsigned short Crvb(byte R, byte G, byte B) ;
void initialisation();
unsigned short moyenne( int x, int y );
void swap();

// ************** retour d'une couleur à partir d'un RVB ***************
unsigned short Crvb(byte R, byte G, byte B)
{ return ((R>>3)<<11) + ((G>>2)<<5) + (B>>3); }

// ************** initialisation ***************************************
void initialisation()
{

clear(0);
for (int x = 0; x<240; x++)
for (int y = 0; y<320; y++)
dummy[x+240*y] = 0;

for(int i=0; i<16; i++)
{
pal[i+48] = Crvb(255,255,255); // blanc
pal[i+32] = Crvb(255,255,i*16); // blanc->jaune
pal[i+16] = Crvb(255,i*16,0); // jaune->rouge
pal[i] = Crvb(i*16,0,0); // rouge->noir
}
}


// ************** average of 3 pix under ************
unsigned short moyenne( int x, int y )
{
int dest = x - 1 + 240*(y+1);
return (int)( (dummy[dest] + dummy[dest+1] + dummy[dest+2] ) / 3 );
}


// ****************** processus feu ******************
void process()
{
int dest;

// ************* last line gives life ****************
for(int x=0; x<240; x++)
if ( randint(100) > 50 )
dummy[x+240*319] = 63;
else
dummy[x+240*319] = 0;
// ******************** all others *******************
for( int i = 1; i<238; i++)
for (int j = 240; j<319; j++)
{
dest = i+240*j;
dummy[dest] = moyenne( i, j);
// colding
if( (dummy[dest] < 18) && (dummy[dest]>0) )
dummy[dest] --;
buff[i+240*j] = pal[dummy[dest]];
}
}

// **************** MAIN *************************************************
void main()
{
buff = (unsigned short*)getbuffer();
while (clicked()) { update(); };

initialisation();

while (!buttonA() && !buttonB())
{
process();
update();
}
}
You can easily make it fast enough. Start with this: The loops below 'all others' contain a nasty multiply. Switch the two for's, that way 'j*240' becomes a constant for 240 pixels. Calculate that value outside the loop. Now it should be (almost) fast enough.
Lewil · 11 Feb 2002, 17:08 UTC — Oh !!!!!!!!!!!!!!!
god, loops were swapped :oops: this explains some problems i had with colding... this makes a much nicer fire (and faster thanks to Phantom)*hopping that will contribute somewhere*

Code:
#include "EasyCE.h"
unsigned short* buff;
unsigned short dummy[240*321];

unsigned short pal[257];

unsigned short Crvb(byte R, byte G, byte B) ;
void initialisation();
unsigned short moyenne( int x, int y );
void swap();

// ************** retour d'une couleur à partir d'un RVB ***************
unsigned short Crvb(byte R, byte G, byte B)
{ return ((R>>3)<<11) + ((G>>2)<<5) + (B>>3); }

// ************** initialisation ***************************************
void initialisation()
{

clear(0);
for (int x = 0; x<240; x++)
{
for (int y = 0; y<320; y++)
dummy[x+240*y] = 0;
dummy[x+320*240] = 63;
}


for(int i=0; i<16; i++)
{
pal[i+48] = Crvb(255,255,255); // blanc
pal[i+32] = Crvb(255,255,i*16); // blanc->jaune
pal[i+16] = Crvb(255,i*16,0); // jaune->rouge
pal[i] = Crvb(i*16,0,0); // rouge->noir
}
}


// ************** average of 3 pix under ************
unsigned short moyenne( int x, int y )
{
int dest = x - 1 + 240*(y+1);
return (int)( (dummy[dest] + dummy[dest+1] + dummy[dest+2] + dummy[dest+241]) / 4 );
}


// ****************** processus feu ******************
void process()
{
int dest;

// ************* last line gives life ****************
for(int x=0; x<240; x++)
if ( randint(100) > 75 )
dummy[x+240*319] = 63;
else
dummy[x+240*319] = 0;
// ******************** all others *******************
for (int j = 240; j<319; j++)
{
dest = 240*j;
for( int i = 1; i<238; i++)
{
dummy[dest+i] = moyenne( i, j);
// colding
if( (randint(100) > 90 ) && (dummy[dest+i] > 1) )
dummy[dest+i] --;
buff[dest+i] = pal[dummy[dest+i]];
}
}
}

// **************** MAIN *************************************************
void main()
{
buff = (unsigned short*)getbuffer();
while (clicked()) { update(); };

initialisation();

while (!buttonA() && !buttonB())
{
process();
update();
}
}
Code:

if( (dummy[dest] < 18) && (dummy[dest]>0) )


I don't think that dummy[dest] will ever get below zero, since dummy is basically a byte array. :) Get rid of the >0 test to gain some more speed.

Also, remove the call to moyenne and paste that code right into the loop. That saves a function call per pixel (expensive!) and you'll notice that you can easily remove another multiply per pixel. Now it should be quite fast.
Jacco, I was thinking the same thing at first. The logic is to prevent dummy[dest] from being decremented if its value is already 0.

Dan East
yeah, dan is right, else dummy will become 0-1=255 and fire effect will look like shit:)
Oops, sorry. Overlooked that. That's OK though, since the other optimisation I suggested (removing the function call) will have much more impact.

By the way, I did some fire effects myself, and I always added four bytes and shifted them by 2. That way, the integer inaccuracy will make sure the fire intensity gets less without any if statement. The amount of decrease in intensity is not selectable that way however.
Lewil · 12 Feb 2002, 09:49 UTC — okay :)
done all this :) works fine

with the pad i control the randint test wich puts 63 or 0 to make the fire more or less intensive and the one that controls the colding (18) => you can burn your pocket the way you want :D

thx all, i try some water now :idea:
laurnet · 12 Feb 2002, 16:19 UTC — firestarter
hello
I am using MVC++ and easyce to compile on the computer ,and hey,it works!
now i can't make your fire code work because of byte undeclared and other stuff but it works well on evc

byte cannot be used ,are there lots of things that aren't same as evc or am i wrong ?
Hmm. Sounds like the EasyCE header may be missing a typedef for non-CE builds? Add this to the top of the source:
#typedef unsigned char byte

Dan East
Lewil · 13 Feb 2002, 11:08 UTC — sorry
i can't help cause i never succeeded to compile with MVC++ ... always saying something missing ie ptc.h even if it's in the dir ... :') evC works well, i keep on with it.

has someone some algorythm about waving a water surface (i've seen some nice effects in java on some web, when your mouse goes on a bmp, you get the little circles growing as if you were touching a water surface with your finger)
There is a doc about the water effect at http://freespace.virgin.net/hugo.elias/

There are also a bunch of other graphics programming related materials there too.
laurnet · 13 Feb 2002, 19:16 UTC — firestarter
dan's help made it work with no problems,
just add this byte declaration and it compile with
easyce and openptc.
Here's a doc about a water effect that I found particularly useful:
http://www.gamedev.net/reference/articl ... cle718.asp
It's written by Tom Hammersley, a 'not so nice guy', but this doc is well written. :)

UPDATE: Check this page for some more effects. At the bottom of the page there's another link to a water effect; the executable that you can download there could provide you with some more inspiration. :)
http://www.gamedev.net/reference/list.asp?categoryid=72

- Jacco.
Lewil · 14 Feb 2002, 13:50 UTC — hu hu :)
this is a very very interesting useless code for waving water surface on the Pocket by a boring meetings day... works well !!!! also added a rain line (remove '//')

a question on how to flip two char arrays (in the code) memcpy method should be slower, i feel an other way but i can't find how ... anyway, it's nice nice nice

Code:
#include "EasyCE.h"
unsigned short* buff;
char oldwater[240*321];
char newwater[240*321];
char flip[240*321];
// char *swap; => see question lower

unsigned short pal[256];

unsigned short Crvb(byte R, byte G, byte B) ;
void initialisation();

// ************** returns a color from RVB ***************
unsigned short Crvb(byte R, byte G, byte B)
{ return ((R>>3)<<11) + ((G>>2)<<5) + (B>>3); }

// ************** initialisation ***************************************
void initialisation()
{
clear(0);

unsigned char r=0,g=0,b=0;
for (short i=0;i<256;i++)
pal[i] = Crvb(0,0,0);
for (i=0;i<63;i++)
{
pal[i] = Crvb( 0,0,4*r);
r++;
}
for (i=63;i<127;i++)
{
pal[i] = Crvb( 0,4*g,4*63);
g++;
}
for (i=127;i<190;i++)
{
pal[i] = Crvb(4*b,4*63,4*63);
b++;
}
for (i=190;i<256;i++)
pal[i] = Crvb(4*63,4*63,4*63);

for (int x = 0; x<240; x++)
for (int y = 0; y<320; y++)
{
oldwater[x+240*y] = 0;
newwater[x+240*y] = 0;
}

}


// **************** MAIN *************************************************
void main()
{
int mousex;
int mousey;
int x;
int y;
int dest;

buff = (unsigned short*)getbuffer();
while (clicked()) { update(); };

initialisation();

while (!buttondown())
{
if ( pendown() )
{
mousex = penxpos();
mousey = penypos();
newwater[mousex + 240*mousey ] = -127;
}
// **** remove '//' to get rain *************
newwater[randint( 240 ) + 240*randint( 320) ] = -127;

for ( y = 1; y< 319; y++ )
{
dest = 240*y;
for ( x = 1; x<239; x++)
{
newwater[dest+x] = (char)( (
oldwater[dest-240+x] +
oldwater[dest+240+x] +
oldwater[dest+x-1] +
oldwater[dest+x+1] ) / 2 -
newwater[dest+x] );
buff[y*240+x] = pal[128-newwater[y*240+x]];
// ************ remove '//' to get smoother surface ****
// newwater[y*240+x] -= (newwater[y*240+x] >> 4);
}
}
// **** flip oldwater and newwater ***
memcpy( flip, oldwater, 240*320 );
memcpy( oldwater, newwater, 240*320 );
memcpy( newwater, flip, 240*320 );
// ??? how can i flip pointers without flipping values in it ?
// *swap = *oldwater;
// *oldwater = *newwater;
// *newwater = *swap;
update();
}
}
huh, the memcpy thing is a disaster:)))
you need to use pointers.
pointer point to a piece of memory.
with pointers instead all the memcpy you do it like this:
a-points to 1st list
b-points to 2nd list
temp-just an empty pointer
now instead memcpy you do
temp=a
a=b
b=temp
and you're done:)
and in practice...


Code:

char* pointera;
char* pointerb;
pointera=&a;//this makes pointera point to a place in memory where value a is stored
pointerb=&b;

and now you use pointera instead a etc.

Hope you can understand something from this, i'm not good at explaining stuff:/ If you can't, read some C book about pointers etc. It's a basic knowledge which you have to know in order to do any proper programming.

another thing you could do to optimize your code is to load 4 pixels at a time and calculate them all together. It might be as much as close to 4 times faster.
I haven't tested (or even compiled) this, so it may need tweaking:
Code:

#include "EasyCE.h"
unsigned short* buff;
char oldwater[240*321];
char newwater[240*321];
char *poldwater;
char *pnewwater;
char *pswap;

unsigned short pal[256];

unsigned short Crvb(byte R, byte G, byte B) ;
void initialisation();

// ************** returns a color from RVB ***************
unsigned short Crvb(byte R, byte G, byte B)
{ return ((R>>3)<<11) + ((G>>2)<<5) + (B>>3); }

// ************** initialisation ***************************************
void initialisation()
{
clear(0);

unsigned char r=0,g=0,b=0;
for (short i=0;i<256;i++)
pal[i] = Crvb(0,0,0);
for (i=0;i<63;i++)
{
pal[i] = Crvb( 0,0,4*r);
r++;
}
for (i=63;i<127;i++)
{
pal[i] = Crvb( 0,4*g,4*63);
g++;
}
for (i=127;i<190;i++)
{
pal[i] = Crvb(4*b,4*63,4*63);
b++;
}
for (i=190;i<256;i++)
pal[i] = Crvb(4*63,4*63,4*63);

for (int x = 0; x<240; x++)
for (int y = 0; y<320; y++)
{
oldwater[x+240*y] = 0;
newwater[x+240*y] = 0;
}

}


// **************** MAIN *************************************************
void main()
{
int mousex;
int mousey;
int x;
int y;
int dest;
char *tmp;

buff = (unsigned short*)getbuffer();
while (clicked()) { update(); };

initialisation();

poldwater=oldwater;
pnewwater=newwater;

while (!buttondown())
{
if ( pendown() )
{
mousex = penxpos();
mousey = penypos();
pnewwater[mousex + 240*mousey ] = -127;
}
// **** remove '//' to get rain *************
pnewwater[randint( 240 ) + 240*randint( 320) ] = -127;

for ( y = 1; y< 319; y++ )
{
dest = 240*y;
for ( x = 1; x<239; x++)
{
pnewwater[dest+x] = (char)( (
poldwater[dest-240+x] +
poldwater[dest+240+x] +
poldwater[dest+x-1] +
poldwater[dest+x+1] ) / 2 -
pnewwater[dest+x] );
buff[y*240+x] = pal[128-pnewwater[y*240+x]];
// ************ remove '//' to get smoother surface ****
// newwater[y*240+x] -= (newwater[y*240+x] >> 4);
}
}
// **** flip oldwater and newwater ***
pswap=poldwater;
poldwater=pnewwater;
pnewwater=pswap;

update();
}
}


Dan East
MirekCz beat me to it while I was typing. :)

Dan East
Lewil · 15 Feb 2002, 09:19 UTC — works fine
thanks for the lesson, i've understood what you've told me to do huhu :) works fine, very fast now.
laurnet · 17 Feb 2002, 16:04 UTC — dispaying a image
hello
i am trying to make a little "synchro with music program" for this water effect,
but i would like to display an image sometimes ,how can it be possible with pointers?
must i use memcpy?
What do you mean by 'display an image sometimes'?
Lewil · 18 Feb 2002, 11:25 UTC — may be
you should run the water engine every frame but make a sub for displaying and call that sub when some event happen ?

Code:

void displayframe()
{
unsigned short dest;
for ( int y=0; y<320; y++)
{
dest = 240*y;
for (int x=0; x<240; x++)
buff[dest+x] = pal[128-pnewwater[dest+x]];
}
}


and then call it from the main, "sometimes"

Code:

if ( event )
displayframe();
laurnet · 19 Feb 2002, 15:34 UTC — sometimes
sorry for my poor english,i am french..
I was meaning that the image is displayed every time the music is synchro :thats what i am calling sometimes
thanks lewill,its what i was looking for,
Lewil · 19 Feb 2002, 17:17 UTC — huhu
c'est ptet ben parceque mon english est aussi riche que le tien que j'ai bien répondu :roll:

Lewil
Angoulême
France

*that could be because my english talking is as nice as yours that i gave good answer*
laurnet · 19 Feb 2002, 17:57 UTC — frenchies
éh bien 2 francais sur le forum!
moi je suis a Paris

sorry no translation available
C'etait evident sans traduction quand-meme :-D

(Je ne suis pas francais mais j'habite en Luxembourg, donc j'apris quelque francais).

Salut!,

Refractor.
Zut alors, parlez vous Anglais sil vous plait. :)
That's about all I remembered from hischool French lessons I'm afraid. O yeah, 'une pain et un vain bitte'. ;)
Lewil · 20 Feb 2002, 09:38 UTC — huhu
for "Pain" i do not know... but for "Vin" (and not Vain) i do cause i'm very close from Bordeaux and Cognac... if i can contribute in EasyCE dev by sending a good bottle of Wine, let me know fantom, it could be a new form of freeware huhu... every one gives local goodies :P
That's very kind of you! However, that won't be neccessary. I have quite a collection at home. :)
laurnet · 20 Feb 2002, 12:47 UTC — nah
:roll: ,
oh moi jai une collection tres petite jai juste de la versinthe,
i got a small collection,only versinthe
Lewil · 22 Feb 2002, 15:36 UTC — hi
Laurnet you should try this:

this is a lightening generator (with a recursive call - i love this C abilitie :twisted: - ) and mixed with blur effect (from fire) it "follows" pen movment (to finish)

Code:

#include "EasyCE.h"
unsigned short* buff;
unsigned char blur[240*321];

unsigned short pal[256];

unsigned short Crvb(byte R, byte G, byte B) ;
void strike( int debx , int deby, int dx, int dy );
void initialisation();

// ************** retour d'une couleur à partir d'un RVB ***************
unsigned short Crvb(byte R, byte G, byte B)
{ return ((R>>3)<<11) + ((G>>2)<<5) + (B>>3); }

// ************** initialisation ***************************************
void initialisation()
{

clear(0);

for (int x = 0; x<240; x++)
for (int y = 0; y<320; y++)
blur[x+240*y] = 0;

unsigned char r=0,g=0,b=0;
for (short i=0;i<256;i++)
pal[i] = Crvb(0,0,0);
for (i=0;i<63;i++)
{
pal[i] = Crvb( 0, 0, 4*r);
r++;
}
for (i=63;i<127;i++)
{
pal[i] = Crvb( 0,4*g,4*g);
g++;
}
for (i=127;i<190;i++)
{
pal[i] = Crvb(4*63,4*63,4*b);
b++;
}
for (i=190;i<256;i++)
pal[i] = Crvb(4*63,4*63,4*63);

}

// ***** recursive strike for split ****************
void strike( int debx , int deby, int dx, int dy )
{
bool split = false;
while( !split)
{
debx+=dx+randint(3)-1;
deby+=dy+randint(3)-1;
if ( debx>0 && deby>0 && debx<240 && deby<318)
{
blur[debx+240*deby] = 255;
if ( randint(1000)>990 )
{
split = true;
strike( debx, deby, dx, dy );
strike( debx, deby, dx, dy );
}
}
else
split = true;
}
}

// **************** MAIN *************************************************
void main()
{
int ab;
int bb;

buff = (unsigned short*)getbuffer();
while (clicked()) { update(); };

initialisation();

while (!buttondown())
{
// **** a strike / each loop ***
if ( pendown())
strike( penxpos(), penypos(), randint(3)-1, randint(3)-1);
// **** blur ********************
for(ab=240;ab>0;ab--)
for(bb=318;bb>0;bb--)
{
blur[ab+bb*240]=((blur[(ab-1)+(bb-1)*240]+blur[(ab-1)+(bb+1)*240]+blur[(ab+1)+(bb-1)*240]+blur[(ab+1)+(bb+1)*240]) >> 2);
buff[ab+bb*240] = pal[blur[ab+bb*240]];
}
update();
}
}
laurnet · 22 Feb 2002, 16:17 UTC — awesome graphic effect!
hey its a really cool code!
Its like your making storms(eclairs)
I compiled it on computer 640*480,its pretty nice and fast,
synchronised with music it could look coool.
my pocketpc screen is broken,so can't test on it..
does someone have a screen to send to me?