The ImageResolver class is responsible for loading and caching images fr.

It stores loaded images in memory to avoid reloading them multiple times, improving performance.

namespace ecs {

    /**
     * An image loader that caches images
     */
    class ImageResolver {
      public:
        ImageResolver(const std::string &);

        ~ImageResolver();

        /**
         * @brief Get the Image content
         * @param path The path to the image
         * @param reload If the image should be reloaded (default: false)
         *
         * @return The image content
         */
        std::string getImage(const std::string &, bool = false);

      protected:
      private:
        std::string _basePath;
        std::unordered_map<std::string, std::string> _cache;
    };

} // namespace ecs

Public Members:

Private Members:

namespace ecs {

    ImageResolver::ImageResolver(const std::string &path) : _basePath(path) {}

    ImageResolver::~ImageResolver() {}

    std::string readFile(const std::string &path)
    {
        std::ifstream file;
        std::string s;

        file.open(path);

        if (file.is_open()) {
            std::string line;
            while (getline(file, line)) {
                s += line;
                s += "\\n";
            }
            file.close();
        }
        return s;
    }

    std::string ImageResolver::getImage(const std::string &path, bool reload)
    {
        if (reload) {
            _cache[path] = readFile(_basePath + path);
            return _cache.at(path);
        }
        try {
            return _cache.at(path);
        } catch (const std::out_of_range &e) {
            std::string s = readFile(_basePath + path);
            if (!s.empty())
                _cache[path] = s;
            return s;
        }
    }

} // namespace ecs

Key Methods:


Example Use Case: