From 2c1103307fbcfdc3a693ebd05eb43b520c068064 Mon Sep 17 00:00:00 2001 From: Vyacheslav Ivanov Date: Fri, 2 Aug 2024 05:33:32 +0300 Subject: [PATCH] fix: optimization: PVS-Studio warning V813 Passing large objects by const reference avoids unnecessary copying and enhances efficiency. Reported by: PVS-Studio Signed-off-by: Vyacheslav Ivanov --- src/audio/AL/alutil.hpp | 3 ++- src/audio/audio.cpp | 10 +++++----- src/audio/audio.hpp | 10 +++++----- src/content/Content.hpp | 2 +- src/data/dynamic.hpp | 20 ++++++++++---------- src/engine.cpp | 2 +- src/files/WorldRegions.cpp | 8 ++++---- src/files/WorldRegions.hpp | 8 ++++---- src/frontend/hud.cpp | 2 +- src/frontend/hud.hpp | 6 +++--- src/graphics/core/Batch2D.cpp | 12 +++++++----- src/graphics/core/Batch2D.hpp | 14 ++++++++------ src/graphics/core/Batch3D.cpp | 20 ++++++++++---------- src/graphics/core/Batch3D.hpp | 17 ++++++++++++----- src/graphics/core/DrawContext.cpp | 2 +- src/graphics/core/DrawContext.hpp | 2 +- src/graphics/core/LineBatch.hpp | 4 ++-- src/graphics/core/Model.cpp | 8 ++++++-- src/graphics/core/Model.hpp | 8 ++++++-- src/graphics/core/Shader.cpp | 6 +++--- src/graphics/core/Shader.hpp | 6 +++--- src/graphics/render/ModelBatch.cpp | 13 ++++++++----- src/graphics/render/ModelBatch.hpp | 14 +++++++++----- src/graphics/render/WorldRenderer.cpp | 8 +++++--- src/graphics/ui/elements/InventoryView.cpp | 4 ++-- src/graphics/ui/elements/InventoryView.hpp | 4 ++-- src/logic/CommandsInterpreter.cpp | 4 ++-- src/logic/scripting/lua/lua_util.hpp | 16 ++++++++-------- src/maths/aabb.hpp | 2 +- src/objects/Entities.cpp | 8 ++++---- src/objects/Entities.hpp | 14 +++++++------- src/objects/Player.cpp | 4 ++-- src/objects/Player.hpp | 4 ++-- src/objects/rigging.cpp | 6 +++--- src/objects/rigging.hpp | 4 ++-- src/util/stringutil.cpp | 4 ++-- src/util/stringutil.hpp | 4 ++-- src/voxels/Block.hpp | 2 +- src/voxels/Chunks.cpp | 12 ++++++------ src/voxels/Chunks.hpp | 12 ++++++------ src/window/Window.cpp | 4 ++-- src/window/Window.hpp | 4 ++-- 42 files changed, 173 insertions(+), 144 deletions(-) diff --git a/src/audio/AL/alutil.hpp b/src/audio/AL/alutil.hpp index 6ceccda4..9aff6433 100644 --- a/src/audio/AL/alutil.hpp +++ b/src/audio/AL/alutil.hpp @@ -40,7 +40,8 @@ namespace AL { /// @param field enum value /// @param def default value will be returned in case of error /// @return field value or default - inline glm::vec3 getSource3f(uint source, ALenum field, glm::vec3 def={}) { + inline glm::vec3 getSource3f(uint source, ALenum field, const glm::vec3& def= {} + ) { glm::vec3 value = def; if (source == 0) return def; diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index 384a54a9..afd96cb5 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -210,10 +210,10 @@ std::unique_ptr audio::open_stream(std::shared_ptr stream, bo void audio::set_listener( - glm::vec3 position, - glm::vec3 velocity, - glm::vec3 lookAt, - glm::vec3 up + const glm::vec3& position, + const glm::vec3& velocity, + const glm::vec3& lookAt, + const glm::vec3& up ) { backend->setListener(position, velocity, lookAt, up); } @@ -310,7 +310,7 @@ speakerid_t audio::play( speakerid_t audio::play_stream( const fs::path& file, - glm::vec3 position, + const glm::vec3& position, bool relative, float volume, float pitch, diff --git a/src/audio/audio.hpp b/src/audio/audio.hpp index 4656de6f..5e58a854 100644 --- a/src/audio/audio.hpp +++ b/src/audio/audio.hpp @@ -399,10 +399,10 @@ namespace audio { /// @param lookAt point the listener look at /// @param up camera up vector void set_listener( - glm::vec3 position, - glm::vec3 velocity, - glm::vec3 lookAt, - glm::vec3 up + const glm::vec3& position, + const glm::vec3& velocity, + const glm::vec3& lookAt, + const glm::vec3& up ); /// @brief Play 3D sound in the world @@ -457,7 +457,7 @@ namespace audio { /// @return speaker id or 0 speakerid_t play_stream( const fs::path& file, - glm::vec3 position, + const glm::vec3& position, bool relative, float volume, float pitch, diff --git a/src/content/Content.hpp b/src/content/Content.hpp index 16e072b7..7f3a7d75 100644 --- a/src/content/Content.hpp +++ b/src/content/Content.hpp @@ -119,7 +119,7 @@ public: static constexpr size_t MISSING = SIZE_MAX; - void add(std::string name, dynamic::Map_sptr map) { + void add(const std::string& name, dynamic::Map_sptr map) { indices[name] = names.size(); names.push_back(name); savedData->push_back(map); diff --git a/src/data/dynamic.hpp b/src/data/dynamic.hpp index 6299a1c7..ab66bdd8 100644 --- a/src/data/dynamic.hpp +++ b/src/data/dynamic.hpp @@ -118,34 +118,34 @@ namespace dynamic { List_sptr list(const std::string& key) const; void flag(const std::string& key, bool& dst) const; - Map& put(std::string key, std::unique_ptr value) { + Map& put(const std::string& key, std::unique_ptr value) { return put(key, Map_sptr(value.release())); } - Map& put(std::string key, std::unique_ptr value) { + Map& put(const std::string& key, std::unique_ptr value) { return put(key, List_sptr(value.release())); } - Map& put(std::string key, int value) { + Map& put(const std::string& key, int value) { return put(key, Value(static_cast(value))); } - Map& put(std::string key, unsigned int value) { + Map& put(const std::string& key, unsigned int value) { return put(key, Value(static_cast(value))); } - Map& put(std::string key, int64_t value) { + Map& put(const std::string& key, int64_t value) { return put(key, Value(static_cast(value))); } - Map& put(std::string key, uint64_t value) { + Map& put(const std::string& key, uint64_t value) { return put(key, Value(static_cast(value))); } - Map& put(std::string key, float value) { + Map& put(const std::string& key, float value) { return put(key, Value(static_cast(value))); } - Map& put(std::string key, double value) { + Map& put(const std::string& key, double value) { return put(key, Value(static_cast(value))); } - Map& put(std::string key, bool value) { + Map& put(const std::string& key, bool value) { return put(key, Value(static_cast(value))); } - Map& put(std::string key, const char* value) { + Map& put(const std::string& key, const char* value) { return put(key, Value(value)); } Map& put(const std::string& key, const Value& value); diff --git a/src/engine.cpp b/src/engine.cpp index cd88298f..f7f6b0e1 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -55,7 +55,7 @@ static void add_world_generators() { WorldGenerators::addGenerator("core:flat"); } -static void create_channel(Engine* engine, std::string name, NumberSetting& setting) { +static void create_channel(Engine* engine, const std::string& name, NumberSetting& setting) { if (name != "master") { audio::create_channel(name); } diff --git a/src/files/WorldRegions.cpp b/src/files/WorldRegions.cpp index 642453f8..f6bf00b9 100644 --- a/src/files/WorldRegions.cpp +++ b/src/files/WorldRegions.cpp @@ -196,7 +196,7 @@ ubyte* WorldRegions::getData(int x, int z, int layer, uint32_t& size) { return nullptr; } -regfile_ptr WorldRegions::useRegFile(glm::ivec3 coord) { +regfile_ptr WorldRegions::useRegFile(const glm::ivec3& coord) { auto* file = openRegFiles[coord].get(); file->inUse = true; return regfile_ptr(file, ®FilesCv); @@ -208,7 +208,7 @@ void WorldRegions::closeRegFile(glm::ivec3 coord) { } // Marks regfile as used and unmarks when shared_ptr dies -regfile_ptr WorldRegions::getRegFile(glm::ivec3 coord, bool create) { +regfile_ptr WorldRegions::getRegFile(const glm::ivec3& coord, bool create) { { std::lock_guard lock(regFilesMutex); const auto found = openRegFiles.find(coord); @@ -225,7 +225,7 @@ regfile_ptr WorldRegions::getRegFile(glm::ivec3 coord, bool create) { return nullptr; } -regfile_ptr WorldRegions::createRegFile(glm::ivec3 coord) { +regfile_ptr WorldRegions::createRegFile(const glm::ivec3& coord) { fs::path file = layers[coord[2]].folder/getRegionFilename(coord[0], coord[1]); if (!fs::exists(file)) { return nullptr; @@ -352,7 +352,7 @@ static std::unique_ptr write_inventories(Chunk* chunk, uint& datasize) } /// @brief Store chunk data (voxels and lights) in region (existing or new) -void WorldRegions::put(Chunk* chunk, std::vector entitiesData){ +void WorldRegions::put(Chunk* chunk, const std::vector& entitiesData){ assert(chunk != nullptr); if (!chunk->flags.lighted) { return; diff --git a/src/files/WorldRegions.hpp b/src/files/WorldRegions.hpp index f681794d..67c6bb9d 100644 --- a/src/files/WorldRegions.hpp +++ b/src/files/WorldRegions.hpp @@ -149,10 +149,10 @@ class WorldRegions { ubyte* getData(int x, int z, int layer, uint32_t& size); - regfile_ptr getRegFile(glm::ivec3 coord, bool create=true); + regfile_ptr getRegFile(const glm::ivec3& coord, bool create=true); void closeRegFile(glm::ivec3 coord); - regfile_ptr useRegFile(glm::ivec3 coord); - regfile_ptr createRegFile(glm::ivec3 coord); + regfile_ptr useRegFile(const glm::ivec3& coord); + regfile_ptr createRegFile(const glm::ivec3& coord); fs::path getRegionFilename(int x, int y) const; @@ -172,7 +172,7 @@ public: ~WorldRegions(); /// @brief Put all chunk data to regions - void put(Chunk* chunk, std::vector entitiesData); + void put(Chunk* chunk, const std::vector& entitiesData); /// @brief Store data in specified region /// @param x chunk.x diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index e4a83052..2983f1af 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -314,7 +314,7 @@ void Hud::openInventory() { } void Hud::openInventory( - glm::ivec3 block, + const glm::ivec3& block, UiDocument* doc, std::shared_ptr blockinv, bool playerInventory diff --git a/src/frontend/hud.hpp b/src/frontend/hud.hpp index a2a778ed..e296d9ac 100644 --- a/src/frontend/hud.hpp +++ b/src/frontend/hud.hpp @@ -140,12 +140,12 @@ public: /// @brief Show block inventory in inventory-mode /// @param block block position /// @param doc block ui layout - /// @param blockInv block inventory + /// @param blockinv block inventory /// @param playerInventory show player inventory too void openInventory( - glm::ivec3 block, + const glm::ivec3& block, UiDocument* doc, - std::shared_ptr blockInv, + std::shared_ptr blockinv, bool playerInventory ); diff --git a/src/graphics/core/Batch2D.cpp b/src/graphics/core/Batch2D.cpp index 387fa4da..c3521b76 100644 --- a/src/graphics/core/Batch2D.cpp +++ b/src/graphics/core/Batch2D.cpp @@ -59,8 +59,8 @@ void Batch2D::vertex( buffer[index++] = a; } void Batch2D::vertex( - glm::vec2 point, - glm::vec2 uvpoint, + const glm::vec2& point, + const glm::vec2& uvpoint, float r, float g, float b, float a ) { buffer[index++] = point.x; @@ -138,7 +138,7 @@ void Batch2D::rect( UVRegion region, bool flippedX, bool flippedY, - glm::vec4 tint + const glm::vec4& tint ) { if (index + 6*B2D_VERTEX_SIZE >= capacity) { flush(); @@ -322,11 +322,13 @@ void Batch2D::rect( vertex(v1, glm::vec2(0, 0), r2,g2,b2,1.0f); } -void Batch2D::sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint){ +void Batch2D::sprite(float x, float y, float w, float h, const UVRegion& region, + const glm::vec4& tint){ rect(x, y, w, h, region.u1, region.v1, region.u2-region.u1, region.v2-region.v1, tint.r, tint.g, tint.b, tint.a); } -void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index, glm::vec4 tint){ +void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index, + const glm::vec4& tint){ float scale = 1.0f / (float)atlasRes; float u = (index % atlasRes) * scale; float v = 1.0f - ((index / atlasRes) * scale) - scale; diff --git a/src/graphics/core/Batch2D.hpp b/src/graphics/core/Batch2D.hpp index ed8f9f70..1e72a65f 100644 --- a/src/graphics/core/Batch2D.hpp +++ b/src/graphics/core/Batch2D.hpp @@ -31,8 +31,8 @@ class Batch2D : public Flushable { ); void vertex( - glm::vec2 point, - glm::vec2 uvpoint, + const glm::vec2& point, + const glm::vec2& uvpoint, float r, float g, float b, float a ); @@ -44,11 +44,13 @@ public: void texture(Texture* texture); void untexture(); void setRegion(UVRegion region); - void sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint); - void sprite(float x, float y, float w, float h, int atlasRes, int index, glm::vec4 tint); + void sprite(float x, float y, float w, float h, const UVRegion& region, + const glm::vec4& tint); + void sprite(float x, float y, float w, float h, int atlasRes, int index, + const glm::vec4& tint); void point(float x, float y, float r, float g, float b, float a); - inline void setColor(glm::vec4 color) { + inline void setColor(const glm::vec4& color) { this->color = color; } inline glm::vec4 getColor() const { @@ -69,7 +71,7 @@ public: float ox, float oy, float angle, UVRegion region, bool flippedX, bool flippedY, - glm::vec4 tint + const glm::vec4& tint ); void rect(float x, float y, float w, float h); diff --git a/src/graphics/core/Batch3D.cpp b/src/graphics/core/Batch3D.cpp index c25d6f0b..979347c6 100644 --- a/src/graphics/core/Batch3D.cpp +++ b/src/graphics/core/Batch3D.cpp @@ -51,7 +51,7 @@ void Batch3D::vertex( buffer[index++] = a; } void Batch3D::vertex( - glm::vec3 coord, float u, float v, + const glm::vec3& coord, float u, float v, float r, float g, float b, float a ) { buffer[index++] = coord.x; @@ -65,8 +65,8 @@ void Batch3D::vertex( buffer[index++] = a; } void Batch3D::vertex( - glm::vec3 point, - glm::vec2 uvpoint, + const glm::vec3& point, + const glm::vec2& uvpoint, float r, float g, float b, float a ) { buffer[index++] = point.x; @@ -118,12 +118,12 @@ void Batch3D::texture(Texture* new_texture){ } void Batch3D::sprite( - glm::vec3 pos, - glm::vec3 up, - glm::vec3 right, + const glm::vec3& pos, + const glm::vec3& up, + const glm::vec3& right, float w, float h, - const UVRegion& uv, - glm::vec4 color + const UVRegion& uv, + const glm::vec4& color ){ const float r = color.r; const float g = color.g; @@ -245,11 +245,11 @@ void Batch3D::blockCube( cube((1.0f - size) * -0.5f, size, texfaces, tint, shading); } -void Batch3D::point(glm::vec3 coord, glm::vec2 uv, glm::vec4 tint) { +void Batch3D::point(const glm::vec3& coord, const glm::vec2& uv, const glm::vec4& tint) { vertex(coord, uv, tint.r, tint.g, tint.b, tint.a); } -void Batch3D::point(glm::vec3 coord, glm::vec4 tint) { +void Batch3D::point(const glm::vec3& coord, const glm::vec4& tint) { point(coord, glm::vec2(), tint); } diff --git a/src/graphics/core/Batch3D.hpp b/src/graphics/core/Batch3D.hpp index 79b17697..93bd0b64 100644 --- a/src/graphics/core/Batch3D.hpp +++ b/src/graphics/core/Batch3D.hpp @@ -27,12 +27,13 @@ class Batch3D : public Flushable { float r, float g, float b, float a ); void vertex( - glm::vec3 coord, + const glm::vec3& coord, float u, float v, float r, float g, float b, float a ); void vertex( - glm::vec3 point, glm::vec2 uvpoint, + const glm::vec3& point, + const glm::vec2& uvpoint, float r, float g, float b, float a ); void face( @@ -49,12 +50,18 @@ public: void begin(); void texture(Texture* texture); - void sprite(glm::vec3 pos, glm::vec3 up, glm::vec3 right, float w, float h, const UVRegion& uv, glm::vec4 tint); + void sprite( + const glm::vec3& pos, + const glm::vec3& up, + const glm::vec3& right, float w, float h, const UVRegion& uv, + const glm::vec4& color + ); void xSprite(float w, float h, const UVRegion& uv, const glm::vec4 tint, bool shading=true); void cube(const glm::vec3 coords, const glm::vec3 size, const UVRegion(&texfaces)[6], const glm::vec4 tint, bool shading=true); void blockCube(const glm::vec3 size, const UVRegion(&texfaces)[6], const glm::vec4 tint, bool shading=true); - void point(glm::vec3 pos, glm::vec2 uv, glm::vec4 tint); - void point(glm::vec3 pos, glm::vec4 tint); + void point( + const glm::vec3& coord, const glm::vec2& uv, const glm::vec4& tint); + void point(const glm::vec3& coord, const glm::vec4& tint); void flush() override; void flushPoints(); }; diff --git a/src/graphics/core/DrawContext.cpp b/src/graphics/core/DrawContext.cpp index 68b31125..0f07db3d 100644 --- a/src/graphics/core/DrawContext.cpp +++ b/src/graphics/core/DrawContext.cpp @@ -148,7 +148,7 @@ void DrawContext::setBlendMode(BlendMode mode) { set_blend_mode(mode); } -void DrawContext::setScissors(glm::vec4 area) { +void DrawContext::setScissors(const glm::vec4& area) { Window::pushScissor(area); scissorsCount++; } diff --git a/src/graphics/core/DrawContext.hpp b/src/graphics/core/DrawContext.hpp index ea152f20..ffe05a37 100644 --- a/src/graphics/core/DrawContext.hpp +++ b/src/graphics/core/DrawContext.hpp @@ -35,7 +35,7 @@ public: void setDepthTest(bool flag); void setCullFace(bool flag); void setBlendMode(BlendMode mode); - void setScissors(glm::vec4 area); + void setScissors(const glm::vec4& area); void setLineWidth(float width); }; diff --git a/src/graphics/core/LineBatch.hpp b/src/graphics/core/LineBatch.hpp index a31b827d..b3dc689a 100644 --- a/src/graphics/core/LineBatch.hpp +++ b/src/graphics/core/LineBatch.hpp @@ -18,7 +18,7 @@ public: LineBatch(size_t capacity=4096); ~LineBatch(); - inline void line(const glm::vec3 a, const glm::vec3 b, const glm::vec4 color) { + inline void line(const glm::vec3 &a, const glm::vec3 &b, const glm::vec4 &color) { line(a.x, a.y, a.z, b.x, b.y, b.z, color.r, color.g, color.b, color.a); } void line(float x1, float y1, float z1, float x2, float y2, float z2, @@ -26,7 +26,7 @@ public: void box(float x, float y, float z, float w, float h, float d, float r, float g, float b, float a); - inline void box(glm::vec3 xyz, glm::vec3 whd, glm::vec4 rgba) { + inline void box(const glm::vec3 &xyz, const glm::vec3 &whd, const glm::vec4 &rgba) { box(xyz.x, xyz.y, xyz.z, whd.x, whd.y, whd.z, rgba.r, rgba.g, rgba.b, rgba.a); } diff --git a/src/graphics/core/Model.cpp b/src/graphics/core/Model.cpp index 78e15354..ee58fefc 100644 --- a/src/graphics/core/Model.cpp +++ b/src/graphics/core/Model.cpp @@ -8,7 +8,11 @@ inline constexpr glm::vec3 X(1, 0, 0); inline constexpr glm::vec3 Y(0, 1, 0); inline constexpr glm::vec3 Z(0, 0, 1); -void Mesh::addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm) { +void Mesh::addPlane( + const glm::vec3 &pos, + const glm::vec3 &right, + const glm::vec3 &up, + const glm::vec3 &norm) { vertices.push_back({pos-right-up, {0,0}, norm}); vertices.push_back({pos+right-up, {1,0}, norm}); vertices.push_back({pos+right+up, {1,1}, norm}); @@ -18,7 +22,7 @@ void Mesh::addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm vertices.push_back({pos-right+up, {0,1}, norm}); } -void Mesh::addBox(glm::vec3 pos, glm::vec3 size) { +void Mesh::addBox(const glm::vec3 &pos, const glm::vec3 &size) { addPlane(pos+Z*size, X*size, Y*size, Z); addPlane(pos-Z*size, -X*size, Y*size, -Z); diff --git a/src/graphics/core/Model.hpp b/src/graphics/core/Model.hpp index 03d00826..e7cae9fd 100644 --- a/src/graphics/core/Model.hpp +++ b/src/graphics/core/Model.hpp @@ -16,8 +16,12 @@ namespace model { std::string texture; std::vector vertices; - void addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm); - void addBox(glm::vec3 pos, glm::vec3 size); + void addPlane( + const glm::vec3& pos, + const glm::vec3& right, + const glm::vec3& up, + const glm::vec3& norm); + void addBox(const glm::vec3& pos, const glm::vec3& size); }; struct Model { diff --git a/src/graphics/core/Shader.cpp b/src/graphics/core/Shader.cpp index 7f551fb5..5917e834 100644 --- a/src/graphics/core/Shader.cpp +++ b/src/graphics/core/Shader.cpp @@ -54,11 +54,11 @@ void Shader::uniform2f(const std::string& name, float x, float y){ glUniform2f(getUniformLocation(name), x, y); } -void Shader::uniform2f(const std::string& name, glm::vec2 xy){ +void Shader::uniform2f(const std::string& name, const glm::vec2& xy){ glUniform2f(getUniformLocation(name), xy.x, xy.y); } -void Shader::uniform2i(const std::string& name, glm::ivec2 xy){ +void Shader::uniform2i(const std::string& name, const glm::ivec2& xy){ glUniform2i(getUniformLocation(name), xy.x, xy.y); } @@ -66,7 +66,7 @@ void Shader::uniform3f(const std::string& name, float x, float y, float z){ glUniform3f(getUniformLocation(name), x,y,z); } -void Shader::uniform3f(const std::string& name, glm::vec3 xyz){ +void Shader::uniform3f(const std::string& name, const glm::vec3& xyz){ glUniform3f(getUniformLocation(name), xyz.x, xyz.y, xyz.z); } diff --git a/src/graphics/core/Shader.hpp b/src/graphics/core/Shader.hpp index 25d182a5..2f5d8b89 100644 --- a/src/graphics/core/Shader.hpp +++ b/src/graphics/core/Shader.hpp @@ -26,10 +26,10 @@ public: void uniform1i(const std::string& name, int x); void uniform1f(const std::string& name, float x); void uniform2f(const std::string& name, float x, float y); - void uniform2f(const std::string& name, glm::vec2 xy); - void uniform2i(const std::string& name, glm::ivec2 xy); + void uniform2f(const std::string& name, const glm::vec2& xy); + void uniform2i(const std::string& name, const glm::ivec2& xy); void uniform3f(const std::string& name, float x, float y, float z); - void uniform3f(const std::string& name, glm::vec3 xyz); + void uniform3f(const std::string& name, const glm::vec3& xyz); /// @brief Create shader program using vertex and fragment shaders source. /// @param vertexFile vertex shader file name diff --git a/src/graphics/render/ModelBatch.cpp b/src/graphics/render/ModelBatch.cpp index 468fcbd5..fd9d24c5 100644 --- a/src/graphics/render/ModelBatch.cpp +++ b/src/graphics/render/ModelBatch.cpp @@ -35,7 +35,7 @@ struct DecomposedMat4 { glm::vec4 perspective; }; -static glm::mat4 extract_rotation(glm::mat4 matrix) { +static glm::mat4 extract_rotation(const glm::mat4& matrix) { DecomposedMat4 decomposed = {}; glm::quat rotation; glm::decompose( @@ -66,8 +66,10 @@ ModelBatch::ModelBatch(size_t capacity, Assets* assets, Chunks* chunks) ModelBatch::~ModelBatch() = default; -void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix, - const glm::mat3& rotation, glm::vec3 tint, +void ModelBatch::draw(const model::Mesh& mesh, + const glm::mat4& matrix, + const glm::mat3& rotation, + const glm::vec3& tint, const texture_names_map* varTextures) { glm::vec3 gpos = matrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); light_t light = chunks->getLight(floor(gpos.x), floor(gpos.y), floor(gpos.z)); @@ -94,8 +96,9 @@ void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix, } } -void ModelBatch::draw(glm::mat4 matrix, - glm::vec3 tint, +void ModelBatch::draw( + const glm::mat4& matrix, + const glm::vec3& tint, const model::Model* model, const texture_names_map* varTextures) { for (const auto& mesh : model->meshes) { diff --git a/src/graphics/render/ModelBatch.hpp b/src/graphics/render/ModelBatch.hpp index 69608ddc..c34f9369 100644 --- a/src/graphics/render/ModelBatch.hpp +++ b/src/graphics/render/ModelBatch.hpp @@ -37,7 +37,10 @@ class ModelBatch { static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f}; inline void vertex( - glm::vec3 pos, glm::vec2 uv, glm::vec4 light, glm::vec3 tint + const glm::vec3& pos, + const glm::vec2& uv, + const glm::vec4& light, + const glm::vec3& tint ) { float* buffer = this->buffer.get(); buffer[index++] = pos.x; @@ -64,8 +67,8 @@ class ModelBatch { void draw(const model::Mesh& mesh, const glm::mat4& matrix, - const glm::mat3& rotation, - glm::vec3 tint, + const glm::mat3& rotation, + const glm::vec3& tint, const texture_names_map* varTextures); void setTexture(const std::string& name, const texture_names_map* varTextures); @@ -84,8 +87,9 @@ public: ModelBatch(size_t capacity, Assets* assets, Chunks* chunks); ~ModelBatch(); - void draw(glm::mat4 matrix, - glm::vec3 tint, + void draw( + const glm::mat4& matrix, + const glm::vec3& tint, const model::Model* model, const texture_names_map* varTextures); void render(); diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index cbe57965..cdc6c26e 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -230,10 +230,12 @@ void WorldRenderer::renderBlockSelection() { : block->hitboxes; lineBatch->lineWidth(2.0f); + constexpr auto boxOffset = glm::vec3(0.02); + constexpr auto boxColor = glm::vec4(0.f, 0.f, 0.f, 0.5f); for (auto& hitbox: hitboxes) { const glm::vec3 center = glm::vec3(pos) + hitbox.center(); const glm::vec3 size = hitbox.size(); - lineBatch->box(center, size + glm::vec3(0.02), glm::vec4(0.f, 0.f, 0.f, 0.5f)); + lineBatch->box(center, size + boxOffset, boxColor); if (player->debug) { lineBatch->line(point, point+norm*0.5f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f)); } @@ -340,12 +342,12 @@ void WorldRenderer::draw( ctx.setDepthTest(true); ctx.setCullFace(true); renderLevel(ctx, camera, settings, delta, pause); - // Debug lines + if (hudVisible){ renderLines(camera, linesShader, ctx); } } - + // Debug lines if (hudVisible && player->debug) { renderDebugLines(wctx, camera, linesShader); } diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index 14b7a59b..8ae93eab 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -52,7 +52,7 @@ InventoryBuilder::InventoryBuilder() { void InventoryBuilder::addGrid( int cols, int count, - glm::vec2 pos, + const glm::vec2 &pos, int padding, bool addpanel, const SlotLayout& slotLayout @@ -381,7 +381,7 @@ void InventoryView::setPos(glm::vec2 pos) { Container::setPos(pos - origin); } -void InventoryView::setOrigin(glm::vec2 origin) { +void InventoryView::setOrigin(const glm::vec2 &origin) { this->origin = origin; } diff --git a/src/graphics/ui/elements/InventoryView.hpp b/src/graphics/ui/elements/InventoryView.hpp index 476b4d99..9aafd51b 100644 --- a/src/graphics/ui/elements/InventoryView.hpp +++ b/src/graphics/ui/elements/InventoryView.hpp @@ -93,7 +93,7 @@ namespace gui { virtual void setPos(glm::vec2 pos) override; - void setOrigin(glm::vec2 origin); + void setOrigin(const glm::vec2 &origin); glm::vec2 getOrigin() const; void setSelected(int index); @@ -130,7 +130,7 @@ namespace gui { /// @param slotLayout slot settings (index and position are ignored) void addGrid( int cols, int count, - glm::vec2 pos, + const glm::vec2 &pos, int padding, bool addpanel, const SlotLayout& slotLayout diff --git a/src/logic/CommandsInterpreter.cpp b/src/logic/CommandsInterpreter.cpp index 85a082e7..b93dbbac 100644 --- a/src/logic/CommandsInterpreter.cpp +++ b/src/logic/CommandsInterpreter.cpp @@ -255,8 +255,8 @@ public: } dynamic::Value applyRelative( - Argument* arg, - dynamic::Value value, + Argument* arg, + const dynamic::Value& value, const dynamic::Value& origin ) { if (origin.index() == 0) { diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 973dfc30..7bfb8fea 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -151,20 +151,20 @@ namespace lua { return 3; } - inline int pushivec3_stack(lua::State* L, glm::ivec3 vec) { + inline int pushivec3_stack(lua::State* L, const glm::ivec3 &vec) { pushinteger(L, vec.x); pushinteger(L, vec.y); pushinteger(L, vec.z); return 3; } - inline int pushvec3_stack(lua::State* L, glm::vec3 vec) { + inline int pushvec3_stack(lua::State* L, const glm::vec3 &vec) { pushnumber(L, vec.x); pushnumber(L, vec.y); pushnumber(L, vec.z); return 3; } - inline int pushvec4_stack(lua::State* L, glm::vec4 vec) { + inline int pushvec4_stack(lua::State* L, const glm::vec4 &vec) { pushnumber(L, vec.x); pushnumber(L, vec.y); pushnumber(L, vec.z); @@ -179,15 +179,15 @@ namespace lua { lua_pushvalue(L, idx); return 1; } - inline int pushvec2(lua::State* L, glm::vec2 vec) { + inline int pushvec2(lua::State* L, const glm::vec2 &vec) { return pushvec(L, vec); } - inline int pushvec3(lua::State* L, glm::vec3 vec) { + inline int pushvec3(lua::State* L, const glm::vec3 &vec) { return pushvec(L, vec); } - inline int pushvec4(lua::State* L, glm::vec4 vec) { + inline int pushvec4(lua::State* L, const glm::vec4 &vec) { return pushvec(L, vec); } inline int pushcolor(lua::State* L, glm::vec4 vec) { @@ -210,7 +210,7 @@ namespace lua { } return 1; } - inline int pushmat4(lua::State* L, glm::mat4 matrix) { + inline int pushmat4(lua::State* L, const glm::mat4 &matrix) { createtable(L, 16, 0); for (uint y = 0; y < 4; y++) { for (uint x = 0; x < 4; x++) { @@ -222,7 +222,7 @@ namespace lua { return 1; } /// @brief pushes matrix table to the stack and updates it with glm matrix - inline int setmat4(lua::State* L, int idx, glm::mat4 matrix) { + inline int setmat4(lua::State* L, int idx, const glm::mat4 &matrix) { pushvalue(L, idx); for (uint y = 0; y < 4; y++) { for (uint x = 0; x < 4; x++) { diff --git a/src/maths/aabb.hpp b/src/maths/aabb.hpp index 1a86338c..57679394 100644 --- a/src/maths/aabb.hpp +++ b/src/maths/aabb.hpp @@ -70,7 +70,7 @@ struct AABB { b = end; } - inline void addPoint(glm::vec3 p) { + inline void addPoint(const glm::vec3& p) { a = glm::min(a, p); b = glm::max(b, p); } diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index b7042d8a..bd4b664e 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -186,7 +186,7 @@ void Entities::loadEntity(const dynamic::Map_sptr& map) { spawn(def, {}, nullptr, map, uid); } -void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) { +void Entities::loadEntity(const dynamic::Map_sptr& map, const Entity &entity) { auto& transform = entity.getTransform(); auto& body = entity.getRigidbody(); auto& skeleton = entity.getSkeleton(); @@ -228,7 +228,7 @@ void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) { } std::optional Entities::rayCast( - glm::vec3 start, glm::vec3 dir, float maxDistance, entityid_t ignore + const glm::vec3 &start, const glm::vec3 &dir, float maxDistance, entityid_t ignore ) { Ray ray(start, dir); auto view = registry.view(); @@ -547,7 +547,7 @@ bool Entities::hasBlockingInside(AABB aabb) { return false; } -std::vector Entities::getAllInside(AABB aabb) { +std::vector Entities::getAllInside(const AABB &aabb) { std::vector collected; auto view = registry.view(); for (auto [entity, transform] : view.each()) { @@ -564,7 +564,7 @@ std::vector Entities::getAllInside(AABB aabb) { return collected; } -std::vector Entities::getAllInRadius(glm::vec3 center, float radius) { +std::vector Entities::getAllInRadius(const glm::vec3 ¢er, float radius) { std::vector collected; auto view = registry.view(); for (auto [entity, transform] : view.each()) { diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 65bad419..e6d55cee 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -49,19 +49,19 @@ struct Transform { void refresh(); - inline void setRot(glm::mat3 m) { + inline void setRot(const glm::mat3 &m) { rot = m; dirty = true; } - inline void setSize(glm::vec3 v) { + inline void setSize(const glm::vec3 &v) { if (glm::distance2(displaySize, v) >= 0.00001f) { dirty = true; } size = v; } - inline void setPos(glm::vec3 v) { + inline void setPos(const glm::vec3 &v) { if (glm::distance2(displayPos, v) >= 0.0001f) { dirty = true; } @@ -212,15 +212,15 @@ public: /// @param ignore Ignored entity ID /// @return An optional structure containing entity, normal and distance std::optional rayCast( - glm::vec3 start, glm::vec3 dir, float maxDistance, entityid_t ignore=-1); + const glm::vec3 &start, const glm::vec3 &dir, float maxDistance, entityid_t ignore=-1); void loadEntities(dynamic::Map_sptr map); void loadEntity(const dynamic::Map_sptr& map); - void loadEntity(const dynamic::Map_sptr& map, Entity entity); + void loadEntity(const dynamic::Map_sptr& map, const Entity &entity); void onSave(const Entity& entity); bool hasBlockingInside(AABB aabb); - std::vector getAllInside(AABB aabb); - std::vector getAllInRadius(glm::vec3 center, float radius); + std::vector getAllInside(const AABB &aabb); + std::vector getAllInRadius(const glm::vec3 ¢er, float radius); void despawn(entityid_t id); dynamic::Value serialize(const Entity& entity); diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index ac924b7e..de71f6e0 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -169,7 +169,7 @@ void Player::postUpdate() { glm::mat4(1.0f), glm::radians(cam.y), glm::vec3(1, 0, 0)); } -void Player::teleport(glm::vec3 position) { +void Player::teleport(const glm::vec3 &position) { this->position = position; if (auto hitbox = getHitbox()) { hitbox->position = position; @@ -239,7 +239,7 @@ std::shared_ptr Player::getInventory() const { return inventory; } -void Player::setSpawnPoint(glm::vec3 spawnpoint) { +void Player::setSpawnPoint(const glm::vec3 &spawnpoint) { this->spawnpoint = spawnpoint; } diff --git a/src/objects/Player.hpp b/src/objects/Player.hpp index 7fa62c5a..0db3e971 100644 --- a/src/objects/Player.hpp +++ b/src/objects/Player.hpp @@ -64,7 +64,7 @@ public: std::shared_ptr inv, entityid_t eid); ~Player(); - void teleport(glm::vec3 position); + void teleport(const glm::vec3 &position); void updateEntity(); void updateInput(PlayerInput& input, float delta); void updateSelectedEntity(); @@ -96,7 +96,7 @@ public: Hitbox* getHitbox(); - void setSpawnPoint(glm::vec3 point); + void setSpawnPoint(const glm::vec3 &spawnpoint); glm::vec3 getSpawnPoint() const; std::unique_ptr serialize() const override; diff --git a/src/objects/rigging.cpp b/src/objects/rigging.cpp index 67b14c18..f3f89f19 100644 --- a/src/objects/rigging.cpp +++ b/src/objects/rigging.cpp @@ -68,8 +68,8 @@ SkeletonConfig::SkeletonConfig(const std::string& name, std::unique_ptr ro size_t SkeletonConfig::update( size_t index, Skeleton& skeleton, - Bone* node, - glm::mat4 matrix) const + Bone* node, + const glm::mat4& matrix) const { auto boneMatrix = skeleton.pose.matrices[index]; auto boneOffset = node->getOffset(); @@ -85,7 +85,7 @@ size_t SkeletonConfig::update( return count; } -void SkeletonConfig::update(Skeleton& skeleton, glm::mat4 matrix) const { +void SkeletonConfig::update(Skeleton& skeleton, const glm::mat4& matrix) const { update(0, skeleton, root.get(), matrix); } diff --git a/src/objects/rigging.hpp b/src/objects/rigging.hpp index 6d4469ee..44a7acae 100644 --- a/src/objects/rigging.hpp +++ b/src/objects/rigging.hpp @@ -103,12 +103,12 @@ namespace rigging { size_t index, Skeleton& skeleton, Bone* node, - glm::mat4 matrix) const; + const glm::mat4& matrix) const; public: SkeletonConfig(const std::string& name, std::unique_ptr root, size_t nodesCount); - void update(Skeleton& skeleton, glm::mat4 matrix) const; + void update(Skeleton& skeleton, const glm::mat4& matrix) const; void render( Assets* assets, ModelBatch& batch, diff --git a/src/util/stringutil.cpp b/src/util/stringutil.cpp index bc155c6a..1f09e0f5 100644 --- a/src/util/stringutil.cpp +++ b/src/util/stringutil.cpp @@ -37,7 +37,7 @@ std::string util::quote(const std::string& s) { return escape(s); } -std::wstring util::lfill(std::wstring s, uint length, wchar_t c) { +std::wstring util::lfill(const std::wstring& s, uint length, wchar_t c) { if (s.length() >= length) { return s; } @@ -49,7 +49,7 @@ std::wstring util::lfill(std::wstring s, uint length, wchar_t c) { return ss.str(); } -std::wstring util::rfill(std::wstring s, uint length, wchar_t c) { +std::wstring util::rfill(const std::wstring& s, uint length, wchar_t c) { if (s.length() >= length) { return s; } diff --git a/src/util/stringutil.hpp b/src/util/stringutil.hpp index 273a9264..fbfc3665 100644 --- a/src/util/stringutil.hpp +++ b/src/util/stringutil.hpp @@ -13,8 +13,8 @@ namespace util { /// @brief Function used for error messages std::string quote(const std::string& s); - std::wstring lfill(std::wstring s, uint length, wchar_t c); - std::wstring rfill(std::wstring s, uint length, wchar_t c); + std::wstring lfill(const std::wstring& s, uint length, wchar_t c); + std::wstring rfill(const std::wstring& s, uint length, wchar_t c); uint encode_utf8(uint32_t c, ubyte* bytes); uint32_t decode_utf8(uint& size, const char* bytes); diff --git a/src/voxels/Block.hpp b/src/voxels/Block.hpp index 5c0dcfaa..0d0fb3f6 100644 --- a/src/voxels/Block.hpp +++ b/src/voxels/Block.hpp @@ -48,7 +48,7 @@ struct CoordSystem { void transform(AABB& aabb) const; - inline bool isVectorHasNegatives(glm::ivec3 vec) { + inline bool isVectorHasNegatives(const glm::ivec3& vec) { return (vec.x < 0 || vec.y < 0 || vec.z < 0); } }; diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index 1b8aff43..5909b76d 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -232,13 +232,13 @@ void Chunks::repairSegments(const Block* def, blockstate state, int x, int y, in } } -bool Chunks::checkReplaceability(const Block* def, blockstate state, glm::ivec3 origin, blockid_t ignore) { +bool Chunks::checkReplaceability(const Block* def, blockstate state, const glm::ivec3 &coord, blockid_t ignore) { const auto& rotation = def->rotations.variants[state.rotation]; const auto size = def->size; for (int sy = 0; sy < size.y; sy++) { for (int sz = 0; sz < size.z; sz++) { for (int sx = 0; sx < size.x; sx++) { - auto pos = origin; + auto pos = coord; pos += rotation.axisX * sx; pos += rotation.axisY * sy; pos += rotation.axisZ * sz; @@ -257,7 +257,7 @@ bool Chunks::checkReplaceability(const Block* def, blockstate state, glm::ivec3 } void Chunks::setRotationExtended( - Block* def, blockstate state, glm::ivec3 origin, uint8_t index + Block* def, blockstate state, const glm::ivec3 &origin, uint8_t index ) { auto newstate = state; newstate.rotation = index; @@ -387,8 +387,8 @@ void Chunks::set(int32_t x, int32_t y, int32_t z, uint32_t id, blockstate state) } voxel* Chunks::rayCast( - glm::vec3 start, - glm::vec3 dir, + const glm::vec3 &start, + const glm::vec3 &dir, float maxDist, glm::vec3& end, glm::ivec3& norm, @@ -520,7 +520,7 @@ voxel* Chunks::rayCast( return nullptr; } -glm::vec3 Chunks::rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist) { +glm::vec3 Chunks::rayCastToObstacle(const glm::vec3 &start, const glm::vec3 &dir, float maxDist) { const float px = start.x; const float py = start.y; const float pz = start.z; diff --git a/src/voxels/Chunks.hpp b/src/voxels/Chunks.hpp index 860d3da0..677fd036 100644 --- a/src/voxels/Chunks.hpp +++ b/src/voxels/Chunks.hpp @@ -27,7 +27,7 @@ class Chunks { void eraseSegments(const Block* def, blockstate state, int x, int y, int z); void repairSegments(const Block* def, blockstate state, int x, int y, int z); - void setRotationExtended(Block* def, blockstate state, glm::ivec3 origin, uint8_t rotation); + void setRotationExtended(Block* def, blockstate state, const glm::ivec3 &origin, uint8_t rotation); public: std::vector> chunks; std::vector> chunksSecond; @@ -48,7 +48,7 @@ public: Chunk* getChunkByVoxel(int32_t x, int32_t y, int32_t z); voxel* get(int32_t x, int32_t y, int32_t z) const; - inline voxel* get(glm::ivec3 pos) { + inline voxel* get(const glm::ivec3 &pos) { return get(pos.x, pos.y, pos.z); } @@ -68,20 +68,20 @@ public: /// @param state the block state /// @param coord position of the zone start /// @param ignore ignored block id (will be counted as replaceable) - bool checkReplaceability(const Block* def, blockstate state, glm::ivec3 coord, blockid_t ignore=0); + bool checkReplaceability(const Block* def, blockstate state, const glm::ivec3 &coord, blockid_t ignore=0); void setRotation(int32_t x, int32_t y, int32_t z, uint8_t rotation); voxel* rayCast( - glm::vec3 start, - glm::vec3 dir, + const glm::vec3 &start, + const glm::vec3 &dir, float maxLength, glm::vec3& end, glm::ivec3& norm, glm::ivec3& iend ); - glm::vec3 rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist); + glm::vec3 rayCastToObstacle(const glm::vec3 &start, const glm::vec3 &dir, float maxDist); const AABB* isObstacleAt(float x, float y, float z); bool isSolidBlock(int32_t x, int32_t y, int32_t z); diff --git a/src/window/Window.cpp b/src/window/Window.cpp index bb266afe..7bfb8fcc 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -205,11 +205,11 @@ void Window::clearDepth() { glClear(GL_DEPTH_BUFFER_BIT); } -void Window::setBgColor(glm::vec3 color) { +void Window::setBgColor(const glm::vec3 &color) { glClearColor(color.r, color.g, color.b, 1.0f); } -void Window::setBgColor(glm::vec4 color) { +void Window::setBgColor(const glm::vec4 &color) { glClearColor(color.r, color.g, color.b, color.a); } diff --git a/src/window/Window.hpp b/src/window/Window.hpp index 44e6e7b6..ba60658d 100644 --- a/src/window/Window.hpp +++ b/src/window/Window.hpp @@ -49,8 +49,8 @@ public: static void clear(); static void clearDepth(); - static void setBgColor(glm::vec3 color); - static void setBgColor(glm::vec4 color); + static void setBgColor(const glm::vec3 &color); + static void setBgColor(const glm::vec4 &color); static double time(); static const char* getClipboardText(); static void setClipboardText(const char* text);