TGC Codebase Backup



2D flame by Anonymous Coder

3rd Mar 2007 7:07
Summary

A simple but heavy 2D flame effect



Description


1. Divide your screen in boxes (2x2 pixels, or more. your choice).
2. Set each box with an warmth intensity variable(initialize them to 0).
3. Put high intensity to bottom boxes and calculate the upper ones.
4. To calculate intensity for a box get a mean value from the intensity of the boxes around it.
5. Do not forget the decay of the flame. The higher the box is the weaker the flame. Add a decay value and associate it with the height position of the current box on the screen.

optionally:
6. pick some boxes randomly and change manually their intensity values(--> UNIQUE FLAMES)



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem Project: 2D Flame
Rem Created: 26/10/2005 1:29:15 ðì

Rem ***** Main Source File *****

REM The general idea is to divide your screen into boxes, set the warmth of the bottom
REM ones and calculate upper pixels by adding the intensity of the pixels around them.
REM It is not hard to implement the idea but the code i produced is choppy and hard to
REM understand. I promised to recode the whole thing in a better way but never managed...


sync on
red = 255
green = 255
randomize timer()

rem the smaller the p value,the smaller the pixels(= more calculation=slow!!)
p = 3
movedown = 3
width = 640/p
height = movedown + (480)/p
cr = 765

dim color(width+1,height+1) as integer

decay = 0

for y = height to height - 5
    decay = decay + p
      for x = 1 to width
         color(x,y) = rnd(cr - decay)
         if color(x,y) <= 0 then color(x,y) = 1
      next x
next y

create bitmap 1,screen width(),screen height()

set current bitmap 0

x = 0
for y = 1 to height
   color(x,y) = 0
next y
x = width + 1
for y = 1 to height
   color(x,y) = 0
next y
y = height + 1
for x = 1 to width
   color(x,y) = 510
next x
y = 0
for x = 1 to width
   color(x,y) = 0
next x

repeat
   movedown = 0
   set current bitmap 1
   rem Set the first 3 lines of pixels with high colors again
   for y = height to height - 2
      for x = 1 to width
         if rnd(5)>1
             color(x,y) = rnd(165)+600
         else
             color(x,y) = rnd(165)
         endif
      next x
   next y

   for y = height to 2 step -1
       for x = 1 to width
           decay = (cr-(cr*y/height))*0.1
           I = color(x,y-1)+color(x-1,y-1)+color(x+1,y-1)+color(x,y)+color(x-1,y)+color(x+1,y)+color(x,y+1)+color(x+1,y+1)+color(x-1,y+1)
           k = rnd(3)-1
           color(x+k,y-2) = int(I/9) - decay
           if color(x+k,y-2) <= 0 then color(x+k,y-2) = 1

           rem calculate red,green values from color(x,y) value
           if color(x+k,y-2) >= 512
              red = 255
              green = 255
           else
              if color(x+k,y-2) >= 255
                 red = 255
                 green = color(x+k,y-2) - red
              else
                 red = color(x+k,y-2)
                 green = 0
              endif
           endif

           rem paste pixel on the screen with the right color
           ink rgb(red,green,0),0
           box p*(x-1),p*(y-2),p*x,p*(y+1)
       next x
   next y

copy bitmap 1,0
sync

rem RErandomize some of our values to produce unique flames
rem randomize only the 5% of the pixels for faster execution

   pixels = width * height
   Percent = int(pixels * 0.08)

   decay = 700
   for y = 1 to height
      decay = decay - (756*p/512)
      for x = 1 to width
         if rnd(pixels) <= percent then color(x,y) = rnd(55)+700-decay
         if rnd(pixels) <= 2*percent and rnd(pixels) > percent then color(x,y) = rnd(765)-decay/3

         if color(x,y) <= 0 then color(x,y) = 1
      next x
   next y

until escapekey() = 1

wait key