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$sourcevenv/bin/activate$pipinstallconan$conan$conan remote add bincrafters https://bincrafters.jfrog.io/artifactory/api/conan/conan
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 ishere.
As there is no package for imgui-sfml in conan-center we will need to download it frombincrafters.
For that we need to create aconanfile.txt
at the root of the project.
./conanfile.txt
[requires]imgui-sfml/2.3@bincrafters/stable[generator]cmake
Next add those two line in yourCMakeLists.txt
.
./CMakeLists.txt
cmake_minimum_required(VERSION 3.16)project(MyProject)+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)+conan_basic_setup()...
Then create the build folder and install the dependencies.
$mkdirbuild$conaninstall.--install-folder ./build# You will have some error but that's normal
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
or
Open the file~/.conan/profile/default
and apply the following change
-compiler.libcxx=libstdc+++compiler.libcxx=libstdc++11
Go back to the project
Here is a code sample (fromhere) 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>intmain(){sf::RenderWindowwindow(sf::VideoMode(640,480),"");window.setVerticalSyncEnabled(true);ImGui::SFML::Init(window);sf::ColorbgColor;floatcolor[3]={0.f,0.f,0.f};// let's use char array as buffer, see next part// for instructions on using std::string with ImGuicharwindowTitle[255]="ImGui + SFML = <3";window.setTitle(windowTitle);window.resetGLStates();// call it if you only draw ImGui. Otherwise not needed.sf::ClockdeltaClock;while(window.isOpen()){sf::Eventevent;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 editif(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 editImGui::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 windowwindow.clear(bgColor);// fill background with colorImGui::SFML::Render(window);window.display();}ImGui::SFML::Shutdown();}
Then in your project
$conaninstall.--install-folder ./build# Now you should not have any error$cmake.-Bbuild$cmake--build build$./build/bin/app
Once that's finished enjoy your application 😉.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse