From 63a18c31ee0ccc5f4ac967aaa2f674eb89f90528 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 26 Apr 2017 00:44:51 +0300 Subject: Improve a bit creating empty atlas. --- src/resources/atlas/atlasmanager.cpp | 94 ++++++++++++++++++++++++++++++++++-- src/resources/atlas/atlasmanager.h | 6 +++ 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/src/resources/atlas/atlasmanager.cpp b/src/resources/atlas/atlasmanager.cpp index f8251ba7d..7b7ab4536 100644 --- a/src/resources/atlas/atlasmanager.cpp +++ b/src/resources/atlas/atlasmanager.cpp @@ -98,10 +98,9 @@ AtlasResource *AtlasManager::loadEmptyAtlas(const std::string &name, AtlasResource *resource = new AtlasResource; loadEmptyImages(files, images); - int maxSize = OpenGLImageHelper::getTextureSize(); // sorting images on atlases. - simpleSort(name, atlases, images, maxSize); + emptySort(name, atlases, images); FOR_EACH (std::vector::iterator, it, atlases) { @@ -110,10 +109,10 @@ AtlasResource *AtlasManager::loadEmptyAtlas(const std::string &name, continue; atlas->atlasImage = new Image(0, - 8192, 8192, - 8192, 8192); + 1024, 1024, + 1024, 1024); // convert SDL images to OpenGL - convertAtlas(atlas); + convertEmptyAtlas(atlas); resource->atlases.push_back(atlas); } @@ -269,6 +268,44 @@ void AtlasManager::simpleSort(const std::string &restrict name, BLOCK_END("AtlasManager::simpleSort") } +void AtlasManager::emptySort(const std::string &restrict name, + std::vector &restrict atlases, + const std::vector &restrict images) +{ + BLOCK_START("AtlasManager::simpleSort") + TextureAtlas *atlas = new TextureAtlas; + std::vector::const_iterator it = images.begin(); + const std::vector::const_iterator it_end = images.end(); + for (it = images.begin(); it != it_end; ++ it) + { + const Image *const img = *it; + if (img) + { + atlas->name = std::string("atlas_").append(name).append( + "_").append(img->mIdPath); + break; + } + } + + for (it = images.begin(); it != it_end; ++ it) + { + Image *const img = *it; + if (img) + { + AtlasItem *const item = new AtlasItem(img); + item->name = img->mIdPath; + item->x = 0; + item->y = 0; + atlas->items.push_back(item); + } + } + if (!atlas->items.empty()) + atlases.push_back(atlas); + else + delete atlas; + BLOCK_END("AtlasManager::simpleSort") +} + void AtlasManager::createSDLAtlas(TextureAtlas *const atlas) { BLOCK_START("AtlasManager::createSDLAtlas") @@ -358,6 +395,53 @@ void AtlasManager::createSDLAtlas(TextureAtlas *const atlas) BLOCK_END("AtlasManager::createSDLAtlas") } +void AtlasManager::convertEmptyAtlas(TextureAtlas *const atlas) +{ + // no check for null pointer in atlas because it was in caller + // convert surface to OpemGL image + Image *const oldImage = atlas->atlasImage; + + if (oldImage->mSDLSurface) + { + atlas->atlasImage = imageHelper->loadSurface( + atlas->atlasImage->mSDLSurface); + oldImage->decRef(); + } + + Image *const image = atlas->atlasImage; + if (!image) + return; + + image->mIdPath = atlas->name; +#ifdef DEBUG_IMAGES + logger->log("set name %p, %s", static_cast(image), + image->mIdPath.c_str()); +#endif // DEBUG_IMAGES + + image->incRef(); + + FOR_EACH (std::vector::iterator, it, atlas->items) + { + AtlasItem *const item = *it; + // delete SDL Image + delete item->image; + // store OpenGL image + item->image = image->getSubImage(item->x, item->y, + item->width, item->height); + Image *const image2 = item->image; + if (image2) + { + image2->mIdPath = item->name; +#ifdef DEBUG_IMAGES + logger->log("set empty name %p, %s", static_cast(image2), + image2->mIdPath.c_str()); +#endif // DEBUG_IMAGES + + image2->incRef(); + } + } +} + void AtlasManager::convertAtlas(TextureAtlas *const atlas) { // no check for null pointer in atlas because it was in caller diff --git a/src/resources/atlas/atlasmanager.h b/src/resources/atlas/atlasmanager.h index e619ce736..13db9717e 100644 --- a/src/resources/atlas/atlasmanager.h +++ b/src/resources/atlas/atlasmanager.h @@ -58,6 +58,10 @@ class AtlasManager final static void loadEmptyImages(const StringVect &files, std::vector &images); + static void emptySort(const std::string &restrict name, + std::vector &restrict atlases, + const std::vector &restrict images); + static void simpleSort(const std::string &restrict name, std::vector &restrict atlases, const std::vector &restrict images, @@ -66,6 +70,8 @@ class AtlasManager final static void createSDLAtlas(TextureAtlas *const atlas) A_NONNULL(1); static void convertAtlas(TextureAtlas *const atlas) A_NONNULL(1); + + static void convertEmptyAtlas(TextureAtlas *const atlas) A_NONNULL(1); }; #endif // USE_OPENGL -- cgit v1.2.3-70-g09d2