From c4e86c103b6438207ec86a8bb22ac4182d785832 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 09:56:12 +0100 Subject: Added examples to the code style --- contributing.md | 344 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 303 insertions(+), 41 deletions(-) (limited to 'contributing.md') diff --git a/contributing.md b/contributing.md index 775119a..720985e 100644 --- a/contributing.md +++ b/contributing.md @@ -18,66 +18,328 @@ # Code style - ASCII only + + ```cpp + // Good + std::string message = "Hello, world!"; + + // Bad + std::string message = "こんにちは世界"; + ``` + - Class names are always singular + + ```cpp + // Good + class Car {}; + + // Bad + class Cars {}; + ``` + - Explanatory comments are placed above the line(s) they are explaining -- Source files should only contain comments that plainly state what the code is - supposed to do -- Explanatory comments in headers may be used to clarify implementation design - decisions -- Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make - format` in the root folder of this repository to format all sources files) -- Header includes are split into paragraphs separated by a blank line. The - order is: + + ```cpp + // Good + // This function adds two numbers + int add(int a, int b) { + return a + b; + } + + // Bad + int add(int a, int b) { + return a + b; // This function adds two numbers + } + ``` + +- Source files should only contain comments that plainly state what the code is supposed to do + + ```cpp + // Good + // Initialize the engine + engine.init(); + + // Bad + // Initialize the engine with default settings and prepare it for running + engine.init(); + ``` + +- Explanatory comments in headers may be used to clarify implementation design decisions + + ```cpp + // Good + // This class handles the rendering process + class Renderer {}; + + // Bad + class Renderer {}; // This class handles the rendering process + ``` + +- Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make format` in the root folder of this repository to format all sources files) + + +- Header includes 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 -- 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). -- All engine-related code is implemented under the `crepe` namespace, - user-facing APIs under `crepe::api` (the folder structure should also reflect - this). + + ```cpp + // Good + #include + + #include "utils/helper.h" + + #include "main.h" + + // Bad + #include "main.h" + #include + #include "utils/helper.h" + ``` + +- 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). + + ```cpp + // Good + #include + + #include + + // Bad + #include + #include + ``` + +- All engine-related code is implemented under the `crepe` namespace, user-facing APIs under `crepe::api` (the folder structure should also reflect this). + + - `using namespace` may not be used in header files, only in source files. -- Do not (indirectly) include private *dependency* headers in API header files, - as these are no longer accessible when the engine is installed -- Getter and setter functions are appropriately prefixed with `get_` and - `set_`. -- Doxygen commands are used with a backslash instead of an at-sign (i.e. - `\brief` instead of `@brief`) -- 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: + + ```cpp + // Good + // header.h + namespace crepe { + void init(); + } + + // source.cpp + #include "header.h" + using namespace crepe; + void init() {} + + // Bad + // header.h + using namespace crepe; + void init(); + ``` + +- Do not (indirectly) include private *dependency* headers in API header files, as these are no longer accessible when the engine is installed + + ```cpp + // Good + // api.h + namespace crepe::api { + void start(); + } + + // Bad + // api.h + #include "private_dependency.h" + namespace crepe::api { + void start(); + } + ``` + +- Getter and setter functions are appropriately prefixed with `get_` and `set_`. + + ```cpp + // Good + class Car { + public: + int get_speed() const; + void set_speed(int speed); + private: + int speed; + }; + + // Bad + class Car { + public: + int speed() const; + void speed(int speed); + private: + int speed; + }; + ``` + +- Doxygen commands are used with a backslash instead of an at-sign (i.e. `\brief` instead of `@brief`) + + ```cpp + // Good + /// \brief This function adds two numbers + int add(int a, int b); + + // Bad + /// @brief This function adds two numbers + int add(int a, int b); + ``` + +- 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: ```cpp class Bad { - static Bad instance; - Bad & get_instance() { return instance; } + static Bad instance; + Bad & get_instance() { return instance; } }; class Good { - Good & get_instance() { - static Good instance; - return instance; - } + Good & get_instance() { + static Good instance; + return instance; + } + }; + ``` + +- Member variable default values should be directly defined in the class declaration instead of using the constructor. + + ```cpp + // Good + class Car { + private: + int speed = 0; + }; + + // Bad + class Car { + private: + int speed; + Car() : speed(0) {} }; ``` -- Member variable default values should be directly defined in the class - declaration instead of using the constructor. -- Header files declare either a single class or symbols within a single - namespace. + +- Header files declare either a single class or symbols within a single namespace. + + ```cpp + // Good + // car.h + namespace crepe { + class Car {}; + } + + // Bad + // car.h + namespace crepe { + class Car {}; + class Engine {}; + } + ``` + - Use of the `auto` type is not allowed, with the following exceptions: - When naming the item type in a range-based for loop - - When calling template factory methods that explicitly name the return type - in the function call signature + - When calling template factory methods that explicitly name the return type in the function call signature - When fetching a singleton instance + + ```cpp + // Good + for (auto item : items) {} + + auto instance = Singleton::get_instance(); + + // Bad + auto speed = car.get_speed(); + ``` + - Only use member initializer lists for non-trivial types. + + ```cpp + // Good + class Car { + public: + Car() : engine("V8") {} + private: + std::string engine; + }; + + // Bad + class Car { + public: + Car() : speed(0) {} + private: + int speed; + }; + ``` + - C++-style structs should define default values for all non-trivial fields. -- Declare incomplete classes instead of including the relevant header where - possible (i.e. if you only need a reference or pointer type). -- Template functions are only declared in a `.h` header, and defined in a - matching `.hpp` header. -- Where possible, end (initializer) lists with a trailing comma (e.g. with - structs, enums) + + ```cpp + // Good + struct Car { + std::string model = "Unknown"; + }; + + // Bad + struct Car { + std::string model; + Car() : model("Unknown") {} + }; + ``` + +- Declare incomplete classes instead of including the relevant header where possible (i.e. if you only need a reference or pointer type). + + ```cpp + // Good + class Engine; + class Car { + Engine* engine; + }; + + // Bad + #include "engine.h" + class Car { + Engine* engine; + }; + ``` + +- Template functions are only declared in a `.h` header, and defined in a matching `.hpp` header. + + ```cpp + // Good + // add.h + template + T add(T a, T b); + + // add.hpp + #include "add.h" + template + T add(T a, T b) { + return a + b; + } + + // Bad + // add.h + template + T add(T a, T b) { + return a + b; + } + ``` + +- Where possible, end (initializer) lists with a trailing comma (e.g. with structs, enums) + + ```cpp + // Good + enum Color { + Red, + Green, + Blue, + }; + + // Bad + enum Color { + Red, + Green, + Blue + }; + ``` ## CMakeLists specific -- cgit v1.2.3 From b082e63fe81a3650599aef79e4d56d5afb2a731a Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 5 Nov 2024 10:00:16 +0100 Subject: Improved example --- contributing.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'contributing.md') diff --git a/contributing.md b/contributing.md index 720985e..4c347bb 100644 --- a/contributing.md +++ b/contributing.md @@ -69,10 +69,15 @@ ```cpp // Good // This class handles the rendering process - class Renderer {}; + class Renderer { + // This method initializes the rendering context + void init_context(); + }; // Bad - class Renderer {}; // This class handles the rendering process + class Renderer { + void init_context(); // This method initializes the rendering context + }; ``` - Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make format` in the root folder of this repository to format all sources files) -- cgit v1.2.3 From 9572c5b35de2d13dbe7f942e3ecc50d28b36e9b8 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 14:21:45 +0100 Subject: update contributing.md --- contributing.md | 467 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 259 insertions(+), 208 deletions(-) (limited to 'contributing.md') diff --git a/contributing.md b/contributing.md index 4c347bb..38a83fd 100644 --- a/contributing.md +++ b/contributing.md @@ -11,351 +11,402 @@ `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 - - TODO: tagging / versions - # Code style +- Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make + format` in the root folder of this repository to format all sources files) - ASCII only +
GoodBad
```cpp - // Good + // crepe startup message std::string message = "Hello, world!"; - - // Bad - std::string message = "こんにちは世界"; ``` + + ```cpp + // crêpe startup message + std::string message = "こんにちは世界"; + ``` +
- Class names are always singular +
GoodBad
```cpp - // Good - class Car {}; - - // Bad - class Cars {}; + class Foo {}; ``` - -- Explanatory comments are placed above the line(s) they are explaining + ```cpp - // Good - // This function adds two numbers - int add(int a, int b) { - return a + b; - } - - // Bad - int add(int a, int b) { - return a + b; // This function adds two numbers - } + class Cars {}; ``` - -- Source files should only contain comments that plainly state what the code is supposed to do +
+- Source files contain the following types of comments: + - What is the code supposed to do (optional) + - Implementation details (if applicable) +- Header files contain the following types of comments: + - Usage 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 - // Good - // Initialize the engine - engine.init(); - - // Bad - // Initialize the engine with default settings and prepare it for running - engine.init(); + int add(int a, int b) { + // add numbers + int out = a + b; + return out; + } ``` - -- Explanatory comments in headers may be used to clarify implementation design decisions + ```cpp - // Good - // This class handles the rendering process - class Renderer { - // This method initializes the rendering context - void init_context(); - }; - - // Bad - class Renderer { - void init_context(); // This method initializes the rendering context - }; + int add(int a, int b) { + int out = a + b; // add numbers + return out; + } ``` - -- Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make format` in the root folder of this repository to format all sources files) - - -- Header includes are split into paragraphs separated by a blank line. The order is: +
+- Header includes 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 - ```cpp - // Good - #include - - #include "utils/helper.h" - - #include "main.h" - - // Bad - #include "main.h" - #include - #include "utils/helper.h" - ``` + > [!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). -- 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). +
GoodBad
```cpp - // Good + #include #include - - #include - - // Bad - #include - #include - ``` -- All engine-related code is implemented under the `crepe` namespace, user-facing APIs under `crepe::api` (the folder structure should also reflect this). + #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" + ``` +
- `using namespace` may not be used in header files, only in source files. +
GoodBad
+ example.h: ```cpp - // Good - // header.h namespace crepe { - void init(); + void foo(); } + ``` - // source.cpp - #include "header.h" - using namespace crepe; - void init() {} - - // Bad - // header.h + example.cpp: + ```cpp + #include "example.h" using namespace crepe; - void init(); + void foo() {} ``` + -- Do not (indirectly) include private *dependency* headers in API header files, as these are no longer accessible when the engine is installed - + example.h: ```cpp - // Good - // api.h - namespace crepe::api { - void start(); + namespace crepe { + template + T foo(); } + ``` - // Bad - // api.h - #include "private_dependency.h" - namespace crepe::api { - void start(); - } + 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 - // Good - class Car { + class Foo { public: - int get_speed() const; - void set_speed(int speed); + int get_speed() const; + void set_speed(int speed); private: - int speed; + int speed; }; - // Bad - class Car { + ``` + + + ```cpp + class Foo { public: - int speed() const; - void speed(int speed); + int speed() const; + void set_speed(int speed); private: - int speed; + int speed; }; ``` +
-- Doxygen commands are used with a backslash instead of an at-sign (i.e. `\brief` instead of `@brief`) +- 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 - // Good - /// \brief This function adds two numbers - int add(int a, int b); - - // Bad - /// @brief This function adds two numbers - int add(int a, int b); + class Foo { + Foo & get_instance() { + static Foo instance; + return instance; + } + }; ``` - -- 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: + ```cpp - class Bad { - static Bad instance; - Bad & get_instance() { return instance; } - }; + Foo Foo::instance {}; - class Good { - Good & get_instance() { - static Good instance; - return instance; - } + class Foo { + static Foo instance; + Foo & get_instance() { return Foo::instance; } }; + ``` +
-- Member variable default values should be directly defined in the class declaration instead of using the constructor. +- Member variable default values should be directly defined in the class/struct + declaration instead of using the constructor. +
GoodBad
```cpp - // Good - class Car { - private: - int speed = 0; + class Foo { + int speed = 0; }; - // Bad - class Car { - private: - int speed; - Car() : speed(0) {} - }; ``` - -- Header files declare either a single class or symbols within a single namespace. + ```cpp - // Good - // car.h - namespace crepe { - class Car {}; - } - - // Bad - // car.h - namespace crepe { - class Car {}; - class Engine {}; - } + 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 - - When calling template factory methods that explicitly name the return type in the function call signature +
+ +- 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 - // Good - for (auto item : items) {} - - auto instance = Singleton::get_instance(); - - // Bad - auto speed = car.get_speed(); - ``` + ```cpp + auto & mgr = crepe::api::Config::get_instance(); + ``` - Only use member initializer lists for non-trivial types. +
GoodBad
```cpp - // Good - class Car { + class Foo { public: - Car() : engine("V8") {} + Foo() : bar("baz") {} private: - std::string engine; + std::string bar; }; - // Bad - class Car { + ``` + + + ```cpp + class Foo { public: - Car() : speed(0) {} + Foo() : bar(0) {} private: - int speed; + int bar; }; ``` +
- C++-style structs should define default values for all non-trivial fields. +
GoodBad
```cpp - // Good - struct Car { - std::string model = "Unknown"; + struct Foo { + int bar; + std::string baz; }; - - // Bad - struct Car { - std::string model; - Car() : model("Unknown") {} + ``` + + + ```cpp + struct Foo { + int bar = 0; + std::string baz; }; ``` +
-- Declare incomplete classes instead of including the relevant header where possible (i.e. if you only need a reference or pointer type). +- Declare incomplete classes instead of including the relevant header where + possible (i.e. if you only need a reference or raw pointer). +
GoodBad
```cpp - // Good - class Engine; - class Car { - Engine* engine; + class Bar; + class Foo { + Bar & bar; }; - // Bad - #include "engine.h" - class Car { - Engine* engine; + ``` + + + ```cpp + #include "Bar.h" + class Foo { + Bar & bar; }; ``` +
-- Template functions are only declared in a `.h` header, and defined in a matching `.hpp` header. +- Template functions are only *declared* in a `.h` header, and *defined* in a + matching `.hpp` header. +
GoodBad
+ add.h: ```cpp - // Good - // add.h template T add(T a, T b); + + #include "add.hpp" + ``` - // add.hpp + add.hpp: + ```cpp #include "add.h" + template T add(T a, T b) { - return a + b; + return a + b; } - - // Bad - // add.h + ``` + + + add.h: + ```cpp template T add(T a, T b) { - return a + b; + return a + b; } ``` +
-- Where possible, end (initializer) lists with a trailing comma (e.g. with structs, enums) +- Where possible, end (initializer) lists with a trailing comma (e.g. with + structs, enums) +
GoodBad
```cpp - // Good enum Color { - Red, - Green, - Blue, + Red, + Green, + Blue, }; - // Bad + ``` + + + ```cpp enum Color { - Red, - Green, - Blue + Red, + Green, + Blue }; ``` +
+- `#pragma` should be used instead of include guards +
GoodBad
-## CMakeLists specific + ```cpp + #pragma once + + // ... + ``` + + + ```cpp + #ifndef __INCLUDED_H + #define __INCLUDED_H + + // ... + + #endif + ``` +
+ +## 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. +# Structure + +- All engine-related code is implemented under the `crepe` namespace, + user-facing APIs under `crepe::api` (the folder structure should also reflect + this). +- Do not (indirectly) include private *dependency* headers in API header files, + as these are no longer accessible when the engine is installed +- Header files declare either a single class or symbols within a single + namespace. +- TODO: folder structure + # Documentation - 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(); + ``` +
# Libraries -- cgit v1.2.3 From 5f75bdbf3d38c94baeae52f4c4889f147ec6885e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 15:34:31 +0100 Subject: move systems + update contributing.md --- contributing.md | 156 +++++++++++++++++++++-------------- src/crepe/CMakeLists.txt | 12 +-- src/crepe/CollisionSystem.cpp | 7 -- src/crepe/CollisionSystem.h | 11 --- src/crepe/ParticleSystem.cpp | 62 -------------- src/crepe/ParticleSystem.h | 18 ---- src/crepe/PhysicsSystem.cpp | 62 -------------- src/crepe/PhysicsSystem.h | 11 --- src/crepe/RenderSystem.cpp | 42 ---------- src/crepe/RenderSystem.h | 17 ---- src/crepe/SDLContext.h | 2 +- src/crepe/ScriptSystem.cpp | 47 ----------- src/crepe/ScriptSystem.h | 26 ------ src/crepe/System.h | 22 ----- src/crepe/system/CMakeLists.txt | 15 ++++ src/crepe/system/CollisionSystem.cpp | 7 ++ src/crepe/system/CollisionSystem.h | 11 +++ src/crepe/system/ParticleSystem.cpp | 62 ++++++++++++++ src/crepe/system/ParticleSystem.h | 18 ++++ src/crepe/system/PhysicsSystem.cpp | 62 ++++++++++++++ src/crepe/system/PhysicsSystem.h | 11 +++ src/crepe/system/RenderSystem.cpp | 42 ++++++++++ src/crepe/system/RenderSystem.h | 17 ++++ src/crepe/system/ScriptSystem.cpp | 47 +++++++++++ src/crepe/system/ScriptSystem.h | 26 ++++++ src/crepe/system/System.h | 22 +++++ src/example/asset_manager.cpp | 2 - src/example/particle.cpp | 13 +-- src/example/physics.cpp | 7 +- src/example/rendering.cpp | 4 +- src/example/script.cpp | 2 +- 31 files changed, 447 insertions(+), 416 deletions(-) delete mode 100644 src/crepe/CollisionSystem.cpp delete mode 100644 src/crepe/CollisionSystem.h delete mode 100644 src/crepe/ParticleSystem.cpp delete mode 100644 src/crepe/ParticleSystem.h delete mode 100644 src/crepe/PhysicsSystem.cpp delete mode 100644 src/crepe/PhysicsSystem.h delete mode 100644 src/crepe/RenderSystem.cpp delete mode 100644 src/crepe/RenderSystem.h delete mode 100644 src/crepe/ScriptSystem.cpp delete mode 100644 src/crepe/ScriptSystem.h delete mode 100644 src/crepe/System.h create mode 100644 src/crepe/system/CMakeLists.txt create mode 100644 src/crepe/system/CollisionSystem.cpp create mode 100644 src/crepe/system/CollisionSystem.h create mode 100644 src/crepe/system/ParticleSystem.cpp create mode 100644 src/crepe/system/ParticleSystem.h create mode 100644 src/crepe/system/PhysicsSystem.cpp create mode 100644 src/crepe/system/PhysicsSystem.h create mode 100644 src/crepe/system/RenderSystem.cpp create mode 100644 src/crepe/system/RenderSystem.h create mode 100644 src/crepe/system/ScriptSystem.cpp create mode 100644 src/crepe/system/ScriptSystem.h create mode 100644 src/crepe/system/System.h (limited to 'contributing.md') diff --git a/contributing.md b/contributing.md index 38a83fd..35d777d 100644 --- a/contributing.md +++ b/contributing.md @@ -1,11 +1,15 @@ -# Contributing new code +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 - -# Git - - 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`) @@ -17,8 +21,9 @@ - Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make format` in the root folder of this repository to format all sources files) -- ASCII only -
GoodBad
+-
+ ASCII only +
GoodBad
```cpp // crepe startup message @@ -30,9 +35,10 @@ // crêpe startup message std::string message = "こんにちは世界"; ``` -
-- Class names are always singular -
GoodBad
+
+-
+ Class names are always singular +
GoodBad
```cpp class Foo {}; @@ -42,7 +48,7 @@ ```cpp class Cars {}; ``` -
+
- Source files contain the following types of comments: - What is the code supposed to do (optional) - Implementation details (if applicable) @@ -50,8 +56,9 @@ - Usage 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
+-
+ Comments are placed *above* the line(s) they are explaining +
GoodBad
```cpp int add(int a, int b) { @@ -68,7 +75,7 @@ return out; } ``` -
+
- Header includes are split into paragraphs separated by a blank line. The order is: 1. system headers (using `<`brackets`>`) @@ -80,6 +87,7 @@ > 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 @@ -100,9 +108,10 @@ #include #include "api/Sprite.h" ``` -
-- `using namespace` may not be used in header files, only in source files. -
GoodBad
+
+-
+ using namespace may not be used in header files, only in source files. +
GoodBad
example.h: ```cpp @@ -134,10 +143,12 @@ template T foo(); ``` -
+
-- Getter and setter functions are appropriately prefixed with `get_` and `set_`. -
GoodBad
+-
+ Getter and setter functions are appropriately prefixed with get_ + and set_. +
GoodBad
```cpp class Foo { @@ -160,12 +171,12 @@ int speed; }; ``` -
- -- A singleton's instance is always accessed using a getter function that +
+-
+ 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
+ scope, instead of storing the instance as a member variable directly. +
GoodBad
```cpp class Foo { @@ -186,11 +197,11 @@ }; ``` -
- -- Member variable default values should be directly defined in the class/struct +
+-
+ Member variable default values should be directly defined in the class/struct declaration instead of using the constructor. -
GoodBad
+
GoodBad
```cpp class Foo { @@ -206,28 +217,39 @@ 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: - + -
+ 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 +
+ -
+ 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 +
+ -
+ When fetching a singleton instance + + ```cpp auto & mgr = crepe::api::Config::get_instance(); ``` +
-- Only use member initializer lists for non-trivial types. -
GoodBad
+-
+ Only use member initializer lists for non-trivial types. +
GoodBad
```cpp class Foo { @@ -248,10 +270,10 @@ int bar; }; ``` -
- -- C++-style structs should define default values for all non-trivial fields. -
GoodBad
+
+-
+ C++-style structs should define default values for all non-trivial fields. +
GoodBad
```cpp struct Foo { @@ -267,11 +289,11 @@ std::string baz; }; ``` -
- -- Declare incomplete classes instead of including the relevant header where +
+-
+ Declare incomplete classes instead of including the relevant header where possible (i.e. if you only need a reference or raw pointer). -
GoodBad
+
GoodBad
```cpp class Bar; @@ -288,11 +310,11 @@ Bar & bar; }; ``` -
- -- Template functions are only *declared* in a `.h` header, and *defined* in a - matching `.hpp` header. -
GoodBad
+
+-
+ Template functions are only declared in a .h header, and + defined in a matching .hpp header. +
GoodBad
add.h: ```cpp @@ -320,11 +342,11 @@ return a + b; } ``` -
- -- Where possible, end (initializer) lists with a trailing comma (e.g. with +
+-
+ Where possible, end (initializer) lists with a trailing comma (e.g. with structs, enums) -
GoodBad
+
GoodBad
```cpp enum Color { @@ -343,9 +365,10 @@ Blue }; ``` -
-- `#pragma` should be used instead of include guards -
GoodBad
+
+-
+ #pragma should be used instead of include guards +
GoodBad
```cpp #pragma once @@ -362,7 +385,7 @@ #endif ``` -
+
## CMakeLists-specific @@ -373,20 +396,25 @@ # Structure -- All engine-related code is implemented under the `crepe` namespace, - user-facing APIs under `crepe::api` (the folder structure should also reflect - this). +- 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| - 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. -- TODO: folder structure # Documentation - All documentation is written in U.S. English -- Doxygen commands are used with a backslash instead of an at-sign. -
GoodBad
+-
+ Doxygen commands are used with a backslash instead of an at-sign. +
GoodBad
```cpp /** @@ -406,7 +434,7 @@ */ void foo(); ``` -
+
# Libraries diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index d938eb8..7f976db 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -3,17 +3,11 @@ target_sources(crepe PUBLIC Sound.cpp SoundContext.cpp Particle.cpp - ParticleSystem.cpp SDLApp.cpp ComponentManager.cpp Component.cpp - ScriptSystem.cpp - PhysicsSystem.cpp - CollisionSystem.cpp Collider.cpp SDLContext.cpp - - RenderSystem.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -24,15 +18,11 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES ComponentManager.h ComponentManager.hpp Component.h - System.h - ScriptSystem.h - PhysicsSystem.h - CollisionSystem.h Collider.h SDLContext.h - RenderSystem.h ) add_subdirectory(api) add_subdirectory(util) +add_subdirectory(system) diff --git a/src/crepe/CollisionSystem.cpp b/src/crepe/CollisionSystem.cpp deleted file mode 100644 index 55e0fdc..0000000 --- a/src/crepe/CollisionSystem.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "CollisionSystem.h" - -using namespace crepe; - -CollisionSystem::CollisionSystem() {} - -void CollisionSystem::update() {} diff --git a/src/crepe/CollisionSystem.h b/src/crepe/CollisionSystem.h deleted file mode 100644 index 1e9f1aa..0000000 --- a/src/crepe/CollisionSystem.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -namespace crepe { - -class CollisionSystem { -public: - CollisionSystem(); - void update(); -}; - -} // namespace crepe diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp deleted file mode 100644 index af6c550..0000000 --- a/src/crepe/ParticleSystem.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include - -#include "api/ParticleEmitter.h" - -#include "ComponentManager.h" -#include "ParticleSystem.h" - -using namespace crepe; - -ParticleSystem::ParticleSystem() : elapsed_time(0.0f) {} - -void ParticleSystem::update() { - ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> emitters - = mgr.get_components_by_type(); - float delta_time = 0.10; - for (ParticleEmitter & emitter : emitters) { - float update_amount = 1 / static_cast(emitter.emission_rate); - for (float i = 0; i < delta_time; i += update_amount) { - emit_particle(emitter); - } - for (size_t j = 0; j < emitter.particles.size(); j++) { - if (emitter.particles[j].active) { - emitter.particles[j].update(delta_time); - } - } - } -} - -void ParticleSystem::emit_particle(ParticleEmitter & emitter) { - Position initial_position = {emitter.position.x, emitter.position.y}; - float random_angle = 0.0f; - if (emitter.max_angle < emitter.min_angle) { - random_angle = ((emitter.min_angle - + (std::rand() - % (static_cast(emitter.max_angle + 360 - - emitter.min_angle + 1)))) - % 360); - } else { - random_angle = emitter.min_angle - + (std::rand() - % (static_cast(emitter.max_angle - - emitter.min_angle + 1))); - } - float angle_in_radians = random_angle * (M_PI / 180.0f); - float random_speed_offset = (static_cast(std::rand()) / RAND_MAX) - * (2 * emitter.speed_offset) - - emitter.speed_offset; - float velocity_x - = (emitter.speed + random_speed_offset) * std::cos(angle_in_radians); - float velocity_y - = (emitter.speed + random_speed_offset) * std::sin(angle_in_radians); - Position initial_velocity = {velocity_x, velocity_y}; - for (size_t i = 0; i < emitter.particles.size(); i++) { - if (!emitter.particles[i].active) { - emitter.particles[i].reset(emitter.end_lifespan, initial_position, - initial_velocity); - break; - } - } -} diff --git a/src/crepe/ParticleSystem.h b/src/crepe/ParticleSystem.h deleted file mode 100644 index ad96eb0..0000000 --- a/src/crepe/ParticleSystem.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "api/ParticleEmitter.h" - -namespace crepe { - -class ParticleSystem { -public: - ParticleSystem(); - void update(); - -private: - void emit_particle(ParticleEmitter & emitter); //emits a new particle - - float elapsed_time; //elapsed time since the last emission -}; - -} // namespace crepe diff --git a/src/crepe/PhysicsSystem.cpp b/src/crepe/PhysicsSystem.cpp deleted file mode 100644 index 16f4c10..0000000 --- a/src/crepe/PhysicsSystem.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include - -#include "api/Force.h" -#include "api/Rigidbody.h" -#include "api/Transform.h" - -#include "ComponentManager.h" -#include "PhysicsSystem.h" - -using namespace crepe; -using namespace crepe::api; - -PhysicsSystem::PhysicsSystem() {} - -void PhysicsSystem::update() { - ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> rigidbodies - = mgr.get_components_by_type(); - std::vector> transforms - = mgr.get_components_by_type(); - - for (Rigidbody & rigidbody : rigidbodies) { - - switch (rigidbody.body_type) { - case BodyType::DYNAMIC: - for (Transform & transform : transforms) { - if (transform.game_object_id == rigidbody.game_object_id) { - rigidbody.velocity_x = 0; - rigidbody.velocity_y = 0; - std::vector> forces - = mgr.get_components_by_id( - rigidbody.game_object_id); - rigidbody.velocity_y - += rigidbody.gravity_scale * 1 * rigidbody.mass; - - for (Force & force : forces) { - rigidbody.velocity_x += force.force_x; - rigidbody.velocity_y += force.force_y; - } - - std::cout << "before transform.postion.x " - << transform.position.x << std::endl; - std::cout << "before transform.postion.y " - << transform.position.y << std::endl; - transform.position.x += rigidbody.velocity_x; - transform.position.y += rigidbody.velocity_y; - std::cout << "after transform.postion.x " - << transform.position.x << std::endl; - std::cout << "after transform.postion.y " - << transform.position.y << std::endl; - } - } - break; - case BodyType::KINEMATIC: - break; //(scripts) - case BodyType::STATIC: - break; //(unmoveable objects) - default: - break; - } - } -} diff --git a/src/crepe/PhysicsSystem.h b/src/crepe/PhysicsSystem.h deleted file mode 100644 index 33b4072..0000000 --- a/src/crepe/PhysicsSystem.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -namespace crepe { - -class PhysicsSystem { -public: - PhysicsSystem(); - void update(); -}; - -} // namespace crepe diff --git a/src/crepe/RenderSystem.cpp b/src/crepe/RenderSystem.cpp deleted file mode 100644 index fae93f0..0000000 --- a/src/crepe/RenderSystem.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include - -#include "api/Sprite.h" -#include "api/Transform.h" -#include "util/log.h" - -#include "ComponentManager.h" -#include "RenderSystem.h" -#include "SDLContext.h" - -using namespace crepe; -using namespace crepe::api; - -RenderSystem::RenderSystem() { dbg_trace(); } - -RenderSystem::~RenderSystem() { dbg_trace(); } - -RenderSystem & RenderSystem::get_instance() { - static RenderSystem instance; - return instance; -} - -void RenderSystem::update() { - - ComponentManager & mgr = ComponentManager::get_instance(); - - std::vector> sprites - = mgr.get_components_by_type(); - - SDLContext & render = SDLContext::get_instance(); - render.clear_screen(); - - for (const Sprite & sprite : sprites) { - std::vector> transforms - = mgr.get_components_by_id(sprite.game_object_id); - for (const Transform & transform : transforms) { - render.draw(sprite, transform); - } - } - render.present_screen(); -} diff --git a/src/crepe/RenderSystem.h b/src/crepe/RenderSystem.h deleted file mode 100644 index 4b910a4..0000000 --- a/src/crepe/RenderSystem.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "System.h" - -namespace crepe { - -class RenderSystem : public System { - -public: - static RenderSystem & get_instance(); - void update(); - -private: - RenderSystem(); - ~RenderSystem(); -}; -} // namespace crepe diff --git a/src/crepe/SDLContext.h b/src/crepe/SDLContext.h index 4d9c1bc..ea05c7b 100644 --- a/src/crepe/SDLContext.h +++ b/src/crepe/SDLContext.h @@ -6,7 +6,7 @@ #include "api/Sprite.h" #include "api/Transform.h" -#include "RenderSystem.h" +#include "system/RenderSystem.h" namespace crepe::api { class Texture; diff --git a/src/crepe/ScriptSystem.cpp b/src/crepe/ScriptSystem.cpp deleted file mode 100644 index 171b490..0000000 --- a/src/crepe/ScriptSystem.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -#include -#include - -#include "api/BehaviorScript.h" -#include "api/Script.h" -#include "util/log.h" - -#include "ComponentManager.h" -#include "ScriptSystem.h" - -using namespace std; -using namespace crepe; -using namespace crepe::api; - -ScriptSystem::ScriptSystem() { dbg_trace(); } -ScriptSystem::~ScriptSystem() { dbg_trace(); } - -ScriptSystem & ScriptSystem::get_instance() { - static ScriptSystem instance; - return instance; -} - -void ScriptSystem::update() { - using namespace std; - dbg_trace(); - - forward_list