6#include <brenta/gl.hpp>
7#include <brenta/gui.hpp>
8#include <brenta/window.hpp>
9#include <brenta/texture.hpp>
10#include <brenta/logger.hpp>
12#ifdef BRENTA_USE_IMGUI
14using namespace brenta;
15using namespace brenta::types;
21const std::string gui::subsystem_name =
"gui";
22bool gui::initialized =
false;
28std::expected<void, subsystem::error> gui::initialize()
33 ImGui::CreateContext();
34 ImGuiIO &io = ImGui::GetIO();
37 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
38 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
39 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
42 ImGui_ImplGlfw_InitForOpenGL(window::get_window(),
true);
43 ImGui_ImplOpenGL3_Init();
44 ImGui::SetNextWindowPos(ImVec2(0, 0));
46 gui::initialized =
true;
47 INFO(
"{} initialized", gui::subsystem_name);
51std::expected<void, subsystem::error> gui::terminate()
55 ImGui_ImplOpenGL3_Shutdown();
56 ImGui_ImplGlfw_Shutdown();
57 ImGui::DestroyContext();
59 gui::initialized =
false;
60 INFO(
"{}: terminated", gui::subsystem_name);
66 return gui::subsystem_name;
71 return gui::initialized;
86 ImGui_ImplOpenGL3_NewFrame();
87 ImGui_ImplGlfw_NewFrame();
90 ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport());
96 ImGui::SetNextWindowSize(ImVec2(window::get_width() * 0.8,
97 window::get_width() * 0.8),
98 ImGuiCond_FirstUseEver);
99 ImGui::Begin(
name.c_str());
101 float window_width = ImGui::GetContentRegionAvail().x;
102 float window_height = ImGui::GetContentRegionAvail().y;
104 fb->
rescale(window_width, window_height);
107 ImGui::Image((
void *) (intptr_t) fb->texture_id,
108 ImGui::GetContentRegionAvail(), ImVec2(0, 1), ImVec2(1, 0));
116 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
125 return gui::instance();
static void set_viewport(int x, int y, int width, int height)
Set Viewport.
std::string name() override
Returns the name of the sybsystem.
static void render()
Render the gui To be called at each frame after rendering.
bool is_initialized() override
Returns true if the subsystem is initialized.
static void new_frame(types::framebuffer *fb, std::string name="Game")
Start a new frame To be called at each frame before rendering.
void rescale(int width, int height)
Rescale the framebuffer.