SFML / imgui-sfml

Dear ImGui backend for use with SFML
MIT License
1.15k stars 172 forks source link

Making imgui-sfml work with custom opengl drawing #276

Open ronikiienko opened 6 months ago

ronikiienko commented 6 months ago

I am trying to use sfml with imgui (and drawing using opengl) and having problems with states. Here's very minimal example:

#include <GL/glew.h>

#include <SFML/Graphics/RenderWindow.hpp>
#include "imgui.h"
#include "imgui-SFML.h"
#include "ShaderProgram.h"
#include <iostream>
#include <SFML/System/Clock.hpp>
#include <SFML/Window/Event.hpp>

int main() {
    sf::RenderWindow window = sf::RenderWindow(sf::VideoMode(800, 600), "window",
                                                 sf::Style::Default, sf::ContextSettings(0, 0, 1));
    window.setActive(true);

    if (!ImGui::SFML::Init(window)){
        throw std::runtime_error("Failed to initialize ImGui-SFML");
    };
    if (glewInit() != GLEW_OK) {
        throw std::runtime_error("Glew Error!");
    }

    unsigned int VAO;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    sf::Clock deltaClock;
    while (window.isOpen()) {
        sf::Event event{};
        while (window.pollEvent(event)) {
            ImGui::SFML::ProcessEvent(window, event);
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }
        window.clear();

        window.pushGLStates();

        ImGui::SFML::Update(window, deltaClock.restart());
        ImGui::Begin("Hello, world!");
        ImGui::End();
        ImGui::SFML::Render(window);

        window.popGLStates();

        window.display();
    }
    glDeleteVertexArrays(1, &VAO);
    ImGui::SFML::Shutdown();
    return 0;
}

Imgui part stops working as soon as i uncomment line glBindVertexArray(VAO); And i get errors "Error description: GL_INVALID_OPERATION The specified operation is not allowed in the current state."

If i call glBindVertexArray(0);, to unbind vertex array, it works. Also tried glPushAttrib(GL_ALL_ATTRIB_BITS); + glPopAttrib() , but it doesn't work as well

Am i doing something wrong? How can i fix it?