5.7 KiB
5.7 KiB
UwUGL Styleguide
This document describes the coding conventions used in UwUGL, inferred from the existing codebase. It prioritizes readability and simplicity for a small graphics library.
1. File Organization
- Single monolithic header:
uwugl.his the public entry point and includes all implementation files. - Header-like
.cppfiles: Every implementation file begins with#pragma onceand is designed to be#included directly byuwugl.h(or by other.cppfiles). There are no traditional separate.h/.cpppairs. - No separate headers for internal modules: Classes and structs are declared and defined directly in
.cppfiles. - Exception:
shaders/shaders.his a small aggregator file that includes the shader.cppfiles. - Directory layout:
primatives/— shape classes (Line,Point,Rectangle,Trapezoid,Triangle)shaders/— shader classes and basewaymini/— external Wayland helper library (C)
2. Naming Conventions
- Namespace: All library code lives in
namespace UwU. - Classes / structs: PascalCase (
Buffer,ColorShader,Rectangle,Helpers). - Functions / methods: camelCase (
drawPixel,lerpColor,bresenham,shouldClose). - Member variables: lowercase with no prefix (
pixels,width,x0,c). - Template parameters: PascalCase ending in
T(ShaderT). - Enums: PascalCase type name; UPPER_SNAKE_CASE values (
ShaderType,COLOR,HGRADIENT,VGRADIENT). - Constants in code: prefer lowercase; UPPER_SNAKE_CASE may be used for local compile-time-ish constants in comments or test code.
- Filenames: lowercase and descriptive (
buffer.cpp,colorshader.cpp,line.cpp).
3. Formatting
- Indentation: 2 spaces.
- Brace style: K&R — opening brace on the same line as the class/function/control statement.
- Pointer/reference syntax: attach
*and&to the variable name:uint8_t* pixelsShaderBase<ShaderT>& shader
- Empty parameter lists: use
()for default constructors, e.g.,Color() {}. - Spacing around operators: use spaces around binary operators for readability (
x + yinstead ofx+y). - Explicit
return;at end of void functions: Acceptable, but not required. - Comments: use
//for both inline and block-style comments. Keep them informal and focused on intent or known issues.
4. Includes
Include order within a file:
#pragma once- C/C++ standard headers (
<stdint.h>,<stdio.h>,<algorithm>) - Project implementation files via relative paths (
"../datatypes.cpp","../buffer.cpp")
External C library headers (e.g., "waymini/waymini.h") are included where needed.
5. Code Patterns
- Composition over inheritance: primitives hold data; shaders are passed into
draw(). - CRTP for shaders:
ShaderBase<ShaderT>uses the Curiously Recurring Template Pattern for static polymorphism. - Template
drawmethods: every primitive'sdraw()is templated onShaderTand takes(Buffer buffer, ShaderBase<ShaderT>& shader). - Manual memory management:
new uint8_t[...]inBuffer; no smart pointers. - C-style casts:
(uint16_t)x,(uint8_t*)ptr. - Bounds checks: primitives guard
draw()withif (x > buffer.width || y > buffer.height) {return;};. - Unused parameter suppression: use
(void)param;to silence warnings.
6. C++ Feature Usage
Used
- Classes and structs
- Namespaces
- Templates (
template <class ShaderT>) - CRTP (Curiously Recurring Template Pattern)
- Function overloading (constructors)
- References
- C-style casts
new/new[]for dynamic allocation- Raw pointers
- Stack-allocated arrays
- C standard headers (
<stdint.h>,<stdio.h>,<stdlib.h>,<string.h>,<time.h>,<algorithm>)
Not Used but Allowed
- Smart pointers (
std::unique_ptr,std::shared_ptr) - Standard containers (
std::vector,std::array, etc.) enum class- Exceptions
std::stringstd::chronostd::function
Not Used
The following features are intentionally not used. This keeps the codebase small, predictable, and easy to reason about.
auto- Range-based
forloops nullptr- Lambda expressions
constexpr/constevalusingtype aliases- Move semantics / rvalue references
- Default member initializers
- Delegating constructors
override/final- Named casts (
static_cast,reinterpret_cast, etc.) - Structured bindings
- Concepts /
requires(C++20) - Virtual functions / runtime polymorphism / abstract base classes
- Operator overloading
- RTTI /
dynamic_cast/typeid <iostream>(use C-styleprintf/fflush)
7. Build System
- Makefile-based.
- C library (
waymini) compiled withgcc. - Main example compiled with
g++. - Flags:
-Wall -Wextra -O3, plus Wayland client flags frompkg-config.
8. Known Inconsistencies and Recommendations
The codebase currently has a few inconsistencies. The following recommendations aim to improve readability without adding complexity:
- Indentation consistency: some blocks are not aligned (e.g.,
line.cppinner loop). Keep all indentation strictly at 2 spaces per level. - Spacing consistency: standardize on spaces around binary operators and after commas.
- Include consistency: some primitives include
../shaders/shaders.hwhilerectangle.cppalso includes../shaders/shaderbase.cppdirectly. Prefer the aggregatorshaders.heverywhere. return;at end of void functions: acceptable, but can be omitted when it adds no clarity.- Bounds check style: the pattern
if (...) {return;};has a redundant semicolon. Preferif (...) { return; }.
This guide should evolve with the project. When in doubt, favor simplicity and consistency with the existing code.