在不裁剪的情况下缩放 SDL Surface 的正确方法?

发布于 2024-07-10 06:12:47 字数 167 浏览 6 评论 0原文

缩放 SDL Surface 的正确方法是什么? 我在网上找到了一种解释,但它需要逐像素地重新绘制表面。 似乎应该有某种方法可以通过 SDL 本地执行此操作,而不是像那样重新绘制图像。 我在 SDL 文档中找不到任何涉及此内容的内容。 我可以通过修改曲面宽度和高度来毫无问题地调整曲面大小,但生成的曲面会被剪裁。

what is the proper way to scale an SDL Surface? I found one explanation online but it required redrawing the Surface pixel by pixel. It seems like there should be some way of doing this natively through SDL rather than redrawing the image like that. I haven't been able to find anything in the SDL documentation that covers this. I am able to resize surfaces without any problem by modifying the surfaces width and height, but the resulting surface is clipped.

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

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

发布评论

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

评论(3

惯饮孤独 2024-07-17 06:12:47

我知道这个答案太晚了,无法帮助提出这个问题的人,但我决定写这篇文章来帮助任何偶然发现这个问题的人。 在 SDL 2.0 中,您可以使用 SDL_BlitScaled() 函数将曲面缩放为目标 SDL_RectLazyFoo 有一个教程对此进行了描述,或者查看此 文档

I know this answer is way too late to assist the person who asked it, but I decided to write this to help anyone who stumbles upon this question. In SDL 2.0 You can use the SDL_BlitScaled() function to scale a surface into a targeted SDL_Rect. There's a tutorial by LazyFoo which describes this, or check out this documentation.

难理解 2024-07-17 06:12:47

SDL 不支持缩放位块传送。 根据 SDL_BlitSurface 的文档:

注意:SDL 传输器(还)没有
有能力扩展
像原来一样向上或向下剪切表面
与其他更复杂的情况
位块传送机制。 你必须
为自己想出一些办法,如果
你想要缩放图像(例如使用
SDL_gfx)。

您可以在此处找到 SDL_gfx。 编写自己的位图传送函数并没有那么糟糕,它可能是一个有趣且有用的学习实验(尽管您会重新发明轮子)。 使用 OpenGL 也是一种选择,因为缩放和旋转等操作可以在单个函数调用中完成。

SDL doesn't support scaled blitting. According to the documentation of SDL_BlitSurface:

Note: the SDL blitter does not (yet)
have the capability of scaling the
blitted surfaces up or down like it is
the case with other more sophisticated
blitting mechanisms. You have to
figure something out for yourself if
you want to scale images (e.g. use
SDL_gfx).

You could find SDL_gfx here. Writing your own blitting function isn't that bad, it might be a fun and useful learning experiment (though you'd be reinventing the wheel). Using OpenGL is also an option, as stuff like scaling and rotating could be done in a single function call.

蝶舞 2024-07-17 06:12:47

为了完整起见,并且由于问题未指定 SDL 版本,因此可以使用 API 方法 SDL_RenderCopyEx 在 SDL2 中进行缩放。 除了基本的 SDL2 库之外,不需要其他库。

int SDL_RenderCopyEx(SDL_Renderer*          renderer,
                     SDL_Texture*           texture,
                     const SDL_Rect*        srcrect,
                     const SDL_Rect*        dstrect,
                     const double           angle,
                     const SDL_Point*       center,
                     const SDL_RendererFlip flip)

通过设置 dstrect 的大小,可以将纹理缩放到整数像素。 还可以同时旋转和翻转纹理。

从技术上讲,这不是缩放表面,而是缩放纹理。 该过程应该是相关的,因为在基于 SDL2 的应用程序中进行渲染之前,表面几乎总是转换为纹理。

参考: https://wiki.libsdl.org/SDL_RenderCopyEx

像往常一样创建纹理:

surface = IMG_Load(filePath);
texture = SDL_CreateTextureFromSurface(renderer, surface);

何时合适要渲染它,请调用 SDL_RenderCopyEx 而不是 SDL_RenderCopy

For completeness, and because the question does not specify SDL version, scaling is possible in SDL2 using the API method SDL_RenderCopyEx. No additional libs besides the basic SDL2 lib are needed.

int SDL_RenderCopyEx(SDL_Renderer*          renderer,
                     SDL_Texture*           texture,
                     const SDL_Rect*        srcrect,
                     const SDL_Rect*        dstrect,
                     const double           angle,
                     const SDL_Point*       center,
                     const SDL_RendererFlip flip)

By setting the size of dstrect one can scale the texture to an integer number of pixels. It is also possible to rotate and flip the texture at the same time.

Technically this is not scaling a surface but rather scaling a texture. The procedure should be as relevant though as surfaces are almost always converted to textures before the rendering happens in SDL2 based applications.

Reference: https://wiki.libsdl.org/SDL_RenderCopyEx

Create your textures as usual:

surface = IMG_Load(filePath);
texture = SDL_CreateTextureFromSurface(renderer, surface);

And when it's time to render it, call SDL_RenderCopyEx instead of SDL_RenderCopy

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