Giter Club home page Giter Club logo

imgui-sfml's Introduction

ImGui + SFML

Library which allows you to use ImGui with SFML Based on this repository with some improvements / changes.

How-to

Detailed tutorial on my blog

Setting up:

  • Download ImGui
  • Add ImGui folder to your include directories
  • Add imgui.cpp and imgui_draw.cpp to your build/project
  • Copy the contents of imconfig-SFML.h to your imconfig.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

In your code:

  • Call ImGui::SFML::Init and pass your sf::Window + sf::RenderTarget or sf::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(deltaTime) where deltaTime is sf::Time

    • Call ImGui functions (ImGui::Begin(), ImGui::Button(), etc.)

    • Call ImGui::EndFrame if you update more than once before rendering (you'll need to include imgui_internal.h for that)

    • Call ImGui::Render()

  • Call ImGui::SFML::Shutdown() at the end of your program

Example code

See example file here

#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(800, 600), "ImGui + SFML = <3");
    window.setFramerateLimit(60);
    ImGui::SFML::Init(window);

    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(deltaClock.restart());

        ImGui::Begin("Hello, world!");
        ImGui::Button("Look at this pretty button");
        ImGui::End();

        window.clear();
        ImGui::Render();
        window.display();
    }

    ImGui::SFML::Shutdown();
}

CMake how-to

  • 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})

SFML related ImGui overloads / new widgets

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);

License

This library is licensed under the MIT License, see LICENSE for more information.

imgui-sfml's People

Contributors

cgloeckner avatar eliasdaler avatar iamphen avatar susnux avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.