top of page

2D SFML Engine

Recently I have been spending my spare time working on a new 2D engine in C++. The engine is built off of SFML (Simple and Fast Media Library) [2] which provides a platform independent layer with several modules that handle the following: System, window, graphics, networking and audio. SFML is a great base to build off of because it lets you skip the tedious beginnings of creating a multi-platform window and rendering system. SFML renders using OpenGL and is low level; focusing on providing basic functionalities without limiting the scope, architecture or design of your program. I have been using Trello [1] to track my tasks which as been working great.

Entity Component System

The main goal of this engine was to try my hand at writing an efficient and modern Entity Component System. I read many many articles on various ECS designs and eventually jumped into writing my own. I decided to go with an ECS where entities are simple POD's (plain old data) which contain a vector of handles to components (and keeps track of the component type). More on handles later. Components of each type are then stored in a custom vector like memory allocator which holds them all in contiguous memory and only ever adds components to an empty space or to the end. It pre-allocates a large pool of memory (user defined) so that it can reduce the chances of it having to perform a reallocation. Contiguous memory of components means that it is very fast to iterate over all components of a single type because there will be 0 (or as close as possible to 0) cache misses. This leads into the 3rd aspect of the ECS which is systems. Systems are essentially a module that run over a list of all components of a certain type (or multiple types). For example, I have a "Movement System" which operates on Transform Components. It handles updating the position / rotation of the transform component if it has any movement values set. There is a lot of data and information about this style of ECS, so I won't go into huge detail onto it. I find it more useful (and hopefully others do too) to show an overview of the actual implementation:

Handle System

I set up a handle system based off of Randy Gaul's SEL engine [8]. The system allocates memory for you when requested and returns a handle which allows you to access this memory. This works like a shared pointer except that the handle will properly update even if the memory has changed. The handle itself has two forms:

- Base handle (contains an index and a counter)

- Template handle (child of a base handle, templated to a type T which it holds and uses when the handle is requested to retrieve the data. This means you don't need to cast the type or know what it is, as it is saved in the template.

Post Mortem

Overall I think the engine works decently and does its job of being an efficient ECS well. I think the handle system is good in practice, but turns out to be a nightmare when trying to debug things because you cannot directly access the pointer without looking it up in the allocator (which is tedious). This handle system was based off of Randy Gaul's SEL engine [8], but I get the feeling that Randy never tried to use his engine to build a game. Once it is working, it works great, but the process of development has too many opportunities for pitfalls and frustration, which is where you spent most of your time when developing games.

The next step for me with this engine was / is to build a Python / Lua scripting system so that I don't have to directly interface with the engine. This would (hopefully) provide less opportunity to create bugs and errors when doing things in C++. It would allow better separation of game logic and engine code.

I may re-approach this engine in the future, it has a lot of room for adding features and development but right now I feel like I am not 100% happy with the final outcome of the engine core. It was a great experience setting up my first ECS and learning all about fast memory based architectures. I also enjoyed reading about various design patterns and spending time incorporating and designing them into my engine.

SFML is an amazing library and I would 100% recommend it to anyone getting into C++ 2D game development. It gives you so much freedom but allows you to skip a large amount of tedious, platform specific boilerplate that once you have done it before, isn't very enjoyable to setup again. It also allows you to extend the library to perfectly fit your needs and build features on top of it.


bottom of page