From 0791aa10969c917b4656c4918d11359616690b10 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 25 May 2023 01:55:33 +0300 Subject: [PATCH] Make sure util::atlas size is power-of-two --- libs/util/include/psemek/util/atlas.hpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libs/util/include/psemek/util/atlas.hpp b/libs/util/include/psemek/util/atlas.hpp index c93139d6..5c9c0076 100644 --- a/libs/util/include/psemek/util/atlas.hpp +++ b/libs/util/include/psemek/util/atlas.hpp @@ -53,14 +53,24 @@ namespace psemek::util part.begin[1] = 0; part.end[1] = part_height; - std::size_t new_width = std::max(array_.width(), free_start_ + part_width); - std::size_t new_height = std::max(array_.height(), part_height); + std::size_t new_width = array_.width(); + std::size_t new_height = array_.height(); - if (new_width > array_.width()) - new_width = std::max(new_width, array_.width() * 2); + while (new_width < free_start_ + part_width) + { + if (new_width == 0) + new_width = 1; + else + new_width *= 2; + } - if (new_height > array_.height()) - new_height = std::max(new_height, array_.height() * 2); + while (new_height < part_height) + { + if (new_height == 0) + new_height = 1; + else + new_height *= 2; + } array_.resize({new_width, new_height}, default_value_);