我们有这样的图像:
我们有 4 个坐标 top:10、bottom:10、left:10、right:10 我们已将大小调整为 newWidth:100、newHeight:35 等值,我们生成了一些 SDL_Rect Sprite
从一些 SDL_Surface *button
如何在该 Sprite 上执行此类调整大小转换?
那么如何在SDL中实现9切片缩放呢?
最佳答案
我制作了一个演示项目,使用 c 执行 9 切片渲染和 sdl-2这里:https://github.com/cxong/sdl2-9-slice
看看 render()
函数,如果您愿意,可以复制它 - 它是经过许可的。
关键是使用SDL_RenderCopy()
的srcrect
和dstrect
参数- 前者是要渲染源纹理的哪一部分,后者是要渲染到目标(渲染目标)的哪一部分。
对于 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/