Making a game using an ECS architecture (C++): Part 1 - Introduction
This series is for someone who wishes to create their own game using C++, using an ECS architecture.
Basic knowledge of C++ and games are required.
ECS stands for entity, component, system, which are the three major elements of this architecture.
- Everything, whether a character or object in game, is an ENTITY.
- Any data that this character has, for example HP, MP, strength, are stored in COMPONENTS.
- SYSTEMS perform calculations and processes different functions in the update of every game loop.
If you are unfamiliar with game programming, everything in a game updates/renders in a loop. All characters in a game may seem to be able to move at the same time, but in actual fact they are moving less than a few milliseconds after one another.
The simplest form of game loop with 1 player and 1 enemy character would be:
bool bIsExitGame = false; int main() { // Anything that only executes once when the game starts goes here. // Create a dummy player and enemy (assuming the class is already implemented). CPlayer* pPlayer = new CPlayer(); CCharacter* pEnemy = new CCharacter(); // Anything that executes once every time the game loops goes here. // Assuming that something inside the loop will set bIsExitGame to true, otherwise an infinite loop will occur. while (!bIsExitGame) { // Do any calculations and process functions here. pPlayer->Update(); pEnemy->Update(); // Render images here. pPlayer->Render(); pEnemy->Render(); } return 0; }
Where the Update
function of pPlayer
and pEnemy
might do something like setting the next position of the character using keyboard inputs (for pPlayer
), or player's current position (for pEnemy
), and the Render
function creates the image of the character.
Since this series is only concerned with building the ECS architecture, a library will be used for basic functions such as displaying an image on the screen and creating a window. There are many libraries available, for example:
- DxLib
- SFML
- raylib
Of course, there are many more libraries with many functions that are available, like:
- Godot
- Unity
- Unreal
However, for the sake of learning from scratch, this series will be using a library with minimal functions, DxLib. This library is apparaently famous in Japan for people creating games as a hobby, but not used so much outside of Japan. The reason I'm using it is because it's the first library I found, but this series should work for whatever library you decide to use, as I will use my own implementations as much as possible, so that the knowledge provided here is usuable for all platforns.
Now, one of the worst practices in programming is "reinventing the wheel", where you try to create something from scratch, when there is already a commercially available library/tool available. This series goes against this concept for the sake of learning. Although it might not be the best, fastest, efficient code, it will help you learn how to do things from scratch, as it is inevitable that you will need to create something from scratch one day, if you are planning for something bigger.
The next part will be about importing DxLib into your project and create a simple window.