71 void Atlas::LoadData(
const std::vector<std::pair<std::string, std::string> >& textures_path_names)
73 if (textures_path_names.size() == 0)
79 std::vector<Texture> textures;
80 textures.reserve(textures_path_names.size());
82 for (
int i = 0; i < textures_path_names.size(); ++i)
84 if (textures_path_names[i].first.empty())
89 unsigned char*
data = stbi_load(textures_path_names[i].first.c_str(), &tex.
width, &tex.
height, &tex.
depth, 0);
93 tex.
identifier = textures_path_names[i].second;
94 std::ifstream animation_file((textures_path_names[i].first +
".mcmeta").c_str());
95 tex.
animated = animation_file.good();
97 textures.push_back(tex);
99 stbi_image_free(
data);
104 std::vector<Texture> kept_textures;
105 kept_textures.reserve(textures.size());
106 for (
int i = 0; i < textures.size(); ++i)
109 if (textures[i].depth < 1 || textures[i].depth > 4)
111 LOG_WARNING(
"Unknown depth format(" << textures[i].depth <<
") for texture " << textures[i].identifier);
114 kept_textures.push_back(textures[i]);
118 using spaces_type = rectpack2D::empty_spaces<false>;
119 using rect_type = rectpack2D::output_rect_t<spaces_type>;
121 std::vector<rect_type> rectangles;
122 rectangles.emplace_back(rectpack2D::rect_xywh(0, 0, 16, 16));
123 for (
int i = 0; i < kept_textures.size(); ++i)
125 rectangles.emplace_back(rectpack2D::rect_xywh(0, 0, kept_textures[i].
width, kept_textures[i].
height));
128 const auto result_size = rectpack2D::find_best_packing<spaces_type>(
134 return rectpack2D::callback_result::CONTINUE_PACKING;
137 LOG_ERROR(
"Error packing the textures, aborting");
138 return rectpack2D::callback_result::ABORT_PACKING;
140 rectpack2D::flipping_option::DISABLED
144 LOG_INFO(
"All textures packed, resultant atlas size: " << result_size.h <<
"x" << result_size.w);
147 Reset(result_size.h, result_size.w);
151 { rectangles[0].w, rectangles[0].h },
152 { rectangles[0].x, rectangles[0].y },
157 for (
int i = 0; i < kept_textures.size(); ++i)
159 const rect_type& rectangle = rectangles[i + 1];
162 { rectangle.w, rectangle.h },
163 { rectangle.x, rectangle.y },
168 for (
int row = 0; row < rectangle.h; ++row)
170 for (
int col = 0; col < rectangle.w; ++col)
177 switch (kept_textures[i].depth)
180 r = kept_textures[i].Get(col, row, 0);
181 g = kept_textures[i].Get(col, row, 0);
182 b = kept_textures[i].Get(col, row, 0);
186 r = kept_textures[i].Get(col, row, 0);
187 g = kept_textures[i].Get(col, row, 0);
188 b = kept_textures[i].Get(col, row, 0);
189 a = kept_textures[i].Get(col, row, 1);
192 r = kept_textures[i].Get(col, row, 0);
193 g = kept_textures[i].Get(col, row, 1);
194 b = kept_textures[i].Get(col, row, 2);
198 r = kept_textures[i].Get(col, row, 0);
199 g = kept_textures[i].Get(col, row, 1);
200 b = kept_textures[i].Get(col, row, 2);
201 a = kept_textures[i].Get(col, row, 3);
205 *(
Get(rectangle.y + row, rectangle.x + col, 0)) = r;
206 *(
Get(rectangle.y + row, rectangle.x + col, 1)) = g;
207 *(
Get(rectangle.y + row, rectangle.x + col, 2)) = b;
208 *(
Get(rectangle.y + row, rectangle.x + col, 3)) = a;
const unsigned char Get(const int x, const int y, const int c) const