在 Java 中加载和缓存图像的最佳方法是什么?

发布于 2024-12-23 12:32:00 字数 184 浏览 3 评论 0原文

我有超过一千个 16 x 16 像素图块图像的大量集合,我在 Java 中制作的游戏需要这些图像。

在不耗尽 JVM 可用内存的情况下存储图块的最佳方法是什么?

我认为生成 1000+ BufferedImages 可能并不明智......

保持图像准备就绪的主要目的是加载地图,它将根据地图文件动态生成。

I have a large collection of over one thousand 16 by 16 pixel tile images that I'll need for a game I'm making in Java.

What would be the best way to store the tiles without exhausting the available memory of the JVM?

I'm thinking that spawning 1000+ BufferedImages might not be wise...

The main purpose for keeping the images at the ready is for loading maps, which will be dynamically spawned based on a map file.

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

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

发布评论

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

评论(3

挽容 2024-12-30 12:32:00

首先,加载所有这些图像确实是一个问题。 1000 张 16 x 16 像素的图像是 256000 像素。即使每个像素使用 4 个字节,也只会产生 1 MB 的图像数据,这也不算多。

如果您确实希望/需要减少加载到内存中的图像数量,则只能将地图所需的视觉图块/图像加载到内存中,以及更多一些以提高游戏的响应速度。

例如,如果您的游戏显示 n × m 个图块的地图,您可以加载 n+2 × m+2< /code> 将图块放入内存中,或直观地表示(其中 * 是可见图块,+ 是额外加载到内存中的图块):

+++++++++++
+*********+
+*********+
+*********+
+++++++++++

当用户移动地图时,您将删除对不再需要的图块的引用并开始加载新图块。由于内存中保留了一块图块,因此移动地图应该仍然会非常顺利。当然,如果您的图块相当小,或者移动地图的速度非常快,您可以增加使用的额外图块数量

First, is loading all those images really a problem. 1000 images of 16 by 16 pixels are 256000 pixels. Even when using 4 bytes for each pixel would result in just one MB of image data, which isn't much.

If you really want/need to reduce the number of images you load into memory, you could only load the visual tiles/images needed for your map into memory, and a few more to increase responsiveness for your game.

For example if your game shows a map of n by m tiles, you could load a n+2 by m+2 tiles into memory, or visually represented (where * are visible tiles and + the extra loaded tiles into memory):

+++++++++++
+*********+
+*********+
+*********+
+++++++++++

When the user moves the map, you remove the references to the tiles you no longer need and start loading the new tiles. Since you have one tile in reserve in memory, moving the map should still happen quite smoothly. Of course, if your tiles are rather small, or moving the map happens pretty fast you can increase the number of extra tiles you use

掩耳倾听 2024-12-30 12:32:00

你尝试过吗?如果每个像素有 4 个字节,则 16 x 16 x 4 x 1000 大约为 1Mb + 开销。那已经不再是那么多了。如果您负担不起,那么也许可以创建更大的图像,加载这些图像,然后根据需要提取图块。

Have you tried? if you have 4 bytes per pixel, 16 x 16 x 4 x 1000 is about 1Mb + overhead. That's not that much any more. If you can't afford that, then maybe create larger images, load those and then extract the tiles out as you need them.

吹泡泡o 2024-12-30 12:32:00

我不认为缓冲 1000+ 图像是最好的选择,因为它会消耗大量内存。可能最好实现不同的逻辑,例如仅缓冲最常用的图像,例如 100 个左右(仅使用随机数,但您需要计算有多少频繁使用的图像),如果有任何图像比这些缓存,则在需要时加载它们。

I don't think buffering 1000+ images is best option, because it consumes lot of memory. May be it is better to implement different logic like buffer only most used images like 100 or so (just used random number, but you need to figure how many frequent used images will be), if any image than these cached, load them when required.

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