Added tiled 9-grid texture rendering function

* New function SDL_RenderTexture9GridTiled, borders and center is tiled instead of stretched
This commit is contained in:
Arkadiusz Lach 2025-01-24 10:17:44 +01:00 committed by Sam Lantinga
parent 954675b32a
commit e25ee22469
3 changed files with 359 additions and 0 deletions

View file

@ -4525,6 +4525,141 @@ bool SDL_RenderTexture9Grid(SDL_Renderer *renderer, SDL_Texture *texture, const
return true;
}
bool SDL_RenderTexture9GridTiled(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, float tileScale)
{
SDL_FRect full_src, full_dst;
SDL_FRect curr_src, curr_dst;
float dst_left_width;
float dst_right_width;
float dst_top_height;
float dst_bottom_height;
CHECK_RENDERER_MAGIC(renderer, false);
CHECK_TEXTURE_MAGIC(texture, false);
if (renderer != texture->renderer) {
return SDL_SetError("Texture was not created with this renderer");
}
if (!srcrect) {
full_src.x = 0;
full_src.y = 0;
full_src.w = (float)texture->w;
full_src.h = (float)texture->h;
srcrect = &full_src;
}
if (!dstrect) {
GetRenderViewportSize(renderer, &full_dst);
dstrect = &full_dst;
}
if (scale <= 0.0f || scale == 1.0f) {
dst_left_width = SDL_ceilf(left_width);
dst_right_width = SDL_ceilf(right_width);
dst_top_height = SDL_ceilf(top_height);
dst_bottom_height = SDL_ceilf(bottom_height);
} else {
dst_left_width = SDL_ceilf(left_width * scale);
dst_right_width = SDL_ceilf(right_width * scale);
dst_top_height = SDL_ceilf(top_height * scale);
dst_bottom_height = SDL_ceilf(bottom_height * scale);
}
// Center
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_RenderTextureTiled(renderer, texture, &curr_src, tileScale, &curr_dst)) {
return false;
}
// Upper-left corner
curr_src.x = srcrect->x;
curr_src.y = srcrect->y;
curr_src.w = left_width;
curr_src.h = top_height;
curr_dst.x = dstrect->x;
curr_dst.y = dstrect->y;
curr_dst.w = dst_left_width;
curr_dst.h = dst_top_height;
if (!SDL_RenderTexture(renderer, texture, &curr_src, &curr_dst)) {
return false;
}
// Upper-right corner
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)) {
return false;
}
// Lower-right corner
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)) {
return false;
}
// 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)) {
return false;
}
// Left
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_RenderTextureTiled(renderer, texture, &curr_src, tileScale, &curr_dst)) {
return false;
}
// Right
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_RenderTextureTiled(renderer, texture, &curr_src, tileScale, &curr_dst)) {
return false;
}
// Top
curr_src.x = srcrect->x + left_width;
curr_src.y = srcrect->y;
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 - dst_left_width - dst_right_width;
curr_dst.h = dst_top_height;
if (!SDL_RenderTextureTiled(renderer, texture, &curr_src, tileScale, &curr_dst)) {
return false;
}
// Bottom
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_RenderTextureTiled(renderer, texture, &curr_src, tileScale, &curr_dst)) {
return false;
}
return true;
}
bool SDL_RenderGeometry(SDL_Renderer *renderer,
SDL_Texture *texture,
const SDL_Vertex *vertices, int num_vertices,