From f7088f69ab7406ea9fefa853fa7ce11f914e88cf Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Fri, 7 Apr 2017 08:50:17 +0200 Subject: Clang-format: fix some header files and remove them from whitelist --- src/sound.h | 66 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 30 deletions(-) (limited to 'src/sound.h') diff --git a/src/sound.h b/src/sound.h index c21401e8b..ba2d629d2 100644 --- a/src/sound.h +++ b/src/sound.h @@ -20,9 +20,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef SOUND_HEADER #define SOUND_HEADER -#include "irrlichttypes_bloated.h" -#include #include +#include +#include "irrlichttypes_bloated.h" class OnDemandSoundFetcher { @@ -36,61 +36,68 @@ struct SimpleSoundSpec { std::string name; float gain; - SimpleSoundSpec(std::string name="", float gain=1.0): - name(name), - gain(gain) - {} - bool exists() {return name != "";} + SimpleSoundSpec(std::string name = "", float gain = 1.0) : name(name), gain(gain) + { + } + bool exists() { return name != ""; } // Serialization intentionally left out }; class ISoundManager { public: - virtual ~ISoundManager(){} - + virtual ~ISoundManager() {} + // Multiple sounds can be loaded per name; when played, the sound // should be chosen randomly from alternatives // Return value determines success/failure - virtual bool loadSoundFile(const std::string &name, - const std::string &filepath) = 0; - virtual bool loadSoundData(const std::string &name, - const std::string &filedata) = 0; + virtual bool loadSoundFile( + const std::string &name, const std::string &filepath) = 0; + virtual bool loadSoundData( + const std::string &name, const std::string &filedata) = 0; virtual void updateListener(v3f pos, v3f vel, v3f at, v3f up) = 0; virtual void setListenerGain(float gain) = 0; // playSound functions return -1 on failure, otherwise a handle to the // sound. If name=="", call should be ignored without error. - virtual int playSound(const std::string &name, bool loop, - float volume) = 0; - virtual int playSoundAt(const std::string &name, bool loop, - float volume, v3f pos) = 0; + virtual int playSound(const std::string &name, bool loop, float volume) = 0; + virtual int playSoundAt( + const std::string &name, bool loop, float volume, v3f pos) = 0; virtual void stopSound(int sound) = 0; virtual bool soundExists(int sound) = 0; virtual void updateSoundPosition(int sound, v3f pos) = 0; int playSound(const SimpleSoundSpec &spec, bool loop) - { return playSound(spec.name, loop, spec.gain); } + { + return playSound(spec.name, loop, spec.gain); + } int playSoundAt(const SimpleSoundSpec &spec, bool loop, v3f pos) - { return playSoundAt(spec.name, loop, spec.gain, pos); } + { + return playSoundAt(spec.name, loop, spec.gain, pos); + } }; -class DummySoundManager: public ISoundManager +class DummySoundManager : public ISoundManager { public: - virtual bool loadSoundFile(const std::string &name, - const std::string &filepath) {return true;} - virtual bool loadSoundData(const std::string &name, - const std::string &filedata) {return true;} + virtual bool loadSoundFile(const std::string &name, const std::string &filepath) + { + return true; + } + virtual bool loadSoundData(const std::string &name, const std::string &filedata) + { + return true; + } void updateListener(v3f pos, v3f vel, v3f at, v3f up) {} void setListenerGain(float gain) {} - int playSound(const std::string &name, bool loop, - float volume) {return 0;} - int playSoundAt(const std::string &name, bool loop, - float volume, v3f pos) {return 0;} + int playSound(const std::string &name, bool loop, float volume) { return 0; } + int playSoundAt(const std::string &name, bool loop, float volume, v3f pos) + { + return 0; + } void stopSound(int sound) {} - bool soundExists(int sound) {return false;} + bool soundExists(int sound) { return false; } void updateSoundPosition(int sound, v3f pos) {} }; @@ -98,4 +105,3 @@ public: extern DummySoundManager dummySoundManager; #endif - -- cgit v1.2.3 From f98bbe193e0093aca8d8957cec82fdbd28639915 Mon Sep 17 00:00:00 2001 From: Loïc Blot Date: Thu, 20 Apr 2017 00:12:52 +0200 Subject: Fix various copy instead of const ref reported by cppcheck (part 3) (#5616) * Also remove 2 non declared but defined functions * Make some functions around const ref changes const --- src/client/joystick_controller.cpp | 3 +- src/client/joystick_controller.h | 2 +- src/clientiface.h | 20 +++++------ src/genericobject.cpp | 23 +++++++------ src/genericobject.h | 9 +++-- src/guiEngine.cpp | 4 +-- src/guiEngine.h | 6 ++-- src/guiFormSpecMenu.cpp | 69 +++++++++++++++++++------------------- src/guiFormSpecMenu.h | 60 +++++++++++++++++---------------- src/guiTable.h | 9 +++-- src/httpfetch.cpp | 17 +++++----- src/httpfetch.h | 36 ++++++++++---------- src/map.cpp | 5 ++- src/map.h | 10 ++---- src/network/connection.h | 10 +++--- src/nodedef.h | 14 ++++---- src/pathfinder.cpp | 4 +-- src/porting.cpp | 2 +- src/script/cpp_api/s_async.cpp | 8 ++--- src/script/cpp_api/s_async.h | 15 +++++++-- src/script/scripting_mainmenu.cpp | 8 +++-- src/script/scripting_mainmenu.h | 4 +-- src/sound.h | 12 ++++--- src/util/string.h | 2 +- src/util/thread.h | 14 +++----- 25 files changed, 183 insertions(+), 183 deletions(-) (limited to 'src/sound.h') diff --git a/src/client/joystick_controller.cpp b/src/client/joystick_controller.cpp index 3e1442793..cb9d64b9f 100644 --- a/src/client/joystick_controller.cpp +++ b/src/client/joystick_controller.cpp @@ -185,7 +185,8 @@ void JoystickController::onJoystickConnect(const std::vector m_joystick_id = id; } -void JoystickController::setLayoutFromControllerName(std::string name) { +void JoystickController::setLayoutFromControllerName(const std::string &name) +{ if (lowercase(name).find("xbox") != std::string::npos) { m_layout = create_xbox_layout(); } else { diff --git a/src/client/joystick_controller.h b/src/client/joystick_controller.h index 867a0c3f2..2c0e7b90a 100644 --- a/src/client/joystick_controller.h +++ b/src/client/joystick_controller.h @@ -149,7 +149,7 @@ public: f32 doubling_dtime; private: - void setLayoutFromControllerName(std::string name); + void setLayoutFromControllerName(const std::string &name); JoystickLayout m_layout; diff --git a/src/clientiface.h b/src/clientiface.h index 403cd0420..11ebdaab6 100644 --- a/src/clientiface.h +++ b/src/clientiface.h @@ -324,14 +324,11 @@ public: */ std::set m_known_objects; - ClientState getState() - { return m_state; } + ClientState getState() const { return m_state; } - std::string getName() - { return m_name; } + std::string getName() const { return m_name; } - void setName(std::string name) - { m_name = name; } + void setName(const std::string &name) { m_name = name; } /* update internal client state */ void notifyEvent(ClientStateEvent event); @@ -350,7 +347,8 @@ public: u32 uptime(); /* set version information */ - void setVersionInfo(u8 major, u8 minor, u8 patch, std::string full) { + void setVersionInfo(u8 major, u8 minor, u8 patch, const std::string &full) + { m_version_major = major; m_version_minor = minor; m_version_patch = patch; @@ -358,10 +356,10 @@ public: } /* read version information */ - u8 getMajor() { return m_version_major; } - u8 getMinor() { return m_version_minor; } - u8 getPatch() { return m_version_patch; } - std::string getVersion() { return m_full_version; } + u8 getMajor() const { return m_version_major; } + u8 getMinor() const { return m_version_minor; } + u8 getPatch() const { return m_version_patch; } + std::string getVersion() const { return m_full_version; } private: // Version is stored in here after INIT before INIT2 u8 m_pending_serialization_version; diff --git a/src/genericobject.cpp b/src/genericobject.cpp index 07d2445b4..58f4b997e 100644 --- a/src/genericobject.cpp +++ b/src/genericobject.cpp @@ -68,7 +68,7 @@ std::string gob_cmd_update_position( std::string gob_cmd_set_texture_mod(const std::string &mod) { std::ostringstream os(std::ios::binary); - // command + // command writeU8(os, GENERIC_CMD_SET_TEXTURE_MOD); // parameters os<queueAsync(serialized_func, serialized_params); } diff --git a/src/guiEngine.h b/src/guiEngine.h index a81813d18..98e88574c 100644 --- a/src/guiEngine.h +++ b/src/guiEngine.h @@ -178,7 +178,8 @@ public: } /** pass async callback to scriptengine **/ - unsigned int queueAsync(std::string serialized_fct,std::string serialized_params); + unsigned int queueAsync(const std::string &serialized_fct, + const std::string &serialized_params); private: @@ -188,9 +189,6 @@ private: /** run main menu loop */ void run(); - /** handler to limit frame rate within main menu */ - void limitFrameRate(); - /** update size of topleftext element */ void updateTopLeftTextSize(); diff --git a/src/guiFormSpecMenu.cpp b/src/guiFormSpecMenu.cpp index 3fe3c5cc2..ab93aeca1 100644 --- a/src/guiFormSpecMenu.cpp +++ b/src/guiFormSpecMenu.cpp @@ -252,7 +252,7 @@ std::vector* GUIFormSpecMenu::getDropDownValues(const std::string & return NULL; } -void GUIFormSpecMenu::parseSize(parserData* data,std::string element) +void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element) { std::vector parts = split(element,','); @@ -278,7 +278,7 @@ void GUIFormSpecMenu::parseSize(parserData* data,std::string element) errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseContainer(parserData* data, std::string element) +void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element) { std::vector parts = split(element, ','); @@ -304,7 +304,7 @@ void GUIFormSpecMenu::parseContainerEnd(parserData* data) } } -void GUIFormSpecMenu::parseList(parserData* data,std::string element) +void GUIFormSpecMenu::parseList(parserData* data, const std::string &element) { if (m_client == 0) { warningstream<<"invalid use of 'list' with m_client==0"< parts = split(element,';'); @@ -450,7 +450,7 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element) errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element) +void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -509,7 +509,7 @@ void GUIFormSpecMenu::parseScrollBar(parserData* data, std::string element) errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseImage(parserData* data,std::string element) +void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -553,7 +553,7 @@ void GUIFormSpecMenu::parseImage(parserData* data,std::string element) errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element) +void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -583,8 +583,8 @@ void GUIFormSpecMenu::parseItemImage(parserData* data,std::string element) errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseButton(parserData* data,std::string element, - std::string type) +void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element, + const std::string &type) { std::vector parts = split(element,';'); @@ -638,7 +638,7 @@ void GUIFormSpecMenu::parseButton(parserData* data,std::string element, errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseBackground(parserData* data,std::string element) +void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -676,7 +676,7 @@ void GUIFormSpecMenu::parseBackground(parserData* data,std::string element) errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element) +void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -688,7 +688,7 @@ void GUIFormSpecMenu::parseTableOptions(parserData* data,std::string element) } } -void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element) +void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -708,7 +708,7 @@ void GUIFormSpecMenu::parseTableColumns(parserData* data,std::string element) } } -void GUIFormSpecMenu::parseTable(parserData* data,std::string element) +void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -776,7 +776,7 @@ void GUIFormSpecMenu::parseTable(parserData* data,std::string element) errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseTextList(parserData* data,std::string element) +void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -849,7 +849,7 @@ void GUIFormSpecMenu::parseTextList(parserData* data,std::string element) } -void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element) +void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -913,8 +913,7 @@ void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element) << element << "'" << std::endl; } -void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, - const std::string &element) +void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element) { std::vector parts = split(element,';'); if (parts.size() == 2 || @@ -923,7 +922,7 @@ void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, } } -void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element) +void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -1084,8 +1083,8 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data, m_fields.push_back(spec); } -void GUIFormSpecMenu::parseTextArea(parserData* data, - std::vector& parts,std::string type) +void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector& parts, + const std::string &type) { std::vector v_pos = split(parts[0],','); @@ -1196,8 +1195,8 @@ void GUIFormSpecMenu::parseTextArea(parserData* data, m_fields.push_back(spec); } -void GUIFormSpecMenu::parseField(parserData* data,std::string element, - std::string type) +void GUIFormSpecMenu::parseField(parserData* data, const std::string &element, + const std::string &type) { std::vector parts = split(element,';'); @@ -1215,7 +1214,7 @@ void GUIFormSpecMenu::parseField(parserData* data,std::string element, errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseLabel(parserData* data,std::string element) +void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -1271,7 +1270,7 @@ void GUIFormSpecMenu::parseLabel(parserData* data,std::string element) errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element) +void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -1321,8 +1320,8 @@ void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element) errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element, - std::string type) +void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element, + const std::string &type) { std::vector parts = split(element,';'); @@ -1410,7 +1409,7 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element, errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element) +void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -1482,7 +1481,7 @@ void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element) << element << "'" << std::endl; } -void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element) +void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element) { if (m_client == 0) { @@ -1556,7 +1555,7 @@ void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element) errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseBox(parserData* data,std::string element) +void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -1592,7 +1591,7 @@ void GUIFormSpecMenu::parseBox(parserData* data,std::string element) errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element) +void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -1610,7 +1609,7 @@ void GUIFormSpecMenu::parseBackgroundColor(parserData* data,std::string element) errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseListColors(parserData* data,std::string element) +void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element) { std::vector parts = split(element,';'); @@ -1638,7 +1637,7 @@ void GUIFormSpecMenu::parseListColors(parserData* data,std::string element) errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl; } -void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element) +void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element) { std::vector parts = split(element,';'); if (parts.size() == 2) { @@ -1658,7 +1657,7 @@ void GUIFormSpecMenu::parseTooltip(parserData* data, std::string element) errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl; } -bool GUIFormSpecMenu::parseVersionDirect(std::string data) +bool GUIFormSpecMenu::parseVersionDirect(const std::string &data) { //some prechecks if (data == "") @@ -1682,7 +1681,7 @@ bool GUIFormSpecMenu::parseVersionDirect(std::string data) return false; } -bool GUIFormSpecMenu::parseSizeDirect(parserData* data, std::string element) +bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element) { if (element == "") return false; diff --git a/src/guiFormSpecMenu.h b/src/guiFormSpecMenu.h index f4383e987..4bc2448d8 100644 --- a/src/guiFormSpecMenu.h +++ b/src/guiFormSpecMenu.h @@ -471,39 +471,41 @@ private: void parseElement(parserData* data, std::string element); - void parseSize(parserData* data, std::string element); - void parseContainer(parserData* data, std::string element); + void parseSize(parserData* data, const std::string &element); + void parseContainer(parserData* data, const std::string &element); void parseContainerEnd(parserData* data); - void parseList(parserData* data, std::string element); - void parseListRing(parserData* data, std::string element); - void parseCheckbox(parserData* data, std::string element); - void parseImage(parserData* data, std::string element); - void parseItemImage(parserData* data,std::string element); - void parseButton(parserData* data,std::string element,std::string typ); - void parseBackground(parserData* data,std::string element); - void parseTableOptions(parserData* data,std::string element); - void parseTableColumns(parserData* data,std::string element); - void parseTable(parserData* data,std::string element); - void parseTextList(parserData* data,std::string element); - void parseDropDown(parserData* data,std::string element); + void parseList(parserData* data, const std::string &element); + void parseListRing(parserData* data, const std::string &element); + void parseCheckbox(parserData* data, const std::string &element); + void parseImage(parserData* data, const std::string &element); + void parseItemImage(parserData* data, const std::string &element); + void parseButton(parserData* data, const std::string &element, + const std::string &typ); + void parseBackground(parserData* data, const std::string &element); + void parseTableOptions(parserData* data, const std::string &element); + void parseTableColumns(parserData* data, const std::string &element); + void parseTable(parserData* data, const std::string &element); + void parseTextList(parserData* data, const std::string &element); + void parseDropDown(parserData* data, const std::string &element); void parseFieldCloseOnEnter(parserData *data, const std::string &element); - void parsePwdField(parserData* data,std::string element); - void parseField(parserData* data,std::string element,std::string type); + void parsePwdField(parserData* data, const std::string &element); + void parseField(parserData* data, const std::string &element, const std::string &type); void parseSimpleField(parserData* data,std::vector &parts); void parseTextArea(parserData* data,std::vector& parts, - std::string type); - void parseLabel(parserData* data,std::string element); - void parseVertLabel(parserData* data,std::string element); - void parseImageButton(parserData* data,std::string element,std::string type); - void parseItemImageButton(parserData* data,std::string element); - void parseTabHeader(parserData* data,std::string element); - void parseBox(parserData* data,std::string element); - void parseBackgroundColor(parserData* data,std::string element); - void parseListColors(parserData* data,std::string element); - void parseTooltip(parserData* data,std::string element); - bool parseVersionDirect(std::string data); - bool parseSizeDirect(parserData* data, std::string element); - void parseScrollBar(parserData* data, std::string element); + const std::string &type); + void parseLabel(parserData* data, const std::string &element); + void parseVertLabel(parserData* data, const std::string &element); + void parseImageButton(parserData* data, const std::string &element, + const std::string &type); + void parseItemImageButton(parserData* data, const std::string &element); + void parseTabHeader(parserData* data, const std::string &element); + void parseBox(parserData* data, const std::string &element); + void parseBackgroundColor(parserData* data, const std::string &element); + void parseListColors(parserData* data, const std::string &element); + void parseTooltip(parserData* data, const std::string &element); + bool parseVersionDirect(const std::string &data); + bool parseSizeDirect(parserData* data, const std::string &element); + void parseScrollBar(parserData* data, const std::string &element); bool parsePositionDirect(parserData *data, const std::string &element); void parsePosition(parserData *data, const std::string &element); bool parseAnchorDirect(parserData *data, const std::string &element); diff --git a/src/guiTable.h b/src/guiTable.h index 4d5b39166..9fbe1c9da 100644 --- a/src/guiTable.h +++ b/src/guiTable.h @@ -74,11 +74,10 @@ public: std::string name; std::string value; - Option(const std::string &name_, const std::string &value_) - { - name = name_; - value = value_; - } + Option(const std::string &name_, const std::string &value_) : + name(name_), + value(value_) + {} }; /* diff --git a/src/httpfetch.cpp b/src/httpfetch.cpp index 21400355a..3b3f5d331 100644 --- a/src/httpfetch.cpp +++ b/src/httpfetch.cpp @@ -40,16 +40,15 @@ Mutex g_httpfetch_mutex; std::map > g_httpfetch_results; PcgRandom g_callerid_randomness; -HTTPFetchRequest::HTTPFetchRequest() +HTTPFetchRequest::HTTPFetchRequest() : + url(""), + caller(HTTPFETCH_DISCARD), + request_id(0), + timeout(g_settings->getS32("curl_timeout")), + connect_timeout(timeout), + multipart(false), + useragent(std::string(PROJECT_NAME_C "/") + g_version_hash + " (" + porting::get_sysinfo() + ")") { - url = ""; - caller = HTTPFETCH_DISCARD; - request_id = 0; - timeout = g_settings->getS32("curl_timeout"); - connect_timeout = timeout; - multipart = false; - - useragent = std::string(PROJECT_NAME_C "/") + g_version_hash + " (" + porting::get_sysinfo() + ")"; } diff --git a/src/httpfetch.h b/src/httpfetch.h index f57ed8789..d64b80b66 100644 --- a/src/httpfetch.h +++ b/src/httpfetch.h @@ -77,25 +77,23 @@ struct HTTPFetchResult unsigned long caller; unsigned long request_id; - HTTPFetchResult() - { - succeeded = false; - timeout = false; - response_code = 0; - data = ""; - caller = HTTPFETCH_DISCARD; - request_id = 0; - } - - HTTPFetchResult(const HTTPFetchRequest &fetch_request) - { - succeeded = false; - timeout = false; - response_code = 0; - data = ""; - caller = fetch_request.caller; - request_id = fetch_request.request_id; - } + HTTPFetchResult() : + succeeded(false), + timeout(false), + response_code(0), + data(""), + caller(HTTPFETCH_DISCARD), + request_id(0) + {} + + HTTPFetchResult(const HTTPFetchRequest &fetch_request) : + succeeded(false), + timeout(false), + response_code(0), + data(""), + caller(fetch_request.caller), + request_id(fetch_request.request_id) + {} }; diff --git a/src/map.cpp b/src/map.cpp index 3da96e77b..f8bbee180 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -2347,16 +2347,15 @@ bool ServerMap::saveBlock(MapBlock *block, Database *db) return ret; } -void ServerMap::loadBlock(std::string sectordir, std::string blockfile, +void ServerMap::loadBlock(const std::string §ordir, const std::string &blockfile, MapSector *sector, bool save_after_load) { DSTACK(FUNCTION_NAME); std::string fullpath = sectordir + DIR_DELIM + blockfile; try { - std::ifstream is(fullpath.c_str(), std::ios_base::binary); - if(is.good() == false) + if (!is.good()) throw FileNotGoodException("Cannot open block file"); v3s16 p3d = getBlockPos(sectordir, blockfile); diff --git a/src/map.h b/src/map.h index 90f97db8b..744a4d1e2 100644 --- a/src/map.h +++ b/src/map.h @@ -456,17 +456,11 @@ public: MapSector* loadSectorMeta(std::string dirname, bool save_after_load); bool loadSectorMeta(v2s16 p2d); - // Full load of a sector including all blocks. - // returns true on success, false on failure. - bool loadSectorFull(v2s16 p2d); - // If sector is not found in memory, try to load it from disk. - // Returns true if sector now resides in memory - //bool deFlushSector(v2s16 p2d); - bool saveBlock(MapBlock *block); static bool saveBlock(MapBlock *block, Database *db); // This will generate a sector with getSector if not found. - void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false); + void loadBlock(const std::string §ordir, const std::string &blockfile, + MapSector *sector, bool save_after_load=false); MapBlock* loadBlock(v3s16 p); // Database version void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false); diff --git a/src/network/connection.h b/src/network/connection.h index 5ee53b9d4..7ba0d086e 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -383,7 +383,7 @@ struct OutgoingPacket bool reliable; bool ack; - OutgoingPacket(u16 peer_id_, u8 channelnum_, SharedBuffer data_, + OutgoingPacket(u16 peer_id_, u8 channelnum_, const SharedBuffer &data_, bool reliable_,bool ack_=false): peer_id(peer_id_), channelnum(channelnum_), @@ -448,7 +448,7 @@ struct ConnectionCommand reliable = reliable_; } - void ack(u16 peer_id_, u8 channelnum_, SharedBuffer data_) + void ack(u16 peer_id_, u8 channelnum_, const SharedBuffer &data_) { type = CONCMD_ACK; peer_id = peer_id_; @@ -457,7 +457,7 @@ struct ConnectionCommand reliable = false; } - void createPeer(u16 peer_id_, SharedBuffer data_) + void createPeer(u16 peer_id_, const SharedBuffer &data_) { type = CONCMD_CREATE_PEER; peer_id = peer_id_; @@ -467,7 +467,7 @@ struct ConnectionCommand raw = true; } - void disableLegacy(u16 peer_id_, SharedBuffer data_) + void disableLegacy(u16 peer_id_, const SharedBuffer &data_) { type = CONCMD_DISABLE_LEGACY; peer_id = peer_id_; @@ -874,7 +874,7 @@ struct ConnectionEvent return "Invalid ConnectionEvent"; } - void dataReceived(u16 peer_id_, SharedBuffer data_) + void dataReceived(u16 peer_id_, const SharedBuffer &data_) { type = CONNEVENT_DATA_RECEIVED; peer_id = peer_id_; diff --git a/src/nodedef.h b/src/nodedef.h index da3345d80..83968ce27 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -218,14 +218,14 @@ struct TileDef struct TileAnimationParams animation; - TileDef() + TileDef() : + name(""), + backface_culling(true), + tileable_horizontal(true), + tileable_vertical(true), + has_color(false), + color(video::SColor(0xFFFFFFFF)) { - name = ""; - backface_culling = true; - tileable_horizontal = true; - tileable_vertical = true; - has_color = false; - color = video::SColor(0xFFFFFFFF); animation.type = TAT_NONE; } diff --git a/src/pathfinder.cpp b/src/pathfinder.cpp index ee06db630..16c5678ee 100644 --- a/src/pathfinder.cpp +++ b/src/pathfinder.cpp @@ -111,7 +111,7 @@ public: * @param dir direction to set cost for * @cost cost to set */ - void setCost(v3s16 dir, PathCost cost); + void setCost(v3s16 dir, const PathCost &cost); bool valid; /**< node is on surface */ bool target; /**< node is target position */ @@ -496,7 +496,7 @@ PathCost PathGridnode::getCost(v3s16 dir) } /******************************************************************************/ -void PathGridnode::setCost(v3s16 dir, PathCost cost) +void PathGridnode::setCost(v3s16 dir, const PathCost &cost) { if (dir.X > 0) { directions[DIR_XP] = cost; diff --git a/src/porting.cpp b/src/porting.cpp index 9d859da7d..8c92a3cba 100644 --- a/src/porting.cpp +++ b/src/porting.cpp @@ -408,7 +408,7 @@ bool setSystemPaths() #endif for (std::list::const_iterator - i = trylist.begin(); i != trylist.end(); i++) { + i = trylist.begin(); i != trylist.end(); ++i) { const std::string &trypath = *i; if (!fs::PathExists(trypath) || !fs::PathExists(trypath + DIR_DELIM + "builtin")) { diff --git a/src/script/cpp_api/s_async.cpp b/src/script/cpp_api/s_async.cpp index 1fb84fab6..a1bec83bf 100644 --- a/src/script/cpp_api/s_async.cpp +++ b/src/script/cpp_api/s_async.cpp @@ -100,7 +100,8 @@ void AsyncEngine::initialize(unsigned int numEngines) } /******************************************************************************/ -unsigned int AsyncEngine::queueAsyncJob(std::string func, std::string params) +unsigned int AsyncEngine::queueAsyncJob(const std::string &func, + const std::string ¶ms) { jobQueueMutex.lock(); LuaJobInfo toAdd; @@ -124,7 +125,6 @@ LuaJobInfo AsyncEngine::getJob() jobQueueMutex.lock(); LuaJobInfo retval; - retval.valid = false; if (!jobQueue.empty()) { retval = jobQueue.front(); @@ -137,7 +137,7 @@ LuaJobInfo AsyncEngine::getJob() } /******************************************************************************/ -void AsyncEngine::putJobResult(LuaJobInfo result) +void AsyncEngine::putJobResult(const LuaJobInfo &result) { resultQueueMutex.lock(); resultQueue.push_back(result); @@ -264,7 +264,7 @@ void* AsyncWorkerThread::run() // Wait for job LuaJobInfo toProcess = jobDispatcher->getJob(); - if (toProcess.valid == false || stopRequested()) { + if (!toProcess.valid || stopRequested()) { continue; } diff --git a/src/script/cpp_api/s_async.h b/src/script/cpp_api/s_async.h index 016381e5f..93e9759b4 100644 --- a/src/script/cpp_api/s_async.h +++ b/src/script/cpp_api/s_async.h @@ -38,7 +38,16 @@ class AsyncEngine; // Declarations // Data required to queue a job -struct LuaJobInfo { +struct LuaJobInfo +{ + LuaJobInfo() : + serializedFunction(""), + serializedParams(""), + serializedResult(""), + id(0), + valid(false) + {} + // Function to be called in async environment std::string serializedFunction; // Parameter to be passed to function @@ -89,7 +98,7 @@ public: * @param params Serialized parameters * @return jobid The job is queued */ - unsigned int queueAsyncJob(std::string func, std::string params); + unsigned int queueAsyncJob(const std::string &func, const std::string ¶ms); /** * Engine step to process finished jobs @@ -116,7 +125,7 @@ protected: * Put a Job result back to result queue * @param result result of completed job */ - void putJobResult(LuaJobInfo result); + void putJobResult(const LuaJobInfo &result); /** * Initialize environment with current registred functions diff --git a/src/script/scripting_mainmenu.cpp b/src/script/scripting_mainmenu.cpp index 61318735d..d79864a95 100644 --- a/src/script/scripting_mainmenu.cpp +++ b/src/script/scripting_mainmenu.cpp @@ -77,13 +77,15 @@ void MainMenuScripting::initializeModApi(lua_State *L, int top) } /******************************************************************************/ -void MainMenuScripting::step() { +void MainMenuScripting::step() +{ asyncEngine.step(getStack()); } /******************************************************************************/ -unsigned int MainMenuScripting::queueAsync(std::string serialized_func, - std::string serialized_param) { +unsigned int MainMenuScripting::queueAsync(const std::string &serialized_func, + const std::string &serialized_param) +{ return asyncEngine.queueAsyncJob(serialized_func, serialized_param); } diff --git a/src/script/scripting_mainmenu.h b/src/script/scripting_mainmenu.h index 3a0795df4..a1385ba9c 100644 --- a/src/script/scripting_mainmenu.h +++ b/src/script/scripting_mainmenu.h @@ -39,8 +39,8 @@ public: void step(); // Pass async events from engine to async threads - unsigned int queueAsync(std::string serialized_func, - std::string serialized_params); + unsigned int queueAsync(const std::string &serialized_func, + const std::string &serialized_params); private: void initializeModApi(lua_State *L, int top); diff --git a/src/sound.h b/src/sound.h index ba2d629d2..d13799eac 100644 --- a/src/sound.h +++ b/src/sound.h @@ -34,13 +34,15 @@ public: struct SimpleSoundSpec { + SimpleSoundSpec(const std::string &name = "", float gain = 1.0) : + name(name), + gain(gain) + {} + + bool exists() const { return name != ""; } + std::string name; float gain; - SimpleSoundSpec(std::string name = "", float gain = 1.0) : name(name), gain(gain) - { - } - bool exists() { return name != ""; } - // Serialization intentionally left out }; class ISoundManager diff --git a/src/util/string.h b/src/util/string.h index 1a0b9f60d..632dd4d7e 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -232,7 +232,7 @@ inline std::vector > str_split( */ inline std::string lowercase(const std::string &str) { - std::string s2; + std::string s2 = ""; s2.reserve(str.size()); diff --git a/src/util/thread.h b/src/util/thread.h index d43e06e0a..f54b8b48f 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -29,9 +29,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "container.h" template -class MutexedVariable { +class MutexedVariable +{ public: - MutexedVariable(T value): + MutexedVariable(const T &value): m_value(value) {} @@ -41,21 +42,14 @@ public: return m_value; } - void set(T value) + void set(const T &value) { MutexAutoLock lock(m_mutex); m_value = value; } - // You'll want to grab this in a SharedPtr - MutexAutoLock *getLock() - { - return new MutexAutoLock(m_mutex); - } - // You pretty surely want to grab the lock when accessing this T m_value; - private: Mutex m_mutex; }; -- cgit v1.2.3 From dc0e9097d3144a3dda81039f666ee43ba29cbc36 Mon Sep 17 00:00:00 2001 From: Loïc Blot Date: Fri, 21 Apr 2017 23:40:48 +0200 Subject: Fix various performance issues reported by cppcheck + code style (CI) (#5635) * Make CI happy with code style on master * guiFileSelectMenu: remove useless includes * some performance fixes pointed by cppcheck * remove some useless casts * TextDest: remove unused setFormSpec function --- src/guiFileSelectMenu.cpp | 22 +++++++---------- src/guiFileSelectMenu.h | 7 ++---- src/guiFormSpecMenu.cpp | 5 ++-- src/guiFormSpecMenu.h | 60 +++++++++++++++++++++++++++++------------------ src/mesh.cpp | 10 ++++---- src/sound.h | 8 +++---- src/wieldmesh.h | 25 +++++++++----------- 7 files changed, 70 insertions(+), 67 deletions(-) (limited to 'src/sound.h') diff --git a/src/guiFileSelectMenu.cpp b/src/guiFileSelectMenu.cpp index 0bb02f8a6..89d34a307 100644 --- a/src/guiFileSelectMenu.cpp +++ b/src/guiFileSelectMenu.cpp @@ -18,19 +18,17 @@ */ #include "guiFileSelectMenu.h" -#include "util/string.h" -#include GUIFileSelectMenu::GUIFileSelectMenu(gui::IGUIEnvironment* env, - gui::IGUIElement* parent, s32 id, IMenuManager *menumgr, - std::string title, std::string formname) : -GUIModalMenu(env, parent, id, menumgr) + gui::IGUIElement* parent, s32 id, IMenuManager *menumgr, + const std::string &title, const std::string &formname) : + GUIModalMenu(env, parent, id, menumgr), + m_title(utf8_to_wide(title)), + m_accepted(false), + m_parent(parent), + m_text_dst(NULL), + m_formname(formname) { - m_title = utf8_to_wide(title); - m_parent = parent; - m_formname = formname; - m_text_dst = 0; - m_accepted = false; } GUIFileSelectMenu::~GUIFileSelectMenu() @@ -107,16 +105,12 @@ bool GUIFileSelectMenu::OnEvent(const SEvent& event) acceptInput(); quitMenu(); return true; - break; - case gui::EGET_DIRECTORY_SELECTED: case gui::EGET_FILE_SELECTED: m_accepted=true; acceptInput(); quitMenu(); return true; - break; - default: //ignore this event break; diff --git a/src/guiFileSelectMenu.h b/src/guiFileSelectMenu.h index e37d3d8df..a266cb399 100644 --- a/src/guiFileSelectMenu.h +++ b/src/guiFileSelectMenu.h @@ -32,8 +32,7 @@ class GUIFileSelectMenu: public GUIModalMenu public: GUIFileSelectMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent, s32 id, IMenuManager *menumgr, - std::string title, - std::string formid); + const std::string &title, const std::string &formid); ~GUIFileSelectMenu(); void removeChildren(); @@ -47,9 +46,7 @@ public: bool OnEvent(const SEvent& event); - bool isRunning() { - return m_running; - } + bool isRunning() const { return m_running; } void setTextDest(TextDest * dest) { m_text_dst = dest; diff --git a/src/guiFormSpecMenu.cpp b/src/guiFormSpecMenu.cpp index ab93aeca1..5861e9a81 100644 --- a/src/guiFormSpecMenu.cpp +++ b/src/guiFormSpecMenu.cpp @@ -1770,10 +1770,11 @@ void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element) return; } - errorstream << "Invalid anchor element (" << parts.size() << "): '" << element << "'" << std::endl; + errorstream << "Invalid anchor element (" << parts.size() << "): '" << element + << "'" << std::endl; } -void GUIFormSpecMenu::parseElement(parserData* data, std::string element) +void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element) { //some prechecks if (element == "") diff --git a/src/guiFormSpecMenu.h b/src/guiFormSpecMenu.h index ec122b617..d70c3a582 100644 --- a/src/guiFormSpecMenu.h +++ b/src/guiFormSpecMenu.h @@ -55,12 +55,10 @@ typedef enum { struct TextDest { - virtual ~TextDest() {}; + virtual ~TextDest() {} // This is deprecated I guess? -celeron55 virtual void gotText(std::wstring text){} virtual void gotText(const StringMap &fields) = 0; - virtual void setFormName(std::string formname) - { m_formname = formname;}; std::string m_formname; }; @@ -80,7 +78,8 @@ class GUIFormSpecMenu : public GUIModalMenu { ItemSpec() : i(-1) - {} + { + } ItemSpec(const InventoryLocation &a_inventoryloc, const std::string &a_listname, @@ -88,7 +87,8 @@ class GUIFormSpecMenu : public GUIModalMenu inventoryloc(a_inventoryloc), listname(a_listname), i(a_i) - {} + { + } bool isValid() const { return i != -1; } @@ -141,7 +141,8 @@ class GUIFormSpecMenu : public GUIModalMenu ImageDrawSpec(): parent_button(NULL), clip(false) - {} + { + } ImageDrawSpec(const std::string &a_name, const std::string &a_item_name, @@ -154,7 +155,8 @@ class GUIFormSpecMenu : public GUIModalMenu geom(a_geom), scale(true), clip(false) - {} + { + } ImageDrawSpec(const std::string &a_name, const std::string &a_item_name, @@ -166,7 +168,8 @@ class GUIFormSpecMenu : public GUIModalMenu geom(a_geom), scale(true), clip(false) - {} + { + } ImageDrawSpec(const std::string &a_name, const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false): @@ -176,7 +179,8 @@ class GUIFormSpecMenu : public GUIModalMenu geom(a_geom), scale(true), clip(clip) - {} + { + } ImageDrawSpec(const std::string &a_name, const v2s32 &a_pos): @@ -185,7 +189,8 @@ class GUIFormSpecMenu : public GUIModalMenu pos(a_pos), scale(false), clip(false) - {} + { + } std::string name; std::string item_name; @@ -210,7 +215,8 @@ class GUIFormSpecMenu : public GUIModalMenu send(false), ftype(f_Unknown), is_exit(false) - {} + { + } std::string fname; std::wstring flabel; @@ -222,7 +228,8 @@ class GUIFormSpecMenu : public GUIModalMenu core::rect rect; }; - struct BoxDrawSpec { + struct BoxDrawSpec + { BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color): pos(a_pos), geom(a_geom), @@ -234,40 +241,45 @@ class GUIFormSpecMenu : public GUIModalMenu irr::video::SColor color; }; - struct TooltipSpec { + struct TooltipSpec + { TooltipSpec() {} - TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor, + TooltipSpec(const std::string &a_tooltip, irr::video::SColor a_bgcolor, irr::video::SColor a_color): tooltip(utf8_to_wide(a_tooltip)), bgcolor(a_bgcolor), color(a_color) - {} + { + } std::wstring tooltip; irr::video::SColor bgcolor; irr::video::SColor color; }; - struct StaticTextSpec { + struct StaticTextSpec + { StaticTextSpec(): parent_button(NULL) { } + StaticTextSpec(const std::wstring &a_text, const core::rect &a_rect): + text(a_text), rect(a_rect), parent_button(NULL) { - //text = unescape_enriched(a_text); - text = a_text; } + StaticTextSpec(const std::wstring &a_text, const core::rect &a_rect, gui::IGUIButton *a_parent_button): text(a_text), rect(a_rect), parent_button(a_parent_button) - {} + { + } std::wstring text; core::rect rect; @@ -288,7 +300,7 @@ public: ~GUIFormSpecMenu(); void setFormSpec(const std::string &formspec_string, - InventoryLocation current_inventory_location) + const InventoryLocation ¤t_inventory_location) { m_formspec_string = formspec_string; m_current_inventory_location = current_inventory_location; @@ -461,7 +473,7 @@ private: fs_key_pendig current_keys_pending; std::string current_field_enter_pending; - void parseElement(parserData* data, std::string element); + void parseElement(parserData* data, const std::string &element); void parseSize(parserData* data, const std::string &element); void parseContainer(parserData* data, const std::string &element); @@ -544,10 +556,12 @@ class FormspecFormSource: public IFormSource public: FormspecFormSource(const std::string &formspec): m_formspec(formspec) - {} + { + } ~FormspecFormSource() - {} + { + } void setForm(const std::string &formspec) { diff --git a/src/mesh.cpp b/src/mesh.cpp index d776f6185..0a5b7fb6c 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -391,7 +391,7 @@ scene::IMeshBuffer* cloneMeshBuffer(scene::IMeshBuffer *mesh_buffer) switch (mesh_buffer->getVertexType()) { case video::EVT_STANDARD: { video::S3DVertex *v = (video::S3DVertex *) mesh_buffer->getVertices(); - u16 *indices = (u16*) mesh_buffer->getIndices(); + u16 *indices = mesh_buffer->getIndices(); scene::SMeshBuffer *temp_buf = new scene::SMeshBuffer(); temp_buf->append(v, mesh_buffer->getVertexCount(), indices, mesh_buffer->getIndexCount()); @@ -401,7 +401,7 @@ scene::IMeshBuffer* cloneMeshBuffer(scene::IMeshBuffer *mesh_buffer) case video::EVT_2TCOORDS: { video::S3DVertex2TCoords *v = (video::S3DVertex2TCoords *) mesh_buffer->getVertices(); - u16 *indices = (u16*) mesh_buffer->getIndices(); + u16 *indices = mesh_buffer->getIndices(); scene::SMeshBufferTangents *temp_buf = new scene::SMeshBufferTangents(); temp_buf->append(v, mesh_buffer->getVertexCount(), indices, mesh_buffer->getIndexCount()); @@ -410,7 +410,7 @@ scene::IMeshBuffer* cloneMeshBuffer(scene::IMeshBuffer *mesh_buffer) case video::EVT_TANGENTS: { video::S3DVertexTangents *v = (video::S3DVertexTangents *) mesh_buffer->getVertices(); - u16 *indices = (u16*) mesh_buffer->getIndices(); + u16 *indices = mesh_buffer->getIndices(); scene::SMeshBufferTangents *temp_buf = new scene::SMeshBufferTangents(); temp_buf->append(v, mesh_buffer->getVertexCount(), indices, mesh_buffer->getIndexCount()); @@ -447,7 +447,7 @@ scene::IMesh* convertNodeboxesToMesh(const std::vector &boxes, buf->drop(); } - video::SColor c(255,255,255,255); + video::SColor c(255,255,255,255); for (std::vector::const_iterator i = boxes.begin(); @@ -534,7 +534,7 @@ scene::IMesh* convertNodeboxesToMesh(const std::vector &boxes, buf->append(vertices + j, 4, indices, 6); } } - return dst_mesh; + return dst_mesh; } struct vcache diff --git a/src/sound.h b/src/sound.h index d13799eac..98f7692d5 100644 --- a/src/sound.h +++ b/src/sound.h @@ -34,10 +34,10 @@ public: struct SimpleSoundSpec { - SimpleSoundSpec(const std::string &name = "", float gain = 1.0) : - name(name), - gain(gain) - {} + SimpleSoundSpec(const std::string &name = "", float gain = 1.0) + : name(name), gain(gain) + { + } bool exists() const { return name != ""; } diff --git a/src/wieldmesh.h b/src/wieldmesh.h index c98b469d9..ef164c11f 100644 --- a/src/wieldmesh.h +++ b/src/wieldmesh.h @@ -31,7 +31,8 @@ struct ContentFeatures; /*! * Holds color information of an item mesh's buffer. */ -struct ItemPartColor { +struct ItemPartColor +{ /*! * If this is false, the global base color of the item * will be used instead of the specific color of the @@ -43,15 +44,12 @@ struct ItemPartColor { */ video::SColor color; - ItemPartColor(): - override_base(false), - color(0) - {} + ItemPartColor() : override_base(false), color(0) {} - ItemPartColor(bool override, video::SColor color): - override_base(override), - color(color) - {} + ItemPartColor(bool override, video::SColor color) + : override_base(override), color(color) + { + } }; struct ItemMesh @@ -75,8 +73,7 @@ public: s32 id = -1, bool lighting = false); virtual ~WieldMeshSceneNode(); - void setCube(const ContentFeatures &f, v3f wield_scale, - ITextureSource *tsrc); + void setCube(const ContentFeatures &f, v3f wield_scale, ITextureSource *tsrc); void setExtruded(const std::string &imagename, v3f wield_scale, ITextureSource *tsrc, u8 num_frames); void setItem(const ItemStack &item, Client *client); @@ -133,7 +130,7 @@ scene::SMesh *getExtrudedMesh(ITextureSource *tsrc, const std::string &imagename * be NULL to leave the original material. * \param colors returns the colors of the mesh buffers in the mesh. */ -void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f, - bool use_shaders, bool set_material, video::E_MATERIAL_TYPE *mattype, - std::vector *colors); +void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f, bool use_shaders, + bool set_material, video::E_MATERIAL_TYPE *mattype, + std::vector *colors); #endif -- cgit v1.2.3 From bd921a7916f0fafc493b1c4d0eeb5e2bb1d6a7c2 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sun, 10 Jul 2016 00:08:26 -0500 Subject: Sound API: Add fading sounds --- doc/lua_api.txt | 7 +++ src/client.cpp | 1 + src/client.h | 1 + src/network/clientopcodes.cpp | 2 +- src/network/clientpackethandler.cpp | 35 +++++++++++++- src/network/networkprotocol.h | 11 ++++- src/script/common/c_content.cpp | 2 + src/script/common/c_converter.h | 2 + src/script/lua_api/l_server.cpp | 11 +++++ src/script/lua_api/l_server.h | 3 ++ src/server.cpp | 64 +++++++++++++++++++++++-- src/server.h | 6 ++- src/sound.h | 28 +++++++---- src/sound_openal.cpp | 94 ++++++++++++++++++++++++++++++++++++- 14 files changed, 248 insertions(+), 19 deletions(-) (limited to 'src/sound.h') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 479e38a2e..77ffb88e2 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -456,11 +456,13 @@ Examples of sound parameter tables: -- Play locationless on all clients { gain = 1.0, -- default + fade = 0.0, -- default, change to a value > 0 to fade the sound in } -- Play locationless to one player { to_player = name, gain = 1.0, -- default + fade = 0.0, -- default, change to a value > 0 to fade the sound in } -- Play locationless to one player, looped { @@ -2587,6 +2589,11 @@ These functions return the leftover itemstack. * `spec` is a `SimpleSoundSpec` * `parameters` is a sound parameter table * `minetest.sound_stop(handle)` +* `minetest.sound_fade(handle, step, gain)` + * `handle` is a handle returned by minetest.sound_play + * `step` determines how fast a sound will fade. + Negative step will lower the sound volume, positive step will increase the sound volume + * `gain` the target gain for the fade. ### Timing * `minetest.after(time, func, ...)` diff --git a/src/client.cpp b/src/client.cpp index 3c5a70f21..3269c573a 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -407,6 +407,7 @@ void Client::step(float dtime) // Step environment m_env.step(dtime); + m_sound->step(dtime); /* Get events diff --git a/src/client.h b/src/client.h index 7cbfadd50..e8db7de44 100644 --- a/src/client.h +++ b/src/client.h @@ -328,6 +328,7 @@ public: void handleCommand_ItemDef(NetworkPacket* pkt); void handleCommand_PlaySound(NetworkPacket* pkt); void handleCommand_StopSound(NetworkPacket* pkt); + void handleCommand_FadeSound(NetworkPacket *pkt); void handleCommand_Privileges(NetworkPacket* pkt); void handleCommand_InventoryFormSpec(NetworkPacket* pkt); void handleCommand_DetachedInventory(NetworkPacket* pkt); diff --git a/src/network/clientopcodes.cpp b/src/network/clientopcodes.cpp index 1be6e5522..bdcb1dfce 100644 --- a/src/network/clientopcodes.cpp +++ b/src/network/clientopcodes.cpp @@ -109,7 +109,7 @@ const ToClientCommandHandler toClientCommandTable[TOCLIENT_NUM_MSG_TYPES] = { "TOCLIENT_EYE_OFFSET", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_EyeOffset }, // 0x52 { "TOCLIENT_DELETE_PARTICLESPAWNER", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_DeleteParticleSpawner }, // 0x53 { "TOCLIENT_CLOUD_PARAMS", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_CloudParams }, // 0x54 - null_command_handler, + { "TOCLIENT_FADE_SOUND", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_FadeSound }, // 0x55 null_command_handler, null_command_handler, null_command_handler, diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index defc83f31..a895acc84 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -755,21 +755,39 @@ void Client::handleCommand_ItemDef(NetworkPacket* pkt) void Client::handleCommand_PlaySound(NetworkPacket* pkt) { + /* + [0] u32 server_id + [4] u16 name length + [6] char name[len] + [ 6 + len] f32 gain + [10 + len] u8 type + [11 + len] (f32 * 3) pos + [23 + len] u16 object_id + [25 + len] bool loop + [26 + len] f32 fade + */ + s32 server_id; std::string name; + float gain; u8 type; // 0=local, 1=positional, 2=object v3f pos; u16 object_id; bool loop; + float fade = 0; *pkt >> server_id >> name >> gain >> type >> pos >> object_id >> loop; + try { + *pkt >> fade; + } catch (SerializationError &e) {}; + // Start playing int client_id = -1; switch(type) { case 0: // local - client_id = m_sound->playSound(name, loop, gain); + client_id = m_sound->playSound(name, loop, gain, fade); break; case 1: // positional client_id = m_sound->playSoundAt(name, loop, gain, pos); @@ -808,6 +826,21 @@ void Client::handleCommand_StopSound(NetworkPacket* pkt) } } +void Client::handleCommand_FadeSound(NetworkPacket *pkt) +{ + s32 sound_id; + float step; + float gain; + + *pkt >> sound_id >> step >> gain; + + UNORDERED_MAP::iterator i = + m_sounds_server_to_client.find(sound_id); + + if (i != m_sounds_server_to_client.end()) + m_sound->fadeSound(i->second, step, gain); +} + void Client::handleCommand_Privileges(NetworkPacket* pkt) { m_privileges.clear(); diff --git a/src/network/networkprotocol.h b/src/network/networkprotocol.h index a1a4f5bfa..70cad85d8 100644 --- a/src/network/networkprotocol.h +++ b/src/network/networkprotocol.h @@ -153,9 +153,11 @@ with this program; if not, write to the Free Software Foundation, Inc., PROTOCOL VERSION 31: Add tile overlay Stop sending TOSERVER_CLIENT_READY + PROTOCOL VERSION 32: + Add fading sounds */ -#define LATEST_PROTOCOL_VERSION 31 +#define LATEST_PROTOCOL_VERSION 32 // Server's supported network protocol range #define SERVER_PROTOCOL_VERSION_MIN 24 @@ -620,6 +622,13 @@ enum ToClientCommand v2f1000 speed */ + TOCLIENT_FADE_SOUND = 0x55, + /* + s32 sound_id + float step + float gain + */ + TOCLIENT_SRP_BYTES_S_B = 0x60, /* Belonging to AUTH_MECHANISM_LEGACY_PASSWORD and AUTH_MECHANISM_SRP. diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 5fe5af58d..8696ad7cb 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -680,6 +680,7 @@ void read_server_sound_params(lua_State *L, int index, if(lua_istable(L, index)){ getfloatfield(L, index, "gain", params.gain); getstringfield(L, index, "to_player", params.to_player); + getfloatfield(L, index, "fade", params.fade); lua_getfield(L, index, "pos"); if(!lua_isnil(L, -1)){ v3f p = read_v3f(L, -1)*BS; @@ -712,6 +713,7 @@ void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec) } else if(lua_istable(L, index)){ getstringfield(L, index, "name", spec.name); getfloatfield(L, index, "gain", spec.gain); + getfloatfield(L, index, "fade", spec.fade); } else if(lua_isstring(L, index)){ spec.name = lua_tostring(L, index); } diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h index a5fbee765..b0f61a8ca 100644 --- a/src/script/common/c_converter.h +++ b/src/script/common/c_converter.h @@ -77,6 +77,8 @@ void setfloatfield(lua_State *L, int table, const char *fieldname, float value); void setboolfield(lua_State *L, int table, const char *fieldname, bool value); +void setstringfield(lua_State *L, int table, + const char *fieldname, const char *value); v3f checkFloatPos (lua_State *L, int index); v2f check_v2f (lua_State *L, int index); diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index 7b723d14c..ea993d7b7 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -455,6 +455,16 @@ int ModApiServer::l_sound_stop(lua_State *L) return 0; } +int ModApiServer::l_sound_fade(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + s32 handle = luaL_checkinteger(L, 1); + float step = luaL_checknumber(L, 2); + float gain = luaL_checknumber(L, 3); + getServer(L)->fadeSound(handle, step, gain); + return 0; +} + // is_singleplayer() int ModApiServer::l_is_singleplayer(lua_State *L) { @@ -518,6 +528,7 @@ void ModApiServer::Initialize(lua_State *L, int top) API_FCT(show_formspec); API_FCT(sound_play); API_FCT(sound_stop); + API_FCT(sound_fade); API_FCT(get_player_information); API_FCT(get_player_privs); diff --git a/src/script/lua_api/l_server.h b/src/script/lua_api/l_server.h index 3a4a917c0..251a0ce89 100644 --- a/src/script/lua_api/l_server.h +++ b/src/script/lua_api/l_server.h @@ -68,6 +68,9 @@ private: // sound_stop(handle) static int l_sound_stop(lua_State *L); + // sound_fade(handle, step, gain) + static int l_sound_fade(lua_State *L); + // get_player_privs(name, text) static int l_get_player_privs(lua_State *L); diff --git a/src/server.cpp b/src/server.cpp index 9ef69cb37..190a1baf2 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2100,15 +2100,23 @@ s32 Server::playSound(const SimpleSoundSpec &spec, m_playing_sounds[id] = ServerPlayingSound(); ServerPlayingSound &psound = m_playing_sounds[id]; psound.params = params; + psound.spec = spec; + float gain = params.gain * spec.gain; NetworkPacket pkt(TOCLIENT_PLAY_SOUND, 0); - pkt << id << spec.name << (float) (spec.gain * params.gain) - << (u8) params.type << pos << params.object << params.loop; + pkt << id << spec.name << gain + << (u8) params.type << pos << params.object + << params.loop << params.fade; - for(std::vector::iterator i = dst_clients.begin(); + // Backwards compability + bool play_sound = gain > 0; + + for (std::vector::iterator i = dst_clients.begin(); i != dst_clients.end(); ++i) { - psound.clients.insert(*i); - m_clients.send(*i, 0, &pkt, true); + if (play_sound || m_clients.getProtocolVersion(*i) >= 32) { + psound.clients.insert(*i); + m_clients.send(*i, 0, &pkt, true); + } } return id; } @@ -2132,6 +2140,52 @@ void Server::stopSound(s32 handle) m_playing_sounds.erase(i); } +void Server::fadeSound(s32 handle, float step, float gain) +{ + // Get sound reference + UNORDERED_MAP::iterator i = + m_playing_sounds.find(handle); + if (i == m_playing_sounds.end()) + return; + + ServerPlayingSound &psound = i->second; + psound.params.gain = gain; + + NetworkPacket pkt(TOCLIENT_FADE_SOUND, 4); + pkt << handle << step << gain; + + // Backwards compability + bool play_sound = gain > 0; + ServerPlayingSound compat_psound = psound; + compat_psound.clients.clear(); + + NetworkPacket compat_pkt(TOCLIENT_STOP_SOUND, 4); + compat_pkt << handle; + + for (UNORDERED_SET::iterator it = psound.clients.begin(); + it != psound.clients.end();) { + if (m_clients.getProtocolVersion(*it) >= 32) { + // Send as reliable + m_clients.send(*it, 0, &pkt, true); + ++it; + } else { + compat_psound.clients.insert(*it); + // Stop old sound + m_clients.send(*it, 0, &compat_pkt, true); + psound.clients.erase(it++); + } + } + + // Remove sound reference + if (!play_sound || psound.clients.size() == 0) + m_playing_sounds.erase(i); + + if (play_sound && compat_psound.clients.size() > 0) { + // Play new sound volume on older clients + playSound(compat_psound.spec, compat_psound.params); + } +} + void Server::sendRemoveNode(v3s16 p, u16 ignore_id, std::vector *far_players, float far_d_nodes) { diff --git a/src/server.h b/src/server.h index 3a082b9a4..5e6211637 100644 --- a/src/server.h +++ b/src/server.h @@ -115,6 +115,7 @@ struct ServerSoundParams u16 object; float max_hear_distance; bool loop; + float fade; ServerSoundParams(): gain(1.0), @@ -123,7 +124,8 @@ struct ServerSoundParams pos(0,0,0), object(0), max_hear_distance(32*BS), - loop(false) + loop(false), + fade(0) {} v3f getPos(ServerEnvironment *env, bool *pos_exists) const; @@ -132,6 +134,7 @@ struct ServerSoundParams struct ServerPlayingSound { ServerSoundParams params; + SimpleSoundSpec spec; UNORDERED_SET clients; // peer ids }; @@ -231,6 +234,7 @@ public: // Envlock s32 playSound(const SimpleSoundSpec &spec, const ServerSoundParams ¶ms); void stopSound(s32 handle); + void fadeSound(s32 handle, float step, float gain); // Envlock std::set getPlayerEffectivePrivs(const std::string &name); diff --git a/src/sound.h b/src/sound.h index 98f7692d5..7bdb6a26b 100644 --- a/src/sound.h +++ b/src/sound.h @@ -34,8 +34,8 @@ public: struct SimpleSoundSpec { - SimpleSoundSpec(const std::string &name = "", float gain = 1.0) - : name(name), gain(gain) + SimpleSoundSpec(const std::string &name = "", float gain = 1.0, float fade = 0.0) + : name(name), gain(gain), fade(fade) { } @@ -43,13 +43,13 @@ struct SimpleSoundSpec std::string name; float gain; + float fade; }; class ISoundManager { public: virtual ~ISoundManager() {} - // Multiple sounds can be loaded per name; when played, the sound // should be chosen randomly from alternatives // Return value determines success/failure @@ -63,16 +63,21 @@ public: // playSound functions return -1 on failure, otherwise a handle to the // sound. If name=="", call should be ignored without error. - virtual int playSound(const std::string &name, bool loop, float volume) = 0; - virtual int playSoundAt( - const std::string &name, bool loop, float volume, v3f pos) = 0; + virtual int playSound(const std::string &name, bool loop, float volume, + float fade = 0) = 0; + virtual int playSoundAt(const std::string &name, bool loop, float volume, + v3f pos) = 0; virtual void stopSound(int sound) = 0; virtual bool soundExists(int sound) = 0; virtual void updateSoundPosition(int sound, v3f pos) = 0; + virtual bool updateSoundGain(int id, float gain) = 0; + virtual float getSoundGain(int id) = 0; + virtual void step(float dtime) = 0; + virtual void fadeSound(int sound, float step, float gain) = 0; int playSound(const SimpleSoundSpec &spec, bool loop) { - return playSound(spec.name, loop, spec.gain); + return playSound(spec.name, loop, spec.gain, spec.fade); } int playSoundAt(const SimpleSoundSpec &spec, bool loop, v3f pos) { @@ -93,7 +98,10 @@ public: } void updateListener(v3f pos, v3f vel, v3f at, v3f up) {} void setListenerGain(float gain) {} - int playSound(const std::string &name, bool loop, float volume) { return 0; } + int playSound(const std::string &name, bool loop, float volume, float fade) + { + return 0; + } int playSoundAt(const std::string &name, bool loop, float volume, v3f pos) { return 0; @@ -101,6 +109,10 @@ public: void stopSound(int sound) {} bool soundExists(int sound) { return false; } void updateSoundPosition(int sound, v3f pos) {} + bool updateSoundGain(int id, float gain) { return false; } + float getSoundGain(int id) { return 0; } + void step(float dtime) { } + void fadeSound(int sound, float step, float gain) { } }; // Global DummySoundManager singleton diff --git a/src/sound_openal.cpp b/src/sound_openal.cpp index b9af9e3a9..a425af827 100644 --- a/src/sound_openal.cpp +++ b/src/sound_openal.cpp @@ -274,6 +274,19 @@ private: UNORDERED_MAP > m_buffers; UNORDERED_MAP m_sounds_playing; v3f m_listener_pos; + struct FadeState { + FadeState() {} + FadeState(float step, float current_gain, float target_gain): + step(step), + current_gain(current_gain), + target_gain(target_gain) {} + float step; + float current_gain; + float target_gain; + }; + + UNORDERED_MAP m_sounds_fading; + float m_fade_delay; public: bool m_is_initialized; OpenALSoundManager(OnDemandSoundFetcher *fetcher): @@ -281,6 +294,7 @@ public: m_device(NULL), m_context(NULL), m_next_id(1), + m_fade_delay(0), m_is_initialized(false) { ALCenum error = ALC_NO_ERROR; @@ -349,6 +363,11 @@ public: infostream<<"Audio: Deinitialized."< >::iterator i = @@ -515,6 +534,7 @@ public: addBuffer(name, buf); return false; } + bool loadSoundData(const std::string &name, const std::string &filedata) { @@ -541,7 +561,7 @@ public: alListenerf(AL_GAIN, gain); } - int playSound(const std::string &name, bool loop, float volume) + int playSound(const std::string &name, bool loop, float volume, float fade) { maintain(); if(name == "") @@ -552,8 +572,16 @@ public: < 0) { + handle = playSoundRaw(buf, loop, 0); + fadeSound(handle, fade, volume); + } else { + handle = playSoundRaw(buf, loop, volume); + } + return handle; } + int playSoundAt(const std::string &name, bool loop, float volume, v3f pos) { maintain(); @@ -567,16 +595,55 @@ public: } return playSoundRawAt(buf, loop, volume, pos); } + void stopSound(int sound) { maintain(); deleteSound(sound); } + + void fadeSound(int soundid, float step, float gain) + { + m_sounds_fading[soundid] = FadeState(step, getSoundGain(soundid), gain); + } + + void doFades(float dtime) + { + m_fade_delay += dtime; + + if (m_fade_delay < 0.1f) + return; + + float chkGain = 0; + for (UNORDERED_MAP::iterator i = m_sounds_fading.begin(); + i != m_sounds_fading.end();) { + if (i->second.step < 0.f) + chkGain = -(i->second.current_gain); + else + chkGain = i->second.current_gain; + + if (chkGain < i->second.target_gain) { + i->second.current_gain += (i->second.step * m_fade_delay); + i->second.current_gain = rangelim(i->second.current_gain, 0, 1); + + updateSoundGain(i->first, i->second.current_gain); + ++i; + } else { + if (i->second.target_gain <= 0.f) + stopSound(i->first); + + m_sounds_fading.erase(i++); + } + } + m_fade_delay = 0; + } + bool soundExists(int sound) { maintain(); return (m_sounds_playing.count(sound) != 0); } + void updateSoundPosition(int id, v3f pos) { UNORDERED_MAP::iterator i = m_sounds_playing.find(id); @@ -589,6 +656,29 @@ public: alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0); alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0); } + + bool updateSoundGain(int id, float gain) + { + UNORDERED_MAP::iterator i = m_sounds_playing.find(id); + if (i == m_sounds_playing.end()) + return false; + + PlayingSound *sound = i->second; + alSourcef(sound->source_id, AL_GAIN, gain); + return true; + } + + float getSoundGain(int id) + { + UNORDERED_MAP::iterator i = m_sounds_playing.find(id); + if (i == m_sounds_playing.end()) + return 0; + + PlayingSound *sound = i->second; + ALfloat gain; + alGetSourcef(sound->source_id, AL_GAIN, &gain); + return gain; + } }; ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher) -- cgit v1.2.3 From 9a9ae7d65c0112f3b3107547505553a07d7e20c3 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 4 May 2017 07:52:31 +0200 Subject: LINT fix since cloud API merge --- src/remoteplayer.h | 5 +---- src/sky.h | 4 ++-- src/sound.h | 8 ++++---- 3 files changed, 7 insertions(+), 10 deletions(-) (limited to 'src/sound.h') diff --git a/src/remoteplayer.h b/src/remoteplayer.h index 7d46205c5..ee0d625b6 100644 --- a/src/remoteplayer.h +++ b/src/remoteplayer.h @@ -107,10 +107,7 @@ public: m_cloud_params = cloud_params; } - const CloudParams &getCloudParams() const - { - return m_cloud_params; - } + const CloudParams &getCloudParams() const { return m_cloud_params; } bool checkModified() const { return m_dirty || inventory.checkModified(); } diff --git a/src/sky.h b/src/sky.h index c9678a80b..a014a920b 100644 --- a/src/sky.h +++ b/src/sky.h @@ -125,8 +125,8 @@ private: bool m_sunlight_seen; float m_brightness; float m_cloud_brightness; - bool m_clouds_visible; // Whether clouds are disabled due to player underground - bool m_clouds_enabled; // Initialised to true, reset only by set_sky API + bool m_clouds_visible; // Whether clouds are disabled due to player underground + bool m_clouds_enabled; // Initialised to true, reset only by set_sky API bool m_directional_colored_fog; video::SColorf m_bgcolor_bright_f; video::SColorf m_skycolor_bright_f; diff --git a/src/sound.h b/src/sound.h index 7bdb6a26b..76c0d1be4 100644 --- a/src/sound.h +++ b/src/sound.h @@ -65,8 +65,8 @@ public: // sound. If name=="", call should be ignored without error. virtual int playSound(const std::string &name, bool loop, float volume, float fade = 0) = 0; - virtual int playSoundAt(const std::string &name, bool loop, float volume, - v3f pos) = 0; + virtual int playSoundAt( + const std::string &name, bool loop, float volume, v3f pos) = 0; virtual void stopSound(int sound) = 0; virtual bool soundExists(int sound) = 0; virtual void updateSoundPosition(int sound, v3f pos) = 0; @@ -111,8 +111,8 @@ public: void updateSoundPosition(int sound, v3f pos) {} bool updateSoundGain(int id, float gain) { return false; } float getSoundGain(int id) { return 0; } - void step(float dtime) { } - void fadeSound(int sound, float step, float gain) { } + void step(float dtime) {} + void fadeSound(int sound, float step, float gain) {} }; // Global DummySoundManager singleton -- cgit v1.2.3