C++ 使用 SDL 旋转图像的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1183900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to rotate an image using SDL?
提问by Justen
I am building a game and the main character's arm will be following the mouse cursor, so it will be rotating quite frequently. What would be the best way to rotate it?
我正在构建一个游戏,主角的手臂将跟随鼠标光标,因此它会经常旋转。旋转它的最佳方法是什么?
回答by Zack The Human
With SDL you have a few choices.
使用 SDL,您有多种选择。
Rotate all your sprites in advance (pre-render all possible rotations) and render them like you would any other sprite. This approach is fast but uses more memory and more sprites. As @Nick Wigglepointed out, RotSpriteis a great tool for generating sprite transformations.
Use something like SDL_gfxto do real-time rotation/zooming. (Not recommended, very slow)
Use SDL in OpenGL mode and render your sprites to primitives, applying a rotation to the primitives.
提前旋转所有精灵(预渲染所有可能的旋转)并像渲染任何其他精灵一样渲染它们。这种方法很快,但使用更多的内存和更多的精灵。正如@Nick Wiggle指出的那样,RotSprite是生成精灵转换的绝佳工具。
使用类似SDL_gfx 的东西来进行实时旋转/缩放。(不推荐,很慢)
在 OpenGL 模式下使用 SDL 并将您的精灵渲染为基元,对基元应用旋转。
Option 3
is probably your best bet because you gain all of the advantages of using OpenGL. It's really up to you how to want to do it. It's also a possibility that you can load your sprites, perform all rotation calculations with SDL_gfx and then save the rotated versions to an SDL_Surface in memory.
Option3
可能是您最好的选择,因为您可以获得使用 OpenGL 的所有优势。这真的取决于你想怎么做。您也可以加载精灵,使用 SDL_gfx 执行所有旋转计算,然后将旋转后的版本保存到内存中的 SDL_Surface。
EDIT: In response to your comment I would recommend checking out Lazyfoo's SDL tutorials. Specifically, this one about rotation. There is also an OpenGl function, glRotatef
, which can be useful in your case. A quick search brought back this little tidbitwhich could also be helpful.
编辑:为了回应您的评论,我建议您查看 Lazyfoo 的 SDL 教程。具体来说,这个关于旋转。还有一个OpenGl 函数,glRotatef
,它在您的情况下很有用。快速搜索带回了这个小花絮,它也可能有帮助。
回答by Julian Goddard
SDL_RenderCopyEx()
has extra arguments for rotation, flipping, and the rotation center.
有用于旋转、翻转和旋转中心的额外参数。
回答by Artur Soler
You can use a library like SDL_gfx
您可以使用像 SDL_gfx 这样的库