Extend the 9-grid functions to handle a non-uniform grid layout (thanks @zaun!)

Fixes https://github.com/libsdl-org/SDL/issues/10389
This commit is contained in:
Sam Lantinga 2024-07-31 22:09:42 -07:00
parent a1c6f99877
commit a5f18048b2
7 changed files with 358 additions and 151 deletions

View file

@ -4213,11 +4213,14 @@ int SDL_RenderTextureTiled(SDL_Renderer *renderer, SDL_Texture *texture, const S
}
}
int SDL_RenderTexture9Grid(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, float corner_size, float scale, const SDL_FRect *dstrect)
int SDL_RenderTexture9Grid(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, float left_width, float right_width, float top_height, float bottom_height, float scale, const SDL_FRect *dstrect)
{
SDL_FRect full_src, full_dst;
SDL_FRect curr_src, curr_dst;
float dst_corner_size;
float dst_left_width;
float dst_right_width;
float dst_top_height;
float dst_bottom_height;
CHECK_RENDERER_MAGIC(renderer, -1);
CHECK_TEXTURE_MAGIC(texture, -1);
@ -4240,96 +4243,111 @@ int SDL_RenderTexture9Grid(SDL_Renderer *renderer, SDL_Texture *texture, const S
}
if (scale <= 0.0f || scale == 1.0f) {
dst_corner_size = corner_size;
dst_left_width = left_width;
dst_right_width = right_width;
dst_top_height = top_height;
dst_bottom_height = bottom_height;
} else {
dst_corner_size = (corner_size * scale);
dst_left_width = (left_width * scale);
dst_right_width = (right_width * scale);
dst_top_height = (top_height * scale);
dst_bottom_height = (bottom_height * scale);
}
// Upper-left corner
curr_src.x = srcrect->x;
curr_src.y = srcrect->y;
curr_src.w = corner_size;
curr_src.h = corner_size;
curr_src.w = left_width;
curr_src.h = top_height;
curr_dst.x = dstrect->x;
curr_dst.y = dstrect->y;
curr_dst.w = dst_corner_size;
curr_dst.h = dst_corner_size;
curr_dst.w = dst_left_width;
curr_dst.h = dst_top_height;
if (SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst) < 0) {
return -1;
}
// Upper-right corner
curr_src.x = srcrect->x + srcrect->w - corner_size;
curr_dst.x = dstrect->x + dstrect->w - dst_corner_size;
curr_src.x = srcrect->x + srcrect->w - right_width;
curr_src.w = right_width;
curr_dst.x = dstrect->x + dstrect->w - dst_right_width;
curr_dst.w = dst_right_width;
if (SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst) < 0) {
return -1;
}
// Lower-right corner
curr_src.y = srcrect->y + srcrect->h - corner_size;
curr_dst.y = dstrect->y + dstrect->h - dst_corner_size;
curr_src.y = srcrect->y + srcrect->h - bottom_height;
curr_dst.y = dstrect->y + dstrect->h - dst_bottom_height;
curr_dst.h = dst_bottom_height;
if (SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst) < 0) {
return -1;
}
// Lower-left corner
curr_src.x = srcrect->x;
curr_src.w = left_width;
curr_dst.x = dstrect->x;
curr_dst.w = dst_left_width;
if (SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst) < 0) {
return -1;
}
// Left
curr_src.y = srcrect->y + corner_size;
curr_src.h = srcrect->h - 2 * corner_size;
curr_dst.y = dstrect->y + dst_corner_size;
curr_dst.h = dstrect->h - 2 * dst_corner_size;
curr_src.y = srcrect->y + top_height;
curr_src.h = srcrect->h - top_height - bottom_height;
curr_dst.y = dstrect->y + dst_top_height;
curr_dst.h = dstrect->h - dst_top_height - dst_bottom_height;
if (SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst) < 0) {
return -1;
}
// Right
curr_src.x = srcrect->x + srcrect->w - corner_size;
curr_dst.x = dstrect->x + dstrect->w - dst_corner_size;
curr_src.x = srcrect->x + srcrect->w - right_width;
curr_src.w = right_width;
curr_dst.x = dstrect->x + dstrect->w - dst_right_width;
curr_dst.w = dst_right_width;
if (SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst) < 0) {
return -1;
}
// Top
curr_src.x = srcrect->x + corner_size;
curr_src.x = srcrect->x + left_width;
curr_src.y = srcrect->y;
curr_src.w = srcrect->w - 2 * corner_size;
curr_src.h = corner_size;
curr_dst.x = dstrect->x + dst_corner_size;
curr_src.w = srcrect->w - left_width - right_width;
curr_src.h = top_height;
curr_dst.x = dstrect->x + dst_left_width;
curr_dst.y = dstrect->y;
curr_dst.w = dstrect->w - 2 * dst_corner_size;
curr_dst.h = dst_corner_size;
curr_dst.w = dstrect->w - dst_left_width - dst_right_width;
curr_dst.h = dst_top_height;
if (SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst) < 0) {
return -1;
}
// Bottom
curr_src.y = srcrect->y + srcrect->h - corner_size;
curr_dst.y = dstrect->y + dstrect->h - dst_corner_size;
curr_src.y = srcrect->y + srcrect->h - bottom_height;
curr_dst.y = dstrect->y + dstrect->h - dst_bottom_height;
curr_dst.h = dst_bottom_height;
if (SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst) < 0) {
return -1;
}
// Center
curr_src.x = srcrect->x + corner_size;
curr_src.y = srcrect->y + corner_size;
curr_src.w = srcrect->w - 2 * corner_size;
curr_src.h = srcrect->h - 2 * corner_size;
curr_dst.x = dstrect->x + dst_corner_size;
curr_dst.y = dstrect->y + dst_corner_size;
curr_dst.w = dstrect->w - 2 * dst_corner_size;
curr_dst.h = dstrect->h - 2 * dst_corner_size;
curr_src.x = srcrect->x + left_width;
curr_src.y = srcrect->y + top_height;
curr_src.w = srcrect->w - left_width - right_width;
curr_src.h = srcrect->h - top_height - bottom_height;
curr_dst.x = dstrect->x + dst_left_width;
curr_dst.y = dstrect->y + dst_top_height;
curr_dst.w = dstrect->w - dst_left_width - dst_right_width;
curr_dst.h = dstrect->h - dst_top_height - dst_bottom_height;
if (SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst) < 0) {
return -1;
}
return 0;
}
int SDL_RenderGeometry(SDL_Renderer *renderer,
SDL_Texture *texture,
const SDL_Vertex *vertices, int num_vertices,