gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:05:32 25 4
gpt4 key购买 nike

我们有这样一张图片:

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-slice scaling呢?

最佳答案

我制作了一个使用 执行 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com