Skip to content

Commit

Permalink
Refactor tile.cpp/h parts except texturesource.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
cx384 authored and SmallJoker committed Mar 3, 2024
1 parent aaf7702 commit 879f7e9
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 130 deletions.
10 changes: 4 additions & 6 deletions src/client/imagefilters.cpp
Expand Up @@ -273,16 +273,14 @@ void imageScaleNNAA(video::IImage *src, const core::rect<s32> &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))
Expand All @@ -304,7 +302,7 @@ video::IImage *Align2Npot2(video::IImage *image,
driver->createImage(video::ECF_A8R8G8B8,
core::dimension2d<u32>(width, height));

if (targetimage != NULL)
if (targetimage != nullptr)
image->copyToScaling(targetimage);
image->drop();
return targetimage;
Expand Down
118 changes: 39 additions & 79 deletions src/client/texturepaths.cpp
Expand Up @@ -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 <vector>

/*
A cache from texture name to texture path
*/
// A cache from texture name to texture path
static MutexedMap<std::string, std::string> 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)
{
Expand All @@ -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;
}
Expand All @@ -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<std::string> getTextureDirs()
{
return fs::GetRecursiveDirs(g_settings->get("texture_path"));
Expand Down
2 changes: 1 addition & 1 deletion src/client/texturepaths.h
Expand Up @@ -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.
Expand Down
67 changes: 66 additions & 1 deletion src/client/tile.cpp
@@ -1 +1,66 @@
// Empty after splitting tile.h
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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;
}
}
45 changes: 2 additions & 43 deletions src/client/tile.h
Expand Up @@ -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
{
Expand Down

0 comments on commit 879f7e9

Please sign in to comment.