feat: canvas from texture atlas element
This commit is contained in:
@@ -49,6 +49,14 @@ ImageData* Atlas::getImage() const {
|
||||
return image.get();
|
||||
}
|
||||
|
||||
std::shared_ptr<Texture> Atlas::shareTexture() const {
|
||||
return texture;
|
||||
}
|
||||
|
||||
std::shared_ptr<ImageData> Atlas::shareImageData() const {
|
||||
return image;
|
||||
}
|
||||
|
||||
void AtlasBuilder::add(const std::string& name, std::unique_ptr<ImageData> image) {
|
||||
entries.push_back(atlasentry{name, std::shared_ptr<ImageData>(image.release())});
|
||||
names.insert(name);
|
||||
|
||||
@@ -14,8 +14,8 @@ class ImageData;
|
||||
class Texture;
|
||||
|
||||
class Atlas {
|
||||
std::unique_ptr<Texture> texture;
|
||||
std::unique_ptr<ImageData> image;
|
||||
std::shared_ptr<Texture> texture;
|
||||
std::shared_ptr<ImageData> image;
|
||||
std::unordered_map<std::string, UVRegion> regions;
|
||||
public:
|
||||
/// @param image atlas raster
|
||||
@@ -36,6 +36,9 @@ public:
|
||||
|
||||
Texture* getTexture() const;
|
||||
ImageData* getImage() const;
|
||||
|
||||
std::shared_ptr<Texture> shareTexture() const;
|
||||
std::shared_ptr<ImageData> shareImageData() const;
|
||||
};
|
||||
|
||||
struct atlasentry {
|
||||
|
||||
@@ -95,6 +95,17 @@ void ImageData::blit(const ImageData& image, int x, int y) {
|
||||
throw std::runtime_error("mismatching format");
|
||||
}
|
||||
|
||||
std::unique_ptr<ImageData> ImageData::cropped(int x, int y, int width, int height) const {
|
||||
width = std::min<int>(width, this->width - x);
|
||||
height = std::min<int>(height, this->height - y);
|
||||
if (width <= 0 || height <= 0) {
|
||||
throw std::runtime_error("invalid crop dimensions");
|
||||
}
|
||||
auto subImage = std::make_unique<ImageData>(format, width, height);
|
||||
subImage->blitMatchingFormat(*this, -x, -y);
|
||||
return subImage;
|
||||
}
|
||||
|
||||
static bool clip_line(int& x1, int& y1, int& x2, int& y2, int width, int height) {
|
||||
const int left = 0;
|
||||
const int right = width;
|
||||
|
||||
@@ -33,6 +33,8 @@ public:
|
||||
void extrude(int x, int y, int w, int h);
|
||||
void fixAlphaColor();
|
||||
|
||||
std::unique_ptr<ImageData> cropped(int x, int y, int width, int height) const;
|
||||
|
||||
ubyte* getData() const {
|
||||
return data.get();
|
||||
}
|
||||
|
||||
@@ -54,6 +54,12 @@ void Texture::reload(const ubyte* data) {
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void Texture::reloadPartial(const ImageData& image, uint x, uint y, uint w, uint h) {
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, image.getData());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
std::unique_ptr<ImageData> Texture::readData() {
|
||||
auto data = std::make_unique<ubyte[]>(width * height * 4);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
|
||||
@@ -19,6 +19,7 @@ public:
|
||||
virtual void bind() const;
|
||||
virtual void unbind() const;
|
||||
void reload(const ubyte* data);
|
||||
void reloadPartial(const ImageData& image, uint x, uint y, uint w, uint h);
|
||||
|
||||
void setNearestFilter();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user