This document contains
examples that you can click on to open them.
# Git - Please do the following *before* sending a pull request: - Merge upstream code (if any) back into your own branch - Run formatters/linters - Push as often as possible - Development is done on separate branches, these follow a pattern of `name/feature` (i.e. `loek/dll-so-poc` or `jaro/class2`) - The master branch is considered stable, and should always contain a working/compiling version of the project - Pull requests for new code include either automated tests for the new code or an explanation as to why the code can not (reliably) be tested - Non-bugfix pull requests must be approved by at least 2 reviewers before being merged - Pull requests should have the following labels (where appropriate) |label|meaning| |:-:|-| |`fix me`|has feedback that should be resolved/discussed by its author| |`review me`|needs additional reviewers (minimum of 2 per PR)| |`do not review`|is actively being worked on or not ready for feedback| |`high priority`|should be worked on before all the others| - PRs start with the `review me` label - Reviewers— - Add the `fix me` label after adding comments - Authors— - Remove the `review me` label if the pull request has enough reviewers - Add the `do not review` label while processing feedback / pushing additional commits # Code style - Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make format` or `make lint`) -
ASCII only
GoodBad
```cpp // crepe startup message std::string message = "Hello, world!"; ``` ```cpp // crêpe startup message std::string message = "こんにちは世界"; ```
-
Class names are always singular
GoodBad
```cpp class Foo {}; ``` ```cpp class Cars {}; ```
- Source files (`.cpp`, `.hpp`) contain the following types of comments: - What is the code supposed to do (optional) - Implementation details (if applicable) - Header files (`.h`) contain the following types of comments: - [Usage documentation](#documentation) (required) - Implementation details (if they affect the header) - Design/data structure decisions (if applicable) -
Comments are placed *above* the line(s) they are explaining
GoodBad
```cpp int add(int a, int b) { // add numbers int out = a + b; return out; } ``` ```cpp int add(int a, int b) { int out = a + b; // add numbers return out; } ```
- Header includes (at the top of files) are split into paragraphs separated by a blank line. The order is: 1. system headers (using `<`brackets`>`) 2. relative headers NOT in the same folder as the current file 3. relative headers in the same folder as the current file > [!NOTE] > When using libraries of which the header include order is important, make > sure to separate the include statements using a blank line (clang-format > may sort include statements, but does not sort across empty lines).
Example
GoodBad
```cpp #include #include #include "api/Sprite.h" #include "util/log.h" #include "SDLContext.h" ``` ```cpp #include #include "SDLContext.h" #include "util/log.h" #include #include "api/Sprite.h" ```
-
If there is one, the matching template header (.hpp) is included at the bottom of the regular header (.h)
GoodBad
Foo.h: ```cpp #pragma once template void foo(); #include "Foo.hpp" ``` Foo.hpp: ```cpp #pragma once #include "Foo.h" template void foo() { // ... } ``` Foo.h: ```cpp #pragma once template void foo(); ``` Foo.hpp: ```cpp #pragma once #include "Foo.h" template void foo() { // ... } ```
-
using namespace may not be used in header files (.h, .hpp), only in source files (.cpp).
GoodBad
example.h: ```cpp namespace crepe { void foo(); } ``` example.cpp: ```cpp #include "example.h" using namespace crepe; void foo() {} ``` example.h: ```cpp namespace crepe { template T foo(); } ``` example.hpp: ```cpp #include "example.h" using namespace crepe; template T foo(); ```
-
Getter and setter functions are appropriately prefixed with get_ and set_.
GoodBad
```cpp class Foo { public: int get_speed() const; void set_speed(int speed); private: int speed; }; ``` ```cpp class Foo { public: int speed() const; void set_speed(int speed); private: int speed; }; ```
-
A singleton's instance is always accessed using a getter function that instantiates its own class as a static variable within the getter function scope, instead of storing the instance as a member variable directly.
GoodBad
```cpp class Foo { Foo & get_instance() { static Foo instance; return instance; } }; ``` ```cpp Foo Foo::instance {}; class Foo { static Foo instance; Foo & get_instance() { return Foo::instance; } }; ```
-
Member variable default values should be directly defined in the class/struct declaration instead of using the constructor.
GoodBad
```cpp class Foo { int speed = 0; }; ``` ```cpp class Foo { Foo() : speed(0) {} int speed; }; ```
- Use of the `auto` type is *not* allowed, with the following exceptions: -
When naming the item type in a range-based for loop ```cpp for (auto & item : foo()) { // ... } ```
-
When calling template factory methods that explicitly name the return type in the function call signature ```cpp auto ptr = make_unique(); ```
-
When fetching a singleton instance ```cpp auto & mgr = crepe::api::Config::get_instance(); ```
-
Only use member initializer lists for non-trivial types.
GoodBad
```cpp class Foo { public: Foo() : bar("baz") {} private: std::string bar; }; ``` ```cpp class Foo { public: Foo() : bar(0) {} private: int bar; }; ```
-
C++-style structs should define default values for all non-trivial fields.
GoodBad
```cpp struct Foo { int bar = 0; std::string baz; }; ``` ```cpp struct Foo { int bar; std::string baz; }; ```
-
Declare incomplete classes instead of including the relevant header where possible (i.e. if you only need a reference or raw pointer).
GoodBad
```cpp class Bar; class Foo { Bar & bar; }; ``` ```cpp #include "Bar.h" class Foo { Bar & bar; }; ```
-
Template functions are only declared in a .h header, and defined in a matching .hpp header.
GoodBad
add.h: ```cpp template T add(T a, T b); #include "add.hpp" ``` add.hpp: ```cpp #include "add.h" template T add(T a, T b) { return a + b; } ``` add.h: ```cpp template T add(T a, T b) { return a + b; } ```
-
Where possible, end (initializer) lists with a trailing comma (e.g. with structs, enums)
GoodBad
```cpp enum Color { Red, Green, Blue, }; ``` ```cpp enum Color { Red, Green, Blue }; ```
-
#pragma should be used instead of include guards
GoodBad
```cpp #pragma once // ... ``` ```cpp #ifndef __INCLUDED_H #define __INCLUDED_H // ... #endif ```
-
Variables that are being moved always use the fully qualified std::move
GoodBad
```cpp using namespace std; string foo = "bar"; ref_fn(std::move(foo)); ``` ```cpp using namespace std; string foo = "bar"; ref_fn(move(foo)); ```
-
If possible, classes and structs are passed to functions by (const) reference
GoodBad
```cpp void foo(const Point & p); ``` ```cpp void foo(Point & p); void bar(Point p); ```
-
Follow the rule of five
GoodBad
```cpp class Foo { public: Foo(); ~Foo(); Foo(Foo &&) noexcept; Foo & operator = (const Foo &); Foo & operator = (Foo &&) noexcept; }; ``` ```cpp class Foo { public: Foo(); ~Foo(); }; ```
-
Ensure const-correctness > [!IMPORTANT] > C-style APIs that work on (possibly internal) references to structs can be > called from const member functions in C++. If the compiler allows you to > mark a function as `const` even though it has side effects, it should > **not** be marked as `const`.
GoodBad
```cpp class Foo { public: int get_value() const; void set_value(int new_value); const std::string & get_name() const; void set_name(const std::string & new_name); private: int value; std::string name; }; ``` ```cpp class Foo { public: int get_value(); void set_value(int new_value); std::string get_name(); void set_name(std::string new_name); private: int value; std::string name; }; ```
-
Files should be named after the class/struct/interface they implement
GoodBad
```cpp MyClass.h MyClass.cpp MyClass.hpp ``` ```cpp my_class.h myClass.cpp my-class.hpp ```
-
Implementations are not allowed in header files, except if the implementation - is `= default` - is `= delete` - is `{}` (empty) - only returns a constant literal
GoodBad
```cpp class Foo { public: int get_value() const { return 42; } }; ``` ```cpp class Foo { public: int calculate_value() const { int result = 0; // complex calculation return result; } }; ```
-
Use angle brackets (<>) only for including system headers and double quotes ("") for including other engine files. > [!NOTE] > Only files in the examples folder should include engine headers with angle > brackets
GoodBad
```cpp #include #include "facade/Sound.h" ``` ```cpp #include #include ```
-
Ensure exception safety by using RAII classes
GoodBad
```cpp auto foo = std::make_unique(); ``` ```cpp Foo* foo = new Foo(); // ... delete foo; ```
-
Do not use C-style memory management APIs (malloc, calloc, free)
GoodBad
```cpp Foo * foo = new Foo(); delete foo; ``` ```cpp Foo * foo = (Foo *) malloc(sizeof(Foo)); free(foo); ```
-
Prefix all class members with this->
GoodBad
```cpp void Foo::bar() { } void Foo::set_value(int value) { this->value = value; this->bar(); } ``` ```cpp void Foo::bar() { } void Foo::set_value(int new_value) { value = new_value; bar(); } ```
-
Assigning booleans should be done with the true/false literals instead of 0/1
GoodBad
```cpp bool foo = true; bool bar = false; ``` ```cpp bool foo = 1; bool bar = 0; ```
-
The reason for friend relations are documented
GoodBad
```cpp //! ComponentManager calls the private constructor of this class friend class ComponentManager; ``` ```cpp friend class ComponentManager; ```
-
Do not pick fixed-width integer types (unless required)
GoodBad
```cpp unsigned long long foo(); ``` ```cpp uint64_t foo(); ```
-
Utilize standard exceptions where appropriate (those found in <stdexcept>)
GoodBad
```cpp #include // ... if (foo == nullptr) { throw std::runtime_error("What is wrong"); } ``` ```cpp if (foo == nullptr) { std::cout << "What is wrong" << std::endl; exit(1); } ```
-
Mention the name of the class when throwing an exception
GoodBad
```cpp Foo::bar() { if (...) throw std::runtime_error("Foo: big error!"); } ``` ```cpp Foo::bar() { if (...) throw std::runtime_error("big error!"); } ```
-
Constructors of classes derived from Component should be protected and ComponentManager should be declared as a friend class.
GoodBad
```cpp class MyComponent : public Component { protected: MyComponent(...); //! Only ComponentManager is allowed to create components friend class ComponentManager; }; ``` ```cpp class MyComponent : public Component { public: MyComponent(...); }; ```
-
C++ std::format should be used instead of C-style format specifiers
GoodBad
```cpp std::string message = std::format("Hello, {}", name); dbg_logf("Here too: {}", 3); throw std::runtime_error(std::format("Or here: {}", 5)); ``` ```cpp char message[50]; sprintf(message, "Hello, %s", name); ```
-
Argument names should be added in .h files (not only in .cpp and .hpp files)
GoodBad
Foo.h: ```cpp void foo(int bar); ``` Foo.cpp: ```cpp void foo(int bar) { // ... } ``` Foo.h: ```cpp void foo(int); ``` Foo.cpp: ```cpp void foo(int bar) { // ... } ```
- Do not implement new classes as singletons -
Retrieving the first or last indices for iterators with a known or expected size should be done using .front() or .back() instead of by index
GoodBad
```cpp vector foo = { 1, 2, 3 }; int bar = foo.first(); ``` ```cpp vector foo = { 1, 2, 3 }; int bar = foo[0]; ```
-
Always explicitly check against NULL (for C APIs) or nullptr (for C++ APIs) when checking if a pointer is valid
GoodBad
```cpp string foo = "Hello world"; if (foo.c_str() == nullptr) // ... void * bar = malloc(); if (bar == NULL) // ... ``` ```cpp string foo = "Hello world"; if (!foo.c_str()) // ... void * bar = malloc(); if (!bar) // ... ```
## CMakeLists-specific - Make sure list arguments (e.g. sources, libraries) given to commands (e.g. `target_sources`, `target_link_libraries`) are on separate lines. This makes resolving merge conflicts when multiple sources were added by different people to the same CMakeLists.txt easier. ## GoogleTest-specific - Unit tests are not *required* to follow all code standards -
Private/protected members may be accessed using preprocessor tricks ```cpp // include unrelated headers before #define private public #define protected public // headers included after *will* be affected ```
- Each test source file defines tests within a single test suite (first parameter of `TEST()` / `TEST_F()` macro) - Test source files match their suite name (or test fixture name in the case of tests that use a fixture) - Tests that measure time or use delays must be [disabled][gtest-disable] (by prepending `DISABLED_` to the suite or case name). These tests will still be compiled, but will only run when the `test_main` binary is run with the `--gtest_also_run_disabled_tests` flag. # Structure - Files are placed in the appropriate directory: |Path|Purpose| |-|-| |`crepe/`|Auxiliary, managers| |`crepe/api/`|User-facing APIs| |`crepe/util/`|Standalone utilities and helper functions| |`crepe/system/`|(ECS) system classes| |`crepe/facade/`|Library façades| - Do not (indirectly) include private *dependency* headers in API header files, as these are no longer accessible when the engine is installed - All code is implemented under the `crepe` namespace. - Header files declare either a single class or symbols within a single namespace. # Documentation [Doxygen commands](https://www.doxygen.nl/manual/commands.html) - All documentation is written in U.S. English -
Doxygen commands are used with a backslash instead of an at-sign.
GoodBad
```cpp /** * \brief do something * * \param bar Magic number */ void foo(int bar); ``` ```cpp /** * @brief do something * * @param bar Magic number */ void foo(); ```
-
The default constructor and destructor aren't required to have a \brief description
GoodBad
```cpp Foo(); virtual ~Foo(); ``` ```cpp //! Create instance of Foo Foo(); //! Destroy instance of Foo virtual ~Foo(); ```
-
Parameter direction shouldn't be specified using Doxygen
GoodBad
```cpp /** * \param bar Reference to Bar */ void foo(const Bar & bar); ``` ```cpp /** * \param[in] bar Reference to Bar */ void foo(const Bar & bar); ```
-
Deleted functions shouldn't have Doxygen comments
GoodBad
```cpp // singleton Foo(const Foo &) = delete; Foo(Foo &&) = delete; Foo & operator=(const Foo &) = delete; Foo & operator=(Foo &&) = delete; ``` ```cpp //! Deleted copy constructor Foo(const Foo &) = delete; //! Deleted move constructor Foo(Foo &&) = delete; //! Deleted copy assignment operator Foo & operator=(const Foo &) = delete; //! Deleted move assignment operator Foo & operator=(Foo &&) = delete; ```
- Do not use markdown headings in Doxygen ## Documenting features Engine features are small 'building blocks' that the user (game developer) may reference when building a game with the engine. Features do not necessarily map 1-1 to engine components or systems. If a component or system has a single, distinct feature it should be named after that feature, not the component or system itself. The sources for these pages are located under `src/doc/feature/`, and have the following format: - A feature description which explains— - the purpose and function of the feature (focus on what it enables or achieves for the user) - additional information about when to implement the feature, such as specific use cases or scenarios - A list of 'see also' references to relevant classes and/or types - A **minimal** example to demonstrate how the feature is used. The example should be written such that the following is clear to the reader: - Which headers need to be included to utilize the feature - *Why* the example works, not what is happening in the example - Where is this code supposed to be called (e.g. inside scene/script functions) - Which restrictions should be kept in mind (e.g. copy/move semantics, max component instances, speed considerations) Features should be documented as clear and concise as possible, so the following points should be kept in mind: -
If a page expands on an example from another page, directly reference the other page using a cross-reference (`\ref`) in a `\note` block at the top of the page. ``` \note This page builds on top of the example shown in \ref feature_script ```
- When explaining the usage of specific functions, qualify them such that Doxygen is able to add a cross-reference or manually add a reference using the `\ref` command. - Users will likely copy-paste examples as-is, so do this yourself to check if the example code actually works! # Libraries - External libraries should be included as Git submodules under the `lib/` subdirectory - When adding new submodules, please set the `shallow` option to `true` in the [.gitmodules](./.gitmodules) file - When adding new libraries, please update the library version table in [readme\.md](./readme.md) [gtest-disable]: https://google.github.io/googletest/advanced.html#temporarily-disabling-tests