Hi!
I’ve just begun creating a framework for 2D games in Java, and I have read some of the posts here. I have set up some code to test the things I have learned so far, but something seems på be wrong. It’s just a small demo that runs for 5 seconds and then shows you the framerate. It draws 1000 sprites @ 128 x 128 as fast as possible (no sleeps). I just made it to find out how to get the best performance from Java2D.
This is the testcode (I outcommented fullscreen for testing purposes):
package main;
import java.awt.;
import java.awt.image.;
import javax.swing.JFrame;
public class Main extends JFrame implements Runnable
{
private static final long serialVersionUID = 1L;
private GraphicsConfiguration gc;
private GraphicsDevice gd;
private BufferStrategy buffer;
private BufferedImage bImage;
public Main()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setSize(800, 600);
setVisible(true);
setIgnoreRepaint(true);
}
public void start()
{
gc = getGraphicsConfiguration();
gd = gc.getDevice();
//gd.setFullScreenWindow(this);
//gd.setDisplayMode(new DisplayMode(800, 600, 16, 60));
createBufferStrategy(2);
buffer = getBufferStrategy();
bImage = gc.createCompatibleImage(128, 128);
new Thread(this).start();
}
public static void main(String args[])
{
Main main = new Main();
main.start();
}
public void run()
{
double fps = 0;
long frames = 0;
Graphics2D g;
long startTime = System.nanoTime() / 1000000L;
long lastFrame = startTime;
while (lastFrame <= (startTime + 5000))
{
g = (Graphics2D)buffer.getDrawGraphics();
g.clearRect(0, 0, 800, 600);
g.setColor(Color.BLUE);
for (int i = 0; i < 1000; i++)
g.drawImage(bImage, 100, 100, null);
g.drawString(String.valueOf(fps), 50, 50);
g.dispose();
if (!buffer.contentsLost())
buffer.show();
frames++;
lastFrame = System.nanoTime() / 1000000L;
fps = (double)frames / (lastFrame - startTime) * 1000;
}
System.out.println(fps);
System.exit(0);
}
}
And this is the trace count:
220 calls to sun.java2d.loops.DrawGlyphList::DrawGlyphList(OpaqueColor, SrcNoEa, AnyInt)
220220 calls to sun.awt.windows.Win32BlitLoops::Blit(“Integer RGB DirectDraw”, SrcNoEa, “Integer RGB DirectDraw”)
1 call to GDIFillRect
2 calls to sun.java2d.loops.Blit::Blit(IntRgb, SrcNoEa, IntRgb)
226 calls to DDFillRect
220669 total calls to 5 different primitives
But from what I’ve read on the forums, I shouldn’t have those Win32BlitLoops, the should be D3DBlitLoops, right? So, what am I doing wrong? I’m not using any -D (other than trace). I’m running Java 1.5 on an XP Pro machine @ Athlon XP 2500+ with 1 GB RAM, with a GeForce FX 5700 Ultra, for those of you who just have to know!
Please help me, it’s driving me insane! And by the way, is there anything that can be done better? (Except the fact that I need to do some Thread.sleep() between render cycles)?