diff --git a/.clang-tidy b/.clang-tidy index 8b148ce..8a6b5e0 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,22 +1,26 @@ --- -# Linux-style naming conventions for C++ projects +# C++ Core Guidelines style (CppCoreGuidelines) Checks: > + cppcoreguidelines-*, readability-identifier-naming, - readability-braces-around-statements, - readability-function-size, - modernize-use-nullptr, - modernize-use-override, + readability-*, + modernize-*, performance-*, - -modernize-use-trailing-return-type + bugprone-*, + -modernize-use-trailing-return-type, + -readability-magic-numbers, + -cppcoreguidelines-avoid-magic-numbers, + -cppcoreguidelines-pro-bounds-pointer-arithmetic, + -cppcoreguidelines-pro-type-reinterpret-cast CheckOptions: - # Classes and structs use CamelCase (C++ convention) + # C++ Core Guidelines: Types use PascalCase - { key: readability-identifier-naming.ClassCase, value: CamelCase } - { key: readability-identifier-naming.StructCase, value: CamelCase } - { key: readability-identifier-naming.EnumCase, value: CamelCase } - { key: readability-identifier-naming.UnionCase, value: CamelCase } - # Functions and methods use snake_case (Linux style) + # C++ Core Guidelines: Functions use snake_case (STL style) - { key: readability-identifier-naming.FunctionCase, value: lower_case } - { key: readability-identifier-naming.MethodCase, value: lower_case } @@ -38,22 +42,18 @@ CheckOptions: - { key: readability-identifier-naming.MacroDefinitionCase, value: UPPER_CASE } - { key: readability-identifier-naming.EnumConstantCase, value: UPPER_CASE } - # Global variables: snake_case with g_ prefix (optional, can remove prefix) + # Global variables: snake_case (Core Guidelines: avoid globals when possible) - { key: readability-identifier-naming.GlobalVariableCase, value: lower_case } - - { key: readability-identifier-naming.GlobalVariablePrefix, value: 'g_' } # Namespaces: snake_case - { key: readability-identifier-naming.NamespaceCase, value: lower_case } - # Template parameters: CamelCase + # Template parameters: PascalCase (following STL conventions) - { key: readability-identifier-naming.TypeTemplateParameterCase, value: CamelCase } - { key: readability-identifier-naming.ValueTemplateParameterCase, value: lower_case } - # Typedef and type aliases: snake_case_t suffix (Linux style) - - { key: readability-identifier-naming.TypedefCase, value: lower_case } - - { key: readability-identifier-naming.TypedefSuffix, value: '_t' } - - { key: readability-identifier-naming.TypeAliasCase, value: lower_case } - - { key: readability-identifier-naming.TypeAliasSuffix, value: '_t' } + # Type aliases: PascalCase (modern C++ style with 'using') + - { key: readability-identifier-naming.TypeAliasCase, value: CamelCase } # Function size limits - { key: readability-function-size.LineThreshold, value: 100 } diff --git a/src/buffer.cpp b/src/buffer.cpp index 9f59f08..d84f1c7 100644 --- a/src/buffer.cpp +++ b/src/buffer.cpp @@ -39,11 +39,11 @@ Buffer::~Buffer() glDeleteBuffers(1, &ebo_); } -void Buffer::Draw(const Shader& shader) const +void Buffer::draw(const Shader& shader) const { glBindVertexArray(vao_); glBindBuffer(GL_ARRAY_BUFFER, vbo_); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_); - shader.SetupAttribs(); + shader.setup_attribs(); glDrawElements(GL_TRIANGLES, index_count_, GL_UNSIGNED_SHORT, nullptr); } diff --git a/src/buffer.h b/src/buffer.h index c0eea64..d524df3 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -14,7 +14,7 @@ public: const std::vector& indices); ~Buffer(); - void Draw(const Shader& shader) const; + void draw(const Shader& shader) const; private: uint32_t vao_, vbo_, ebo_; diff --git a/src/camera.cpp b/src/camera.cpp index 093b04e..032df4d 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -6,14 +6,14 @@ Camera::Camera() : projection_(glm::mat4(1.0f)) , viewport_(0, 0, 800, 600) - , clearColor_(0.0f, 0.0f, 0.0f) + , clear_color_(0.0f, 0.0f, 0.0f) { } void Camera::prepare() { // Set projection matrix - State::projectionMatrix = projection_; + State::projection_matrix_ = projection_; // Calculate view matrix // For a camera, we need the inverse transformation: @@ -29,13 +29,13 @@ void Camera::prepare() // Inverse translation (translate in opposite direction) view = glm::translate(view, -position_); - State::viewMatrix = view; + State::view_matrix_ = view; // Set viewport glViewport(viewport_.x, viewport_.y, viewport_.z, viewport_.w); glScissor(viewport_.x, viewport_.y, viewport_.z, viewport_.w); // Set clear color and clear buffers - glClearColor(clearColor_.r, clearColor_.g, clearColor_.b, 1.0f); + glClearColor(clear_color_.r, clear_color_.g, clear_color_.b, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } diff --git a/src/camera.h b/src/camera.h index 61f2fe5..9fde915 100644 --- a/src/camera.h +++ b/src/camera.h @@ -8,31 +8,31 @@ class Camera : public Entity { public: Camera(); - const glm::mat4& getProjection() const + [[nodiscard]] const glm::mat4& projection() const { return projection_; } - void setProjection(const glm::mat4& proj) + void set_projection(const glm::mat4& proj) { projection_ = proj; } - const glm::ivec4& getViewport() const + [[nodiscard]] const glm::ivec4& viewport() const { return viewport_; } - void setViewport(const glm::ivec4& vp) + void set_viewport(const glm::ivec4& vp) { viewport_ = vp; } - const glm::vec3& getClearColor() const + [[nodiscard]] const glm::vec3& clear_color() const { - return clearColor_; + return clear_color_; } - void setClearColor(const glm::vec3& color) + void set_clear_color(const glm::vec3& color) { - clearColor_ = color; + clear_color_ = color; } void prepare(); @@ -40,7 +40,7 @@ public: private: glm::mat4 projection_; glm::ivec4 viewport_; - glm::vec3 clearColor_; + glm::vec3 clear_color_; }; #endif // CAMERA_H_ diff --git a/src/entity.h b/src/entity.h index 59063d5..e5471bd 100644 --- a/src/entity.h +++ b/src/entity.h @@ -6,44 +6,44 @@ class Entity { public: Entity(); - virtual ~Entity() - { - } + virtual ~Entity() = default; - const glm::vec3& getPosition() const + [[nodiscard]] const glm::vec3& position() const { return position_; } - void setPosition(const glm::vec3& pos) + void set_position(const glm::vec3& pos) { position_ = pos; } - const glm::vec3& getRotation() const + [[nodiscard]] const glm::vec3& rotation() const { return rotation_; } - void setRotation(const glm::vec3& rot) + void set_rotation(const glm::vec3& rot) { rotation_ = rot; } - const glm::vec3& getScale() const + [[nodiscard]] const glm::vec3& scale() const { return scale_; } - void setScale(const glm::vec3& scale) + void set_scale(const glm::vec3& scale) { scale_ = scale; } void move(const glm::vec3& vec); - virtual void update(float deltaTime) + virtual void update(float delta_time) { + // ... } virtual void draw() { + // ... } protected: diff --git a/src/main.cpp b/src/main.cpp index 80f4cf8..6e06698 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -65,11 +65,11 @@ int main() } // Initialize default shader - State::defaultShader = + State::default_shader_ = std::make_shared("data/vertex.glsl", "data/fragment.glsl"); - if (std::strlen(State::defaultShader->getError()) > 0) { + if (std::strlen(State::default_shader_->error()) > 0) { std::cerr << "Failed to initialize shaders: " - << State::defaultShader->getError() << "\n"; + << State::default_shader_->error() << "\n"; } // Create world @@ -77,13 +77,13 @@ int main() // Create camera auto camera = std::make_shared(); - camera->setPosition(glm::vec3(0.0f, 0.0f, 6.0f)); - camera->setProjection(glm::perspective( + camera->set_position(glm::vec3(0.0f, 0.0f, 6.0f)); + camera->set_projection(glm::perspective( glm::radians(45.0f), static_cast(SCREEN_WIDTH) / SCREEN_HEIGHT, 0.1f, 100.0f)); - camera->setViewport(glm::ivec4(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)); - camera->setClearColor(glm::vec3(0.1f, 0.1f, 0.1f)); - world.addEntity(camera); + camera->set_viewport(glm::ivec4(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)); + camera->set_clear_color(glm::vec3(0.1f, 0.1f, 0.1f)); + world.add_entity(camera); // Create triangle mesh std::vector vertices = { @@ -94,18 +94,18 @@ int main() auto buffer = std::make_shared(vertices, indices); auto mesh = std::make_shared(); - mesh->addBuffer(buffer); + mesh->add_buffer(buffer); // Create 9 models in a 3x3 grid std::vector> models; for (int row = 0; row < 3; ++row) { for (int col = 0; col < 3; ++col) { auto model = std::make_shared(mesh); - model->setPosition( + model->set_position( glm::vec3(-3.0f + static_cast(col) * 3.0f, 0.0f, static_cast(-row) * 3.0f)); models.push_back(model); - world.addEntity(model); + world.add_entity(model); } } @@ -124,7 +124,7 @@ int main() // Update rotation for all models for (auto& model : models) { - model->setRotation(glm::vec3( + model->set_rotation(glm::vec3( 0.0f, glm::radians(static_cast(angle)), 0.0f)); } diff --git a/src/mesh.cpp b/src/mesh.cpp index d0c0e7e..b5f6be5 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -3,8 +3,8 @@ #include "shader.h" #include "state.h" -void Mesh::addBuffer(const std::shared_ptr& buffer, - const std::shared_ptr& shader) +void Mesh::add_buffer(const std::shared_ptr& buffer, + const std::shared_ptr& shader) { buffers_.push_back(buffer); shaders_.push_back(shader); @@ -13,21 +13,21 @@ void Mesh::addBuffer(const std::shared_ptr& buffer, void Mesh::draw() { // Calculate MVP matrix - glm::mat4 mvp = - State::projectionMatrix * State::viewMatrix * State::modelMatrix; + glm::mat4 mvp = State::projection_matrix_ * State::view_matrix_ + * State::model_matrix_; // Draw each buffer with its shader for (size_t i = 0; i < buffers_.size(); ++i) { // Use buffer's shader if available, otherwise use default // shader std::shared_ptr shader = - shaders_[i] ? shaders_[i] : State::defaultShader; + shaders_[i] ? shaders_[i] : State::default_shader_; if (shader) { - shader->Use(); - int mvpLoc = shader->getLocation("mvp"); - Shader::setMat4(mvpLoc, mvp); - buffers_[i]->Draw(*shader); + shader->use(); + int mvpLoc = shader->uniform_location("mvp"); + Shader::set_mat4(mvpLoc, mvp); + buffers_[i]->draw(*shader); } } } diff --git a/src/mesh.h b/src/mesh.h index 0efd1ec..6f8ba6c 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -9,20 +9,21 @@ class Shader; class Mesh { public: - Mesh() = default; + Mesh() = default; + ~Mesh() = default; - void addBuffer(const std::shared_ptr& buffer, - const std::shared_ptr& shader = nullptr); + void add_buffer(const std::shared_ptr& buffer, + const std::shared_ptr& shader = nullptr); - size_t getNumBuffers() const + [[nodiscard]] size_t num_buffers() const { return buffers_.size(); } - const std::shared_ptr& getBuffer(size_t index) const + [[nodiscard]] const std::shared_ptr& buffer(size_t index) const { return buffers_[index]; } - std::shared_ptr& getBuffer(size_t index) + [[nodiscard]] std::shared_ptr& buffer(size_t index) { return buffers_[index]; } diff --git a/src/model.cpp b/src/model.cpp index e21d907..e5c631d 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -28,7 +28,7 @@ void Model::draw() model = glm::scale(model, scale_); // Set the model matrix in State - State::modelMatrix = model; + State::model_matrix_ = model; // Draw the mesh mesh_->draw(); diff --git a/src/shader.cpp b/src/shader.cpp index 72fe60b..a761d5d 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -12,9 +12,9 @@ Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath) { - const uint32_t vshader = CompileShader(GL_VERTEX_SHADER, vertexPath); + const uint32_t vshader = compile_shader(GL_VERTEX_SHADER, vertexPath); const uint32_t fshader = - CompileShader(GL_FRAGMENT_SHADER, fragmentPath); + compile_shader(GL_FRAGMENT_SHADER, fragmentPath); if (vshader == 0 || fshader == 0) { program_id_ = 0; @@ -42,13 +42,13 @@ Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath) glDeleteShader(fshader); } -void Shader::Use() const +void Shader::use() const { if (program_id_ != 0) glUseProgram(program_id_); } -void Shader::SetupAttribs() const +void Shader::setup_attribs() const { int loc = glGetAttribLocation(program_id_, "vpos"); if (loc != -1) { @@ -67,42 +67,42 @@ void Shader::SetupAttribs() const } } -int Shader::getLocation(const char* key) const +int Shader::uniform_location(const char* key) const { return glGetUniformLocation(program_id_, key); } -void Shader::setInt(int loc, int value) +void Shader::set_int(int loc, int value) { if (loc != -1) glUniform1i(loc, value); } -void Shader::setFloat(int loc, float value) +void Shader::set_float(int loc, float value) { if (loc != -1) glUniform1f(loc, value); } -void Shader::setVec3(int loc, const glm::vec3& value) +void Shader::set_vec3(int loc, const glm::vec3& value) { if (loc != -1) glUniform3fv(loc, 1, &value[0]); } -void Shader::setVec4(int loc, const glm::vec4& value) +void Shader::set_vec4(int loc, const glm::vec4& value) { if (loc != -1) glUniform4fv(loc, 1, &value[0]); } -void Shader::setMat4(int loc, const glm::mat4& value) +void Shader::set_mat4(int loc, const glm::mat4& value) { if (loc != -1) glUniformMatrix4fv(loc, 1, GL_FALSE, &value[0][0]); } -std::string Shader::ReadShaderFile(const std::string& filename) +std::string Shader::read_shader_file(const std::string& filename) { std::ifstream file(filename); if (!file) { @@ -115,9 +115,9 @@ std::string Shader::ReadShaderFile(const std::string& filename) return buffer.str(); } -uint32_t Shader::CompileShader(uint32_t type, const std::string& source_path) +uint32_t Shader::compile_shader(uint32_t type, const std::string& source_path) { - std::string source = ReadShaderFile(source_path); + std::string source = read_shader_file(source_path); // std::cout << "SHADER FILE: " << source << "\n"; if (source.empty()) return 0; diff --git a/src/shader.h b/src/shader.h index 76a93e1..c9791a8 100644 --- a/src/shader.h +++ b/src/shader.h @@ -12,32 +12,32 @@ public: Shader(const std::string& vertex_path, const std::string& fragment_path); - void Use() const; - void SetupAttribs() const; + void use() const; + void setup_attribs() const; - uint32_t getId() const + [[nodiscard]] uint32_t id() const { return program_id_; } - const char* getError() const + [[nodiscard]] const char* error() const { return error_.c_str(); } - int getLocation(const char* key) const; + [[nodiscard]] int uniform_location(const char* key) const; - static void setInt(int loc, int value); - static void setFloat(int loc, float value); - static void setVec3(int loc, const glm::vec3& value); - static void setVec4(int loc, const glm::vec4& value); - static void setMat4(int loc, const glm::mat4& value); + static void set_int(int loc, int value); + static void set_float(int loc, float value); + static void set_vec3(int loc, const glm::vec3& value); + static void set_vec4(int loc, const glm::vec4& value); + static void set_mat4(int loc, const glm::mat4& value); private: uint32_t program_id_; std::string error_; - std::string ReadShaderFile(const std::string& filename); - uint32_t CompileShader(uint32_t type, const std::string& source_path); + std::string read_shader_file(const std::string& filename); + uint32_t compile_shader(uint32_t type, const std::string& source_path); }; #endif // SHADER_H_ diff --git a/src/state.cpp b/src/state.cpp index e38facc..744f11a 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -1,6 +1,6 @@ #include "state.h" -std::shared_ptr State::defaultShader = nullptr; -glm::mat4 State::projectionMatrix = glm::mat4(1.0f); -glm::mat4 State::viewMatrix = glm::mat4(1.0f); -glm::mat4 State::modelMatrix = glm::mat4(1.0f); +std::shared_ptr State::default_shader_ = nullptr; +glm::mat4 State::projection_matrix_ = glm::mat4(1.0f); +glm::mat4 State::view_matrix_ = glm::mat4(1.0f); +glm::mat4 State::model_matrix_ = glm::mat4(1.0f); diff --git a/src/state.h b/src/state.h index a6811a2..bd24ae7 100644 --- a/src/state.h +++ b/src/state.h @@ -8,10 +8,10 @@ class Shader; class State { public: - static std::shared_ptr defaultShader; - static glm::mat4 projectionMatrix; - static glm::mat4 viewMatrix; - static glm::mat4 modelMatrix; + static std::shared_ptr default_shader_; + static glm::mat4 projection_matrix_; + static glm::mat4 view_matrix_; + static glm::mat4 model_matrix_; }; #endif // STATE_H_ diff --git a/src/world.cpp b/src/world.cpp index 11ba6ab..ef2e610 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -3,7 +3,7 @@ #include "camera.h" #include -void World::addEntity(const std::shared_ptr& entity) +void World::add_entity(const std::shared_ptr& entity) { entities_.push_back(entity); @@ -15,7 +15,7 @@ void World::addEntity(const std::shared_ptr& entity) } } -void World::removeEntity(const std::shared_ptr& entity) +void World::remove_entity(const std::shared_ptr& entity) { // Remove from entities list auto it = std::find(entities_.begin(), entities_.end(), entity); @@ -35,10 +35,10 @@ void World::removeEntity(const std::shared_ptr& entity) } } -void World::update(float deltaTime) +void World::update(float delta_time) { for (auto& entity : entities_) { - entity->update(deltaTime); + entity->update(delta_time); } } diff --git a/src/world.h b/src/world.h index 85e82ba..bf8dbf4 100644 --- a/src/world.h +++ b/src/world.h @@ -11,23 +11,23 @@ class World { public: World() = default; - void addEntity(const std::shared_ptr& entity); - void removeEntity(const std::shared_ptr& entity); + void add_entity(const std::shared_ptr& entity); + void remove_entity(const std::shared_ptr& entity); - size_t getNumEntities() const + [[nodiscard]] size_t num_entities() const { return entities_.size(); } - const std::shared_ptr& getEntity(size_t index) const + [[nodiscard]] const std::shared_ptr& entity(size_t index) const { return entities_[index]; } - std::shared_ptr& getEntity(size_t index) + [[nodiscard]] std::shared_ptr& entity(size_t index) { return entities_[index]; } - void update(float deltaTime); + void update(float delta_time); void draw(); private: