Sdl 1.3:如何实现简单的scale-9-grid用于图像大小调整?

问题描述 投票:1回答:1

我们有一个像这样的图像:

我们有4个坐标顶部:10,底部:10,左:10,右:10我们已经调整为像newWidth这样的值:100,newHeight:35我们有一些SDL_Rect Sprite是从一些SDL_Surface *button生成的如何在Sprite上执行此类调整大小转变?

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

c++ c sdl nine-patch scale9grid
1个回答
0
投票

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

看看render()功能,如果你愿意,可以复制它 - 这是许可的。

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

对于9切片,角落按原样复制;对于中间部分,取决于你想要渲染的方式 - 拉伸或重复 - srcrect将是相同的,但dstrect将拉伸或重复。

另一件事是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;
}
© www.soinside.com 2019 - 2024. All rights reserved.