在 Java 中读取精灵表的最佳方式是什么?

发布于 2024-07-08 21:02:52 字数 345 浏览 6 评论 0原文

我正在编写一个基本的精灵引擎,以供自己娱乐并更好地熟悉 Java 的 2d API。 目前,我正在使用大量具有透明背景的独立 .png 文件来表示我需要的各种精灵和不同的动画帧。 大多数“现实世界”游戏开发项目似乎都使用“精灵表”,其中在单个文件中包含多个精灵或动画帧。 此外,人们通常不使用原生图像透明度支持,而是指定未出现在精灵调色板中的任意颜色作为透明颜色。 如何以编程方式管理这样的文件?

  • 你怎么知道一个精灵在哪里 开始,下一个开始
  • 你如何处理透明度

可能还有其他我没有想到的因素,所以我可能会在我想到事情或人们提出建议时添加到上面的列表中(请在评论中这样做) )。

I'm writing a basic sprite engine for my own amusement and to get better aquainted with Java's 2d API. Currently I am making use of large numbers of separate .png files with transparent backgrounds to represent the various sprites and different frames of animation that I need. Most 'real world' game development projects seem to make use of 'sprite sheets' which contain multiple sprites or frames of animation within a single file. Also, rather than making use of native image transparency support, people often nominate an arbitrary colour that does not appear in the sprite pallette to be the transparent colour. How does one manage a file like this programatically?

  • how do you know where one sprite
    starts and the next begins
  • how do you deal with transparency

There may be other factors that I've not thought of here, so I may add to the list above as I think of things or as people make suggestions (please do so in the comments).

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

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

发布评论

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

评论(4

岁吢 2024-07-15 21:02:52

我目前使用由简单精灵编辑器生成的 XML 文件,该编辑器将精灵存储为(可选动画)姿势的集合,而姿势又是帧或单元的集合。 帧存储每帧信息,例如工作表中帧的 x 和 y 偏移量、单元格宽度和高度以及任何转换(调整大小/旋转/色调/等)。 姿势存储各个帧和动画信息(例如速度)以及姿势名称,以便在程序中轻松识别它们(hero.pose = sprite.pose[“stand_right”])。 精灵充当文档根来保存多个姿势,例如每个面向方向的姿势。

我之前使用的一个不太灵活的替代方案是指定单元格和工作表的固定大小,并根据这些值计算帧偏移和大小(例如,宽度始终为 32 像素,因此第三个精灵为 32 * 2)。 后来我开始在文件名中为不适合固定单元尺寸的精灵指定这些尺寸(例如 sprite_name_32x64.png)。 我更喜欢这种新方法,尤其是使用一个简单的编辑器,它可以为我填充大部分值,并允许我使用精灵作为其他精灵的模板。

我直接使用存储在 PNG 图像中的 Alpha 和透明度信息,因此我不需要担心将其存储在其他地方,尽管其他方法是为每个精灵选择一个固定值并存储在某个地方,如果您知道它总是空的,如果您正在使用那些、精灵蒙版或其他什么,请使用特定的调色板条目。

I currently use XML files generated by a simple sprite editor that store the sprite as a collection of (optionally animated) poses, which are in turn a collection of frames or cells. Frames store per-frame information like the x and y offset of the frame in sheet, cell width and height, and any transformation (resize/rotation/hue/etc.). Poses store individual frames and animation information (speed, for example), and a pose name to easily identify them in the program (hero.pose = sprite.pose["standing_right"]). Sprites serve as a document root to hold several poses, such as a pose for each facing direction.

A less flexible alternative I used earlier was to specify fixed sizes for cells and sheets and calculate frame offsets and sizes based on these values (e.g. width is always 32 pixels, so third sprite is at 32 * 2). Later I started specifying these dimensions in the file name (e.g. sprite_name_32x64.png) for sprites that don't fit the fixed cell dimensions. I like the new approach more, especially with a simplistic editor that fills most values for me and allows me to use sprites as templates for other sprites.

I use the alpha and transparency information stored in PNG images directly so I don't need to worry about storing it elsewhere, although other approaches would be to pick a fixed value per sprite and store somewhere, use the leftmost pixel in the pose if you know it's always empty, use a specific palette entry if you're using those, sprite masks, or what have you.

初心 2024-07-15 21:02:52

与 java 无关,但通常您可以使所有精灵具有相同的大小。 因此,您将能够通过简单的 for 循环在游戏(或应用程序)中生成精灵。

但对于不同大小的精灵,精灵表大小可能会出现问题(它可能比预期大)。 因此,您必须为 spritesheet 定义 xml 或 json 文件,才能在代码中查找 sprite 图像。 您可以使用精灵表编辑器(有很多,我正在使用Sprite Master)来快速和生成精灵表和坐标数据的简单方法。

Not about java but generally you can make your all sprites in the same size. Thus, you will be able to generate your sprites in your game (or app) with a simple for loops.

But for different sized sprites there may be problem for spritesheet size (it can be larger than expected). So you must define an xml or json file for your spritesheet to find your sprite images in your code. You can use sprite sheet editors (there are plenty of them, I'm using Sprite Master) for quick and easy way to generate sprite sheet and coordinate datas.

无法回应 2024-07-15 21:02:52

让你的精灵表知道每个序列的大小和数量。

抓取工作表的缓冲图像并使用如下内容:

currentframe=spritesheet.getSubimage(x, y, w, h); 

您的 x 和 y 将根据您所在的框架而变化。 保持宽度和高度相同,让事情变得简单。

忘记试图将整个游戏保留在一张纸上。 这很疯狂而且很难管理。 为每个动画序列使用新的 png。 如果您热衷于节省空间,则只需创建向右移动的动画,然后实时翻转缓冲图像以向左移动。

Java 将读取带有 alpha 的 png 文件,因此不必担心透明度颜色。 用 png 绘制所有内容。 使用 Photoshop 或 Gimp。

在 google 中搜索 java 图像 TYPE_INT_ARGB

Make your sprite sheet knowing the size and number of each sequence.

Grab a buffered image of your sheet and use something like this:

currentframe=spritesheet.getSubimage(x, y, w, h); 

Your x and y will change based on the frame you are on. Keep the width and height the same to make things easy on yourself.

Forget trying to keep the entire game on one sheet. It's nuts and hard to manage. Use a new png for each animation sequence. If you are anal about saving space only create moving right animations and just flip the buffered image real time to move left.

Java will read png files with the alpha so don't worry about the transparency colour. Draw everything in pngs. Use Photoshop or Gimp.

Search google for java image TYPE_INT_ARGB

浅听莫相离 2024-07-15 21:02:52

好吧,由于其中大多数都是自定义的,因此这些细节取决于实现者。
通常,文件以标头信息开头,其中包含高度/宽度以及编码、透明度等详细信息。

很多时候,内容都在一个文件中,因为与打开相比,打开/读取多个文件的成本非常昂贵/读取一个文件。 许多游戏引擎使用 zip 或“ziplike”文件进行 0 压缩,将单个文件视为文件系统。

Well, since most of them are custom, those details are up to the implementor.
You'd generally have the file start with header information that contains the details of height/width and encoding, transparency, etc.

A lot of the time things are in one file because it is very expensive to open/read multiple files compared to open/read one file. Many game engines use zip or "ziplike" files with 0 compression to treat a single file as a filesystem.

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