Skip to content

Commit

Permalink
Fixed getTextureData() when failed to load texture
Browse files Browse the repository at this point in the history
getTextureData() created an image based on width and height values that are undefined if image failed to load.

Replaced with default image and image is constructed only if successfully loaded.
  • Loading branch information
marcusklang committed Jan 7, 2020
1 parent e62983a commit fa66f28
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/core/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ getTextureData(std::string const& filename, u32& width, u32& height, bool flip)
auto const path = config::resources_path(filename);
auto const channels_nb = 4u;
unsigned char* image_data = stbi_load(path.c_str(), reinterpret_cast<int*>(&width), reinterpret_cast<int*>(&height), nullptr, channels_nb);
std::vector<unsigned char> image(width * height * channels_nb);
if (image_data == nullptr) {
LogWarning("Couldn't load or decode image file %s", path.c_str());
return image;

// Provide a small empty image instead in case of failure.
width = 16;
height = 16;
return std::vector<unsigned char>(width * height * channels_nb);
}

std::vector<unsigned char> image(width * height * channels_nb);
if (!flip) {
std::memcpy(image.data(), image_data, image.size());
stbi_image_free(image_data);
Expand Down

0 comments on commit fa66f28

Please sign in to comment.