App fails to start when trying to load JPG image

I was loading my PNG background images for my game, but since JPG’s are much smaller I thought I would try with that, but that causes the app to vomit.

I added images via Projucer as binary then relaunched VS 2019.

This works fine;

rawBackground = ImageFileFormat::loadFrom (BinaryData::space1_png, (size_t) BinaryData::space1_png);

This one, even after shutting down VS, adding JPG as binary via Projucer, saving, and relaunching VSl

rawBackground = ImageFileFormat::loadFrom (BinaryData::space1_jpg, (size_t) BinaryData::space1_jpg);

Causes app to immediately error with;
error

The second parameter should be the int from the BinaryData BinaryData::space1_jpgSize instead of the sizeof. The reader needs to know the length of BinaryData::space1_jpg, and since it is binary and not a null terminated string, the system cannot deduce that value.

Also a simplification is to use the ImageCache:

rawBackground = ImageCache::getFromMemory (BinaryData::space1_png, BinaryData::space1_pngSize);

The benefit is, it will hold a shared copy of the image, so it doesn’t need to read the data again. That also saves the need for a member variable, you can simply call getFromMemory in the paint() routine without any slowdown.

1 Like

Ok thank you very much I’ll that immediately.

Btw I got 10 images cycling through, changing at every completed game level, and when I get the next image, I stretch it to app size, which can be changed from within the app, and I do not want to use a switch or If in paint for 10 tests. So although your hint may work fine for just one background, I rather save a few CPU cycles in paint and not to do any condition tests.

Yep worked like a charm, tyvm!

rawBackground = ImageCache::getFromMemory (BinaryData::space1_jpg, BinaryData::space1_jpgSize);