c++ - SDL 1.3 : how to inplement simple scale-9-grid for image resize?

标签 c++ c sdl nine-patch scale9grid

我们有这样的图像:

enter image description here

我们有 4 个坐标 top:10、bottom:10、left:10、right:10 我们已将大小调整为 newWidth:100、newHeight:35 等值,我们生成了一些 SDL_Rect Sprite从一些 SDL_Surface *button 如何在该 Sprite 上执行此类调整大小转换?

那么如何在SDL中实现9切片缩放呢?

最佳答案

我制作了一个演示项目,使用 执行 9 切片渲染和 这里:https://github.com/cxong/sdl2-9-slice

screenshot

看看 render() 函数,如果您愿意,可以复制它 - 它是经过许可的。

关键是使用SDL_RenderCopy()srcrectdstrect参数- 前者是要渲染源纹理的哪一部分,后者是要渲染到目标(渲染目标)的哪一部分。

对于 9 个切片,角点按原样复制;对于中间部分,根据您想要的渲染方式(拉伸(stretch)或重复),srcrect 将是相同的,但 dstrect 将拉伸(stretch)或重复。

另一件事是SDL does not do texture repeating (然而)。所以如果你想渲染为重复模式,你需要使用循环。

这是项目终止时的函数:

int render(
    SDL_Renderer *renderer, SDL_Surface *s, SDL_Texture *t,
    int x, int y, int top, int bottom, int left, int right, int w, int h,
    bool repeat)
{
    const int srcX[] = {0, left, s->w - right};
    const int srcY[] = {0, top, s->h - bottom};
    const int srcW[] = {left, s->w - right - left, right};
    const int srcH[] = {top, s->h - bottom - top, bottom};
    const int dstX[] = {x, x + left, x + w - right, x + w};
    const int dstY[] = {y, y + top, y + h - bottom, y + h};
    const int dstW[] = {left, w - right - left, right};
    const int dstH[] = {top, h - bottom - top, bottom};
    SDL_Rect src;
    SDL_Rect dst;
    for (int i = 0; i < 3; i++)
    {
        src.x = srcX[i];
        src.w = srcW[i];
        dst.w = repeat ? srcW[i] : dstW[i];
        for (dst.x = dstX[i]; dst.x < dstX[i + 1]; dst.x += dst.w)
        {
            if (dst.x + dst.w > dstX[i + 1])
            {
                src.w = dst.w = dstX[i + 1] - dst.x;
            }
            for (int j = 0; j < 3; j++)
            {
                src.y = srcY[j];
                src.h = srcH[j];
                dst.h = repeat ? srcH[j] : dstH[j];
                for (dst.y = dstY[j]; dst.y < dstY[j + 1]; dst.y += dst.h)
                {
                    if (dst.y + dst.h > dstY[j + 1])
                    {
                        src.h = dst.h = dstY[j + 1] - dst.y;
                    }
                    const int res = SDL_RenderCopy(renderer, t, &src, &dst);
                    if (res != 0)
                    {
                        return res;
                    }
                }
            }
        }
    }
    return 0;
}

关于c++ - SDL 1.3 : how to inplement simple scale-9-grid for image resize?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6479387/

相关文章:

c++ - 在 CMake 中设置默认编译器

c++ - BHO HtmlEventObject 崩溃

php - 打印由 C 程序回显的行并在 PHP 网络服务器上显示它们

c - 某些程序在退出前无法从共享内存段分离后如何删除共享内存段?

c++ - 如果我有固定数量的相互独立的计算,多线程是否会显着提高性能?

c++ - 无法通过终端运行应用程序,但应用程序在 XCode 中运行良好

c++ - 用于解析资源 (.rc) 文件的正则表达式

c - 为什么可以将指针转换为整数而不是 double ? (C)

c++ - SDL iOS 禁用事件泵

c++ - SDL多线程