Brenta Engine 1.2
Loading...
Searching...
No Matches
gui.cpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
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>
11
12#ifdef BRENTA_USE_IMGUI
13
14using namespace brenta;
15using namespace brenta::types;
16
17//
18// Static variables
19//
20
21const std::string gui::subsystem_name = "gui";
22bool gui::initialized = false;
23
24//
25// Subsystem interface
26//
27
28std::expected<void, subsystem::error> gui::initialize()
29{
30 if (this->is_initialized()) return {};
31
32 IMGUI_CHECKVERSION();
33 ImGui::CreateContext();
34 ImGuiIO &io = ImGui::GetIO();
35
36 // Enable Keyboard and Gamepad Controls
37 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
38 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
39 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
40
41 // Setup Platform/Renderer backends
42 ImGui_ImplGlfw_InitForOpenGL(window::get_window(), true);
43 ImGui_ImplOpenGL3_Init();
44 ImGui::SetNextWindowPos(ImVec2(0, 0));
45
46 gui::initialized = true;
47 INFO("{} initialized", gui::subsystem_name);
48 return {};
49}
50
51std::expected<void, subsystem::error> gui::terminate()
52{
53 if (!this->is_initialized()) return {};
54
55 ImGui_ImplOpenGL3_Shutdown();
56 ImGui_ImplGlfw_Shutdown();
57 ImGui::DestroyContext();
58
59 gui::initialized = false;
60 INFO("{}: terminated", gui::subsystem_name);
61 return {};
62}
63
64std::string gui::name()
65{
66 return gui::subsystem_name;
67}
68
70{
71 return gui::initialized;
72}
73
74//
75// Member functions
76//
77
78gui &gui::instance()
79{
80 static gui _gui;
81 return _gui;
82}
83
84void gui::new_frame(framebuffer *fb, std::string name)
85{
86 ImGui_ImplOpenGL3_NewFrame();
87 ImGui_ImplGlfw_NewFrame();
88 ImGui::NewFrame();
89
90 ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport());
91
92 //
93 // Game window
94 //
95
96 ImGui::SetNextWindowSize(ImVec2(window::get_width() * 0.8,
97 window::get_width() * 0.8),
98 ImGuiCond_FirstUseEver);
99 ImGui::Begin(name.c_str());
100
101 float window_width = ImGui::GetContentRegionAvail().x;
102 float window_height = ImGui::GetContentRegionAvail().y;
103
104 fb->rescale(window_width, window_height);
105 gl::set_viewport(0, 0, window_width, window_height);
106
107 ImGui::Image((void *) (intptr_t) fb->texture_id,
108 ImGui::GetContentRegionAvail(), ImVec2(0, 1), ImVec2(1, 0));
109
110 ImGui::End();
111}
112
114{
115 ImGui::Render();
116 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
117}
118
119//
120// Builder
121//
122
123subsystem &gui::builder::build()
124{
125 return gui::instance();
126}
127
128#endif
static void set_viewport(int x, int y, int width, int height)
Set Viewport.
Definition gl.cpp:150
Gui class.
Definition gui.hpp:27
std::string name() override
Returns the name of the sybsystem.
Definition gui.cpp:64
static void render()
Render the gui To be called at each frame after rendering.
Definition gui.cpp:113
bool is_initialized() override
Returns true if the subsystem is initialized.
Definition gui.cpp:69
static void new_frame(types::framebuffer *fb, std::string name="Game")
Start a new frame To be called at each frame before rendering.
Definition gui.cpp:84
Subsystem interface.
Definition subsystem.hpp:22
void rescale(int width, int height)
Rescale the framebuffer.