diff --git a/STYLEGUIDE.md b/STYLEGUIDE.md new file mode 100644 index 0000000..43f0e53 --- /dev/null +++ b/STYLEGUIDE.md @@ -0,0 +1,127 @@ +# 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.h` is the public entry point and includes all implementation files. +- **Header-like `.cpp` files**: Every implementation file begins with `#pragma once` and is designed to be `#include`d directly by `uwugl.h` (or by other `.cpp` files). There are no traditional separate `.h`/`.cpp` pairs. +- **No separate headers for internal modules**: Classes and structs are declared and defined directly in `.cpp` files. +- **Exception**: `shaders/shaders.h` is a small aggregator file that includes the shader `.cpp` files. +- **Directory layout**: + - `primatives/` — shape classes (`Line`, `Point`, `Rectangle`, `Trapezoid`, `Triangle`) + - `shaders/` — shader classes and base + - `waymini/` — 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* pixels` + - `ShaderBase& shader` +- **Empty parameter lists**: use `()` for default constructors, e.g., `Color() {}`. +- **Spacing around operators**: use spaces around binary operators for readability (`x + y` instead of `x+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: + +1. `#pragma once` +2. C/C++ standard headers (``, ``, ``) +3. 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` uses the Curiously Recurring Template Pattern for static polymorphism. +- **Template `draw` methods**: every primitive's `draw()` is templated on `ShaderT` and takes `(Buffer buffer, ShaderBase& shader)`. +- **Manual memory management**: `new uint8_t[...]` in `Buffer`; no smart pointers. +- **C-style casts**: `(uint16_t)x`, `(uint8_t*)ptr`. +- **Bounds checks**: primitives guard `draw()` with `if (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 `) +- 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 (``, ``, ``, ``, ``, ``) + +### Not Used but Allowed +- Smart pointers (`std::unique_ptr`, `std::shared_ptr`) +- Standard containers (`std::vector`, `std::array`, etc.) +- `enum class` +- Exceptions +- `std::string` +- `std::chrono` +- `std::function` + +### Not Used + +The following features are intentionally not used. This keeps the codebase small, predictable, and easy to reason about. + +- `auto` +- Range-based `for` loops +- `nullptr` +- Lambda expressions +- `constexpr` / `consteval` +- `using` type 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` +- `` (use C-style `printf`/`fflush`) + +## 7. Build System + +- **Makefile-based**. +- C library (`waymini`) compiled with `gcc`. +- Main example compiled with `g++`. +- Flags: `-Wall -Wextra -O3`, plus Wayland client flags from `pkg-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.cpp` inner 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.h` while `rectangle.cpp` also includes `../shaders/shaderbase.cpp` directly. Prefer the aggregator `shaders.h` everywhere. +- **`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. Prefer `if (...) { return; }`. + +--- + +This guide should evolve with the project. When in doubt, favor simplicity and consistency with the existing code. diff --git a/example b/example index 30c8e65..01d258c 100755 Binary files a/example and b/example differ diff --git a/example.cpp b/example.cpp index 1fb0291..b3373d5 100644 --- a/example.cpp +++ b/example.cpp @@ -38,9 +38,10 @@ int main(void) { UwU::Surface surface = UwU::Surface(w, h, onKey); - UwU::ColorShader fuchsia = UwU::ColorShader(UwU::Color(0xb00b69ff)); + // UwU::ColorShader fuchsia = UwU::ColorShader(UwU::Color(0xb00b69ff)); + UwU::VGradientShader bisexual = UwU::VGradientShader(69, 420, UwU::Color(0xff0000ff), UwU::Color(0x0000ffff)); UwU::Rectangle rectangle = UwU::Rectangle(69, 69, 420, 420); - rectangle.draw(surface.buffer, fuchsia); + rectangle.draw(surface.buffer, bisexual); UwU::ColorShader green = UwU::ColorShader(UwU::Color(0x00ff00ff)); UwU::Point point = UwU::Point(187, 37); diff --git a/shader.cpp b/shader.cpp deleted file mode 100644 index c99036b..0000000 --- a/shader.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#pragma once - -#include - -#include "datatypes.cpp" -#include "helpers.cpp" -#include "buffer.cpp" - -namespace UwU { - // shader prototype - class Shader { - public: - //COLOR Shader - Shader(Color color) { - this->shaderType = COLOR; - this->c0 = color; - } - - //HGRADIENT or VGRADIENT shader - Shader(Direction direction, uint16_t uv0, uint16_t uv1, Color c0, Color c1) { - this->c0 = c0; - this->c1 = c1; - switch (direction) { - case H: - shaderType = HGRADIENT; - this->u0 = uv0; - this->scale = 0xffffffff / (1 + (uint32_t)uv1 - (uint32_t)uv0); - break; - case V: - shaderType = VGRADIENT; - this->v0 = uv0; - this->scale = 0xffffffff / (1 + (uint32_t)uv1 - (uint32_t)uv0); - break; - } - } - - // BUFFER shader - Shader(uint16_t u, uint16_t v, Buffer buffer) { - this->shaderType = BUFFER; - this->u0 = u; - this->v0 = v; - this->buffer = buffer; - } - - ShaderType shaderType; - uint16_t u0; - uint16_t v0; - Color c0; - Color c1; - uint32_t scale; - Buffer buffer; - }; - - class ShaderMethods { - public: - static Color gradient(uint16_t uv0, uint32_t scale, Color c0, Color c1, uint16_t xy) { - uint32_t t = (((uint32_t)xy - (uint32_t)uv0) * scale) / 0x01000000; - return Helpers::lerpColor(c0, c1, (uint8_t)t); - } - - static Color buffer(uint16_t u0, uint16_t v0, Buffer buffer, uint16_t x, uint16_t y) { - size_t i = ((size_t)y - (size_t)v0) * buffer.stride + ((size_t)x - (size_t)u0) * 4; - uint8_t b = buffer.pixels[i + 0]; - uint8_t g = buffer.pixels[i + 1]; - uint8_t r = buffer.pixels[i + 2]; - uint8_t a = buffer.pixels[i + 3]; - return Color(r, g, b, a); - } - }; -} \ No newline at end of file diff --git a/shaders/hgradientshader.cpp b/shaders/hgradientshader.cpp index f6bb993..c3f3c5e 100644 --- a/shaders/hgradientshader.cpp +++ b/shaders/hgradientshader.cpp @@ -8,21 +8,22 @@ #include "shaderbase.cpp" namespace UwU { - class HGradienthader : public ShaderBase { + class HGradientShader : public ShaderBase { public: HGradientShader(uint16_t u0, uint16_t u1, Color c0, Color c1) { - this->u0; + this->u0 = (uint32_t)u0; this->uScale = 0xffffffff / (1 + (uint32_t)u1 - (uint32_t)u0); this->c0 = c0; this->c1 = c1; } - uint16_t u0; - uint16_t uScale; + uint32_t u0; + uint32_t uScale; Color c0; Color c1; - Color shade(uint16_t u, uint16_t u) { - uint32_t t = (((uint32_t)u - (uint32_t)u0) * uScale) / 0x01000000; + Color shade(uint16_t u, uint16_t v) { + (void)v; + uint32_t t = (((uint32_t)u - u0) * uScale) / 0x01000000; return Helpers::lerpColor(c0, c1, (uint8_t)t); } }; diff --git a/shaders/shaders.h b/shaders/shaders.h index fc3bb96..47aa080 100644 --- a/shaders/shaders.h +++ b/shaders/shaders.h @@ -1,4 +1,4 @@ #include "shaderbase.cpp" #include "colorshader.cpp" -// #include "hgradientshader.cpp" -// #include "vgradientshader.cpp" \ No newline at end of file +#include "hgradientshader.cpp" +#include "vgradientshader.cpp" \ No newline at end of file diff --git a/shaders/vgradientshader.cpp b/shaders/vgradientshader.cpp index 29c664f..b878c2c 100644 --- a/shaders/vgradientshader.cpp +++ b/shaders/vgradientshader.cpp @@ -8,21 +8,22 @@ #include "shaderbase.cpp" namespace UwU { - class VGradienthader : public ShaderBase { + class VGradientShader : public ShaderBase { public: VGradientShader(uint16_t v0, uint16_t v1, Color c0, Color c1) { - this->v0; + this->v0 = (uint32_t)v0; this->vScale = 0xffffffff / (1 + (uint32_t)v1 - (uint32_t)v0); this->c0 = c0; this->c1 = c1; } - uint16_t v0; - uint16_t vScale; + uint32_t v0; + uint32_t vScale; Color c0; Color c1; - Color shade(uint16_t u, uint16_t u) { - uint32_t t = (((uint32_t)v - (uint32_t)v0) * vScale) / 0x01000000; + Color shade(uint16_t u, uint16_t v) { + (void)u; + uint32_t t = (((uint32_t)v - v0) * vScale) / 0x01000000; return Helpers::lerpColor(c0, c1, (uint8_t)t); } }; diff --git a/uwugl.h b/uwugl.h index 04f1ef9..622401b 100644 --- a/uwugl.h +++ b/uwugl.h @@ -7,4 +7,4 @@ #include "primatives/rectangle.cpp" #include "primatives/trapezoid.cpp" #include "primatives/triangle.cpp" -#include "shader.cpp" \ No newline at end of file +#include "shaders/shaders.h" \ No newline at end of file diff --git a/waymini/libwaymini.a b/waymini/libwaymini.a index 131cff3..0c521f8 100644 Binary files a/waymini/libwaymini.a and b/waymini/libwaymini.a differ diff --git a/waymini/waymini.o b/waymini/waymini.o index 52701a6..f6e37d8 100644 Binary files a/waymini/waymini.o and b/waymini/waymini.o differ diff --git a/waymini/xdg-shell-client-protocol.o b/waymini/xdg-shell-client-protocol.o index c672324..1c579b6 100644 Binary files a/waymini/xdg-shell-client-protocol.o and b/waymini/xdg-shell-client-protocol.o differ