Skip to content

Commit

Permalink
Refactor texturepaths.cpp and SourceImageCache
Browse files Browse the repository at this point in the history
  • Loading branch information
cx384 committed Mar 8, 2024
1 parent f6aba81 commit 5cf6d90
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 131 deletions.
15 changes: 8 additions & 7 deletions src/client/imagegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ void SourceImageCache::insert(const std::string &name, video::IImage *img, bool
toadd->grab();
m_images[name] = toadd;
}

video::IImage* SourceImageCache::get(const std::string &name)
{
std::map<std::string, video::IImage*>::iterator n;
n = m_images.find(name);
if (n != m_images.end())
return n->second;
return NULL;
return nullptr;
}

// Primarily fetches from cache, secondarily tries to read from filesystem
Expand All @@ -96,12 +97,12 @@ video::IImage* SourceImageCache::getOrLoad(const std::string &name)
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
std::string path = getTexturePath(name);
if (path.empty()) {
infostream<<"SourceImageCache::getOrLoad(): No path found for \""
<<name<<"\""<<std::endl;
return NULL;
infostream << "SourceImageCache::getOrLoad(): No path found for \""
<< name << "\"" << std::endl;
return nullptr;
}
infostream<<"SourceImageCache::getOrLoad(): Loading path \""<<path
<<"\""<<std::endl;
infostream << "SourceImageCache::getOrLoad(): Loading path \"" << path
<< "\"" << std::endl;
video::IImage *img = driver->createImageFromFile(path.c_str());

if (img){
Expand Down Expand Up @@ -1910,6 +1911,6 @@ video::SColor ImageGen::getImageAverageColor(const video::IImage &image)
return c;
}

void ImageGen::insertImage(const std::string &name, video::IImage *img, bool prefer_local) {
void ImageGen::insertSourceImage(const std::string &name, video::IImage *img, bool prefer_local) {
m_sourcecache.insert(name, img, prefer_local);
}
36 changes: 18 additions & 18 deletions src/client/imagegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string>
#include "settings.h"

// This file is only for internal generation/modification of images.
// Use texturesource.h instead to handle textures.
// This file is only used for internal generation of images.
// Use texturesource.h to handle textures.

// A cache used for storing source images.
// (A "source image" is an unmodified image directly taken from the filesystem.)
// Does not contain modified images.
class SourceImageCache {
public:
~SourceImageCache();
Expand All @@ -35,42 +37,40 @@ class SourceImageCache {

video::IImage* get(const std::string &name);

// Primarily fetches from cache, secondarily tries to read from filesystem
// Primarily fetches from cache, secondarily tries to read from filesystem.
video::IImage *getOrLoad(const std::string &name);
private:
std::map<std::string, video::IImage*> m_images;
};

/*
* Generates and caches images.
* The image name defines the image by filename and texture modifiers.
*/
// Generates images using texture modifiers, and caches source images.
struct ImageGen {
/*! Generates an image from a full string like
* "stone.png^mineral_coal.png^[crack:1:0".
* The returned Image should be dropped.
* source_image_names is important to determine when to flush the image from a cache (dynamic media)
*/
// Generates an image from a full string like "stone.png^mineral_coal.png^[crack:1:0".
// The returned Image should be dropped.
// source_image_names stores all source images which have been used to generate the image.
// It is important to determine when to flush the image from a cache (dynamic media).
video::IImage* generateImage(std::string_view name, std::set<std::string> &source_image_names);

// To add self made images.
void insertImage(const std::string &name, video::IImage *img, bool prefer_local);
// Insert a source image into the cache without touching the filesystem.
void insertSourceImage(const std::string &name, video::IImage *img, bool prefer_local);

// TODO should probably be moved elsewhere
static video::SColor getImageAverageColor(const video::IImage &image);

private:

// Generate image based on a string like "stone.png" or "[crack:1:0".
// if baseimg is NULL, it is created. Otherwise stuff is made on it.
// source_image_names is important to determine when to flush the image from a cache (dynamic media)
bool generateImagePart(std::string_view part_of_name, video::IImage *& baseimg, std::set<std::string> &source_image_names);
// If baseimg is NULL, it is created. Otherwise stuff is made on it.
// source_image_names is important to determine when to flush the image from a cache (dynamic media).
bool generateImagePart(std::string_view part_of_name, video::IImage *& baseimg,
std::set<std::string> &source_image_names);

// Cache of source images
SourceImageCache m_sourcecache;

// TODO refactor
// Cached settings needed for making textures from meshes
// Note: Since this is only done once, the game must be restarted
// for these settings to take effect.
bool m_setting_mipmap = g_settings->getBool("mip_map");
bool m_setting_trilinear_filter = g_settings->getBool("trilinear_filter");
bool m_setting_bilinear_filter = g_settings->getBool("bilinear_filter");
Expand Down

0 comments on commit 5cf6d90

Please sign in to comment.