簡體   English   中英

為什么此Java渲染使用嵌套的do-while循環?

[英]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#contentsRestoredBufferStrategy#contentsLostVolatileImageBufferStrategy和BufferCapabilities

如果contentsRestoredtrue ,則丟棄到Graphics上下文的任何contentsRestored都將被丟棄,需要重繪。 目的是確保通過BufferStrategy#show僅將完全實現的映像推送到硬件。

如果contentsLosttrue ,則支持BufferStrategyVolitileImage已丟失,需要重新構造和顯示。

該代碼摘自本頁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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM