Hi, I’m just getting started with Dear ImGui.
I’m trying to make something very simple, which is a 2D map editor where you can create, open, edit and save map. However since I am just getting started, I’m only looking to create a menu bar like this https://cloud.githubusercontent.com/assets/8225057/20628927/33e14cac-b329-11e6-80f6-9524e93b048a.png for now.
As you see that program has a menu bar at the very top, not attached to any of the widgets/windows.
How will I achieve this?
Any help is much appreciated, thank you.
edit: I of course have to clarify that I am using Allegro 5, if it is of any concern.
Here’s a quick example hope it helps, I would recommend checking imgui_demo.cpp too.
if(ImGui::BeginMainMenuBar())
{
if (ImGui::BeginMenu("File"))
{
if(ImGui::MenuItem("New"))
{
//Do something
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
Hi, thanks for the sample code, however when I enter it into the programs loop and debug, it crashes with abort() error code: Assertion failed: g.FrameScopeActive, file c:\users\matias\source\repos\map editor\map editor\imgui.cpp, line 4918
bool running = true;
while (running)
{
if (ImGui::BeginMainMenuBar())
{
if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("New"))
{
//Do something
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
ALLEGRO_EVENT ev;
while (al_get_next_event(queue, &ev))
{
ImGui_ImplAllegro5_ProcessEvent(&ev);
if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
running = false;
if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE)
{
ImGui_ImplAllegro5_InvalidateDeviceObjects();
al_acknowledge_resize(display);
ImGui_ImplAllegro5_CreateDeviceObjects();
}
}
// Start the Dear ImGui frame
ImGui_ImplAllegro5_NewFrame();
ImGui::NewFrame();
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
{
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Configuration"); // Create a window called "Hello, world!" and append into it.
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Map Editor", &show_mapeditor_window); // Map editor window
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}
// 3. Show another simple window.
if (show_mapeditor_window)
{
ImGui::Begin("Another Window", &show_mapeditor_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
ImGui::Text("Hello from another window!");
if (ImGui::Button("Close Me"))
show_mapeditor_window = false;
ImGui::End();
}
// Rendering
ImGui::Render();
al_clear_to_color(al_map_rgba_f(clear_color.x, clear_color.y, clear_color.z, clear_color.w));
ImGui_ImplAllegro5_RenderDrawData(ImGui::GetDrawData());
al_flip_display();
}
Read the comment on the line of your assert you will understand what you have done wrong.