January 12, 2019

In this post I will explain how to create example project using SFML 2.5 and EnTT using modern C++, Conan and CMake. If you want just to get it done, you can skip Introduction part.

Introduction

What is Conan? C/C++ package manager

What is SFML? Simple and Fast Multimedia Library

What is EnTT? Entity-Component System library for modern C++

My platform(both linux):

desktop- Elementary OS Juno, Intel Pentium G3258, GeForce 950, g++ 7.3.0

netbook- newest Manjaro, ASUS EeeBook X205TA

Driven by the desire to understand and use ECS for creating game I started want to create simple game another way. But when I started configuring project I found out that the way of how configure CMake config for SFML change since 2.4 version. I updated my config, it worked fine but I started to wonder if there is better way to handle this task. I didn't write any serious stuff in Rust, but I still am amazed how working with Cargo(Rust package manager) was easy. After that I started to look for anything like that for C++. I checked only two of them. First one- vcpk- was quite easy to use, except one thing- building SFML fails on linking and I didn't manage way to handle that. Second one is in the Conan and I did manage way to handle it and that is why I wrote this post.

Requirements

C++ compiler with at least C++17 support(required by EnTT)

Conan installed in our system- Conan docs

CMake

Prepare project

First- open terminal and add remote lists of packages to our package manager:

$ conan remote add bincrafters https://api.bintray.com/conan/bincrafters/public-conan $ conan remote add skypjack https://api.bintray.com/conan/skypjack/conan

Sidenote for those who use GCC >= 5.1:

$ conan profile new default --detect # Generates default profile detecting GCC and sets old ABI $ conan profile update settings.compiler.libcxx=libstdc++11 default # Sets libcxx to C++11 ABI If you are using GCC compiler >= 5.1, Conan will set the compiler.libcxx to the old ABI for backwards compatibility. You can change this with the following commands: You will find more information in How to manage the GCC >= 5 ABI

Set compiler to compile C++17:

$ conan profile update settings.cppstd=17 default

After that create empty folder and open it in terminal. Then create in root folder of your project conanfile.txt with this:

[requires] sfml/2.5.1@bincrafters/stable entt/3.x.y-WIP@skypjack/stable [options] sfml:graphics=True sfml:window=True sfml:audio=False sfml:network=False [generators] cmake

In the requires section are required libraries, options contain options for each package and in generators we can specify which make system we will use. More info here

We also need to prepare basic CMake file which will be using our conan generated files.

cmake_minimum_required(VERSION 2.8.12) project(conanAdventures) set(CMAKE_CXX_STANDARD 17) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup() add_executable(conanAdventures main.cpp) target_link_libraries(conanAdventures ${CONAN_LIBS})

And at least, lets create main.cpp file in project root folder with code that is combination of examples for SFML and EnTT:

#include <entt/entt.hpp> #include <SFML/Graphics.hpp> #include <cstdint> struct position { float x; float y; }; struct velocity { float dx; float dy; }; struct shape { sf::RectangleShape rectangle; sf::Vector2f size; }; void update(entt::registry<>& registry) { auto transform = registry.view (); for(auto entity: transform) { // gets only the components that are going to be used ... auto &vel = transform.get (entity); vel.dx = 0.; vel.dy = 0.; // ... } } void update(std::uint64_t dt, entt::registry<>& registry) { registry.view ().each([dt](const auto, auto &pos, auto &vel) { // gets all the components of the view at once ... pos.x += vel.dx * dt; pos.y += vel.dy * dt; // ... }); } int main() { entt::registry registry; std::uint64_t dt = 16; for(auto i = 0; i < 10; ++i) { auto entity = registry.create(); registry.assign (entity, i * 1.f, i * 1.f); if(i % 2 == 0) { registry.assign (entity, i * .1f, i * .1f); } } sf::RenderWindow sfmlWin(sf::VideoMode(600, 360), "Conan adventure begins!"); while (sfmlWin.isOpen()) { sf::Event e; while (sfmlWin.pollEvent(e)) { switch (e.type) { case sf::Event::EventType::Closed: sfmlWin.close(); break; } } update(dt, registry); update(registry); sfmlWin.clear(sf::Color(0, 116, 217)); sfmlWin.display(); } return 0; }

Build it

Now I suppose that you have everything you need to build your project.

Make sure that you are in root directory of project in terminal and type this commands:

mkdir build && cd build - doesn't need explanation, I guess

conan install .. - installs the requirements specified in a recipe(conanfile.txt)

Now create cmake project and build it:

(win) $ cmake .. -G "Visual Studio 15 Win64" $ cmake --build . --config Release (linux, mac) $ cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release $ cmake --build .

Lets run it:

$ ./bin/conanAdventure

Tags: cpp, programming