From 22b39d50d17fa7751e2c168480ccedf8b6905bfb Mon Sep 17 00:00:00 2001 From: Pedro Jorquera Date: Wed, 13 Mar 2024 22:47:42 +0100 Subject: [PATCH] Refactor, added support for loading images from parent folders --- src/image.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/image.cpp b/src/image.cpp index f43aeea..1c4b2d0 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -4,19 +4,31 @@ #define STBI_FAILURE_USERMSG #include "stb_image.h" +#include + +using namespace std; + inline int clamp(int x, int low, int high) { return (x < low) ? low : ((x < high) ? x : high - 1); } -Image::Image(const std::string& filename) { - load(filename); +Image::Image(const string& filename) { + if(load(filename)) return; + if(load("resources/" + filename)) return; + if(load("../resources/" + filename)) return; + if(load("../../resources/" + filename)) return; + if(load("../../../resources/" + filename)) return; + if(load("../../../../resources/" + filename)) return; + if(load("../../../../../resources/" + filename)) return; + if(load("../../../../../../resources/" + filename)) return; + cerr << "ERROR: Could not load image file '" << filename << "'" << endl; } Image::~Image() { STBI_FREE(_data); } -bool Image::load(const std::string& filename) { +bool Image::load(const string& filename) { auto n = _bytesPerPixel; _data = stbi_load(filename.c_str(), &_width, &_height, &n, _bytesPerPixel); _bytesPerScanline = _width * _bytesPerPixel;