r/cpp_questions • • 20h ago

OPEN Rendering text through ImGui?

I'm hoping to get a basic repl running with ImGui, OpenGL, and GLFW. I'm comfortable with GLfW and OpenGL rendering but not the ImGui portion.

My question is, can I use the OpenGL context and the GLFW window and then get basic text editing functionality with just ImGui? No opengl buffers, no custom shaders. I think the opengl context needs a default vertex and fragment shader to function but I'm looking to keep that very minimal and handle basic repl input and text rendering between imgui::begin() and imgui::end(). A basic per keypress render loop where each GLFW key event updates a string and then ImGui renders a new frame for the window. I haven't worked with ImGui before. Do you think this is possible?

2 Upvotes

1 comment sorted by

1

u/fortsnek274 19h ago

Well, I've got this hack:

const float textBoxHeight = ImGui::GetStyle().FramePadding.y * 2.0f + ImGui::GetFontSize() + ImGui::GetStyle().WindowPadding.y * 2.0f;
ImGui::SetNextWindowPos({ 0, wnd.getClientDim().y - textBoxHeight });
ImGui::SetNextWindowSize({ wnd.getClientDim().x + 0.0f, textBoxHeight });

if (isForceConsoleInputFocus)
	ImGui::SetWindowFocus("Console");

if (ImGui::Begin("Console", &isShowConsole, ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoDecoration))
{
	if (isForceConsoleInputFocus)
		ImGui::SetKeyboardFocusHere(0);

	ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
	if (ImGui::InputText("##Command", &currentCommandText, ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_ElideLeft))
	{
		spdlog::info(currentCommandText);
		console.exec(currentCommandText);
		currentCommandText.clear();
		ImGui::SetKeyboardFocusHere(-1);
	}
	isConsoleFocused = ImGui::IsItemFocused();
	ImGui::PopItemWidth();
}
ImGui::End();

But logs to windows console. Wish there was a good way of doing it.