[英]Why does the do-while loop not run as expected in this Java program?
[英]Why does this Java rendering use a nested do-while loop?
我一直在通過教程學習如何用Java制作2D游戲,卻無法弄清楚為什么渲染在do-while循環中使用了do-while循環,如下所示:
do {
do {
Graphics g = null;
try {
g = bs.getDrawGraphics();
g.clearRect(0, 0, getWidth(), getHeight());
render(g);
} finally {
if (g != null) {
g.dispose();
}
}
} while (bs.contentsRestored());
bs.show();
} while (bs.contentsLost());
我理解該方法中的其余內容,我只是想不出為什么它對contentLost使用do-while循環,而對內部contentRestored使用do-while循環。
這似乎是從BufferStrategy
JavaDocs中直接刪除的 。
基本上,這可以確保最后一次使用getDrawGraphics
嘗試產生了可行的輸出,因為它們通常由VolatileImage
提供支持,而該VolatileImage
可以由系統隨時回收。
閱讀BufferStrategy#contentsRestored
, BufferStrategy#contentsLost
, VolatileImage
和BufferStrategy和BufferCapabilities
如果contentsRestored
為true
,則丟棄到Graphics
上下文的任何contentsRestored
都將被丟棄,需要重繪。 目的是確保通過BufferStrategy#show
僅將完全實現的映像推送到硬件。
如果contentsLost
為true
,則支持BufferStrategy
的VolitileImage
已丟失,需要重新構造和顯示。
該代碼摘自本頁的BufferStrategy
,作為雙緩沖區渲染的示例
文檔說:
contentsLost() : Returns whether the drawing buffer was
lost since the last call to getDrawGraphics.
contentsRestored(): Returns whether the drawing
buffer was recently restored from a
lost state and reinitialized to the
default background color (white).
因此,我認為循環是為了確保由於恢復了內容而丟失了所有圖形作品。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.