我正在構建一個遊戲,我需要將〜30個64x64塊渲染到屏幕。 通常我會用libgdx做類似的工作,但這次我想使用本地Java方法。高效地渲染多個圖像
對於一個樣本測試,我加載10塊並渲染它們 - 將我的fps從大約300下降到35. 我在這裏的筆記本電腦有點不好,但我沒想到他這麼早就崩潰了。如果你只是看看我的代碼,並給我提示如何更有效地渲染 - 這將是很好的。我想我也沒有100%的理解BufferedImage和Graphics的概念。
順便說一句,我的規格是: *睿i5-2520M CPU @ 2.5GHZ(獲得30%左右的工作量,同時調試) * 4GB內存 * NO GPU
---------- ------- CODE ----------------------------------------
的圖形和圖像的聲明:
public BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
public Graphics2D g;
的環路
while (running) {
long now = System.nanoTime();
unprocessed += (now - lastTime)/nsPerTick;
GraphicUtil.setDelta(now - lastTime);
lastTime = now;
boolean shouldRender = true;
while (unprocessed >= 1) {
ticks++;
gameTick();
unprocessed -= 1;
shouldRender = true;
}
if (shouldRender) {
frames++;
input.tick();
render();
}
if (System.currentTimeMillis() - lastTimer1 > 1000) {
lastTimer1 += 1000;
System.out.println(ticks + " ticks, " + frames + " fps");
frames = 0;
ticks = 0;
}
}
渲染() - 在我的主循環方法
public void render() {
BufferStrategy bs = getBufferStrategy();
if(bs == null) {
createBufferStrategy(2);
return;
}
switch(currentScreenId) {
case Screen.MENUSCREENID :
menuScreen.handleInput();
menuScreen.render();
break;
case Screen.HOWTOSCREENID :
howToScreen.handleInput();
howToScreen.render();
break;
case Screen.GAMESCREENID :
gameScreen.handleInput();
gameScreen.render();
break;
}
Graphics g2 = bs.getDrawGraphics();
g2.drawImage(GraphicUtil.toCompatibleImage(image), 0, 0, getWidth(), getHeight(), null);
g2.dispose();
//
bs.show();
}
渲染() - 在我gameScreen
public void render() {
clearScreen(Color.BLACK);
//Render blocks
for(Block b : blocks.values()) {
game.g.drawImage(blockImage, (int) b.getBounds().x, (int) b.getBounds().y, blockImage.getWidth(null), blockImage.getHeight(null), null);
}
}