Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Using ImGui with SFML (Visual Studio 2017)  (Read 17948 times)

0 Members and 1 Guest are viewing this topic.

Toby

  • Newbie
  • *
  • Posts: 17
    • View Profile
Using ImGui with SFML (Visual Studio 2017)
« on: March 18, 2017, 03:53:28 pm »
I've been trying to get ImGui to work with SFML for several hours now. There is a binding made for SFML, or two rather: https://github.com/eliasdaler/imgui-sfml which is based on https://github.com/Mischa-Alff/imgui-backends
I'm using the former and I can't for the life of me get it to work. I've tried using both of them to no avail. The first one gives me linker errors and the second one gives me an error about how something isn't a member of a class. I can provide the detailed errors if needed but I'm starting to think that they are not compatible with either VS2017 (which wouldn't make sense) or that they are outdated and no longer compatible with SFML.
Considering they are just simple cpp and h files it shouldn't be this hard to get them to compile.

Has anyone gotten ImGui to work with SFML? Some help would be appreciated.

Edit:

Code: [Select]
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui::Render(void)" (?Render@ImGui@@YAXXZ) referenced in function main
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui::ShowTestWindow(bool *)" (?ShowTestWindow@ImGui@@YAXPEA_N@Z) referenced in function main
1>Main.obj : error LNK2019: unresolved external symbol "bool __cdecl ImGui::Begin(char const *,bool *,int)" (?Begin@ImGui@@YA_NPEBDPEA_NH@Z) referenced in function main
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui::End(void)" (?End@ImGui@@YAXXZ) referenced in function main
1>Main.obj : error LNK2019: unresolved external symbol "bool __cdecl ImGui::Button(char const *,struct ImVec2 const &)" (?Button@ImGui@@YA_NPEBDAEBUImVec2@@@Z) referenced in function main
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui::SFML::Init(class sf::RenderTarget &,class sf::Texture *)" (?Init@SFML@ImGui@@YAXAEAVRenderTarget@sf@@PEAVTexture@4@@Z) referenced in function main
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui::SFML::ProcessEvent(class sf::Event const &)" (?ProcessEvent@SFML@ImGui@@YAXAEBVEvent@sf@@@Z) referenced in function main
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui::SFML::Update(class sf::RenderWindow &,class sf::Time)" (?Update@SFML@ImGui@@YAXAEAVRenderWindow@sf@@VTime@4@@Z) referenced in function main
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl ImGui::SFML::Shutdown(void)" (?Shutdown@SFML@ImGui@@YAXXZ) referenced in function main

Figured I might as well post the linker error I get when I try and use the SFML binding from  https://github.com/eliasdaler/imgui-sfml trying to compile his sample code.
« Last Edit: March 18, 2017, 04:07:07 pm by Toby »

mos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #1 on: March 18, 2017, 08:59:48 pm »
Using this one: https://github.com/eliasdaler/imgui-sfml. It is quite easy to setup.
  • Right click your project -> Properties
  • Change Configuration on top to All Configurations
  • Go to C/C++ -> General -> Additional Include Directories
  • Add the imgui (https://github.com/ocornut/imgui) folder path in there
  • Open up imconfig-SFML.h and imconfig.h. Add the contents of imconfig-SFML.h inside imconfig.h in their proper positions in the top of the file (#include with the other includes, defines with the other defines, etc...).
  • Add the following files to your project: imgui.cpp, imgui_draw.cpp, imgui-SFML.cpp

Try out this test program:

#include <imgui.h>
#include <imgui-sfml\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(640, 480), "");
        window.setVerticalSyncEnabled(true);
        ImGui::SFML::Init(window);

        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);
        window.resetGLStates(); // call it if you only draw ImGui. Otherwise not needed.
        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(window, 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, 500);

                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 window

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

        ImGui::SFML::Shutdown();
}

I am using Visual Studio 2017 as well and it works great without any errors. All the instructions take from here: https://eliasdaler.github.io/using-imgui-with-sfml-pt1/. I had to change the following two lines in the test program:

#include "imgui.h"
#include "imgui-sfml.h"

to this:

#include <imgui.h>
#include <imgui-sfml\imgui-SFML.h>

to make it work. Though if you want to stay to the original then you just include the imgui-SFML directory as well. Hope this helps!

EDIT: If  the problem still persists, the link mentions to try linking OpenGL to your project.
« Last Edit: March 18, 2017, 09:02:04 pm by mos »

Toby

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #2 on: March 18, 2017, 11:04:18 pm »
I suspect I have to include opengl since my link errors all mention gl commands. But I find it odd that I would have to do that since SFML already uses OpenGL. How would I link OpenGL so that imgui works with it? Since intellisense itself has no problem with the imgui and imgui-sfml commands. They all seem to work, it only breaks when I try to compile.

mos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #3 on: March 18, 2017, 11:18:59 pm »
You should have opengl32.lib in "Additional Dependencies" under Linker -> Input for Debug and Release. If you have that, then maybe you're missing a component. When I had linking issues with SFML, I installed "Windows Universal CRT SDK" in the Visual Studio Installer (can be found under Compilers, build tools, and runtimes) to make them go away.

So check if you have the right dependencies and components.

Toby

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #4 on: March 18, 2017, 11:26:00 pm »
That solved it! Thank you so much! Adding opengl32.lib did the trick. I've been stuck all day trying to fix this. You have no idea how happy I am right now.

mos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #5 on: March 18, 2017, 11:32:59 pm »
You're welcome. Glad I could help.

Toby

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #6 on: March 18, 2017, 11:52:56 pm »
My only problem now is that while the demo code works I can't seem to use it the same way as they do it:

Code: [Select]
#include "imgui.h"
#include "imgui-SFML.h"

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/CircleShape.hpp>

int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "ImGui + SFML = <3");
window.setFramerateLimit(60);
ImGui::SFML::Init(window);

sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);

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

//ImGui::ShowTestWindow();

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

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

ImGui::SFML::Shutdown();
}

I try and replicate this structure but in my game class and it crashes and says something about an assertion failing relating go g.Initialized so I assume it is my usage of ImGui::SFML::Init that fails. In that example code they just pass in a window but when I look at the function it is supposed to take a window and an sf::Texture* fontTexture, but I'm unsure if that is why it crashes since then it should crash in the demo code too?

Edit:
When I try and use the
Code: [Select]
ImGui::Begin("Hello, world!");
ImGui::Button("Look at this pretty button");
ImGui::End();

window.clear();
ImGui::Render();
window.display();
It crashes. I use the ImGui::SFML::Init(window); in my game class constructor, ImGui::SFML::ProcessEvent(event); in my process event function and ImGui::SFML::Update(window, deltaClock.restart()); in my update function and finally the ImGui::Begin etc in my render loop. So I'm unsure as to what I'm doing differently.
« Last Edit: March 18, 2017, 11:56:21 pm by Toby »

Toby

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #7 on: March 18, 2017, 11:57:45 pm »
Oh I had to call ImGui::Newframe prior to rendering. It works now! Strange that their code didn't have to do that.

mos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #8 on: March 19, 2017, 12:00:36 am »
Equally weird is that your demo code works fine for me and adding ImGui::NewFrame makes the whole screen black.

Toby

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #9 on: March 19, 2017, 12:16:01 am »
My final issue seems to be that I can't interact with my own windows. They are completely static.

Code: [Select]
sf::Event event;
while (_window.pollEvent(event))
{
ImGui::SFML::ProcessEvent(event); ///IMGUI

if (event.type == sf::Event::Closed)
_window.close();
}

My event handling loop is very straight forward and just like the demo. However I can't interact with the window at all. Which is very strange. They do render at least.

Edit:

Actually seem to be related to the update function. Since commenting out the event update part for ImGui still lets me move the windows around etc in the demo code.
« Last Edit: March 19, 2017, 12:24:25 am by Toby »

mos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #10 on: March 19, 2017, 12:29:06 am »
I feel like you're still missing some dependencies as I am having none of the issues you are. Check the dependency table here: http://www.sfml-dev.org/tutorials/2.4/start-vc.php (only use "-s" if using SFML_STATIC). Make sure you have the correct ones for Debug and Release.

Toby

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #11 on: March 19, 2017, 12:44:41 am »
I had placed ImGui::NewFrame(); prior to my ImGui::Render() in my render loop, but if I place it in my constructor where it runs once it seems to work fine. So I guess that's that. Or well, actually, now I noticed my test sprite isn't showing up anymore and unlike the demo I can't double click to collapse the window. Interesting.. Anyways regarding dependencies, I am sure that SFML is functioning as intended since I've tried drawing a sprite before and it worked fine.

It's almost as if it is working, but at the same time now. :P Ah well, it is getting late. I'll have to try it more tomorrow.

This is my current mess  for the record http://pastebin.com/fz9WcvLc I commented out ImGui::Render() intentionally but when it is active it renders over everything instead of working in tandem with SFML. (I see my sprite flicker for a split second when the program starts and then it is gone)

Seems to work with sf::CircleShape but not with sf::Sprite. Odd.
« Last Edit: March 19, 2017, 01:10:51 am by Toby »

mos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #12 on: March 19, 2017, 05:21:44 am »

Toby

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Using ImGui with SFML (Visual Studio 2017)
« Reply #13 on: March 19, 2017, 12:50:59 pm »
Thanks, I think that was the last of my issues. Now I can finally get to work.

 

anything