Skip to content

Commit 21d5737

Browse files
committed
feat: improve image loading error handling
Refactor image loading to return detailed result codes instead of pointers. Add checks for non-image files, failed loads, and images exceeding atlas size. Update main loop to log appropriate warnings or errors for each case and exit on oversized images. This improves robustness and user feedback during packing.
1 parent f2abc47 commit 21d5737

5 files changed

Lines changed: 57 additions & 15 deletions

File tree

src/Atlas/AtlasSize.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@ cAtlasSize::cAtlasSize(const sConfig& config)
1717
{
1818
}
1919

20+
bool cAtlasSize::isFitToMaxSize(const sSize& size) const
21+
{
22+
// Include atlas BORDER
23+
auto width = size.width + m_config.border * 2u;
24+
auto height = size.height + m_config.border * 2u;
25+
26+
return !(width > m_config.maxAtlasSize || height > m_config.maxAtlasSize);
27+
}
28+
2029
void cAtlasSize::addRect(const sSize& size)
2130
{
31+
// include sprite PADDING
2232
auto width = size.width + m_config.padding * 2u;
2333
auto height = size.height + m_config.padding * 2u;
2434

src/Atlas/AtlasSize.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class cAtlasSize final
1717
public:
1818
explicit cAtlasSize(const sConfig& config);
1919

20+
bool isFitToMaxSize(const sSize& size) const;
21+
2022
void addRect(const sSize& size);
2123
uint32_t getArea() const;
2224

src/ImageList.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,32 @@ cImageList::~cImageList()
3838
delete m_trim;
3939
}
4040

41-
const cImage* cImageList::loadImage(const std::string& path, uint32_t trimCount)
41+
cImageList::Result cImageList::loadImage(const std::string& path, uint32_t trimCount)
4242
{
43-
if (cImage::IsImage(path.c_str()))
43+
if (cImage::IsImage(path.c_str()) == false)
4444
{
45-
std::unique_ptr<cImage> image(new cImage());
45+
return Result::NotAnImage;
46+
}
4647

47-
if (image->load(path.c_str(), trimCount, m_trim) == true)
48-
{
49-
auto& bmp = image->getBitmap();
50-
auto& size = bmp.getSize();
51-
m_size.addRect(size);
48+
std::unique_ptr<cImage> image(new cImage());
5249

53-
m_images.push_back(image.release());
50+
if (image->load(path.c_str(), trimCount, m_trim) == false)
51+
{
52+
return Result::CannotOpen;
53+
}
5454

55-
return m_images.back();
56-
}
55+
auto& bmp = image->getBitmap();
56+
auto& size = bmp.getSize();
57+
if (m_size.isFitToMaxSize(size) == false)
58+
{
59+
return Result::TooBig;
5760
}
5861

59-
return nullptr;
62+
m_size.addRect(size);
63+
64+
m_images.push_back(image.release());
65+
66+
return Result::OK;
6067
}
6168

6269
bool cImageList::doPacking(const char* desiredAtlasName, const char* outputResName,

src/ImageList.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ class cImageList final
2323
cImageList(const sConfig& config, uint32_t reserve);
2424
~cImageList();
2525

26-
const cImage* loadImage(const std::string& path, uint32_t trimCount);
26+
enum class Result
27+
{
28+
OK,
29+
NotAnImage,
30+
CannotOpen,
31+
TooBig,
32+
};
33+
Result loadImage(const std::string& path, uint32_t trimCount);
2734

2835
bool doPacking(const char* desiredAtlasName, const char* outputResName,
2936
const char* resPathPrefix, sSize& atlasSize);

src/main.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,26 @@ int main(int argc, char* argv[])
216216

217217
for (const auto& f : files)
218218
{
219-
auto image = imageList.loadImage(f.path, f.trimCount);
220-
if (image == nullptr)
219+
auto result = imageList.loadImage(f.path, f.trimCount);
220+
switch (result)
221221
{
222+
case cImageList::Result::OK:
223+
break;
224+
225+
case cImageList::Result::NotAnImage:
226+
cLog::Warning("File '{}' is not an image.", f.path);
227+
break;
228+
229+
case cImageList::Result::CannotOpen:
222230
cLog::Warning("File '{}' not loaded.", f.path);
231+
break;
232+
233+
case cImageList::Result::TooBig:
234+
cLog::Error("Image '{}' is too large for the atlas (max size: {} x {}).",
235+
f.path,
236+
config.maxAtlasSize, config.maxAtlasSize);
237+
238+
return -1;
223239
}
224240
}
225241

0 commit comments

Comments
 (0)