如何在 C 和 SDL 中拉伸位图?

发布于 2024-08-24 07:39:00 字数 98 浏览 11 评论 0原文

我正在为我的 CS 课制作一个游戏,但我在网上找到的精灵太小了。如何使用 SDL“拉伸”位图...使其变大? (我需要将其大小增加 50%,所有大小都相同。)示例代码片段将不胜感激。

I am making a game for my CS class and the sprites I have found online are too small. How do you 'stretch' a bitmap... make them bigger using SDL? (I need to increase there size 50%, there all the same size.) A snippet of example code would be appreciated.

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

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

发布评论

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

评论(4

涙—继续流 2024-08-31 07:39:00

这个问题没有指定 SDL 版本,即使在编写问题时 SDL2 不可用,我相信 SDL2 答案会在这里增加完整性。

与 SDL1.2 不同,在 SDL2 中可以使用 API 方法 SDL_RenderCopyEx 进行缩放。除了基本的 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 的大小,可以将纹理缩放到整数像素。也可以同时旋转和翻转纹理。

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

像往常一样创建纹理:

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

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

This question does not specify SDL version, and even though SDL2 was not available when the question was written, an SDL2 answer would add completeness here i believe.

Unlike SDL1.2, 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.

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

很酷不放纵 2024-08-31 07:39:00

使用专为此任务设计的软件,您将获得更好的结果。一个不错的选择是 ImageMagick。它可以从命令行或以编程方式使用。

例如,从命令行输入:

convert sprite.bmp -resize 150% bigsprite.bmp

如果出于某种奇怪的原因您想编写自己的双线性调整大小,这家伙看起来知道自己在做什么。

You're going to get a better looking result using software that is designed for this task. A good option is ImageMagick. It can be used from the command line or programatically.

For example, from the command line you just enter:

convert sprite.bmp -resize 150% bigsprite.bmp

If for some strange reason you want to write your own bilinear resize, this guy looks like he knows what he is doing.

梦中楼上月下 2024-08-31 07:39:00

你尝试过吗?

    SDL_Rect src, dest;
    src.x = 0;
    src.y = 0;
    src.w = image->w;
    src.h = image->h;
    dest.x = 100;
    dest.y = 100;
    dest.w = image->w*1.5;
    dest.h = image->h*1.5;
    SDL_BlitSurface(image, &src, screen, &dest);

have you tried?

    SDL_Rect src, dest;
    src.x = 0;
    src.y = 0;
    src.w = image->w;
    src.h = image->h;
    dest.x = 100;
    dest.y = 100;
    dest.w = image->w*1.5;
    dest.h = image->h*1.5;
    SDL_BlitSurface(image, &src, screen, &dest);
无尽的现实 2024-08-31 07:39:00

用于扩展 SDL 的未记录函数:
extern DECLSPEC int SDLCALL SDL_SoftStretch(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);

如:

SDL_SoftStretch(image, &src, screen, &dest);

Use for stretch the undocumented function of SDL:
extern DECLSPEC int SDLCALL SDL_SoftStretch(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);

Like:

SDL_SoftStretch(image, &src, screen, &dest);

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