SFML/imgui-sfml

It is possible to use ImGui stuff without isOpen loop?

sndth opened this issue · 2 comments

sndth commented

Hello, I have this code:

class XYZ
{
	sf::RenderWindow Window;

public:
	void CreateWindow(int X, int Y, const char* Title)
	{
		Window.create(sf::VideoMode(X, Y), Title);
	}

	void SetFramerate(int FPS)
	{
		Window.setFramerateLimit(FPS);
	}

	void InitializeWindow()
	{
		sf::Clock DeltaClock;

		ImGui::SFML::Init(Window);

		while (Window.isOpen())
		{
			sf::Event Event;

			while (Window.pollEvent(Event))
			{
				ImGui::SFML::ProcessEvent(Window, Event);

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

			ImGui::SFML::Update(Window, DeltaClock.restart());

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

			Window.clear();

			ImGui::SFML::Render(Window);

			Window.display();
		}

		ImGui::SFML::Shutdown();
	}
};

It is possible to create separate function for e.g. create button for 'Window' variable?
I want to use this function like this:

XYZ Engine;
Engine.CreateWindow(800, 600, "XYZ");
Engine.SetFramerate(60);
// Engine.CreateButton("Button name");
Engine.InitializeWindow();

Yes, you can. As long as you call ImGui-SFML's ProcessEvent/Update/Render functions somewhere in your game loop for each window that you create, e.g.

XYZ Engine;
Engine.CreateWindow(800, 600, "XYZ"); // ImGui::SFML::Init should be inside CreateWindow function
Engine.SetFramerate(60);
while (gameIsRunning) {
    Engine.ProcessEvents() //  call ImGui::SFML::ProcessEvents for each window here
    Engine.Update() // call ImGui::SFML::Update for each window
    Engine.Draw() // call ImGui::SFML::Render here OR in the end of Update
}
... // shutdown stuff, ImGui::SFML::Shutdown should be called here for each window

As for "CreateButton" on init - you can't do that. This is not how Dear ImGui works - you must call ImGui widget functions on each frame (between ImGui::SFML::Update and ImGui::SFML::Render calls for given window)

sndth commented

Okay, thank you for reply!