donderdag 7 april 2011

Single Core Atom Netbook gets 100 Frames per second on full screen pixel manipulation with Java.

I found a listing which builds a full screen with rainbow like colors. The frame rate is good. 100 frames per second. A single core netbook can do quake 3 with almost 60 frames per second.

I am studying the code and see the Raster is used.

Let me place the code here : ( I found it online - There is no credits in the src file.)

 
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

final public class JAppletLoop extends JFrame
{
    private static final int SCREEN_WIDTH = 1024;                           // Screen Width
    private static final int SCREEN_HEIGHT = 768;                           // Screen Height
    private static final int WIDTH_MUL_HEIGHT_MINUS_ONE=SCREEN_WIDTH*SCREEN_HEIGHT-1;

    final public static  void main( String[] JAppletLoop) throws Exception
    {
        new JAppletLoop();
    }

    JAppletLoop() throws Exception
    {

        Graphics2D g;
        final BufferedImage image= new BufferedImage(SCREEN_WIDTH,SCREEN_HEIGHT,BufferedImage.TYPE_INT_RGB);

/****************************************************************************************************************
 * Initalize Screen
 */
        // the buffer strategey used for the video double buffer
        BufferStrategy strategy;

        setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
        show();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        createBufferStrategy(2);
        strategy = getBufferStrategy();

        int frameCount=0;
        long threashold=System.currentTimeMillis();
        long time;
        int i;

        int[] pixelData=((DataBufferInt)image.getRaster().getDataBuffer()).getData();

        /****************************************************************************************************************
         * Game Loop
         */
        do
        {
            time=System.currentTimeMillis();    // the current timestamp
            frameCount++;
            if(time>threashold)
            {
                setTitle(""+frameCount);
                frameCount=0;
                threashold=time+1000-(time-threashold);
            }

            for (i=WIDTH_MUL_HEIGHT_MINUS_ONE;i>=0;i--)
            {
                pixelData[i]=(i)&0xFFFFFF;
            }



            // Get hold of a graphics context for the accelerated surface
            g = (Graphics2D) strategy.getDrawGraphics();
            g.drawImage(image,0,0,null);

            strategy.show();

        //    // give some cpu time to other processes
            Thread.yield();
        } while (true);

    }

}

(you can copy and paste the code in jbuilder or other java editor - name the project as the class name - be sure that the upper and lower case are identical.)

Geen opmerkingen:

Een reactie posten