Qbasicnews.com

Full Version: Fire in Java
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, I just had to made another thread. This one is really cool.

I just wish there was a faster way to access the screen. Anyone know how I can reach Toshi? He may know this...

http://www.venosoft.com/ninkazu/fire.html

Code:
import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;
import java.util.Random;

public class fire extends Applet implements Runnable
{
    int maxparts = 9999;
    int [] firex = new int[maxparts];
    int [] firey = new int[maxparts];
    int [] firec = new int[maxparts];
    int [] presin = new int[361];
    int [] divide = new int[1024];
    int [] fullscreen = new int[64001];
    int [] yoff = new int [200];
    Color [] pal = new Color[256];

      Image imgbuffer = null;
    Graphics gbuffer = null;
    Random rnd = new Random();
    Thread app = null;

    public void start()
    {
        if( app == null )
        {
            app = new Thread(this);
            app.start();             //  This invokes run() (see below)
        }
    }

    public void init()
    {
        setBackground(Color.black);
        imgbuffer = createImage(320, 200);
        gbuffer = imgbuffer.getGraphics();
        gbuffer.setColor(Color.black);
          gbuffer.fillRect(0, 0, 320, 200);

        for (int k = 0; k<maxparts; k++)
        {
            firex[k] = 0;
            firey[k] = 0;
            firec[k] = 0;
        }
        for (int k = 0; k<200; k++)
        {
            yoff[k] = k*320;
        }
        for (int k = 0; k< 1024; k++){
            divide[k] = (int)(k/4.025);
        }
        for (int i = 0; i<=19; i++)
        {
            pal[i] = new Color((i*2)*4,0,0);
        }
        for (int i = 0; i<=31; i++)
        {
            pal[i+20] = new Color((int)((40+i/31.0*23.0)*4),(i*2)*4,0);
        }
        for (int i = 0; i<=31; i++)
        {
            pal[i+52] = new Color(254,254,i*4);
        }
    }
    public void paint(Graphics screen)
    {
        if (imgbuffer != null){
            screen.drawImage(imgbuffer, 0, 0, this);
        }
    }
    
    public void update(Graphics g)
    {
        paint(g);
    }

    public void run()
    {
        Graphics g = getGraphics();
        while (app != null)
        {
            eraseApp();
            paintApp(g);
            try
              {
                  Thread.sleep(10);    
              }catch(InterruptedException e) { stop(); }

        }
    }

    public void stop()
    {
        app = null;
    }

    public void paintApp(Graphics g)
    {
        int pix = 0, pix2=0, cc=0;
        for (int l = 0; l<maxparts; l++)
        {
            firey[l]--;
            firec[l]--;
            if (firec[l] < 0)
            {
                firec[l] = rnd.nextInt(68);
                firey[l] = 198;
                firex[l] = rnd.nextInt(318)+1;
            }
            fullscreen[yoff[firey[l]]+firex[l]] = firec[l];
        }

        for (int pass = 0; pass<=1; pass++)
        {
            pix = 32000;
            for (int y = 100; y<=198; y++)
            {
                for (int x = 1; x<=318; x++)
                {
                    pix2 = pix+x;
                    cc = divide[fullscreen[pix2-320] + fullscreen[pix2+320] + fullscreen[pix2-1] + fullscreen[pix2 + 1]];
                    fullscreen[pix2] = cc;
                }
                pix += 320;
            }
        }
        pix = 32000;
        for (int y = 100; y<=199; y++)
        {
            for (int x = 0; x<=319; x++)
            {
                pix2 = pix + x;
                gbuffer.setColor(pal[fullscreen[pix2]]);
                gbuffer.fillRect(x,y,1,1);
            }
            pix += 320;
        }
        g.drawImage(imgbuffer, 0, 0, this);
    }

    public void eraseApp()
    {
        gbuffer.setColor(Color.black);
        gbuffer.fillRect(0,0,320,200);
    }
}

And the QB
Code:
DEFINT A-Z
'$DYNAMIC

TYPE FireType
x AS INTEGER
y AS INTEGER
c AS INTEGER
END TYPE

CONST maxparts = 9999
DIM SHARED db(32001), YOff&(199), divide(256 * 4 - 1)
DIM SHARED Fire(maxparts) AS FireType

FOR i = 0 TO 256 * 4 - 1
divide(i) = i \ 4.025 'Lower this number to get more lingering flames
NEXT

db(0) = 2560
db(1) = 200

DEF SEG = VARSEG(db(0))

FOR i = 0 TO 199
YOff&(i) = i * 320& + 4
NEXT

SCREEN 13: CLS
PRINT "PRES A KEYE TO C TEH FIERZ!!!!11one"
SLEEP
CLS

'MACK TEH FIRY PALETET OMGLOL

OUT 968, 0

FOR i = 0 TO 19
   OUT 969, i * 2
   OUT 969, 0
   OUT 969, 0
NEXT

FOR i = 0 TO 31
   OUT 969, 40 + i / 31 * 23
   OUT 969, i * 2
   OUT 969, 0
NEXT
FOR i = 0 TO 31
   OUT 969, 63
   OUT 969, 63
   OUT 969, i
NEXT
'STRAT TEH MAI NLOOP

DO UNTIL INKEY$ = CHR$(27)

FOR i = 0 TO maxparts

'ADVNACE TH EFIRE PATRICLES
Fire(i).y = Fire(i).y - 1
Fire(i).c = Fire(i).c - 1

IF Fire(i).c < 0 THEN
   Fire(i).c = INT(RND * 68)
   Fire(i).y = 198
   Fire(i).x = INT(RND * 318) + 1
END IF

'DRAWE
p& = YOff&(Fire(i).y) + Fire(i).x
'IF Fire(i).c > PEEK(p&) THEN POKE p&, Fire(i).c
POKE p&, Fire(i).c
NEXT

'BLURZ0RZ

FOR pass = 0 TO 1
pix& = YOff&(100)
FOR y = 100 TO 198
FOR x = 1 TO 318
pix2& = pix& + x
cc = divide(PEEK(pix2& - 320) + PEEK(pix2& + 320) + PEEK(pix2& - 1) + PEEK(pix2& + 1))
POKE pix2&, cc
NEXT
pix& = pix& + 320
NEXT
NEXT

PUT (0, 0), db, PSET

LOOP
The one made in qb is quite fast. Can't say the same about the one made in Java.

Is this just your project to test the limits of Java? or You have to make it in Java?
test the limits. I did find a faster way to read and write pixels, so if you reload the fire applet, it should be going faster now.
Yeah, but that isnt the way you want to make it faster, is it?
The only thing that was slow were the graphics.... now it's fast. What more is there?