使用 OpenGL 和 SDL 的 Sprite 表
我一直在开发一款新游戏,终于开始对主角的动作进行编码,但我对如何做到这一点有疑问。
以前,我在 Allegro 中制作了两个游戏,因此 spritesheet 很容易实现,因为我在图像上建立了帧和位置,并将每个帧保存在不同的位图中,但我知道使用 OpenGL 这样做是不必要的并且花费多一点。
所以,我一直在思考如何保存我的精灵表并在我的程序中使用,但我只有一个想法。
我加载了图像并转换为纹理,在帮助我制作动画的函数中,我只需抓取纹理的一部分来绘制,而不是将每个纹理存储在程序中。
这是最好的方法吗?
预先感谢您的帮助。
I been working in a new game, and finally reached the point where I started to code the motion of my main character but I have a doubt about how do that.
Previously, I make two games in Allegro, so the spritesheets are kind of easy to implement, because I establish the frame and position on the image, and save every frame in a different bitmap, but I know that do that with OpenGL it's not neccesary and cost a little bit more.
So, I been thinking in how save my spritesheet and used in my program and I have only one idea.
I loaded the image and transformed in a texture, in my function that help me animate I simply grab a portion of the texture to draw instead of store every single texture in my program.
This is the best way to do that?
Thanks beforehand for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你走在正确的轨道上。
需要考虑的事项:
You're on the right track.
Things to consider:
你的想法是正确的,如果你有一堆精灵,最好将它们全部粘在一个大纹理中。只需将精灵绘制为纹理四边形,其纹理坐标索引到精灵的框架中。您可以进行一些优化,但大多数优化都是围绕尝试充分利用纹理内存并将精灵紧密地打包在一起而避免混合问题。
You got the right idea, if you have a bunch of sprites it is much better to just stick them all in one big textures. Just draw your sprites as textured quads whose texture coordinates index into the frame of the sprite. You can do a few optimizations, but most of them revolve around trying to get the most out of your texture memory and packing the sprites closely together with out blending issues.
为什么不呢?将大量精灵放入单个纹理中并没有真正的缺点。您需要做的就是更改纹理坐标以从纹理中选取有问题的区域。
Why not? There are no real downsides to putting a lot of sprites into a single texture. All you need to do is change the texture coordinates to pick the region in question out of the texture.