From 879f7e9f0373cd331eca188f7dbc80ad73fac58f Mon Sep 17 00:00:00 2001 From: cx384 Date: Tue, 27 Feb 2024 19:03:46 +0100 Subject: [PATCH] Refactor tile.cpp/h parts except texturesource.cpp --- src/client/imagefilters.cpp | 10 ++- src/client/texturepaths.cpp | 118 ++++++++++++------------------------ src/client/texturepaths.h | 2 +- src/client/tile.cpp | 67 +++++++++++++++++++- src/client/tile.h | 45 +------------- 5 files changed, 112 insertions(+), 130 deletions(-) diff --git a/src/client/imagefilters.cpp b/src/client/imagefilters.cpp index 7c4dc094f..db6523ad3 100644 --- a/src/client/imagefilters.cpp +++ b/src/client/imagefilters.cpp @@ -273,16 +273,14 @@ void imageScaleNNAA(video::IImage *src, const core::rect &srcrect, video::I } } -/** - * Check and align image to npot2 if required by hardware +/* Check and align image to npot2 if required by hardware * @param image image to check for npot2 alignment * @param driver driver to use for image operations * @return image or copy of image aligned to npot2 */ -video::IImage *Align2Npot2(video::IImage *image, - video::IVideoDriver *driver) +video::IImage *Align2Npot2(video::IImage *image, video::IVideoDriver *driver) { - if (image == NULL) + if (image == nullptr) return image; if (driver->queryFeature(video::EVDF_TEXTURE_NPOT)) @@ -304,7 +302,7 @@ video::IImage *Align2Npot2(video::IImage *image, driver->createImage(video::ECF_A8R8G8B8, core::dimension2d(width, height)); - if (targetimage != NULL) + if (targetimage != nullptr) image->copyToScaling(targetimage); image->drop(); return targetimage; diff --git a/src/client/texturepaths.cpp b/src/client/texturepaths.cpp index b4bb93ee7..ba28a243c 100644 --- a/src/client/texturepaths.cpp +++ b/src/client/texturepaths.cpp @@ -23,77 +23,51 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "settings.h" #include "filesys.h" #include "porting.h" +#include -/* - A cache from texture name to texture path -*/ +// A cache from texture name to texture path static MutexedMap g_texturename_to_path_cache; -/* - Replaces the filename extension. - eg: - std::string image = "a/image.png" - replace_ext(image, "jpg") - -> image = "a/image.jpg" - Returns true on success. -*/ -static bool replace_ext(std::string &path, const char *ext) +void clearTextureNameCache() { - if (ext == NULL) - return false; - // Find place of last dot, fail if \ or / found. - s32 last_dot_i = -1; - for (s32 i=path.size()-1; i>=0; i--) - { - if (path[i] == '.') - { - last_dot_i = i; - break; - } - - if (path[i] == '\\' || path[i] == '/') - break; - } - // If not found, return an empty string - if (last_dot_i == -1) - return false; - // Else make the new path - path = path.substr(0, last_dot_i+1) + ext; - return true; + g_texturename_to_path_cache.clear(); } -/* - Find out the full path of an image by trying different filename - extensions. - If failed, return "". -*/ -std::string getImagePath(std::string path) +// Find out the full path of an image by trying different filename extensions. +// If failed, return "". +std::string getImagePath(std::string_view path) { // A NULL-ended list of possible image extensions - const char *extensions[] = { "png", "jpg", "bmp", "tga", NULL }; - // If there is no extension, assume PNG - if (removeStringEnd(path, extensions).empty()) - path = path + ".png"; - // Check paths until something is found to exist - const char **ext = extensions; - do{ - bool r = replace_ext(path, *ext); - if (!r) - return ""; - if (fs::PathExists(path)) - return path; + // (In newer C++ versions a fixed size array and ranges::subrange could be used + // or just modernise removeStringEnd.) + static const char *extensions[] = {".png", ".jpg", ".bmp", ".tga", nullptr}; + + // Remove present extension + std::string_view stripped_path = removeStringEnd(path, extensions); + // If there is no known extension, assume it has been omitted. + if (stripped_path.empty()) + stripped_path = path; + + for (const char *ext : extensions) { + if (!ext) + break; + std::string extended_path(stripped_path); + extended_path.append(ext); + if (fs::PathExists(extended_path)) + return extended_path; } - while((++ext) != NULL); return ""; } -/* - Gets the path to a texture by first checking if the texture exists - in texture_path and if not, using the data path. - Checks all supported extensions by replacing the original extension. - If not found, returns "". - Utilizes a thread-safe cache. +/* Gets the path to a texture by first checking if the texture exists + * in texture_path and if not, using the data path. + * + * Checks all supported extensions by replacing the original extension. + * + * If not found, returns "". + * + * Utilizes a thread-safe cache. */ std::string getTexturePath(const std::string &filename, bool *is_base_pack) { @@ -103,35 +77,26 @@ std::string getTexturePath(const std::string &filename, bool *is_base_pack) // is_base_pack is only passed when initializing the textures the first time if (is_base_pack) *is_base_pack = false; - /* - Check from cache - */ + + // Check from cache bool incache = g_texturename_to_path_cache.get(filename, &fullpath); if (incache) return fullpath; - /* - Check from texture_path - */ + // Check from texture_path setting for (const auto &path : getTextureDirs()) { - std::string testpath = path + DIR_DELIM; - testpath.append(filename); // Check all filename extensions. Returns "" if not found. - fullpath = getImagePath(testpath); + fullpath = getImagePath(path + DIR_DELIM + filename); if (!fullpath.empty()) break; } - /* - Check from default data directory - */ - if (fullpath.empty()) - { + // Check from default data directory i.e. .../minetest/textures/base/pack + if (fullpath.empty()) { std::string base_path = porting::path_share + DIR_DELIM + "textures" + DIR_DELIM + "base" + DIR_DELIM + "pack"; - std::string testpath = base_path + DIR_DELIM + filename; // Check all filename extensions. Returns "" if not found. - fullpath = getImagePath(testpath); + fullpath = getImagePath(base_path + DIR_DELIM + filename); if (is_base_pack && !fullpath.empty()) *is_base_pack = true; } @@ -143,11 +108,6 @@ std::string getTexturePath(const std::string &filename, bool *is_base_pack) return fullpath; } -void clearTextureNameCache() -{ - g_texturename_to_path_cache.clear(); -} - std::vector getTextureDirs() { return fs::GetRecursiveDirs(g_settings->get("texture_path")); diff --git a/src/client/texturepaths.h b/src/client/texturepaths.h index 38ed0325d..dc5119188 100644 --- a/src/client/texturepaths.h +++ b/src/client/texturepaths.h @@ -27,7 +27,7 @@ void clearTextureNameCache(); // Find out the full path of an image by trying different filename extensions. // If failed, return "". -std::string getImagePath(std::string path); +std::string getImagePath(std::string_view path); /* Gets the path to a texture by first checking if the texture exists * in texture_path and if not, using the data path. diff --git a/src/client/tile.cpp b/src/client/tile.cpp index 0dc817b7b..1870547da 100644 --- a/src/client/tile.cpp +++ b/src/client/tile.cpp @@ -1 +1,66 @@ -// Empty after splitting tile.h +/* +Minetest +Copyright (C) 2010-2013 celeron55, Perttu Ahola + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "tile.h" + +// Sets everything else except the texture in the material +void TileLayer::applyMaterialOptions(video::SMaterial &material) const +{ + switch (material_type) { + case TILE_MATERIAL_OPAQUE: + case TILE_MATERIAL_LIQUID_OPAQUE: + case TILE_MATERIAL_WAVING_LIQUID_OPAQUE: + material.MaterialType = video::EMT_SOLID; + break; + case TILE_MATERIAL_BASIC: + case TILE_MATERIAL_WAVING_LEAVES: + case TILE_MATERIAL_WAVING_PLANTS: + case TILE_MATERIAL_WAVING_LIQUID_BASIC: + material.MaterialTypeParam = 0.5; + material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; + break; + case TILE_MATERIAL_ALPHA: + case TILE_MATERIAL_LIQUID_TRANSPARENT: + case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT: + material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; + break; + default: + break; + } + material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0; + if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) { + material.TextureLayers[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; + } + if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) { + material.TextureLayers[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; + } +} + +void TileLayer::applyMaterialOptionsWithShaders(video::SMaterial &material) const +{ + material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0; + if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) { + material.TextureLayers[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; + material.TextureLayers[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE; + } + if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) { + material.TextureLayers[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; + material.TextureLayers[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE; + } +} diff --git a/src/client/tile.h b/src/client/tile.h index 3460edc2c..d761eefdd 100644 --- a/src/client/tile.h +++ b/src/client/tile.h @@ -96,50 +96,9 @@ struct TileLayer } // Sets everything else except the texture in the material - void applyMaterialOptions(video::SMaterial &material) const - { - switch (material_type) { - case TILE_MATERIAL_OPAQUE: - case TILE_MATERIAL_LIQUID_OPAQUE: - case TILE_MATERIAL_WAVING_LIQUID_OPAQUE: - material.MaterialType = video::EMT_SOLID; - break; - case TILE_MATERIAL_BASIC: - case TILE_MATERIAL_WAVING_LEAVES: - case TILE_MATERIAL_WAVING_PLANTS: - case TILE_MATERIAL_WAVING_LIQUID_BASIC: - material.MaterialTypeParam = 0.5; - material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; - break; - case TILE_MATERIAL_ALPHA: - case TILE_MATERIAL_LIQUID_TRANSPARENT: - case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT: - material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; - break; - default: - break; - } - material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0; - if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) { - material.TextureLayers[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; - } - if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) { - material.TextureLayers[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; - } - } + void applyMaterialOptions(video::SMaterial &material) const; - void applyMaterialOptionsWithShaders(video::SMaterial &material) const - { - material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0; - if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) { - material.TextureLayers[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; - material.TextureLayers[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE; - } - if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) { - material.TextureLayers[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; - material.TextureLayers[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE; - } - } + void applyMaterialOptionsWithShaders(video::SMaterial &material) const; bool isTransparent() const {