2022-10-02 04:29:51 +08:00
|
|
|
#include <blah_subtexture.h>
|
|
|
|
#include <blah_calc.h>
|
2020-08-26 15:38:01 +08:00
|
|
|
|
|
|
|
using namespace Blah;
|
|
|
|
|
2020-12-24 08:57:49 +08:00
|
|
|
Subtexture::Subtexture() {}
|
2020-08-26 15:38:01 +08:00
|
|
|
|
2020-12-24 08:57:49 +08:00
|
|
|
Subtexture::Subtexture(const TextureRef& texture)
|
2021-12-13 12:41:23 +08:00
|
|
|
: Subtexture(texture, Rectf(0, 0, (float)texture->width(), (float)texture->height())) {}
|
2020-12-24 08:57:49 +08:00
|
|
|
|
2021-12-13 12:41:23 +08:00
|
|
|
Subtexture::Subtexture(const TextureRef& texture, Rectf source)
|
|
|
|
: Subtexture(texture, source, Rectf(0, 0, source.w, source.h)) {}
|
2020-08-26 15:38:01 +08:00
|
|
|
|
2021-12-13 12:41:23 +08:00
|
|
|
Subtexture::Subtexture(const TextureRef& texture, Rectf source, Rectf frame)
|
2020-08-26 15:38:01 +08:00
|
|
|
: texture(texture), source(source), frame(frame)
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Subtexture::update()
|
|
|
|
{
|
|
|
|
draw_coords[0].x = -frame.x;
|
|
|
|
draw_coords[0].y = -frame.y;
|
|
|
|
draw_coords[1].x = -frame.x + source.w;
|
|
|
|
draw_coords[1].y = -frame.y;
|
|
|
|
draw_coords[2].x = -frame.x + source.w;
|
|
|
|
draw_coords[2].y = -frame.y + source.h;
|
|
|
|
draw_coords[3].x = -frame.x;
|
|
|
|
draw_coords[3].y = -frame.y + source.h;
|
|
|
|
|
2020-12-24 08:16:09 +08:00
|
|
|
if (texture)
|
2020-08-26 15:38:01 +08:00
|
|
|
{
|
|
|
|
float uvx = 1.0f / (float)texture->width();
|
|
|
|
float uvy = 1.0f / (float)texture->height();
|
|
|
|
|
|
|
|
tex_coords[0].x = source.x * uvx;
|
|
|
|
tex_coords[0].y = source.y * uvy;
|
|
|
|
tex_coords[1].x = (source.x + source.w ) * uvx;
|
|
|
|
tex_coords[1].y = source.y * uvy;
|
|
|
|
tex_coords[2].x = (source.x + source.w) * uvx;
|
|
|
|
tex_coords[2].y = (source.y + source.h) * uvy;
|
|
|
|
tex_coords[3].x = source.x * uvx;
|
|
|
|
tex_coords[3].y = (source.y + source.h) * uvy;
|
|
|
|
}
|
|
|
|
}
|
2020-12-24 16:19:58 +08:00
|
|
|
|
2021-12-13 12:41:23 +08:00
|
|
|
void Subtexture::crop_info(const Rectf& clip, Rectf* dest_source, Rectf* dest_frame) const
|
2020-12-24 16:19:58 +08:00
|
|
|
{
|
|
|
|
*dest_source = (clip + source.top_left() + frame.top_left()).overlap_rect(source);
|
|
|
|
|
2020-12-27 06:44:48 +08:00
|
|
|
dest_frame->x = Calc::min(0.0f, frame.x + clip.x);
|
|
|
|
dest_frame->y = Calc::min(0.0f, frame.y + clip.y);
|
2020-12-24 16:19:58 +08:00
|
|
|
dest_frame->w = clip.w;
|
|
|
|
dest_frame->h = clip.h;
|
|
|
|
}
|
|
|
|
|
2021-12-13 12:41:23 +08:00
|
|
|
Subtexture Subtexture::crop(const Rectf& clip) const
|
2020-12-24 16:19:58 +08:00
|
|
|
{
|
|
|
|
Subtexture dst;
|
|
|
|
dst.texture = texture;
|
|
|
|
crop_info(clip, &dst.source, &dst.frame);
|
|
|
|
dst.update();
|
|
|
|
return dst;
|
2021-03-24 16:07:49 +08:00
|
|
|
}
|