Hello, I recently started to search for high FPS 2D screen pixels manipulation, and realized I needed something faster than java2D. The big bottleneck is the permanent transfer between cpu memory and video card memory, especially for a full screen high resolution buffer. Until recently I worked on a 2D/3D creation software. It didn’t need high FPS but needed high quality and totally pixel controlled results (for the 2D creation part). I see in many posts that direct pixel manipulation is kind of discouraged by experienced posters. Well for now I would like to manipulate pixels anyway, and later learn other ways to do fast rendering. But I didn’t find a very clear example that does just that. I’m a little lost with the PBO / pbuffers /frame buffers usage. Here is a short class that just draws a random color on each pixel and shows FPS for java2D.
My question is what would be the equivalent code if I used LWJGL?
import java.awt.AWTException;
import java.awt.BufferCapabilities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.ImageCapabilities;
import java.awt.BufferCapabilities.FlipContents;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Random;
import javax.swing.JFrame;
public class Test
{ private final static GraphicsDevice screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
private final static Random random = new Random(System.currentTimeMillis());
public static void main(String[] args)
{ System.setProperty("sun.java2d.opengl", "true");
final JFrame f = new JFrame("TEST");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setUndecorated(true);
f.setResizable(false);
f.setIgnoreRepaint(true);
screen.setFullScreenWindow(f);
f.addKeyListener(new KeyListener()
{ public void keyPressed(KeyEvent e) {System.exit(0);}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
});
final int w = f.getWidth(), h = f.getHeight();
try {f.createBufferStrategy(2, new BufferCapabilities(new ImageCapabilities(true), new ImageCapabilities(true), FlipContents.UNDEFINED));}
catch (AWTException e) {e.printStackTrace();}
final BufferStrategy s = f.getBufferStrategy();
final BufferedImage img = screen.getDefaultConfiguration().createCompatibleImage(w, h);
final int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
long time = 0, frames = 0;
final long start = System.currentTimeMillis();
while (true) try
{ final Graphics g = s.getDrawGraphics();
for (int k = w*h; k-->0;) pixels[k] = random.nextInt();
g.drawImage(img, 0, 0, null);
time = System.currentTimeMillis()-start;
frames++;
if (time>0)
{ String m = "FPS : "+(frames*1000.0/time); int sw = g.getFontMetrics().stringWidth(m);
g.setColor(Color.BLACK); g.fillRect((w-sw)/2, h/2-20, sw, 30);
g.setColor(Color.WHITE); g.drawString(m, (w-sw)/2, h/2);
}
s.show();
g.dispose();
}
catch (Exception e)
{ e.printStackTrace();
System.exit(0);
}
}
}