From 989b10986f1626d1a50fceb8a33174108c03da93 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 10 Apr 2023 14:46:18 +0100 Subject: Load texture fragment images as we go, make the image a member --- assetFactory/assetFactory.cpp | 19 ++++++------------- assetFactory/textureFragment.cpp | 8 ++++++++ assetFactory/textureFragment.h | 3 +++ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index ed1af58..ec0e19f 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -11,7 +11,6 @@ #include "resource.h" #include "saxParse-persistence.h" #include "texturePacker.h" -#include AssetFactory::AssetFactory() : shapes { @@ -105,30 +104,24 @@ void AssetFactory::createTexutre() const { if (!textureFragments.empty() && (!texture || textureFragmentPositions.empty())) { - // * load images - std::vector> images; - std::transform( - textureFragments.begin(), textureFragments.end(), std::back_inserter(images), [](const auto & tf) { - return std::make_unique(Resource::mapPath(tf.second->path), STBI_rgb_alpha); - }); // * layout images std::vector imageSizes; - std::transform(images.begin(), images.end(), std::back_inserter(imageSizes), [](const auto & image) { - return TexturePacker::Image {image->width, image->height}; + std::transform( + textureFragments.begin(), textureFragments.end(), std::back_inserter(imageSizes), [](const auto & tf) { + return TexturePacker::Image {tf.second->image->width, tf.second->image->height}; }); const auto [layout, outSize] = TexturePacker {imageSizes}.pack(); // * create texture texture = std::make_shared(outSize.x, outSize.y, TextureOptions {.wrap = GL_CLAMP_TO_EDGE}); std::transform(textureFragments.begin(), textureFragments.end(), std::inserter(textureFragmentPositions, textureFragmentPositions.end()), - [position = layout.begin(), image = images.begin(), size = imageSizes.begin(), - outSize = glm::vec2 {outSize}](const auto & tf) mutable { + [position = layout.begin(), size = imageSizes.begin(), outSize = glm::vec2 {outSize}]( + const auto & tf) mutable { const auto positionFraction = glm::vec4 {*position, *position + *size} / outSize.xyxy(); glTexSubImage2D(GL_TEXTURE_2D, 0, static_cast(position->x), static_cast(position->y), static_cast(size->x), static_cast(size->y), GL_RGBA, GL_UNSIGNED_BYTE, - image->get()->data.data()); + tf.second->image->data.data()); position++; - image++; size++; return decltype(textureFragmentPositions)::value_type {tf.first, positionFraction}; }); diff --git a/assetFactory/textureFragment.cpp b/assetFactory/textureFragment.cpp index 72107a5..d153688 100644 --- a/assetFactory/textureFragment.cpp +++ b/assetFactory/textureFragment.cpp @@ -1,7 +1,15 @@ #include "textureFragment.h" +#include "resource.h" +#include bool TextureFragment::persist(Persistence::PersistenceStore & store) { return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(path); } + +void +TextureFragment::postLoad() +{ + image = std::make_unique(Resource::mapPath(path), STBI_rgb_alpha); +} diff --git a/assetFactory/textureFragment.h b/assetFactory/textureFragment.h index 52f2591..d03affd 100644 --- a/assetFactory/textureFragment.h +++ b/assetFactory/textureFragment.h @@ -1,5 +1,6 @@ #pragma once +#include "gfx/image.h" #include "persistence.h" #include "stdTypeDefs.hpp" @@ -7,8 +8,10 @@ class TextureFragment : public Persistence::Persistable, public StdTypeDefs image; private: friend Persistence::SelectionPtrBase; bool persist(Persistence::PersistenceStore & store) override; + void postLoad() override; }; -- cgit v1.2.3