2020-08-26 15:38:01 +08:00
|
|
|
#pragma once
|
2022-10-02 04:29:51 +08:00
|
|
|
#include <blah_graphics.h>
|
|
|
|
#include <blah_spatial.h>
|
2020-08-26 15:38:01 +08:00
|
|
|
|
|
|
|
namespace Blah
|
|
|
|
{
|
2021-01-01 05:43:23 +08:00
|
|
|
// Subtexture is a view into a texture, and can be used to
|
|
|
|
// easily represent sprites in a larger texture atlas.
|
2020-08-26 15:38:01 +08:00
|
|
|
struct Subtexture
|
|
|
|
{
|
2020-12-24 08:57:49 +08:00
|
|
|
// Reference to our Texture
|
|
|
|
TextureRef texture;
|
|
|
|
|
|
|
|
// Source rectangle, in pixels
|
2021-12-13 12:41:23 +08:00
|
|
|
Rectf source;
|
2020-12-24 08:57:49 +08:00
|
|
|
|
|
|
|
// Frame rectangle, in pixels. This describes padding around the image.
|
|
|
|
// This is useful for drawing images that have been trimmed. Ex. if the source
|
|
|
|
// is 32,32, but the original image was 64,64, the frame could be -16,-16,64,64
|
2021-12-13 12:41:23 +08:00
|
|
|
Rectf frame;
|
2020-12-24 08:57:49 +08:00
|
|
|
|
|
|
|
// `draw_coords` are automatically assigned through `update` method
|
2021-12-13 12:41:23 +08:00
|
|
|
Vec2f draw_coords[4];
|
2020-12-24 08:57:49 +08:00
|
|
|
|
|
|
|
// `tex_coords` are automatically assigned through the `update` method
|
2021-12-13 12:41:23 +08:00
|
|
|
Vec2f tex_coords[4];
|
2020-08-26 15:38:01 +08:00
|
|
|
|
|
|
|
Subtexture();
|
2020-12-24 08:57:49 +08:00
|
|
|
Subtexture(const TextureRef& texture);
|
2021-12-13 12:41:23 +08:00
|
|
|
Subtexture(const TextureRef& texture, Rectf source);
|
|
|
|
Subtexture(const TextureRef& texture, Rectf source, Rectf frame);
|
2020-08-26 15:38:01 +08:00
|
|
|
|
2020-12-24 08:57:49 +08:00
|
|
|
// Returns the width of the image
|
2020-08-26 15:38:01 +08:00
|
|
|
float width() const { return frame.w; }
|
2020-12-24 08:57:49 +08:00
|
|
|
|
|
|
|
// Returns the height of the image
|
2020-08-26 15:38:01 +08:00
|
|
|
float height() const { return frame.h; }
|
|
|
|
|
2020-12-24 08:57:49 +08:00
|
|
|
// updates the `draw_coords` and `tex_coords`
|
2020-08-26 15:38:01 +08:00
|
|
|
void update();
|
2020-12-24 16:19:58 +08:00
|
|
|
|
|
|
|
// returns resulting source and frame rectangles based on the provided clip rectangle
|
2021-12-13 12:41:23 +08:00
|
|
|
void crop_info(const Rectf& clip, Rectf* dest_source, Rectf* dest_frame) const;
|
2020-12-24 16:19:58 +08:00
|
|
|
|
|
|
|
// returns a subtexture cropped to the provided rectangle
|
2021-12-13 12:41:23 +08:00
|
|
|
Subtexture crop(const Rectf& clip) const;
|
2020-08-26 15:38:01 +08:00
|
|
|
};
|
|
|
|
}
|