From 9dd0f952e09471f15f59a98c1ae650d99499cd77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Blot?= Date: Tue, 15 Aug 2017 20:30:30 +0200 Subject: [PATCH] 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 --- src/client/clientlauncher.cpp | 27 ++++--- src/client/clientlauncher.h | 2 +- src/client/inputhandler.cpp | 3 +- src/client/inputhandler.h | 13 ++-- src/client/joystick_controller.cpp | 20 +++--- src/client/joystick_controller.h | 6 +- src/client/renderingengine.cpp | 3 +- src/client/tile.cpp | 112 +++++++++++++---------------- src/client/tile.h | 31 ++++---- 9 files changed, 104 insertions(+), 113 deletions(-) diff --git a/src/client/clientlauncher.cpp b/src/client/clientlauncher.cpp index a5c1784aa..644adbbf9 100644 --- a/src/client/clientlauncher.cpp +++ b/src/client/clientlauncher.cpp @@ -132,8 +132,7 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args) g_menuclouds->setHeight(100.0f); g_menuclouds->update(v3f(0, 0, 0), video::SColor(255, 200, 200, 255)); scene::ICameraSceneNode* camera; - camera = g_menucloudsmgr->addCameraSceneNode(0, - v3f(0, 0, 0), v3f(0, 60, 100)); + camera = g_menucloudsmgr->addCameraSceneNode(NULL, v3f(0, 0, 0), v3f(0, 60, 100)); camera->setFarValue(10000); /* @@ -192,13 +191,13 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args) if (!game_has_run) { if (skip_main_menu) break; - else - continue; + + continue; } // Break out of menu-game loop to shut down cleanly if (!RenderingEngine::get_raw_device()->run() || *kill) { - if (g_settings_path != "") + if (!g_settings_path.empty()) g_settings->updateConfigFile(g_settings_path.c_str()); break; } @@ -285,7 +284,7 @@ void ClientLauncher::init_args(GameParams &game_params, const Settings &cmd_args * supplied on the command line */ address = g_settings->get("address"); - if (game_params.world_path != "" && !skip_main_menu) + if (!game_params.world_path.empty() && !skip_main_menu) address = ""; else if (cmd_args.exists("address")) address = cmd_args.get("address"); @@ -355,11 +354,11 @@ bool ClientLauncher::launch_game(std::string &error_message, menudata.password = cmd_args.get("password"); // If a world was commanded, append and select it - if (game_params.world_path != "") { + if (!game_params.world_path.empty()) { worldspec.gameid = getWorldGameId(game_params.world_path, true); worldspec.name = _("[--world parameter]"); - if (worldspec.gameid == "") { // Create new + if (worldspec.gameid.empty()) { // Create new worldspec.gameid = g_settings->get("default_game"); worldspec.name += " [new]"; } @@ -400,7 +399,7 @@ bool ClientLauncher::launch_game(std::string &error_message, return false; } - if (menudata.name == "" && !simple_singleplayer_mode) { + if (menudata.name.empty() && !simple_singleplayer_mode) { error_message = gettext("Please choose a name!"); errorstream << error_message << std::endl; return false; @@ -416,14 +415,14 @@ bool ClientLauncher::launch_game(std::string &error_message, // If using simple singleplayer mode, override if (simple_singleplayer_mode) { - assert(skip_main_menu == false); + assert(!skip_main_menu); current_playername = "singleplayer"; current_password = ""; current_address = ""; current_port = myrand_range(49152, 65535); } else { g_settings->set("name", playername); - if (address != "") { + if (!address.empty()) { ServerListSpec server; server["name"] = menudata.servername; server["address"] = menudata.address; @@ -436,8 +435,8 @@ bool ClientLauncher::launch_game(std::string &error_message, infostream << "Selected world: " << worldspec.name << " [" << worldspec.path << "]" << std::endl; - if (current_address == "") { // If local game - if (worldspec.path == "") { + if (current_address.empty()) { // If local game + if (worldspec.path.empty()) { error_message = gettext("No world selected and no address " "provided. Nothing to do."); errorstream << error_message << std::endl; @@ -488,7 +487,7 @@ void ClientLauncher::main_menu(MainMenuData *menudata) video::IVideoDriver *driver = RenderingEngine::get_video_driver(); infostream << "Waiting for other menus" << std::endl; - while (RenderingEngine::get_raw_device()->run() && *kill == false) { + while (RenderingEngine::get_raw_device()->run() && !*kill) { if (!isMenuActive()) break; driver->beginScene(true, true, video::SColor(255, 128, 128, 128)); diff --git a/src/client/clientlauncher.h b/src/client/clientlauncher.h index dd5e6b8d6..6e3741182 100644 --- a/src/client/clientlauncher.h +++ b/src/client/clientlauncher.h @@ -29,7 +29,7 @@ class RenderingEngine; class ClientLauncher { public: - ClientLauncher() {} + ClientLauncher() = default; ~ClientLauncher(); diff --git a/src/client/inputhandler.cpp b/src/client/inputhandler.cpp index 9c7a94c4e..b176f3ad7 100644 --- a/src/client/inputhandler.cpp +++ b/src/client/inputhandler.cpp @@ -102,8 +102,7 @@ bool MyEventReceiver::OnEvent(const SEvent &event) }; assert(event.LogEvent.Level < ARRLEN(irr_loglev_conv)); g_logger.log(irr_loglev_conv[event.LogEvent.Level], - std::string("Irrlicht: ") + - (const char *)event.LogEvent.Text); + std::string("Irrlicht: ") + event.LogEvent.Text); return true; } /* always return false in order to continue processing events */ diff --git a/src/client/inputhandler.h b/src/client/inputhandler.h index 822e27863..bc8251887 100644 --- a/src/client/inputhandler.h +++ b/src/client/inputhandler.h @@ -180,8 +180,9 @@ private: class InputHandler { public: - InputHandler() {} - virtual ~InputHandler() {} + InputHandler() = default; + + virtual ~InputHandler() = default; virtual bool isKeyDown(const KeyPress &keyCode) = 0; virtual bool wasKeyDown(const KeyPress &keyCode) = 0; @@ -243,9 +244,10 @@ public: return RenderingEngine::get_raw_device() ->getCursorControl() ->getPosition(); - } else { - return m_mousepos; } + + return m_mousepos; + } virtual void setMousePos(s32 x, s32 y) { @@ -287,7 +289,8 @@ private: class RandomInputHandler : public InputHandler { public: - RandomInputHandler() {} + RandomInputHandler() = default; + virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; } virtual bool wasKeyDown(const KeyPress &keyCode) { return false; } virtual v2s32 getMousePos() { return mousepos; } diff --git a/src/client/joystick_controller.cpp b/src/client/joystick_controller.cpp index 5a3d35d7c..95bd77bc4 100644 --- a/src/client/joystick_controller.cpp +++ b/src/client/joystick_controller.cpp @@ -41,8 +41,8 @@ bool JoystickAxisCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const } // spares many characters -#define JLO_B_PB(A, B, C) jlo.button_keys.push_back(JoystickButtonCmb(A, B, C)) -#define JLO_A_PB(A, B, C, D) jlo.axis_keys.push_back(JoystickAxisCmb(A, B, C, D)) +#define JLO_B_PB(A, B, C) jlo.button_keys.emplace_back(A, B, C) +#define JLO_A_PB(A, B, C, D) jlo.axis_keys.emplace_back(A, B, C, D) JoystickLayout create_default_layout() { @@ -157,8 +157,8 @@ JoystickLayout create_xbox_layout() JoystickController::JoystickController() : doubling_dtime(g_settings->getFloat("repeat_joystick_button_time")) { - for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) { - m_past_pressed_time[i] = 0; + for (float &i : m_past_pressed_time) { + i = 0; } clear(); } @@ -203,15 +203,15 @@ bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev) // First generate a list of keys pressed - for (size_t i = 0; i < m_layout.button_keys.size(); i++) { - if (m_layout.button_keys[i].isTriggered(ev)) { - keys_pressed.set(m_layout.button_keys[i].key); + for (const auto &button_key : m_layout.button_keys) { + if (button_key.isTriggered(ev)) { + keys_pressed.set(button_key.key); } } - for (size_t i = 0; i < m_layout.axis_keys.size(); i++) { - if (m_layout.axis_keys[i].isTriggered(ev)) { - keys_pressed.set(m_layout.axis_keys[i].key); + for (const auto &axis_key : m_layout.axis_keys) { + if (axis_key.isTriggered(ev)) { + keys_pressed.set(axis_key.key); } } diff --git a/src/client/joystick_controller.h b/src/client/joystick_controller.h index baec4e412..4a2cdf555 100644 --- a/src/client/joystick_controller.h +++ b/src/client/joystick_controller.h @@ -52,7 +52,8 @@ struct JoystickCombination { struct JoystickButtonCmb : public JoystickCombination { - JoystickButtonCmb() {} + JoystickButtonCmb() = default; + JoystickButtonCmb(GameKeyType key, u32 filter_mask, u32 compare_mask) : filter_mask(filter_mask), compare_mask(compare_mask) @@ -68,7 +69,8 @@ struct JoystickButtonCmb : public JoystickCombination { struct JoystickAxisCmb : public JoystickCombination { - JoystickAxisCmb() {} + JoystickAxisCmb() = default; + JoystickAxisCmb(GameKeyType key, u16 axis_to_compare, int direction, s16 thresh) : axis_to_compare(axis_to_compare), direction(direction), diff --git a/src/client/renderingengine.cpp b/src/client/renderingengine.cpp index ce8f643b9..21af8948a 100644 --- a/src/client/renderingengine.cpp +++ b/src/client/renderingengine.cpp @@ -419,8 +419,7 @@ std::vector> RenderingEngine::getSupportedVideoModes() for (s32 i = 0; i != num_modes; i++) { core::dimension2d mode_res = modelist->getVideoModeResolution(i); u32 mode_depth = (u32)modelist->getVideoModeDepth(i); - mlist.push_back(core::vector3d( - mode_res.Width, mode_res.Height, mode_depth)); + mlist.emplace_back(mode_res.Width, mode_res.Height, mode_depth); } nulldevice->drop(); diff --git a/src/client/tile.cpp b/src/client/tile.cpp index 1bcbdc6f9..150e621d9 100644 --- a/src/client/tile.cpp +++ b/src/client/tile.cpp @@ -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::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 \"" <getTextureCount(); - for (std::vector::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::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"<::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; iname); + 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 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 imageTransformDimension(u32 transform, core::dimension2d< { if (transform % 2 == 0) return dim; - else - return core::dimension2d(dim.Height, dim.Width); + + return core::dimension2d(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(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(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); + } diff --git a/src/client/tile.h b/src/client/tile.h index cfdcf30bd..cd3c3880e 100644 --- a/src/client/tile.h +++ b/src/client/tile.h @@ -90,8 +90,10 @@ struct TextureFromMeshParams class ISimpleTextureSource { public: - ISimpleTextureSource(){} - virtual ~ISimpleTextureSource(){} + ISimpleTextureSource() = default; + + virtual ~ISimpleTextureSource() = default; + virtual video::ITexture* getTexture( const std::string &name, u32 *id = nullptr) = 0; }; @@ -99,8 +101,10 @@ public: class ITextureSource : public ISimpleTextureSource { public: - ITextureSource(){} - virtual ~ITextureSource(){} + ITextureSource() = default; + + virtual ~ITextureSource() = default; + virtual u32 getTextureId(const std::string &name)=0; virtual std::string getTextureName(u32 id)=0; virtual video::ITexture* getTexture(u32 id)=0; @@ -126,8 +130,10 @@ public: class IWritableTextureSource : public ITextureSource { public: - IWritableTextureSource(){} - virtual ~IWritableTextureSource(){} + IWritableTextureSource() = default; + + virtual ~IWritableTextureSource() = default; + virtual u32 getTextureId(const std::string &name)=0; virtual std::string getTextureName(u32 id)=0; virtual video::ITexture* getTexture(u32 id)=0; @@ -170,7 +176,7 @@ enum MaterialType{ // Ignored if MATERIAL_FLAG_CRACK is not set. #define MATERIAL_FLAG_CRACK_OVERLAY 0x04 #define MATERIAL_FLAG_ANIMATION 0x08 -#define MATERIAL_FLAG_HIGHLIGHTED 0x10 +//#define MATERIAL_FLAG_HIGHLIGHTED 0x10 #define MATERIAL_FLAG_TILEABLE_HORIZONTAL 0x20 #define MATERIAL_FLAG_TILEABLE_VERTICAL 0x40 @@ -180,7 +186,8 @@ enum MaterialType{ */ struct FrameSpec { - FrameSpec() {} + FrameSpec() = default; + u32 texture_id = 0; video::ITexture *texture = nullptr; video::ITexture *normal_texture = nullptr; @@ -192,7 +199,7 @@ struct FrameSpec //! Defines a layer of a tile. struct TileLayer { - TileLayer() {} + TileLayer() = default; /*! * Two layers are equal if they can be merged. @@ -232,8 +239,7 @@ struct TileLayer material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; break; } - material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) - ? true : false; + material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0; if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) { material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; } @@ -244,8 +250,7 @@ struct TileLayer void applyMaterialOptionsWithShaders(video::SMaterial &material) const { - material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) - ? true : false; + material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0; if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) { material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; material.TextureLayer[1].TextureWrapU = video::ETC_CLAMP_TO_EDGE;