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: >
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 }

View File

@@ -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);
}

View File

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

View File

@@ -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);
}

View File

@@ -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_

View File

@@ -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:

View File

@@ -65,11 +65,11 @@ int main()
}
// Initialize default shader
State::defaultShader =
State::default_shader_ =
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: "
<< 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>();
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<float>(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<Vertex> vertices = {
@@ -94,18 +94,18 @@ int main()
auto buffer = std::make_shared<Buffer>(vertices, indices);
auto mesh = std::make_shared<Mesh>();
mesh->addBuffer(buffer);
mesh->add_buffer(buffer);
// Create 9 models in a 3x3 grid
std::vector<std::shared_ptr<Model>> models;
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
auto model = std::make_shared<Model>(mesh);
model->setPosition(
model->set_position(
glm::vec3(-3.0f + static_cast<float>(col) * 3.0f,
0.0f, static_cast<float>(-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<float>(angle)),
0.0f));
}

View File

@@ -3,8 +3,8 @@
#include "shader.h"
#include "state.h"
void Mesh::addBuffer(const std::shared_ptr<Buffer>& buffer,
const std::shared_ptr<Shader>& shader)
void Mesh::add_buffer(const std::shared_ptr<Buffer>& buffer,
const std::shared_ptr<Shader>& shader)
{
buffers_.push_back(buffer);
shaders_.push_back(shader);
@@ -13,21 +13,21 @@ void Mesh::addBuffer(const std::shared_ptr<Buffer>& 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> 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);
}
}
}

View File

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

View File

@@ -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();

View File

@@ -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;

View File

@@ -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_

View File

@@ -1,6 +1,6 @@
#include "state.h"
std::shared_ptr<Shader> 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<Shader> 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);

View File

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

View File

@@ -3,7 +3,7 @@
#include "camera.h"
#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);
@@ -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
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_) {
entity->update(deltaTime);
entity->update(delta_time);
}
}

View File

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