From ba762584a78a04fa154cda47e41e42794264e77f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 18 Mar 2024 20:06:58 +0300 Subject: [PATCH] Atlas minor refactor --- src/graphics/Atlas.cpp | 41 ++++++++++++++++------------------------- src/graphics/Atlas.h | 12 ++++++------ 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/graphics/Atlas.cpp b/src/graphics/Atlas.cpp index b4a0077b..cd49e0c5 100644 --- a/src/graphics/Atlas.cpp +++ b/src/graphics/Atlas.cpp @@ -5,51 +5,42 @@ #include "Texture.h" #include "ImageData.h" -using std::vector; -using std::string; -using std::unique_ptr; -using std::shared_ptr; -using std::unordered_map; - -Atlas::Atlas(ImageData* image, - unordered_map regions) - : texture(Texture::from(image)), - image(image), - regions(regions) { +Atlas::Atlas(ImageData* image, std::unordered_map regions) + : texture(Texture::from(image)), + image(image), + regions(regions) { } Atlas::~Atlas() { - delete image; - delete texture; } -bool Atlas::has(string name) const { +bool Atlas::has(const std::string& name) const { return regions.find(name) != regions.end(); } -const UVRegion& Atlas::get(string name) const { +const UVRegion& Atlas::get(const std::string& name) const { return regions.at(name); } Texture* Atlas::getTexture() const { - return texture; + return texture.get(); } ImageData* Atlas::getImage() const { - return image; + return image.get(); } -void AtlasBuilder::add(string name, ImageData* image) { - entries.push_back(atlasentry{name, shared_ptr(image)}); +void AtlasBuilder::add(std::string name, ImageData* image) { + entries.push_back(atlasentry{name, std::shared_ptr(image)}); names.insert(name); } -bool AtlasBuilder::has(string name) const { +bool AtlasBuilder::has(const std::string& name) const { return names.find(name) != names.end(); } Atlas* AtlasBuilder::build(uint extrusion, uint maxResolution) { - unique_ptr sizes (new uint[entries.size() * 2]); + auto sizes = std::make_unique(entries.size() * 2); uint index = 0; for (auto& entry : entries) { auto image = entry.image; @@ -73,9 +64,9 @@ Atlas* AtlasBuilder::build(uint extrusion, uint maxResolution) { } } - unordered_map regions; - unique_ptr canvas (new ImageData(ImageFormat::rgba8888, width, height)); - vector rects = packer.getResult(); + auto canvas = std::make_unique(ImageFormat::rgba8888, width, height); + std::unordered_map regions; + std::vector rects = packer.getResult(); for (uint i = 0; i < entries.size(); i++) { const rectangle& rect = rects[i]; const atlasentry& entry = entries[rect.idx]; @@ -93,4 +84,4 @@ Atlas* AtlasBuilder::build(uint extrusion, uint maxResolution) { unitX * (x + w), unitY * (y + h)); } return new Atlas(canvas.release(), regions); -} \ No newline at end of file +} diff --git a/src/graphics/Atlas.h b/src/graphics/Atlas.h index fb6ec84e..ed1c84e4 100644 --- a/src/graphics/Atlas.h +++ b/src/graphics/Atlas.h @@ -13,15 +13,15 @@ class ImageData; class Texture; class Atlas { - Texture* texture; - ImageData* image; + std::unique_ptr texture; + std::unique_ptr image; std::unordered_map regions; public: Atlas(ImageData* image, std::unordered_map regions); ~Atlas(); - bool has(std::string name) const; - const UVRegion& get(std::string name) const; + bool has(const std::string& name) const; + const UVRegion& get(const std::string& name) const; Texture* getTexture() const; ImageData* getImage() const; @@ -39,10 +39,10 @@ class AtlasBuilder { public: AtlasBuilder() {} void add(std::string name, ImageData* image); - bool has(std::string name) const; + bool has(const std::string& name) const; const std::set& getNames() { return names; }; Atlas* build(uint extrusion, uint maxResolution=8192); }; -#endif // GRAPHICS_ATLAS_H_ \ No newline at end of file +#endif // GRAPHICS_ATLAS_H_