在 SDL 中缩放精灵

发布于 2024-08-21 19:41:48 字数 21 浏览 3 评论 0原文

如何在 SDL 中缩放精灵?

How can i scale sprites in SDL?

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

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

发布评论

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

评论(5

柠栀 2024-08-28 19:41:48

SDL 不直接提供缩放功能,但有一个名为 SDL_gfx 的附加库 提供旋转和缩放功能。还有另一个名为 Sprig 的库也提供了类似的功能。

SDL doesn't provide scaling functionality directly, but there's an additional library called SDL_gfx which provides rotation and zooming capabilities. There's also another library called Sprig which provides similar features.

蹲墙角沉默 2024-08-28 19:41:48

如果您使用 SDL_RenderCopy() 从纹理中获取精灵,则可以进行缩放,但我不能保证您可以抗锯齿。

使用函数 SDL_RenderCopy(),您可以传递 4 个参数:

  • 指向渲染器(您将在其中进行渲染)的指针。
  • 指向纹理的指针(您将在其中获取精灵)。
  • 指向源矩形的指针(在纹理上获取精灵的区域和位置)。
  • 和指向目标矩形的指针(您要绘制的渲染器上的区域和位置)。

您应该只修改您的目标矩形,例如,如果您要渲染 300 x 300 的图像并且希望对其进行缩放,则您的目标矩形应该类似于 150 x 150 或 72 x 72 或您想要缩放的任何值。

You can do scaling if you are getting sprites from a texture with SDL_RenderCopy() but i cannot guarantee you antialiasing.

With function SDL_RenderCopy() you pass 4 params:

  • a pointer to a renderer (where you are going to renderize).
  • a pointer to a texture (where you are going to get the sprite).
  • pointer to source rect(the area and position where you get the sprite on the texture).
  • and pointer to dest rect(the area and position on the renderer you are going to draw).

You should only modify your dest rect like for example, if you are going to render an image 300 x 300 and you want it scaled, your dest rect should be like 150 x 150 or 72 x 72 or whatever value you wanted to scale.

药祭#氼 2024-08-28 19:41:48

您没有提供任何代码,所以我假设您正在使用纹理和 SDL_Renderer:

当使用 SDL_RenderCopy() 时,纹理将被拉伸以适合目标 SDL_Rect,因此,如果您使目标 SDL_Rect 更大或更小,您将可以对纹理进行简单的缩放。

https://wiki.libsdl.org/SDL_RenderCopy

You haven't provided any code, so I'm going to assume you are using textures and an SDL_Renderer:

When using SDL_RenderCopy() the texture will be stretched to fit the destination SDL_Rect, so if you make the destination SDL_Rect larger or smaller you can perform a simple scaling of the texture.

https://wiki.libsdl.org/SDL_RenderCopy

楠木可依 2024-08-28 19:41:48

Ibrahim CS 的解决方案有效。

让我扩展这个解决方案并提供代码。
另一件需要注意的事情是计算新位置 (x,y),以左上角为原点来渲染缩放纹理。

我这样做

// calculate new x and y
int new_x = (x + texture->w/2) - (texture->w/2 * new_scale);
int new_y = (y + texture->h/2) - (texture->h/2 * new_scale);
// form new destination rect
SDL_Rect dest_rect = { new_x, new_y, texture->w * scale, texture->h * scale };
// render
SDL_RenderCopy(renderer, texture, NULL, &dest_rect);

假设 textureSDL_TexturerendererSDL_Renderer,并且您完全从输入渲染纹理到目的地。

Solution from Ibrahim CS works.

Let me expand on top of this solution and providing the code.
Another thing to note is to calculate new position (x,y) with top-left is origin to render scaled texture.

I do it like this

// calculate new x and y
int new_x = (x + texture->w/2) - (texture->w/2 * new_scale);
int new_y = (y + texture->h/2) - (texture->h/2 * new_scale);
// form new destination rect
SDL_Rect dest_rect = { new_x, new_y, texture->w * scale, texture->h * scale };
// render
SDL_RenderCopy(renderer, texture, NULL, &dest_rect);

assume that texture is SDL_Texture, and renderer is SDL_Renderer, and you render fully from input texture to destination.

枯叶蝶 2024-08-28 19:41:48

如果您使用 SFML ,那么您将获得一组非常相似的跨平台功能,但图形是硬件加速,并且缩放和旋转等功能是免费的,既不需要额外的依赖项,也不需要花费明显的 CPU 时间来操作。

If you use SFML instead then you get a very similar set of cross-platform capabilities but the graphics are hardware accelerated and features such as scaling and rotation come for free, both in terms of needing no additional dependencies and in terms of taking no noticeable CPU time to operate.

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