sync on : sync rate 0
 
`Fire colors
dim Color(2)
Color(1) = RGB(0,0,0) : Color(2) = RGB(255,79,0)
 
`Create memblock images
cls
get image 1,0,0,128,128, 1
make memblock from image 1, 1
make memblock from image 2, 1
make memblock from image 5, 1
 
`Create overlayer to fade flames slowly
box 0, 0, 128, 64, rgb(0, 0, 0), rgb(255, 255, 255), rgb(0,0,0), rgb(255,255,255)
get image 3, 0, 0, 128, 128, 1
make memblock from image 3, 3
 
`init
fimg = 2
 
cls
paste image 1, 0, 0
 
do
 
   `Draw fire
   for i = 0 to 127
      col = 1 + rnd(1)
      ink Color(col), 0
      box i, 127, i + 1, 128
   next i
 
   `Get image, blur it and position it one unit higher
   get image 1, 0, 0, 128, 128, 1
   make memblock from image 1, 1
   BlurImage(1, 4)
   BlendMapImageMemblocks(4, 5, 2, 3)
   make image from memblock 2, 2
   paste image 2, 0, -1
 
   sync
loop
 
function BlurImage(mem, retmem)
 
   `Get data
   width = memblock dword(mem, 0)
   height = memblock dword(mem, 4)
 
   `Create returned memblock
   if memblock exist(retmem) = 0 then make memblock retmem, get memblock size(mem)
   write memblock dword retmem, 0, width
   write memblock dword retmem, 4, height
   write memblock dword retmem, 8, 32
 
   `Write data
   `No edges:
   for y = 2 to height - 1
      for x = 2 to width - 1
 
         `Get position
         pos = 12 + ((y-1)*width*4) + ((x-1)*4)
 
         `Initialize values
         blue# = memblock byte(mem, pos) * 0.5
         green# = memblock byte(mem, pos+1) * 0.5
         red# = memblock byte(mem, pos+2) * 0.5
 
         `Get surrounding data
         `Left
         npos = pos - 4
         blue# = blue# + (memblock byte(mem, npos)*0.08)
         green# = green# + (memblock byte(mem, npos+1)*0.08)
         red# = red# + (memblock byte(mem, npos+2)*0.08)
 
         `+-> Top Left
         npos = npos - (width*4)
         blue# = blue# + (memblock byte(mem, npos)*0.045)
         green# = green# + (memblock byte(mem, npos+1)*0.045)
         red# = red# + (memblock byte(mem, npos+2)*0.045)
 
         `Right
         npos = pos + 4
         blue# = blue# + (memblock byte(mem, npos)*0.08)
         green# = green# + (memblock byte(mem, npos+1)*0.08)
         red# = red# + (memblock byte(mem, npos+2)*0.08)
 
         `+-> Bottom right
         npos = npos + (width*4)
         blue# = blue# + (memblock byte(mem, npos)*0.045)
         green# = green# + (memblock byte(mem, npos+1)*0.045)
         red# = red# + (memblock byte(mem, npos+2)*0.045)
 
         `Top
         npos = pos - (width*4)
         blue# = blue# + (memblock byte(mem, npos)*0.08)
         green# = green# + (memblock byte(mem, npos+1)*0.08)
         red# = red# + (memblock byte(mem, npos+2)*0.08)
 
         `+-> top right
         npos = npos + 4
         blue# = blue# + (memblock byte(mem, npos)*0.045)
         green# = green# + (memblock byte(mem, npos+1)*0.045)
         red# = red# + (memblock byte(mem, npos+2)*0.045)
 
         `Bottom
         npos = pos + (width*4)
         blue# = blue# + (memblock byte(mem, npos)*0.08)
         green# = green# + (memblock byte(mem, npos+1)*0.08)
         red# = red# + (memblock byte(mem, npos+2)*0.08)
 
         `+-> Bottom left
         npos = npos - 4
         blue# = blue# + (memblock byte(mem, npos)*0.045)
         green# = green# + (memblock byte(mem, npos+1)*0.045)
         red# = red# + (memblock byte(mem, npos+2)*0.045)
 
         `Write away data
         write memblock byte retmem, pos, int(blue#+0.5)
         write memblock byte retmem, pos+1, int(green#+0.5)
         write memblock byte retmem, pos+2, int(red#+0.5)
         write memblock byte retmem, pos+3, 255
 
      next x
   next y
 
   `Edges
   for y = 1 to height step (height-1)
      for x = 1 to width
 
         `Get position
         pos = 12 + ((y-1)*width*4) + ((x-1)*4)
 
         `Initialize colors
         if x = 1 or x = width
            blue# = memblock byte(mem, pos) * 0.795
            green# = memblock byte(mem, pos+1) * 0.795
            red# = memblock byte(mem, pos+2) * 0.795
         else
            blue# = memblock byte(mem, pos) * 0.67
            green# = memblock byte(mem, pos+1) * 0.67
            red# = memblock byte(mem, pos+2) * 0.67
         endif
 
         `Get other colors
         if x > 1
            npos = pos - 4
            blue# = blue# + memblock byte(mem, npos) * 0.08
            green# = green# + memblock byte(mem, npos+1) * 0.08
            red# = red# + memblock byte(mem, npos+2) * 0.08
         endif
         if x < width
            npos = pos + 4
            blue# = blue# + memblock byte(mem, npos) * 0.08
            green# = green# + memblock byte(mem, npos+1) * 0.08
            red# = red# + memblock byte(mem, npos+2) * 0.08
         endif
 
         if y > 1 then npos = pos - (width*4) else npos = pos + (width*4)
         blue# = blue# + memblock byte(mem, npos) * 0.08
         green# = green# + memblock byte(mem, npos+1) * 0.08
         red# = red# + memblock byte(mem, npos+2) * 0.08
 
         if x > 1
            mpos = npos - 4
            blue# = blue# + memblock byte(mem, mpos) * 0.045
            green# = green# + memblock byte(mem, mpos+1) * 0.045
            red# = red# + memblock byte(mem, mpos+2) * 0.045
         endif
         if x < width
            mpos = npos + 4
            blue# = blue# + memblock byte(mem, mpos) * 0.045
            green# = green# + memblock byte(mem, mpos+1) * 0.045
            red# = red# + memblock byte(mem, mpos+2) * 0.045
         endif
 
         `Write away data
         write memblock byte retmem, pos, int(blue#+0.5)
         write memblock byte retmem, pos+1, int(green#+0.5)
         write memblock byte retmem, pos+2, int(red#+0.5)
         write memblock byte retmem, pos+3, 255
 
      next x
   next y
 
   `Left and right edges (no corners anymore)
   for x = 1 to width step (width-1)
      for y = 2 to height - 1
 
         `Get position
         pos = 12 + ((y-1)*width*4) + ((x-1)*4)
 
         `Initialize colors
         blue# = memblock byte(mem, pos) * 0.67
         green# = memblock byte(mem, pos+1) * 0.67
         red# = memblock byte(mem, pos+2) * 0.67
 
         `Get surrounding data
         npos = pos - (width*4)
         blue# = blue# + memblock byte(mem, npos) * 0.08
         green# = green# + memblock byte(mem, npos+1) * 0.08
         red# = red# + memblock byte(mem, npos+2) * 0.08
 
         npos = pos + (width*4)
         blue# = blue# + memblock byte(mem, npos) * 0.08
         green# = green# + memblock byte(mem, npos+1) * 0.08
         red# = red# + memblock byte(mem, npos+2) * 0.08
 
         if x > 1 then npos = pos - 4 else npos = pos + 4
         blue# = blue# + memblock byte(mem, npos) * 0.08
         green# = green# + memblock byte(mem, npos+1) * 0.08
         red# = red# + memblock byte(mem, npos+2) * 0.08
 
         mpos = npos - (width*4)
         blue# = blue# + memblock byte(mem, mpos) * 0.045
         green# = green# + memblock byte(mem, mpos+1) * 0.045
         red# = red# + memblock byte(mem, mpos+2) * 0.045
         mpos = npos + (width*4)
         blue# = blue# + memblock byte(mem, mpos) * 0.045
         green# = green# + memblock byte(mem, mpos+1) * 0.045
         red# = red# + memblock byte(mem, mpos+2) * 0.045
 
         `Write away data
         write memblock byte retmem, pos, int(blue#+0.5)
         write memblock byte retmem, pos+1, int(green#+0.5)
         write memblock byte retmem, pos+2, int(red#+0.5)
         write memblock byte retmem, pos+3, 255
 
      next y
   next x
 
endfunction
 
function BlendMapImageMemblocks(Mem1, Mem2, RetMem, Map)
 
   `Get data
   Width = memblock dword(Mem1, 0)
   Height = memblock dword(Mem1, 4)
 
   `setup teh returned memblock
   if memblock exist(RetMem)=0 then make memblock RetMem, 12 + (Width*Height*4)
   write memblock dword RetMem, 0, Width
   write memblock dword RetMem, 4, Height
   write memblock dword RetMem, 8, 32
 
   `blend
   for y = 1 to Height
      for x = 1 to Width
         pos = 12 + ((y-1)*Width*4) + ((x-1)*4)
         difblue#  = (memblock byte(Mem2, pos)   - memblock byte(Mem1, pos))
         difgreen# = (memblock byte(Mem2, pos+1) - memblock byte(Mem1, pos+1))
         difred#   = (memblock byte(Mem2, pos+2) - memblock byte(Mem1, pos+2))
         blue  = memblock byte(Mem1, pos)   + int(difblue# / 255.0 * memblock byte(Map, pos))
         green = memblock byte(Mem1, pos+1) + int(difgreen#/ 255.0 * memblock byte(Map, pos+1))
         red   = memblock byte(Mem1, pos+2) + int(difred#  / 255.0 * memblock byte(Map, pos+2))
         write memblock byte RetMem, pos, blue
         write memblock byte RetMem, pos+1, green
         write memblock byte RetMem, pos+2, red
         write memblock byte RetMem, pos+3, 255
      next x
   next y
 
endfunction