DEV Community

Gryfenfer
Gryfenfer

Posted on • Edited on

How to install imgui-sfml from conan

Conan is a powerful C++ package manager which simplify a lot the dependency managment in your projects.

Here I will talk about the installation of imgui-sfml from the perspective of someone who has never used it.

Requirements

To follow this tutorial you will need to have on your machine :

  • Python3 (we will use pip to install conan)
  • CMake

Conan Installation

First we will need to install conan.
In your project directory run

$ python -m venv venv
$ source venv/bin/activate
$ pip install conan
$ conan
$ conan remote add bincrafters https://bincrafters.jfrog.io/artifactory/api/conan/conan
Enter fullscreen mode Exit fullscreen mode

Here is the expected output :
Output of the command  raw `conan` endraw

To deactivate the virtualenv just run

$ deactivate

Let's build the project

In this section we will use CMake but if you want to use any other generator the documentation is here.

As there is no package for imgui-sfml in conan-center we will need to download it from bincrafters.

For that we need to create a conanfile.txt at the root of the project.
./conanfile.txt

[requires]
imgui-sfml/2.3@bincrafters/stable

[generator]
cmake
Enter fullscreen mode Exit fullscreen mode

Next add those two line in your CMakeLists.txt.
./CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(MyProject)
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup()

...
Enter fullscreen mode Exit fullscreen mode

Then create the build folder and install the dependencies.

$ mkdir build
$ conan install . --install-folder ./build # You will have some error but that's normal
Enter fullscreen mode Exit fullscreen mode

The point of the last command was to generate the file ~/.conan/profile/default.

Change the libcxx version

SFML which is a dependancy of imgui-sfml needs the libstdc++11 to work properly. However conan still use libstdc++ by default for backwards compatibility so you have to edit it by yourself.

Run the command:

conan profile update settings.compiler.libcxx=libstdc++11 default
Enter fullscreen mode Exit fullscreen mode

or

Open the file ~/.conan/profile/default and apply the following change

-compiler.libcxx=libstdc++
+compiler.libcxx=libstdc++11
Enter fullscreen mode Exit fullscreen mode

Go back to the project

Here is a code sample (from here) to test imgui-sfml.

#include "imgui.h"
#include "imgui-SFML.h"

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/Window/Event.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "");
    window.setVerticalSyncEnabled(true);
    ImGui::SFML::Init(window);

    sf::Color bgColor;

    float color[3] = { 0.f, 0.f, 0.f };

    // let's use char array as buffer, see next part
    // for instructions on using std::string with ImGui
    char windowTitle[255] = "ImGui + SFML = <3";

    window.setTitle(windowTitle);
    window.resetGLStates(); // call it if you only draw ImGui. Otherwise not needed.
    sf::Clock deltaClock;
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            ImGui::SFML::ProcessEvent(event);

            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }

        ImGui::SFML::Update(window, deltaClock.restart());

        ImGui::Begin("Sample window"); // begin window

                                       // Background color edit
        if (ImGui::ColorEdit3("Background color", color)) {
            // this code gets called if color value changes, so
            // the background color is upgraded automatically!
            bgColor.r = static_cast<sf::Uint8>(color[0] * 255.f);
            bgColor.g = static_cast<sf::Uint8>(color[1] * 255.f);
            bgColor.b = static_cast<sf::Uint8>(color[2] * 255.f);
        }

        // Window title text edit
        ImGui::InputText("Window title", windowTitle, 255);

        if (ImGui::Button("Update window title")) {
            // this code gets if user clicks on the button
            // yes, you could have written if(ImGui::InputText(...))
            // but I do this to show how buttons work :)
            window.setTitle(windowTitle);
        }
        ImGui::End(); // end window

        window.clear(bgColor); // fill background with color
        ImGui::SFML::Render(window);
        window.display();
    }

    ImGui::SFML::Shutdown();
}
Enter fullscreen mode Exit fullscreen mode

Then in your project

$ conan install . --install-folder ./build # Now you should not have any error
$ cmake . -Bbuild
$ cmake --build build
$ ./build/bin/app
Enter fullscreen mode Exit fullscreen mode

Once that's finished enjoy your application πŸ˜‰.

Top comments (0)