渲染大量位图时出现奇怪的伪像
我正在编写一个简单的 2D 游戏引擎,并通过调用 Canvas.drawBitmap (所有相同的源位图,但通常不使用 1:1 源到目标缩放)将各种对象渲染到锁定的 SurfaceVew。
一切似乎都按预期工作,直到每帧有大量对drawBitmap 的调用。发生这种情况时,我偶尔会看到停顿(100 毫秒左右),并伴有看起来好像多个渲染帧一起发生的效果,即某些对象将在两个屏幕位置绘制两次或一对以相同速度移动的对象似乎会暂时靠近或远离。
该应用程序的结构如下(为了示例而简化);
initialiseGameObjects();
while(quit==false)
{
processGameLogic(); // update object positions
Canvas c = surface_holder.lockCanvas();
if (c != null) {
drawGameObjects(c); // draw all objects
surface_holder.unlockCanvasAndPost(c);
}
}
据我了解,画布是绘制请求的持有者,这些请求在发布帖子时执行。大概它可以容纳的请求数量是有限制的,我想知道我是否超过了这个限制(我正在根据屏幕上的任何时间绘制多达一百个小位图)并无意中引发一些尽管我没有找到任何文档来证实或反驳这一点,但它以某种方式刷新并扰乱了双缓冲。
有人知道会发生什么吗?
问候, 史蒂夫
I'm writing a simple 2D game engine and rendering various objects to a locked SurfaceVew by calling Canvas.drawBitmap (all the same source bitmap but generally not with 1:1 source to destination scaling).
Everything appears to work as expected until there are a lot of calls per frame to drawBitmap. When this happens I occasionally see a stall (100mS or so) accompanied by an effect that looks as if several frames of rendering have occurred together, i.e. some objects will be drawn twice at two screen locations or a pair of objects moving at the same speed will appear to momentarily get closer together or further apart.
The application is structured as follows (simplified for the sake of example);
initialiseGameObjects();
while(quit==false)
{
processGameLogic(); // update object positions
Canvas c = surface_holder.lockCanvas();
if (c != null) {
drawGameObjects(c); // draw all objects
surface_holder.unlockCanvasAndPost(c);
}
}
As I understand it, the Canvas is a holder for draw requests which get executed when the Post is issued. Presumably there is a limit to the number of requests it can hold and I'm wondering if I'm exceeding this limit (I'm drawing up to a hundred small bitmaps depending on what's on screen at any one time) and inadvertently provoking some kind of flush and upsetting the double buffering in some way, although I've not managed to find any documentation to confirm or disprove this.
Does anyone have any idea what might be going on?
Regards,
Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要同步对画布绘图的调用。检查游戏驱动线程的示例:OnDraw() 未触发,surfaceView 中未绘制任何内容 - Android
编辑:
不过我认为这可能与这个问题有关:
Android SurfaceView/Canvas 在尝试清除后闪烁
You need to sychronize calls to canvas drawing. Check this example of a game driving thread: OnDraw() is not fired, nothing is drawn in surfaceView - Android
EDIT:
However I think it may be related to this problem:
Android SurfaceView/Canvas flickering after trying to clear it