style: naming convention

This commit is contained in:
2025-10-13 16:22:57 +02:00
parent f981cee4e0
commit f1a24f576b
16 changed files with 114 additions and 113 deletions

View File

@@ -1,22 +1,26 @@
--- ---
# Linux-style naming conventions for C++ projects # C++ Core Guidelines style (CppCoreGuidelines)
Checks: > Checks: >
cppcoreguidelines-*,
readability-identifier-naming, readability-identifier-naming,
readability-braces-around-statements, readability-*,
readability-function-size, modernize-*,
modernize-use-nullptr,
modernize-use-override,
performance-*, 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: 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.ClassCase, value: CamelCase }
- { key: readability-identifier-naming.StructCase, value: CamelCase } - { key: readability-identifier-naming.StructCase, value: CamelCase }
- { key: readability-identifier-naming.EnumCase, value: CamelCase } - { key: readability-identifier-naming.EnumCase, value: CamelCase }
- { key: readability-identifier-naming.UnionCase, 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.FunctionCase, value: lower_case }
- { key: readability-identifier-naming.MethodCase, 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.MacroDefinitionCase, value: UPPER_CASE }
- { key: readability-identifier-naming.EnumConstantCase, 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.GlobalVariableCase, value: lower_case }
- { key: readability-identifier-naming.GlobalVariablePrefix, value: 'g_' }
# Namespaces: snake_case # Namespaces: snake_case
- { key: readability-identifier-naming.NamespaceCase, value: lower_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.TypeTemplateParameterCase, value: CamelCase }
- { key: readability-identifier-naming.ValueTemplateParameterCase, value: lower_case } - { key: readability-identifier-naming.ValueTemplateParameterCase, value: lower_case }
# Typedef and type aliases: snake_case_t suffix (Linux style) # Type aliases: PascalCase (modern C++ style with 'using')
- { key: readability-identifier-naming.TypedefCase, value: lower_case } - { key: readability-identifier-naming.TypeAliasCase, value: CamelCase }
- { key: readability-identifier-naming.TypedefSuffix, value: '_t' }
- { key: readability-identifier-naming.TypeAliasCase, value: lower_case }
- { key: readability-identifier-naming.TypeAliasSuffix, value: '_t' }
# Function size limits # Function size limits
- { key: readability-function-size.LineThreshold, value: 100 } - { key: readability-function-size.LineThreshold, value: 100 }

View File

@@ -39,11 +39,11 @@ Buffer::~Buffer()
glDeleteBuffers(1, &ebo_); glDeleteBuffers(1, &ebo_);
} }
void Buffer::Draw(const Shader& shader) const void Buffer::draw(const Shader& shader) const
{ {
glBindVertexArray(vao_); glBindVertexArray(vao_);
glBindBuffer(GL_ARRAY_BUFFER, vbo_); glBindBuffer(GL_ARRAY_BUFFER, vbo_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
shader.SetupAttribs(); shader.setup_attribs();
glDrawElements(GL_TRIANGLES, index_count_, GL_UNSIGNED_SHORT, nullptr); glDrawElements(GL_TRIANGLES, index_count_, GL_UNSIGNED_SHORT, nullptr);
} }

View File

@@ -14,7 +14,7 @@ public:
const std::vector<uint16_t>& indices); const std::vector<uint16_t>& indices);
~Buffer(); ~Buffer();
void Draw(const Shader& shader) const; void draw(const Shader& shader) const;
private: private:
uint32_t vao_, vbo_, ebo_; uint32_t vao_, vbo_, ebo_;

View File

@@ -6,14 +6,14 @@
Camera::Camera() Camera::Camera()
: projection_(glm::mat4(1.0f)) : projection_(glm::mat4(1.0f))
, viewport_(0, 0, 800, 600) , viewport_(0, 0, 800, 600)
, clearColor_(0.0f, 0.0f, 0.0f) , clear_color_(0.0f, 0.0f, 0.0f)
{ {
} }
void Camera::prepare() void Camera::prepare()
{ {
// Set projection matrix // Set projection matrix
State::projectionMatrix = projection_; State::projection_matrix_ = projection_;
// Calculate view matrix // Calculate view matrix
// For a camera, we need the inverse transformation: // For a camera, we need the inverse transformation:
@@ -29,13 +29,13 @@ void Camera::prepare()
// Inverse translation (translate in opposite direction) // Inverse translation (translate in opposite direction)
view = glm::translate(view, -position_); view = glm::translate(view, -position_);
State::viewMatrix = view; State::view_matrix_ = view;
// Set viewport // Set viewport
glViewport(viewport_.x, viewport_.y, viewport_.z, viewport_.w); glViewport(viewport_.x, viewport_.y, viewport_.z, viewport_.w);
glScissor(viewport_.x, viewport_.y, viewport_.z, viewport_.w); glScissor(viewport_.x, viewport_.y, viewport_.z, viewport_.w);
// Set clear color and clear buffers // 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); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} }

View File

@@ -8,31 +8,31 @@ class Camera : public Entity {
public: public:
Camera(); Camera();
const glm::mat4& getProjection() const [[nodiscard]] const glm::mat4& projection() const
{ {
return projection_; return projection_;
} }
void setProjection(const glm::mat4& proj) void set_projection(const glm::mat4& proj)
{ {
projection_ = proj; projection_ = proj;
} }
const glm::ivec4& getViewport() const [[nodiscard]] const glm::ivec4& viewport() const
{ {
return viewport_; return viewport_;
} }
void setViewport(const glm::ivec4& vp) void set_viewport(const glm::ivec4& vp)
{ {
viewport_ = 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(); void prepare();
@@ -40,7 +40,7 @@ public:
private: private:
glm::mat4 projection_; glm::mat4 projection_;
glm::ivec4 viewport_; glm::ivec4 viewport_;
glm::vec3 clearColor_; glm::vec3 clear_color_;
}; };
#endif // CAMERA_H_ #endif // CAMERA_H_

View File

@@ -6,44 +6,44 @@
class Entity { class Entity {
public: public:
Entity(); Entity();
virtual ~Entity() virtual ~Entity() = default;
{
}
const glm::vec3& getPosition() const [[nodiscard]] const glm::vec3& position() const
{ {
return position_; return position_;
} }
void setPosition(const glm::vec3& pos) void set_position(const glm::vec3& pos)
{ {
position_ = pos; position_ = pos;
} }
const glm::vec3& getRotation() const [[nodiscard]] const glm::vec3& rotation() const
{ {
return rotation_; return rotation_;
} }
void setRotation(const glm::vec3& rot) void set_rotation(const glm::vec3& rot)
{ {
rotation_ = rot; rotation_ = rot;
} }
const glm::vec3& getScale() const [[nodiscard]] const glm::vec3& scale() const
{ {
return scale_; return scale_;
} }
void setScale(const glm::vec3& scale) void set_scale(const glm::vec3& scale)
{ {
scale_ = scale; scale_ = scale;
} }
void move(const glm::vec3& vec); void move(const glm::vec3& vec);
virtual void update(float deltaTime) virtual void update(float delta_time)
{ {
// ...
} }
virtual void draw() virtual void draw()
{ {
// ...
} }
protected: protected:

View File

@@ -65,11 +65,11 @@ int main()
} }
// Initialize default shader // Initialize default shader
State::defaultShader = State::default_shader_ =
std::make_shared<Shader>("data/vertex.glsl", "data/fragment.glsl"); std::make_shared<Shader>("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: " std::cerr << "Failed to initialize shaders: "
<< State::defaultShader->getError() << "\n"; << State::default_shader_->error() << "\n";
} }
// Create world // Create world
@@ -77,13 +77,13 @@ int main()
// Create camera // Create camera
auto camera = std::make_shared<Camera>(); auto camera = std::make_shared<Camera>();
camera->setPosition(glm::vec3(0.0f, 0.0f, 6.0f)); camera->set_position(glm::vec3(0.0f, 0.0f, 6.0f));
camera->setProjection(glm::perspective( camera->set_projection(glm::perspective(
glm::radians(45.0f), glm::radians(45.0f),
static_cast<float>(SCREEN_WIDTH) / SCREEN_HEIGHT, 0.1f, 100.0f)); static_cast<float>(SCREEN_WIDTH) / SCREEN_HEIGHT, 0.1f, 100.0f));
camera->setViewport(glm::ivec4(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)); camera->set_viewport(glm::ivec4(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT));
camera->setClearColor(glm::vec3(0.1f, 0.1f, 0.1f)); camera->set_clear_color(glm::vec3(0.1f, 0.1f, 0.1f));
world.addEntity(camera); world.add_entity(camera);
// Create triangle mesh // Create triangle mesh
std::vector<Vertex> vertices = { std::vector<Vertex> vertices = {
@@ -94,18 +94,18 @@ int main()
auto buffer = std::make_shared<Buffer>(vertices, indices); auto buffer = std::make_shared<Buffer>(vertices, indices);
auto mesh = std::make_shared<Mesh>(); auto mesh = std::make_shared<Mesh>();
mesh->addBuffer(buffer); mesh->add_buffer(buffer);
// Create 9 models in a 3x3 grid // Create 9 models in a 3x3 grid
std::vector<std::shared_ptr<Model>> models; std::vector<std::shared_ptr<Model>> models;
for (int row = 0; row < 3; ++row) { for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) { for (int col = 0; col < 3; ++col) {
auto model = std::make_shared<Model>(mesh); auto model = std::make_shared<Model>(mesh);
model->setPosition( model->set_position(
glm::vec3(-3.0f + static_cast<float>(col) * 3.0f, glm::vec3(-3.0f + static_cast<float>(col) * 3.0f,
0.0f, static_cast<float>(-row) * 3.0f)); 0.0f, static_cast<float>(-row) * 3.0f));
models.push_back(model); models.push_back(model);
world.addEntity(model); world.add_entity(model);
} }
} }
@@ -124,7 +124,7 @@ int main()
// Update rotation for all models // Update rotation for all models
for (auto& model : models) { for (auto& model : models) {
model->setRotation(glm::vec3( model->set_rotation(glm::vec3(
0.0f, glm::radians(static_cast<float>(angle)), 0.0f, glm::radians(static_cast<float>(angle)),
0.0f)); 0.0f));
} }

View File

@@ -3,8 +3,8 @@
#include "shader.h" #include "shader.h"
#include "state.h" #include "state.h"
void Mesh::addBuffer(const std::shared_ptr<Buffer>& buffer, void Mesh::add_buffer(const std::shared_ptr<Buffer>& buffer,
const std::shared_ptr<Shader>& shader) const std::shared_ptr<Shader>& shader)
{ {
buffers_.push_back(buffer); buffers_.push_back(buffer);
shaders_.push_back(shader); shaders_.push_back(shader);
@@ -13,21 +13,21 @@ void Mesh::addBuffer(const std::shared_ptr<Buffer>& buffer,
void Mesh::draw() void Mesh::draw()
{ {
// Calculate MVP matrix // Calculate MVP matrix
glm::mat4 mvp = glm::mat4 mvp = State::projection_matrix_ * State::view_matrix_
State::projectionMatrix * State::viewMatrix * State::modelMatrix; * State::model_matrix_;
// Draw each buffer with its shader // Draw each buffer with its shader
for (size_t i = 0; i < buffers_.size(); ++i) { for (size_t i = 0; i < buffers_.size(); ++i) {
// Use buffer's shader if available, otherwise use default // Use buffer's shader if available, otherwise use default
// shader // shader
std::shared_ptr<Shader> shader = std::shared_ptr<Shader> shader =
shaders_[i] ? shaders_[i] : State::defaultShader; shaders_[i] ? shaders_[i] : State::default_shader_;
if (shader) { if (shader) {
shader->Use(); shader->use();
int mvpLoc = shader->getLocation("mvp"); int mvpLoc = shader->uniform_location("mvp");
Shader::setMat4(mvpLoc, mvp); Shader::set_mat4(mvpLoc, mvp);
buffers_[i]->Draw(*shader); buffers_[i]->draw(*shader);
} }
} }
} }

View File

@@ -9,20 +9,21 @@ class Shader;
class Mesh { class Mesh {
public: public:
Mesh() = default; Mesh() = default;
~Mesh() = default;
void addBuffer(const std::shared_ptr<Buffer>& buffer, void add_buffer(const std::shared_ptr<Buffer>& buffer,
const std::shared_ptr<Shader>& shader = nullptr); const std::shared_ptr<Shader>& shader = nullptr);
size_t getNumBuffers() const [[nodiscard]] size_t num_buffers() const
{ {
return buffers_.size(); return buffers_.size();
} }
const std::shared_ptr<Buffer>& getBuffer(size_t index) const [[nodiscard]] const std::shared_ptr<Buffer>& buffer(size_t index) const
{ {
return buffers_[index]; return buffers_[index];
} }
std::shared_ptr<Buffer>& getBuffer(size_t index) [[nodiscard]] std::shared_ptr<Buffer>& buffer(size_t index)
{ {
return buffers_[index]; return buffers_[index];
} }

View File

@@ -28,7 +28,7 @@ void Model::draw()
model = glm::scale(model, scale_); model = glm::scale(model, scale_);
// Set the model matrix in State // Set the model matrix in State
State::modelMatrix = model; State::model_matrix_ = model;
// Draw the mesh // Draw the mesh
mesh_->draw(); mesh_->draw();

View File

@@ -12,9 +12,9 @@
Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath) 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 = const uint32_t fshader =
CompileShader(GL_FRAGMENT_SHADER, fragmentPath); compile_shader(GL_FRAGMENT_SHADER, fragmentPath);
if (vshader == 0 || fshader == 0) { if (vshader == 0 || fshader == 0) {
program_id_ = 0; program_id_ = 0;
@@ -42,13 +42,13 @@ Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath)
glDeleteShader(fshader); glDeleteShader(fshader);
} }
void Shader::Use() const void Shader::use() const
{ {
if (program_id_ != 0) if (program_id_ != 0)
glUseProgram(program_id_); glUseProgram(program_id_);
} }
void Shader::SetupAttribs() const void Shader::setup_attribs() const
{ {
int loc = glGetAttribLocation(program_id_, "vpos"); int loc = glGetAttribLocation(program_id_, "vpos");
if (loc != -1) { 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); return glGetUniformLocation(program_id_, key);
} }
void Shader::setInt(int loc, int value) void Shader::set_int(int loc, int value)
{ {
if (loc != -1) if (loc != -1)
glUniform1i(loc, value); glUniform1i(loc, value);
} }
void Shader::setFloat(int loc, float value) void Shader::set_float(int loc, float value)
{ {
if (loc != -1) if (loc != -1)
glUniform1f(loc, value); 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) if (loc != -1)
glUniform3fv(loc, 1, &value[0]); 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) if (loc != -1)
glUniform4fv(loc, 1, &value[0]); 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) if (loc != -1)
glUniformMatrix4fv(loc, 1, GL_FALSE, &value[0][0]); 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); std::ifstream file(filename);
if (!file) { if (!file) {
@@ -115,9 +115,9 @@ std::string Shader::ReadShaderFile(const std::string& filename)
return buffer.str(); 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"; // std::cout << "SHADER FILE: " << source << "\n";
if (source.empty()) if (source.empty())
return 0; return 0;

View File

@@ -12,32 +12,32 @@ public:
Shader(const std::string& vertex_path, Shader(const std::string& vertex_path,
const std::string& fragment_path); const std::string& fragment_path);
void Use() const; void use() const;
void SetupAttribs() const; void setup_attribs() const;
uint32_t getId() const [[nodiscard]] uint32_t id() const
{ {
return program_id_; return program_id_;
} }
const char* getError() const [[nodiscard]] const char* error() const
{ {
return error_.c_str(); 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 set_int(int loc, int value);
static void setFloat(int loc, float value); static void set_float(int loc, float value);
static void setVec3(int loc, const glm::vec3& value); static void set_vec3(int loc, const glm::vec3& value);
static void setVec4(int loc, const glm::vec4& value); static void set_vec4(int loc, const glm::vec4& value);
static void setMat4(int loc, const glm::mat4& value); static void set_mat4(int loc, const glm::mat4& value);
private: private:
uint32_t program_id_; uint32_t program_id_;
std::string error_; std::string error_;
std::string ReadShaderFile(const std::string& filename); std::string read_shader_file(const std::string& filename);
uint32_t CompileShader(uint32_t type, const std::string& source_path); uint32_t compile_shader(uint32_t type, const std::string& source_path);
}; };
#endif // SHADER_H_ #endif // SHADER_H_

View File

@@ -1,6 +1,6 @@
#include "state.h" #include "state.h"
std::shared_ptr<Shader> State::defaultShader = nullptr; std::shared_ptr<Shader> State::default_shader_ = nullptr;
glm::mat4 State::projectionMatrix = glm::mat4(1.0f); glm::mat4 State::projection_matrix_ = glm::mat4(1.0f);
glm::mat4 State::viewMatrix = glm::mat4(1.0f); glm::mat4 State::view_matrix_ = glm::mat4(1.0f);
glm::mat4 State::modelMatrix = glm::mat4(1.0f); glm::mat4 State::model_matrix_ = glm::mat4(1.0f);

View File

@@ -8,10 +8,10 @@ class Shader;
class State { class State {
public: public:
static std::shared_ptr<Shader> defaultShader; static std::shared_ptr<Shader> default_shader_;
static glm::mat4 projectionMatrix; static glm::mat4 projection_matrix_;
static glm::mat4 viewMatrix; static glm::mat4 view_matrix_;
static glm::mat4 modelMatrix; static glm::mat4 model_matrix_;
}; };
#endif // STATE_H_ #endif // STATE_H_

View File

@@ -3,7 +3,7 @@
#include "camera.h" #include "camera.h"
#include <algorithm> #include <algorithm>
void World::addEntity(const std::shared_ptr<Entity>& entity) void World::add_entity(const std::shared_ptr<Entity>& entity)
{ {
entities_.push_back(entity); entities_.push_back(entity);
@@ -15,7 +15,7 @@ void World::addEntity(const std::shared_ptr<Entity>& entity)
} }
} }
void World::removeEntity(const std::shared_ptr<Entity>& entity) void World::remove_entity(const std::shared_ptr<Entity>& entity)
{ {
// Remove from entities list // Remove from entities list
auto it = std::find(entities_.begin(), entities_.end(), entity); auto it = std::find(entities_.begin(), entities_.end(), entity);
@@ -35,10 +35,10 @@ void World::removeEntity(const std::shared_ptr<Entity>& entity)
} }
} }
void World::update(float deltaTime) void World::update(float delta_time)
{ {
for (auto& entity : entities_) { for (auto& entity : entities_) {
entity->update(deltaTime); entity->update(delta_time);
} }
} }

View File

@@ -11,23 +11,23 @@ class World {
public: public:
World() = default; World() = default;
void addEntity(const std::shared_ptr<Entity>& entity); void add_entity(const std::shared_ptr<Entity>& entity);
void removeEntity(const std::shared_ptr<Entity>& entity); void remove_entity(const std::shared_ptr<Entity>& entity);
size_t getNumEntities() const [[nodiscard]] size_t num_entities() const
{ {
return entities_.size(); return entities_.size();
} }
const std::shared_ptr<Entity>& getEntity(size_t index) const [[nodiscard]] const std::shared_ptr<Entity>& entity(size_t index) const
{ {
return entities_[index]; return entities_[index];
} }
std::shared_ptr<Entity>& getEntity(size_t index) [[nodiscard]] std::shared_ptr<Entity>& entity(size_t index)
{ {
return entities_[index]; return entities_[index];
} }
void update(float deltaTime); void update(float delta_time);
void draw(); void draw();
private: private: