Library which allows you to use ImGui with SFML Based on this repository with some improvements / changes.
Setting up:
- Download ImGui
- Add ImGui folder to your include directories
- Add
imgui.cpp
andimgui_draw.cpp
to your build/project - Copy the contents of
imconfig-SFML.h
to yourimconfig.h
file. (to be able to cast ImVec2 to sf::Vector2f and vice versa) - Add a folder which contains
imgui-SFML.h
to your include directories - Add
imgui-SFML.cpp
to your build/project
CMake Builds:
- Checkout the repository as a submoudle
- Set IMGUI_ROOT
- Modify your builds to copy imgui-SFML and dependencies (sfml) to your project
add_subdirectory(repos/imgui-sfml)
include_directories("${IMGUI_SFML_INCLUDE_DIRS}")
add_executable(MY_PROJECT ${IMGUI_SOURCES} ${IMGUI_SFML_SOURCES} ${SRCS})
...
target_link_libraries(MY_PROJECT ${IMGUI_SFML_DEPENDENCIES})
In your code:
-
Call
ImGui::SFML::Init
and pass yoursf::Window
+sf::RenderTarget
orsf::RenderWindow
there -
For each iteration of a game loop:
-
Poll and process events:
sf::Event event; while (window.pollEvent(event)) { ImGui::SFML::ProcessEvent(event); ... }
-
Call
ImGui::SFML::Update()
-
Call ImGui functions (
ImGui::Begin()
,ImGui::Button()
, etc.) -
Call
ImGui::EndFrame
if you update more than once before rendering (you'll need to includeimgui_internal.h
for that) -
Call
ImGui::Render()
-
-
Call
ImGui::SFML::Shutdown()
at the end of your program
See example file here
#include "imgui.h"
#include "imgui-SFML.h"
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "ImGui + SFML = <3");
window.setFramerateLimit(60);
ImGui::SFML::Init(window);
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();
ImGui::Begin("Hello, world!");
ImGui::Button("Look at this pretty button");
ImGui::End();
window.clear();
ImGui::Render();
window.display();
}
ImGui::SFML::Shutdown();
}
There are some useful overloads implemented for SFML objects (see header for overloads):
ImGui::Image(const sf::Sprite& sprite);
ImGui::Image(const sf::Texture& texture);
ImGui::ImageButton(const sf::Sprite& sprite);
ImGui::ImageButton(const sf::Texture& texture);
This library is licensed under the MIT License, see LICENSE for more information.