Skip to content

Commit

Permalink
Add clipped version of the drawStretchedHV function.
Browse files Browse the repository at this point in the history
  • Loading branch information
daid committed Feb 20, 2022
1 parent 377a0e2 commit 91b8a71
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
124 changes: 124 additions & 0 deletions src/graphics/renderTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,130 @@ void RenderTarget::drawStretchedHV(sp::Rect rect, float corner_size, std::string
finish(info.texture);
}

void RenderTarget::drawStretchedHVClipped(sp::Rect rect, sp::Rect clip_rect, float corner_size, std::string_view texture, glm::u8vec4 color)
{
if (clip_rect.size.x < 0 || clip_rect.size.y < 0)
return;

auto info = getTextureInfo(texture);
if (info.texture)
finish();
const auto& uv_rect = info.uv_rect;

corner_size = std::min(corner_size, rect.size.y / 2.0f);
corner_size = std::min(corner_size, rect.size.x / 2.0f);

auto n = vertex_data.size();
index_data.insert(index_data.end(), {
uint16_t(n + 0), uint16_t(n + 4), uint16_t(n + 1),
uint16_t(n + 1), uint16_t(n + 4), uint16_t(n + 5),
uint16_t(n + 1), uint16_t(n + 5), uint16_t(n + 2),
uint16_t(n + 2), uint16_t(n + 5), uint16_t(n + 6),
uint16_t(n + 2), uint16_t(n + 6), uint16_t(n + 3),
uint16_t(n + 3), uint16_t(n + 6), uint16_t(n + 7),

uint16_t(n + 4), uint16_t(n + 8), uint16_t(n + 5),
uint16_t(n + 5), uint16_t(n + 8), uint16_t(n + 9),
uint16_t(n + 5), uint16_t(n + 9), uint16_t(n + 6),
uint16_t(n + 6), uint16_t(n + 9), uint16_t(n + 10),
uint16_t(n + 6), uint16_t(n + 10), uint16_t(n + 7),
uint16_t(n + 7), uint16_t(n + 10), uint16_t(n + 11),

uint16_t(n + 8), uint16_t(n + 12), uint16_t(n + 9),
uint16_t(n + 9), uint16_t(n + 12), uint16_t(n + 13),
uint16_t(n + 9), uint16_t(n + 13), uint16_t(n + 10),
uint16_t(n + 10), uint16_t(n + 13), uint16_t(n + 14),
uint16_t(n + 10), uint16_t(n + 14), uint16_t(n + 11),
uint16_t(n + 11), uint16_t(n + 14), uint16_t(n + 15),
});
float x0 = rect.position.x;
float x1 = rect.position.x + corner_size;
float x2 = rect.position.x + rect.size.x - corner_size;
float x3 = rect.position.x + rect.size.x;
float y0 = rect.position.y;
float y1 = rect.position.y + corner_size;
float y2 = rect.position.y + rect.size.y - corner_size;
float y3 = rect.position.y + rect.size.y;
float uvx0 = uv_rect.position.x;
float uvx1 = uv_rect.position.x + uv_rect.size.x * 0.5f;
float uvx2 = uv_rect.position.x + uv_rect.size.x;
float uvy0 = uv_rect.position.y;
float uvy1 = uv_rect.position.y + uv_rect.size.y * 0.5f;
float uvy2 = uv_rect.position.y + uv_rect.size.y;

if (x3 < clip_rect.position.x) {
return;
} else if (x2 < clip_rect.position.x) {
uvx0 = uvx1 = uvx1 + (uvx2 - uvx1) * ((clip_rect.position.x - x2) / (x3 - x2));
x0 = x1 = x2 = clip_rect.position.x;
} else if (x1 < clip_rect.position.x) {
uvx0 = uvx1;
x0 = x1 = clip_rect.position.x;
} else if (x0 < clip_rect.position.x) {
uvx0 = uvx0 + (uvx1 - uvx0) * ((clip_rect.position.x - x0) / (x1 - x0));
x0 = clip_rect.position.x;
}
if (x0 > clip_rect.position.x + clip_rect.size.x) {
return;
} else if (x1 > clip_rect.position.x + clip_rect.size.x) {
uvx1 = uvx2 = uvx0 + (uvx1 - uvx0) * ((clip_rect.position.x + clip_rect.size.x - x0) / (x1 - x0));
x1 = x2 = x3 = clip_rect.position.x + clip_rect.size.x;
} else if (x2 > clip_rect.position.x + clip_rect.size.x) {
uvx2 = uvx1;
x2 = x3 = clip_rect.position.x + clip_rect.size.x;
} else if (x3 > clip_rect.position.x + clip_rect.size.x) {
uvx2 = uvx1 + (uvx2 - uvx1) * ((clip_rect.position.x + clip_rect.size.x - x2) / (x3 - x2));
x3 = clip_rect.position.x + clip_rect.size.x;
}
if (y3 < clip_rect.position.y) {
return;
} else if (y2 < clip_rect.position.y) {
uvy0 = uvy1 = uvy1 + (uvy2 - uvy1) * ((clip_rect.position.y - y2) / (y3 - y2));
y0 = y1 = y2 = clip_rect.position.y;
} else if (y1 < clip_rect.position.y) {
uvy0 = uvy1;
y0 = y1 = clip_rect.position.y;
} else if (y0 < clip_rect.position.y) {
uvy0 = uvy0 + (uvy1 - uvy0) * ((clip_rect.position.y - y0) / (y1 - y0));
y0 = clip_rect.position.y;
}
if (y0 > clip_rect.position.y + clip_rect.size.y) {
return;
} else if (y1 > clip_rect.position.y + clip_rect.size.y) {
uvy1 = uvy2 = uvy0 + (uvy1 - uvy0) * ((clip_rect.position.y + clip_rect.size.y - y0) / (y1 - y0));
y1 = y2 = y3 = clip_rect.position.y + clip_rect.size.y;
} else if (y2 > clip_rect.position.y + clip_rect.size.y) {
uvy2 = uvy1;
y2 = y3 = clip_rect.position.y + clip_rect.size.y;
} else if (y3 > clip_rect.position.y + clip_rect.size.y) {
uvy2 = uvy1 + (uvy2 - uvy1) * ((clip_rect.position.y + clip_rect.size.y - y2) / (y3 - y2));
y3 = clip_rect.position.y + clip_rect.size.y;
}

vertex_data.push_back({ {x0, y0}, color, {uvx0, uvy0}});
vertex_data.push_back({ {x1, y0}, color, {uvx1, uvy0}});
vertex_data.push_back({ {x2, y0}, color, {uvx1, uvy0}});
vertex_data.push_back({ {x3, y0}, color, {uvx2, uvy0}});

vertex_data.push_back({ {x0, y1}, color, {uvx0, uvy1}});
vertex_data.push_back({ {x1, y1}, color, {uvx1, uvy1}});
vertex_data.push_back({ {x2, y1}, color, {uvx1, uvy1}});
vertex_data.push_back({ {x3, y1}, color, {uvx2, uvy1}});

vertex_data.push_back({ {x0, y2}, color, {uvx0, uvy1}});
vertex_data.push_back({ {x1, y2}, color, {uvx1, uvy1}});
vertex_data.push_back({ {x2, y2}, color, {uvx1, uvy1}});
vertex_data.push_back({ {x3, y2}, color, {uvx2, uvy1}});

vertex_data.push_back({ {x0, y3}, color, {uvx0, uvy2}});
vertex_data.push_back({ {x1, y3}, color, {uvx1, uvy2}});
vertex_data.push_back({ {x2, y3}, color, {uvx1, uvy2}});
vertex_data.push_back({ {x3, y3}, color, {uvx2, uvy2}});

if (info.texture)
finish(info.texture);
}

void RenderTarget::finish()
{
finish(atlas_texture);
Expand Down
1 change: 1 addition & 0 deletions src/graphics/renderTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class RenderTarget : sp::NonCopyable
void drawStretchedH(sp::Rect rect, std::string_view texture, glm::u8vec4 color={255,255,255,255});
void drawStretchedV(sp::Rect rect, std::string_view texture, glm::u8vec4 color={255,255,255,255});
void drawStretchedHV(sp::Rect rect, float corner_size, std::string_view texture, glm::u8vec4 color={255,255,255,255});
void drawStretchedHVClipped(sp::Rect rect, sp::Rect clip_rect, float corner_size, std::string_view texture, glm::u8vec4 color={255,255,255,255});

void finish();

Expand Down

0 comments on commit 91b8a71

Please sign in to comment.