1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Modernize client code (#6250)

* Various code style fixes
* Use range based for loops
* Use empty instead of empty objects
* Use C++11 default keyword for trivial constructors and destructors
* Drop some useless casts
* Use emplace_back instead of push_back to improve performance of some vectors push
This commit is contained in:
Loïc Blot 2017-08-15 20:30:30 +02:00 committed by GitHub
parent 64c7a689ad
commit 9dd0f952e0
9 changed files with 104 additions and 113 deletions

View file

@ -23,19 +23,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/string.h"
#include "util/container.h"
#include "util/thread.h"
#include "util/numeric.h"
#include "irrlichttypes_extrabloated.h"
#include "debug.h"
#include "filesys.h"
#include "settings.h"
#include "mesh.h"
#include "log.h"
#include "gamedef.h"
#include "util/strfnd.h"
#include "util/string.h" // for parseColorString()
#include "imagefilters.h"
#include "guiscalingfilter.h"
#include "nodedef.h"
#include "renderingengine.h"
@ -96,13 +90,13 @@ std::string getImagePath(std::string path)
NULL
};
// If there is no extension, add one
if (removeStringEnd(path, extensions) == "")
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 == false)
if (!r)
return "";
if (fs::PathExists(path))
return path;
@ -124,7 +118,7 @@ std::string getImagePath(std::string path)
*/
std::string getTexturePath(const std::string &filename)
{
std::string fullpath = "";
std::string fullpath;
/*
Check from cache
*/
@ -136,7 +130,7 @@ std::string getTexturePath(const std::string &filename)
Check from texture_path
*/
const std::string &texture_path = g_settings->get("texture_path");
if (texture_path != "") {
if (!texture_path.empty()) {
std::string testpath = texture_path + DIR_DELIM + filename;
// Check all filename extensions. Returns "" if not found.
fullpath = getImagePath(testpath);
@ -145,7 +139,7 @@ std::string getTexturePath(const std::string &filename)
/*
Check from default data directory
*/
if (fullpath == "")
if (fullpath.empty())
{
std::string base_path = porting::path_share + DIR_DELIM + "textures"
+ DIR_DELIM + "base" + DIR_DELIM + "pack";
@ -193,9 +187,8 @@ class SourceImageCache
{
public:
~SourceImageCache() {
for (std::map<std::string, video::IImage*>::iterator iter = m_images.begin();
iter != m_images.end(); ++iter) {
iter->second->drop();
for (auto &m_image : m_images) {
m_image.second->drop();
}
m_images.clear();
}
@ -216,7 +209,7 @@ public:
// Try to use local texture instead if asked to
if (prefer_local){
std::string path = getTexturePath(name);
if (path != ""){
if (!path.empty()) {
video::IImage *img2 = RenderingEngine::get_video_driver()->
createImageFromFile(path.c_str());
if (img2){
@ -249,7 +242,7 @@ public:
}
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
std::string path = getTexturePath(name);
if (path == ""){
if (path.empty()) {
infostream<<"SourceImageCache::getOrLoad(): No path found for \""
<<name<<"\""<<std::endl;
return NULL;
@ -351,7 +344,7 @@ public:
if (cache_found)
return is_known;
// Not found in cache; find out if a local file exists
is_known = (getTexturePath(name) != "");
is_known = (!getTexturePath(name).empty());
m_source_image_existence.set(name, is_known);
return is_known;
}
@ -438,7 +431,7 @@ TextureSource::TextureSource()
m_main_thread = std::this_thread::get_id();
// Add a NULL TextureInfo as the first index, named ""
m_textureinfo_cache.push_back(TextureInfo(""));
m_textureinfo_cache.emplace_back("");
m_name_to_id[""] = 0;
// Cache some settings
@ -455,21 +448,14 @@ TextureSource::~TextureSource()
unsigned int textures_before = driver->getTextureCount();
for (std::vector<TextureInfo>::iterator iter =
m_textureinfo_cache.begin();
iter != m_textureinfo_cache.end(); ++iter)
{
for (const auto &iter : m_textureinfo_cache) {
//cleanup texture
if (iter->texture)
driver->removeTexture(iter->texture);
if (iter.texture)
driver->removeTexture(iter.texture);
}
m_textureinfo_cache.clear();
for (std::vector<video::ITexture*>::iterator iter =
m_texture_trash.begin(); iter != m_texture_trash.end();
++iter) {
video::ITexture *t = *iter;
for (auto t : m_texture_trash) {
//cleanup trashed texture
driver->removeTexture(t);
}
@ -586,7 +572,7 @@ u32 TextureSource::generateTexture(const std::string &name)
//infostream << "generateTexture(): name=\"" << name << "\"" << std::endl;
// Empty name means texture 0
if (name == "") {
if (name.empty()) {
infostream<<"generateTexture(): name is empty"<<std::endl;
return 0;
}
@ -690,7 +676,7 @@ Palette* TextureSource::getPalette(const std::string &name)
if (name == "")
return NULL;
std::unordered_map<std::string, Palette>::iterator it = m_palettes.find(name);
auto it = m_palettes.find(name);
if (it == m_palettes.end()) {
// Create palette
video::IImage *img = generateImage(name);
@ -728,7 +714,7 @@ Palette* TextureSource::getPalette(const std::string &name)
img->drop();
// Fill in remaining elements
while (new_palette.size() < 256)
new_palette.push_back(video::SColor(0xFFFFFFFF));
new_palette.emplace_back(0xFFFFFFFF);
m_palettes[name] = new_palette;
it = m_palettes.find(name);
}
@ -775,22 +761,21 @@ void TextureSource::rebuildImagesAndTextures()
sanity_check(driver);
// Recreate textures
for (u32 i=0; i<m_textureinfo_cache.size(); i++){
TextureInfo *ti = &m_textureinfo_cache[i];
video::IImage *img = generateImage(ti->name);
for (TextureInfo &ti : m_textureinfo_cache) {
video::IImage *img = generateImage(ti.name);
#ifdef __ANDROID__
img = Align2Npot2(img, driver);
#endif
// Create texture from resulting image
video::ITexture *t = NULL;
if (img) {
t = driver->addTexture(ti->name.c_str(), img);
guiScalingCache(io::path(ti->name.c_str()), driver, img);
t = driver->addTexture(ti.name.c_str(), img);
guiScalingCache(io::path(ti.name.c_str()), driver, img);
img->drop();
}
video::ITexture *t_old = ti->texture;
video::ITexture *t_old = ti.texture;
// Replace texture
ti->texture = t;
ti.texture = t;
if (t_old)
m_texture_trash.push_back(t_old);
@ -1185,14 +1170,13 @@ bool TextureSource::generateImagePart(std::string part_of_name,
sanity_check(driver);
// Stuff starting with [ are special commands
if (part_of_name.size() == 0 || part_of_name[0] != '[')
{
if (part_of_name.empty() || part_of_name[0] != '[') {
video::IImage *image = m_sourcecache.getOrLoad(part_of_name);
#ifdef __ANDROID__
image = Align2Npot2(image, driver);
#endif
if (image == NULL) {
if (part_of_name != "") {
if (!part_of_name.empty()) {
// Do not create normalmap dummies
if (part_of_name.find("_normal.png") != std::string::npos) {
@ -1343,7 +1327,7 @@ bool TextureSource::generateImagePart(std::string part_of_name,
baseimg = driver->createImage(video::ECF_A8R8G8B8, dim);
baseimg->fill(video::SColor(0,0,0,0));
}
while (sf.at_end() == false) {
while (!sf.at_end()) {
u32 x = stoi(sf.next(","));
u32 y = stoi(sf.next("="));
std::string filename = unescape_string(sf.next_esc(":", escape), escape);
@ -1896,13 +1880,13 @@ bool TextureSource::generateImagePart(std::string part_of_name,
std::string mode = sf.next("");
u32 mask = 0;
if (mode.find("a") != std::string::npos)
if (mode.find('a') != std::string::npos)
mask |= 0xff000000UL;
if (mode.find("r") != std::string::npos)
if (mode.find('r') != std::string::npos)
mask |= 0x00ff0000UL;
if (mode.find("g") != std::string::npos)
if (mode.find('g') != std::string::npos)
mask |= 0x0000ff00UL;
if (mode.find("b") != std::string::npos)
if (mode.find('b') != std::string::npos)
mask |= 0x000000ffUL;
core::dimension2d<u32> dim = baseimg->getDimension();
@ -2240,9 +2224,8 @@ u32 parseImageTransform(const std::string& s)
pos++;
break;
}
else if (!(name_i.empty()) &&
lowercase(s.substr(pos, name_i.size())) == name_i)
{
if (!(name_i.empty()) && lowercase(s.substr(pos, name_i.size())) == name_i) {
transform = i;
pos += name_i.size();
break;
@ -2269,8 +2252,8 @@ core::dimension2d<u32> imageTransformDimension(u32 transform, core::dimension2d<
{
if (transform % 2 == 0)
return dim;
else
return core::dimension2d<u32>(dim.Height, dim.Width);
return core::dimension2d<u32>(dim.Height, dim.Width);
}
void imageTransform(u32 transform, video::IImage *src, video::IImage *dst)
@ -2325,12 +2308,12 @@ video::ITexture* TextureSource::getNormalTexture(const std::string &name)
std::string fname_base = name;
static const char *normal_ext = "_normal.png";
static const u32 normal_ext_size = strlen(normal_ext);
size_t pos = fname_base.find(".");
size_t pos = fname_base.find('.');
std::string fname_normal = fname_base.substr(0, pos) + normal_ext;
if (isKnownSourceImage(fname_normal)) {
// look for image extension and replace it
size_t i = 0;
while ((i = fname_base.find(".", i)) != std::string::npos) {
while ((i = fname_base.find('.', i)) != std::string::npos) {
fname_base.replace(i, 4, normal_ext);
i += normal_ext_size;
}
@ -2384,15 +2367,16 @@ video::ITexture *TextureSource::getShaderFlagsTexture(bool normalmap_present)
if (isKnownSourceImage(tname)) {
return getTexture(tname);
} else {
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
video::IImage *flags_image = driver->createImage(
video::ECF_A8R8G8B8, core::dimension2d<u32>(1, 1));
sanity_check(flags_image != NULL);
video::SColor c(255, normalmap_present ? 255 : 0, 0, 0);
flags_image->setPixel(0, 0, c);
insertSourceImage(tname, flags_image);
flags_image->drop();
return getTexture(tname);
}
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
video::IImage *flags_image = driver->createImage(
video::ECF_A8R8G8B8, core::dimension2d<u32>(1, 1));
sanity_check(flags_image != NULL);
video::SColor c(255, normalmap_present ? 255 : 0, 0, 0);
flags_image->setPixel(0, 0, c);
insertSourceImage(tname, flags_image);
flags_image->drop();
return getTexture(tname);
}