Wish I had control over the palette. Anyway, simple method for a classic effect.
type Constants
ScreenWidth as word
ScreenHeight as word
endtype
Global System as Constants
System.ScreenWidth = 320
System.ScreenHeight = 240
set display mode 320,240,32
dim fire(320,240) as dword
rem controls height of flame value = [4,5]
Global flame as float
flame = 4.02
sync on
repeat
lock pixels
for y = 1 to 239
for x = 1 to 320
fire(x,y) = newValue(x,y)
pDot(x,y,fire(x,y))
next x
next y
for x = 1 to 320
r = rnd(255)
g = rnd(255)
if g>r then g = r
fire(x,240) = rgb(r,0,0)
pDot(x,240,fire(x,y))
next x
unlock pixels
if upkey() then inc flame, 0.02
if downkey() then dec flame, 0.02
text 1,1,"FPS: "+str$(screen fps())
text 1,12,str$(flame)
sync
until spacekey()
function newValue(x as integer, y as integer)
local c as dword
local r as integer
local g as integer
local b as integer
c = getColor(x,y+1)
r = rgbr(c)
g = rgbg(c)
b = rgbb(c)
c = getColor(x-1,y+1)
r = r + rgbr(c)
g = g + rgbg(c)
b = b + rgbb(c)
c = getColor(x+1,y+1)
r = r + rgbr(c)
g = g + rgbg(c)
b = b + rgbb(c)
c = getColor(x,y+2)
r = r + rgbr(c)
g = g + rgbg(c)
b = b + rgbb(c)
r = r/flame
g = g/flame
b = b/flame
c = rgb(r,g,b)
endfunction c
rem safe way to retrieve value from array
function getColor(x as integer, y as integer)
rem if index out of bounds
if x < 1 or x > 320 or y < 1 or y > 240 then exitfunction rgb(0,0,0)
v as dword
v = fire(x,y)
endfunction v
REM function borrowed from Coding Fodder's Rain code
function pDot(x as integer, y as integer, color_value as dword )
if x > 0 and x < System.ScreenWidth and y > 0 and y < System.ScreenHeight
start = get pixels pointer()
repeat_number = get pixels pitch()
bits_per_pixel = bitmap depth(num)/8
pointer = start + y*repeat_number + x*bits_per_pixel
*pointer = color_value
endif
endfunction
I made a java applet out of this code as well, here's a pic of that:
java applet code for those who are interested:
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.util.Date;
/**
* Class Fire - write a description of the class here
*
* @author Dr.Zimnox
* @version 9-13-2005
*/
public class Fire extends JApplet implements Runnable
{
public int width;
public int height;
public Color[][] fire;
private int time = 0;
private boolean isAnimating = false;
double flame = 4.04;
private int offset = 10;
public void init()
{
JRootPane rootPane = this.getRootPane();
rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
}
public void start()
{
Dimension d = this.getSize();
width = (int)d.getWidth();
height = (int)d.getHeight();
fire = new Color[width][height];
for(int y=0; y<height; y++)
for(int x=0; x<width; x++)
fire[x][y] = new Color(0,0,0);
startAnimating();
}
public void startAnimating()
{
Thread drawer = new Thread(this);
setIsAnimating(true);
drawer.start();
}
public void stopAnimating()
{
setIsAnimating(false);
}
public synchronized void setIsAnimating(boolean b)
{
isAnimating = b;
}
private synchronized boolean getIsAnimating()
{
return isAnimating;
}
public void stop()
{
this.stopAnimating();
}
public void paint(Graphics g)
{
//setBackground(new Color(100,100,100));
calculateNewPixels(g);
}
public void run()
{
while(getIsAnimating())
{
try
{
Thread.currentThread().sleep(2);
this.repaint();
}
catch(InterruptedException e)
{
System.out.println(e);
return;
}
}
this.repaint();
}
private void calculateNewPixels(Graphics g)
{
for(int y=0; y<height-1; y++)
for(int x=0; x<width; x++)
{
fire[x][y] = getNewColorValue(x,y);
g.setColor(fire[x][y]);
g.fillRect(x,y,1,1);
}
Random rand = new Random(new Date().getTime());
int y = height - 1;
for(int x=0; x<width; x++)
{
int v = rand.nextInt(256);
fire[x][y] = new Color(0,v,0);
g.setColor(fire[x][y]);
g.fillRect(x,y,1,1);
}
}
private Color getNewColorValue(int x, int y)
{
Color c = null;
int r = 0;
int g = 0;
int b = 0;
c = getColor(x,y+1);
r = c.getRed();
g = c.getGreen();
b = c.getBlue();
c = getColor(x-1,y+1);
r += c.getRed();
g += c.getGreen();
b += c.getBlue();
c = getColor(x+1,y+1);
r += c.getRed();
g += c.getGreen();
b += c.getBlue();
c = getColor(x,y+2);
r += c.getRed();
g += c.getGreen();
b += c.getBlue();
r /= flame;
g /= flame;
b /= flame;
return new Color(r,g,b);
}
private Color getColor(int x, int y)
{
if (x < 0 || x >= width || y < 0 || y >= height)
return new Color(0,0,0);
return fire[x][y];
}
public void destroy()
{
}
public String getAppletInfo()
{
return "Title: \nAuthor: \nA simple applet example description. ";
}
public String[][] getParameterInfo()
{
String paramInfo[][] = {
{"firstParameter", "1-10", "description of first parameter"},
{"status", "boolean", "description of second parameter"},
{"images", "url", "description of third parameter"}
};
return paramInfo;
}
}
Deadly Night Assassins