Skip to content

Commit

Permalink
Refactor, added support for loading images from parent folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Jorquera committed Mar 13, 2024
1 parent 316f4d3 commit 22b39d5
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@
#define STBI_FAILURE_USERMSG
#include "stb_image.h"

#include <iostream>

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;
Expand Down

0 comments on commit 22b39d5

Please sign in to comment.