|
| 1 | +# AGENTS.md - Agentic Coding Guidelines for tiny_ui |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +tiny_ui is a C++17 library for creating simple UIs on Windows and Linux, using SDL2 as the renderer. The project is built with CMake and vcpkg for dependency management. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Build, Lint, and Test Commands |
| 10 | + |
| 11 | +### Building the Project |
| 12 | + |
| 13 | +```bash |
| 14 | +# Configure with CMake (uses vcpkg toolchain) |
| 15 | +cmake -B build -DCMAKE_BUILD_TYPE=Release |
| 16 | + |
| 17 | +# Build the library and samples |
| 18 | +cmake --build build --config Release |
| 19 | +``` |
| 20 | + |
| 21 | +On Windows with Visual Studio, you can also open `tiny_ui.slnx` or use the `.vcxproj` files. |
| 22 | + |
| 23 | +### Dependencies |
| 24 | + |
| 25 | +The project requires: |
| 26 | +- SDL2 |
| 27 | +- SDL2_image |
| 28 | +- SDL2_ttf |
| 29 | +- glm |
| 30 | + |
| 31 | +These are managed via vcpkg (see `vcpkg.json`). |
| 32 | + |
| 33 | +### Running the Sample Application |
| 34 | + |
| 35 | +```bash |
| 36 | +# After building, run the sample |
| 37 | +./bin/release/tiny_ui_sample.exe # Windows |
| 38 | +./bin/tiny_ui_sample # Linux |
| 39 | +``` |
| 40 | + |
| 41 | +### Linting and Formatting |
| 42 | + |
| 43 | +The project uses `.clang-format` for code formatting: |
| 44 | + |
| 45 | +```bash |
| 46 | +# Format a single file |
| 47 | +clang-format -i src/tinyui.cpp |
| 48 | + |
| 49 | +# Format all source files |
| 50 | +find src -name "*.cpp" -o -name "*.h" | xargs clang-format -i |
| 51 | +``` |
| 52 | + |
| 53 | +### Testing |
| 54 | + |
| 55 | +**Note**: This project currently has no automated tests. The test step in CI (`.github/workflows/cmake.yml`) is commented out. If adding tests: |
| 56 | + |
| 57 | +```bash |
| 58 | +# Run tests via CTest (when implemented) |
| 59 | +cd build |
| 60 | +ctest -C Release |
| 61 | +``` |
| 62 | + |
| 63 | +To run a single test (when tests exist), use CTest's filtering: |
| 64 | +```bash |
| 65 | +ctest -R test_name_pattern |
| 66 | +``` |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## Code Style Guidelines |
| 71 | + |
| 72 | +### General Style |
| 73 | + |
| 74 | +- **C++ Standard**: C++17 |
| 75 | +- **Formatting**: Uses `.clang-format` (LLVM-based, see `.clang-format`) |
| 76 | +- **Indentation**: 4 spaces, no tabs |
| 77 | +- **Column Limit**: 0 (unlimited) |
| 78 | + |
| 79 | +### File Organization |
| 80 | + |
| 81 | +- Header files (`.h`) go in `src/` |
| 82 | +- Implementation files (`.cpp`) go in `src/` |
| 83 | +- Backend-specific code goes in `src/backends/` |
| 84 | +- Samples go in `samples/` |
| 85 | +- All source files must include the MIT License header |
| 86 | + |
| 87 | +### Imports and Includes |
| 88 | + |
| 89 | +```cpp |
| 90 | +// Order: 1. Project headers, 2. STL/Standard headers, 3. External libraries |
| 91 | +#include "tinyui.h" |
| 92 | +#include "widgets.h" |
| 93 | +#include "backends/sdl2_renderer.h" |
| 94 | + |
| 95 | +#include <iostream> |
| 96 | +#include <cstring> |
| 97 | + |
| 98 | +#include "stb_image.h" |
| 99 | +``` |
| 100 | + |
| 101 | +Include order (per `.clang-format`): |
| 102 | +1. Local project headers (`"*.h"`) |
| 103 | +2. System headers (`<*.h>`) |
| 104 | +3. Other system headers (`<*>`) |
| 105 | + |
| 106 | +### Naming Conventions |
| 107 | + |
| 108 | +- **Types/Structs/Enums**: PascalCase (e.g., `struct Widget`, `enum class Alignment`) |
| 109 | +- **Functions**: camelCase (e.g., `createContext()`, `initScreen()`) |
| 110 | +- **Member Variables**: `m` prefix (e.g., `mId`, `mType`, `mParent`) |
| 111 | +- **Constants**: PascalCase or `k` prefix (e.g., `InvalidHandle`, `kDefaultSize`) |
| 112 | +- **Namespaces**: lowercase (e.g., `namespace tinyui`) |
| 113 | + |
| 114 | +### Type Conventions |
| 115 | + |
| 116 | +- Use `int32_t` for explicit-width integers |
| 117 | +- Use `ret_code` (defined as `int32_t`) for return codes |
| 118 | +- Use `nullptr` instead of `NULL` |
| 119 | +- Use `const` extensively for pointer parameters and member functions |
| 120 | +- Use `constexpr` for compile-time constants |
| 121 | +- Use `static constexpr` for class-level constants |
| 122 | + |
| 123 | +### Return Codes |
| 124 | + |
| 125 | +The library uses a consistent return code pattern: |
| 126 | +```cpp |
| 127 | +static constexpr ret_code InvalidRenderHandle = -3; |
| 128 | +static constexpr ret_code InvalidHandle = -2; |
| 129 | +static constexpr ret_code ErrorCode = -1; |
| 130 | +static constexpr ret_code ResultOk = 0; |
| 131 | +``` |
| 132 | + |
| 133 | +### Error Handling |
| 134 | + |
| 135 | +- Return error codes for recoverable errors |
| 136 | +- Use `assert()` for programming errors/invariants (development only) |
| 137 | +- Validate input parameters at the start of functions |
| 138 | + |
| 139 | +### Enums |
| 140 | + |
| 141 | +Use `enum class` for type-safe enums: |
| 142 | +```cpp |
| 143 | +enum class Alignment : int32_t { |
| 144 | + Invalid = -1, |
| 145 | + Left = 0, |
| 146 | + Center, |
| 147 | + Right, |
| 148 | + Count // Use Count for array sizing |
| 149 | +}; |
| 150 | +``` |
| 151 | +
|
| 152 | +### Structs and Classes |
| 153 | +
|
| 154 | +- Use structs for POD (Plain Old Data) types |
| 155 | +- Use `= default` for trivial constructors/destructors |
| 156 | +- Initialize member variables with default values in class definition |
| 157 | +- Use Doxygen-style comments for documentation |
| 158 | +
|
| 159 | +### Code Example |
| 160 | +
|
| 161 | +```cpp |
| 162 | +/// @brief The tiny ui context. |
| 163 | +struct Context { |
| 164 | + bool mCreated{false}; |
| 165 | + bool mRequestShutdown{false}; |
| 166 | + const char *mAppTitle{nullptr}; |
| 167 | + Widget *mRoot{nullptr}; |
| 168 | +
|
| 169 | + /// @brief Will create a new tiny ui context. |
| 170 | + /// @param title The title of the context. |
| 171 | + /// @return The created context. |
| 172 | + static Context *create(const char *title); |
| 173 | + |
| 174 | + /// @brief Will destroy a valid tinyui context. |
| 175 | + /// @param ctx The context to destroy. |
| 176 | + static void destroy(Context *ctx); |
| 177 | +}; |
| 178 | +``` |
| 179 | + |
| 180 | +### Common Patterns |
| 181 | + |
| 182 | +```cpp |
| 183 | +// Function returning error code |
| 184 | +ret_code TinyUi::initScreen(int32_t x, int32_t y, int32_t w, int32_t h) { |
| 185 | + if (w <= 0 || h <= 0) { |
| 186 | + return ErrorCode; |
| 187 | + } |
| 188 | + // ... implementation |
| 189 | + return ResultOk; |
| 190 | +} |
| 191 | + |
| 192 | +// Callback interface |
| 193 | +struct CallbackI { |
| 194 | + typedef int (*funcCallback) (uint32_t id, void *data); |
| 195 | + funcCallback mfuncCallback[Events::NumEvents]; |
| 196 | + void *mInstance{nullptr}; |
| 197 | +}; |
| 198 | +``` |
| 199 | +
|
| 200 | +--- |
| 201 | +
|
| 202 | +## CI/CD |
| 203 | +
|
| 204 | +The project uses GitHub Actions (see `.github/workflows/cmake.yml`): |
| 205 | +- Builds on Ubuntu latest |
| 206 | +- Installs SDL2, SDL2-image, SDL2-ttf via apt |
| 207 | +- Runs CMake configure and build |
| 208 | +
|
| 209 | +--- |
| 210 | +
|
| 211 | +## Key Files |
| 212 | +
|
| 213 | +- `CMakeLists.txt` - Main build configuration |
| 214 | +- `vcpkg.json` - Dependency manifest |
| 215 | +- `.clang-format` - Code formatting rules |
| 216 | +- `.github/workflows/cmake.yml` - CI pipeline |
| 217 | +- `src/tinyui.h` - Main header with core types |
| 218 | +- `src/widgets.h` - Widget definitions |
| 219 | +- `src/backends/` - Platform-specific rendering backends |
0 commit comments