Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Linux] Rendering a sprite in the same window garbles dialog text #8

Closed
drautb opened this issue Jul 29, 2016 · 10 comments
Closed

[Linux] Rendering a sprite in the same window garbles dialog text #8

drautb opened this issue Jul 29, 2016 · 10 comments

Comments

@drautb
Copy link

drautb commented Jul 29, 2016

Without rendering sf::Sprite:

without-sprite

With rendering sf::Sprite:

with-sprite

This is taken directly from the sample program, but with a few lines added to load/render the sprite.

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

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.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::Texture texture;
    texture.loadFromFile("image.jpg");

    sf::Sprite sprite;
    sprite.setTexture(texture);

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

    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("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);
        }

        // Demo
        // ImGui::ShowTestWindow();

        ImGui::End(); // end window

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

    ImGui::SFML::Shutdown();
}

The only difference between the two screenshots was commenting out window.draw(sprite).

Have you seen this before?

@drautb
Copy link
Author

drautb commented Jul 29, 2016

Whoops, I didn't know this was necessary, but surrounding the sprite rendering calls with window.pushGLStates() and window.popGLStates() fixed the issue. Sorry for the noise.

@drautb drautb closed this as completed Jul 29, 2016
@eliasdaler
Copy link
Contributor

eliasdaler commented Jul 31, 2016

I don't think that closes the issue! Try drawing the sprite before the ImGui::Render(), it should work correctly. I've never drawn sprites after ImGui so never had problems like this (and btw, your code works okay on Windows)
So probably the problem is with ImGui-SFML messing up the SFML OpenGL context somehow...and that's a valid issue.

Does swapping ImGui::Render() and window.draw(sprite) works for you?

@drautb
Copy link
Author

drautb commented Jul 31, 2016

I tried calling ImGui::Render both ways, before and after rendering the sprite. The result was the same.

It works fine for me too on Windows and OSX. Just Linux seems to have a problem.

@eliasdaler
Copy link
Contributor

eliasdaler commented Jul 31, 2016

Hmm, that's pretty strange. What SFML are you using? Maybe SFML is the problem... (though it's probably my fault, he-he, just want to be sure that I'll be able to reproduce the problem on my PC)

@drautb
Copy link
Author

drautb commented Jul 31, 2016

Im using SFML 2, not sure of the minor versions.

I did find this however, that indicates that you must manually preserve the OpenGL states when you mix SFML and OpenGL.

@eliasdaler
Copy link
Contributor

You can check version of SFML in Config.hpp, like this. SFML 2.3.2. is the most stable, maybe there were previously bugs related to this in SFML.

And yes, I preserve OpenGL state and I'm not sure what I'm doing wrong at the moment but I'll try to fix it (I need to have reproducible fail at first, though.)

@aggsol
Copy link

aggsol commented Aug 1, 2016

Cannot reproduce on Ubuntu 16.04, sfml 2.3.2, g++ 6.1.1 Are you using a Linux in a VM? Of course I am not using the same sprite as you do. Can you test with a PNG or share you image.jpg?

@drautb
Copy link
Author

drautb commented Aug 1, 2016

I'm running Ubuntu 14.04 natively, with g++ 4.8 IIRC. (It actually stopped booting up Saturday evening, which may have had something to do with this too. That's also why I couldn't verify the minor SFML version.)

If I can get my linux computer to boot up again, I'll attach the image.jpg that I used. However, I saw the same problem with multiple different sprites. I noticed the problem in a game I'm working on, and then reproduced it in the demo to eliminate more variables.

I did notice that this code does try to preserve the GL state, but at that point, my SFML rendering code has already run, which could have corrupted the GL state. In the SFML docs, they said you needed to push the GL states before doing any SFML rendering. That's why I closed the issue. :) I'm happy to keep troubleshooting though if you still think there may be something wrong in imgui-SFML. (Assuming I can get my computer running again. 😛

@eliasdaler
Copy link
Contributor

Okay, I'll close this for now, if there's a problem with ImGui-SFML, feel free to reopen/open new issue. :)

@drautb
Copy link
Author

drautb commented Aug 1, 2016

Ok, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants