Android gameLoop使用SurfaceView;快速更换相机“镜头”;导致黑色闪光等

发布于 2024-10-16 23:49:11 字数 734 浏览 2 评论 0原文

我使用 SurfaceView 构建了一个简单的游戏引擎。

我使用 RectF 作为“相机”窗口,来表示 SurfaceView 画布的缩放像素宽度和高度,并找出屏幕上的内容。

我使用 canvas.translate 移动到相机“指向”的位置。 这是绘制循环:

mCamera.UpdateCamera();
scaleWorld(c, mCamera);
c.translate(-mScreen.left, mScreen.top - miHeight
* (1 / mCamera.getScaleLevel()));
drawBehavior.drawGame(c);

无论如何,如果我发射射弹,游戏就会顺利渲染,并且我看不到黑色的视觉闪烁,这是我用来刷新画布的颜色。

然而,如果我立即改变相机可以看到的内容,也许距离 3 个或更多“屏幕”远,经常会出现黑色闪烁,但并非总是如此。有时我很“幸运”,过渡是无缝的。

这些闪光通常位于我的位图的位置,因为我使用一两个位图递归地绘制地面和背景。

在我看来,突然的变化就是问题所在,所以我试图本质上废弃 gameLoop 线程来停止一切,然后设置新的 Rect 坐标,然后用另一个线程重新启动,但这不起作用。

我想,经过反思,我只是通过这样做来延迟对系统的“冲击”,而不是治疗它。

在尝试通过淡出或其他视觉技巧来掩盖这种可能残暴的设计之前,我想知道是否有人对这个问题有更深入的了解,并且可以引导我找到一个更优雅的解决方案。

I built a simple game engine using SurfaceView.

I use a RectF as a 'camera' window, to represent the scaled pixel width and height of the SurfaceView canvas, and to figure out what is on screen.

I use canvas.translate to move to where the camera is 'pointing' at.
Here is the draw loop:

mCamera.UpdateCamera();
scaleWorld(c, mCamera);
c.translate(-mScreen.left, mScreen.top - miHeight
* (1 / mCamera.getScaleLevel()));
drawBehavior.drawGame(c);

Any way, if I shoot a projectile, the game renders smoothly, and I see no visual flashes of black, which is the color I am using to flush the canvas.

However, if I instantly change what the camera can see, perhaps 3 or more 'screens' away, there is often, but not always, flashes of black. Sometimes I get 'lucky' and the transition is seamless.

These flashes are usually in the positions of my bitmaps, as I draw the ground and background recursively using one or two bitmaps.

It seemed to me that the sudden change was the issue, so I tried to essentially trash the gameLoop thread to stop everything, then set the new Rect coordinates, then restart with another thread, but this does not work.

I suppose on reflection, I was simply delaying the 'shock' to the system by doing this, not treating it.

Before attempting to sweep this perhaps atrocious design under the rug by perhaps a fade out or other visual trick, I am wondering if anyone has a deeper understanding of the issue and could lead me to a more elegant solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是在用心讲痛 2024-10-23 23:49:11

我猜屏幕在抽奖完成之前正在刷新。
多线程仍然让我有点迷失——但这里有一些值得尝试的事情:
尝试在游戏循环开始时锁定画布,并在结束时解锁:
http://developer.android.com/reference/android/view/SurfaceHolder .html#lockCanvas()

I am guessing the screen is refreshing before the draw is complete.
Multi threading still kinda loses me a bit - but here is something to try:
Try locking the canvas at the start of your game loop, and unlock it at the end:
http://developer.android.com/reference/android/view/SurfaceHolder.html#lockCanvas()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文