繁体   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