Created
November 26, 2020 17:20
-
-
Save Journeyman1337/d518e94961a77325c1ad8c9a56f519cd to your computer and use it in GitHub Desktop.
atlas building
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2020 Daniel Valcour | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
void jtk::SpriteAtlas::Build(jtk::TextureLibrary* library, const std::vector<std::string>& identifiers, size_t atlas_size) | |
{ | |
if (identifiers.size() == 0) | |
{ | |
return; | |
} | |
auto rects = std::vector<AtlasRect>(); | |
auto page_dimensions = jtk::ivec2(0, 0); | |
size_t page_index = 0; | |
for (size_t i = 0; i < identifiers.size(); i++) | |
{ | |
std::optional<jtk::tex_sptr_t> t = library->GetTexture(identifiers[i]); | |
if (t.has_value()) | |
{ | |
rects.push_back(AtlasRect(identifiers[i], t.value())); | |
} | |
//todo else log error expected sprite texture not found | |
} | |
std::sort(rects.begin(), rects.end()); | |
auto& last_rect = rects[rects.size() - 1]; | |
if (last_rect.w > atlas_size || last_rect.h > atlas_size) | |
{ | |
std::string message = "Texture of name \""; | |
message += last_rect.identifier; | |
message += "\" cannot be added to sprite atlas because it has one or more dimensions that are larger than the page size."; | |
throw jtk::Exception("SPRITE ATLAS BUILD FAILURE", message.c_str()); | |
} | |
{ | |
size_t rect_index = rects.size() - 1; | |
auto nodes = std::vector<AtlasRect>(); | |
auto current_rect = &rects[rect_index--]; | |
page_dimensions = jtk::ivec2(current_rect->w, current_rect->h); | |
current_rect->x = 0; | |
current_rect->y = 0; | |
current_rect->p = page_index; | |
while (rect_index != -1) | |
{ | |
current_rect = &rects[rect_index--]; | |
bool placed = false; | |
size_t n = 0; | |
while (!placed && n < nodes.size()) | |
{ | |
AtlasRect current_node = nodes[n]; | |
if (current_rect->w <= current_node.w && current_rect->h <= current_node.h) | |
{ | |
current_rect->x = current_node.x; | |
current_rect->y = current_node.y; | |
current_rect->p = current_node.p; | |
if (current_node.w - current_rect->w > current_node.h - current_rect->h) | |
{ | |
if (current_rect->w < current_node.w) | |
{ | |
nodes.push_back(AtlasRect(current_rect->x + current_rect->w, current_rect->y, current_node.w - current_rect->w, current_node.h, current_node.p)); | |
} | |
if (current_rect->h < current_node.h) | |
{ | |
nodes.push_back(AtlasRect(current_rect->x, current_rect->y + current_rect->h, current_rect->w, current_node.h - current_rect->h, current_node.p)); | |
} | |
} | |
else | |
{ | |
if (current_rect->w < current_node.w) | |
{ | |
nodes.push_back(AtlasRect(current_rect->x + current_rect->w, current_rect->y, current_node.w - current_rect->w, current_rect->h, current_node.p)); | |
} | |
if (current_rect->h < current_node.h) | |
{ | |
nodes.push_back(AtlasRect(current_rect->x, current_rect->y + current_rect->h, current_node.w, current_node.h - current_rect->h, current_node.p)); | |
} | |
} | |
nodes[n] = nodes[nodes.size() - 1]; | |
nodes.pop_back(); | |
std::sort(nodes.begin(), nodes.end()); | |
placed = true; | |
} | |
n++; | |
} | |
if (!placed) | |
{ | |
if (current_rect->h <= page_dimensions.y && page_dimensions.y >= page_dimensions.x && page_dimensions.x + current_rect->w < atlas_size) | |
{ | |
current_rect->x = page_dimensions.x; | |
current_rect->y = 0; | |
current_rect->p = page_index; | |
page_dimensions.x += static_cast<jtk::int_t>(current_rect->w); | |
nodes.push_back(AtlasRect(current_rect->x, current_rect->h, current_rect->w, page_dimensions.y - current_rect->h, page_index)); | |
} | |
else if (current_rect->w <= page_dimensions.x && page_dimensions.x >= page_dimensions.y && page_dimensions.y + current_rect->h < atlas_size) | |
{ | |
current_rect->x = 0; | |
current_rect->y = page_dimensions.y; | |
current_rect->p = page_index; | |
page_dimensions.y += static_cast<jtk::int_t>(current_rect->h); | |
nodes.push_back(AtlasRect(current_rect->w, current_rect->y, page_dimensions.x - current_rect->w, current_rect->h, page_index)); | |
} | |
else if (current_rect->h <= page_dimensions.y && page_dimensions.x + current_rect->w < atlas_size) | |
{ | |
current_rect->x = page_dimensions.x; | |
current_rect->y = 0; | |
current_rect->p = page_index; | |
page_dimensions.x += static_cast<jtk::int_t>(current_rect->w); | |
nodes.push_back(AtlasRect(current_rect->x, current_rect->h, current_rect->w, page_dimensions.y - current_rect->h, page_index)); | |
} | |
else if (current_rect->w <= page_dimensions.x && page_dimensions.y + current_rect->h < atlas_size) | |
{ | |
current_rect->x = 0; | |
current_rect->y = page_dimensions.y; | |
current_rect->p = page_index; | |
page_dimensions.y += static_cast<jtk::int_t>(current_rect->h); | |
nodes.push_back(AtlasRect(current_rect->w, current_rect->y, page_dimensions.x - current_rect->w, current_rect->h, page_index)); | |
} | |
else | |
{ | |
if (page_dimensions.x < atlas_size - 1) //create nodes for leftover space | |
{ | |
nodes.push_back(AtlasRect(page_dimensions.x, 0, atlas_size - page_dimensions.x, atlas_size, page_index)); | |
} | |
if (page_dimensions.y < atlas_size - 1) | |
{ | |
nodes.push_back(AtlasRect(0, page_dimensions.y, page_dimensions.x, atlas_size - page_dimensions.y, page_index)); | |
} | |
page_index++; | |
page_dimensions = jtk::ivec2(current_rect->w, current_rect->h); | |
current_rect->x = 0; | |
current_rect->y = 0; | |
current_rect->p = page_index; | |
} | |
} | |
} | |
{ | |
for (size_t page = 0; page <= page_index; page++) | |
{ | |
pages.push_back(jtk::Texture::CreateShared(atlas_size, atlas_size, jtk::rgba8(0, 0, 0, 0))); | |
} | |
for (size_t rect_index = 0; rect_index < rects.size(); rect_index++) | |
{ | |
auto& rect = rects[rect_index]; | |
auto& page = pages[rect.p]; | |
page->CopyTexture(rect.tex, rect.x, rect.y); | |
fontmap.push_back(jtk::SpriteCoordinates( | |
static_cast<jtk::int_t>(rect.p), | |
(float)rect.x / (float)atlas_size, | |
(float)rect.y / (float)atlas_size, | |
(float)(rect.x + rect.w) / (float)atlas_size, | |
(float(rect.y + rect.h)) / (float)atlas_size | |
)); | |
sprite_map[rect.identifier] = jtk::Sprite(this, static_cast<jtk::uint_t>(rect_index)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment