2
與早期的Java 6 u 3相比,我獲得了兩倍的幀速率。很奇怪。任何人都可以解釋一下嗎?Java 6更新中的繪圖性能19,20與Java 6更新3中的繪圖性能?
在酷睿2 1.83GHz的,集成視頻(只有一個核心使用) - 1500(以前的Java)與700 fps的 論的Athlon 64 3500+,離散視頻 - 120(較老的java)與55 fps的
該應用程序是一個移動矩形的簡單遊戲。我使用Graphics2D從循環中繪製。
編輯:一些代碼。整個事情很大,這只是一些重要的部分。
public class SimpleRenderer extends JFrame{
SimpleGameEngine sge;
Canvas canvas; // Our drawing component
static final int WIDTH = 640;
static final int HEIGHT = 480;
Graphics2D g2d = null;
Graphics graphics = null;
Color background = Color.BLACK;
BufferedImage bi;
BufferStrategy buffer;
public SimpleRenderer(KeyboardInput keyboard, SimpleGameEngine sge) throws HeadlessException {
this.sge = sge;
setIgnoreRepaint(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas = new Canvas();
canvas.setIgnoreRepaint(true);
canvas.setSize(WIDTH, HEIGHT);
add(canvas);
pack();
// Hookup keyboard polling
addKeyListener(keyboard);
canvas.addKeyListener(keyboard);
canvas.createBufferStrategy(2);
buffer = canvas.getBufferStrategy();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
bi = gc.createCompatibleImage(WIDTH, HEIGHT);
this.setVisible(true);
}
public void draw(int fps) {
g2d = bi.createGraphics();
g2d.setColor(background);
g2d.fillRect(0, 0, WIDTH, HEIGHT);
g2d.setColor( Color.GREEN);
g2d.drawString("Use arrow keys to move rect", 100, 20);
g2d.drawString("Press esc to exit", 100, 32);
g2d.setColor( Color.GREEN);
g2d.drawString("FPS: "+fps, 20, 20);
g2d.drawRect(sge.bob.x, sge.bob.y, sge.bob.w, sge.bob.h);
graphics = buffer.getDrawGraphics();
graphics.drawImage(bi, 0, 0, null);
if(!buffer.contentsLost())
buffer.show();
}
...
遊戲循環:
...
long loop =0;
long update = 0;
long start = System.currentTimeMillis();
long lastIterationTime = System.nanoTime();
long nanoseccount=0;
int cyclec = 0;
int fps=0;
System.out.println("start");
while(run) {
long now = System.nanoTime();
loop++;
while(lastIterationTime + StepSize*1000000 <= now && run ==true) {
Update(StepSize);
update++;
lastIterationTime += StepSize*1000000;
}
Draw(fps);
nanoseccount += System.nanoTime()-now;
cyclec++;
if (nanoseccount >= 1000*1000000) {
fps = (int)Math.round((double)cyclec/(nanoseccount/1000000000));
nanoseccount = 0;
cyclec = 0;
continue;
}
}
System.out.println("loop "+ loop +" # update "+ update+ " # u/l " + ((double)update/loop)*100);
long runtime = (System.currentTimeMillis()-start);
System.out.println("run time "+ (double)runtime/1000 +"s # loop/s "+ ((double)loop/((double)runtime/1000)));
System.out.println("updates/s "+ ((double)update/((double)runtime/1000)));
...
使用合適的顯卡,您的代碼速度提高了十幾倍?這是真正的WTF。 – 2010-04-19 21:31:14
事實上,具有更好(離散)視頻的計算機 - 速龍運行速度較慢。但是CPU太舊了。 我想這個簡單的2D東西沒有硬件加速。 – Pesho 2010-04-19 21:42:52
一個小型自包含的例子會很好。 – 2010-04-19 21:44:24