Android:巨大的多层滚动背景
我正在开发一款横向卷轴 Android 游戏,但遇到了一个问题。
游戏正在 Canvas 对象上渲染图形。游戏有大约 6 个滚动层,主要地面层、碰撞层、前景层和两个背景层,所有这些层都以不同速度滚动以适应相机深度。
现在,问题就出在这里: 游戏的图形具有巨大的独特关卡,并且全部绘制出来。它不是基于图块的,没有重复的图形。但是,当加载整个图形层时,我收到内存不足异常。为了解决这个问题,我将所有图层切成 200 x 300 独特的矩形图块,并且游戏仅在可见时加载可见的内容。它无法一次加载所有图块,因为它会抛出内存不足异常。因此,当然,这要求我在内存块不再可见时将其丢弃,否则我将继续将这些块添加到内存中并再次遇到内存不足异常。这意味着我必须使用 BitmapFactory 从游戏循环中的资源为每个可见的图块创建一个新的位图。当然,随着这些图块位图在游戏循环中初始化,每当图块离开可见性时,我都必须处理垃圾收集器滞后。
有没有更好的方法来仅使用 Canvas 并保留我独特的“宏”图块而不诉诸可重复使用的微图块?使用 OpenGL 会有好处吗?如何或为何?
I'm developing a side-scrolling android game and I've come across one hiccup.
The game is rendering graphics on the Canvas object. The game features about 6 scrolling layers, the main ground layer, a collision layer, a foreground layer, and two background layers, which all scroll with variable speed for camera depth.
Now, the problem resides here:
The game's graphics feature huge unique levels that are drawn out in their entirety. It is not tilebased, no graphics are repeated. However, when loading an entire graphic layer, I receive an out of memory exception. To remedy this, I've sliced up all my layers to 200 x 300 unique rectangular tiles, and the game only loads what's visible when it is visible. It can't load all the tiles at once, again because it will throw an Out of Memory exception. So of course, that requires that I dispose from memory tiles when they are no longer visible, otherwise i'll just keep adding these tiles to the memory and again hit that Out of Memory exception. This means that I have to use BitmapFactory to create a new bitmap from a resource within the game loop for every tile that becomes visible. Of course, with these tile bitmaps initialized within the game loop, whenever tiles leave visibility I have to deal with Garbage Collector lag.
Is there a better way to go about this solely using Canvas and keeping my unique "macro" tiles and not resorting to reusable micro tiles? Would it benefit using OpenGL instead? How or why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OpenGL 将允许您对何时从内存加载/卸载纹理进行一些显式控制。您也许可以利用 OpenGL 的一些功能来开发“分页”系统来引入和卸载纹理。这将使您避免垃圾收集器。
但您通常会做正确的事情 - 从显存中删除不相关的图形,为需要显示的图形腾出空间。
OpenGL would allow you some explicit control over when textures are loaded/unloaded from memory. You may be able to make use of some of OpenGL's features to develop a 'paging' system for bringing in and unloading textures. That would allow you to avoid the garbage collector.
But you are generally doing the correct thing - irrelevant graphics are removed from graphics memory to make room for those that need displaying.