diff --git a/src/ImageHolder.cpp b/src/ImageHolder.cpp index 499718b..11d07e5 100644 --- a/src/ImageHolder.cpp +++ b/src/ImageHolder.cpp @@ -31,6 +31,7 @@ ImageHolder::ImageHolder(ImageHolder&& other) { source_image_width = other.source_image_width; source_image_height = other.source_image_height; source_filename = other.source_filename; + do_lazy_load = other.do_lazy_load; other.pictureTex = nullptr; } @@ -61,9 +62,13 @@ void ImageHolder::LoadPictureFromFile(const std::string& filename, SDL_Renderer* source_image_width = pictureSurface->w; source_image_height = pictureSurface->h; -SDL_FreeSurface(pictureSurface); + SDL_FreeSurface(pictureSurface); } +void ImageHolder::LoadPictureFromFileLazy(const std::string& filename) { + source_filename = filename; + do_lazy_load = true; +} int ImageHolder::GetWidth() const { @@ -78,9 +83,15 @@ SDL_Texture* ImageHolder::GetTexture() const { return pictureTex; } -void ImageHolder::Draw(SDL_Renderer* target, int x, int y, int max_w, int max_h) const { +void ImageHolder::Draw(SDL_Renderer* target, int x, int y, int max_w, int max_h) { + if (do_lazy_load) { + printf("Lazy loading image: %s\n", source_filename.c_str()); + std::string filename = source_filename; + LoadPictureFromFile(filename, target); + do_lazy_load = false; + } if (pictureTex == nullptr) { - printf("ImageHolder::Draw() called with no image loaded. Filename: %s\n", source_filename.c_str()); + //printf("ImageHolder::Draw() called with no image loaded. Filename: %s\n", source_filename.c_str()); return; } int dest_w = max_w; diff --git a/src/ImageHolder.hpp b/src/ImageHolder.hpp index 214f4e5..9c8bc46 100644 --- a/src/ImageHolder.hpp +++ b/src/ImageHolder.hpp @@ -46,6 +46,7 @@ public: // Other member functions and variables void LoadPictureFromFile(const std::string& filename, SDL_Renderer* renderer); + void LoadPictureFromFileLazy(const std::string& filename); int GetWidth() const; int GetHeight() const; SDL_Texture* GetTexture() const; @@ -60,11 +61,12 @@ public: * @param max_w The maximum width to draw. If 0 then the image width is used. * @param max_h The maximum height to draw. If 0 then the image height is used. */ - void Draw(SDL_Renderer* target, int x, int y, int max_w = 0, int max_h = 0) const; + void Draw(SDL_Renderer* target, int x, int y, int max_w = 0, int max_h = 0); private: // Private member variables SDL_Texture* pictureTex = NULL; + bool do_lazy_load = false; int source_image_height = 1; int source_image_width = 1; std::string source_filename; diff --git a/src/ImageSelectState.cpp b/src/ImageSelectState.cpp index f207e5a..6deb43c 100644 --- a/src/ImageSelectState.cpp +++ b/src/ImageSelectState.cpp @@ -189,7 +189,7 @@ void ImageSelectState::Init() { setFontText(globalData.dataHolder, field, entry.path().filename().string().c_str()); imageNameFields.push_back(std::move(field)); imageHolders.emplace_back(); - imageHolders.back().LoadPictureFromFile(entry.path().string(), globalData.screen); + imageHolders.back().LoadPictureFromFileLazy(entry.path().string()); } } }