[英]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.