Brenta Engine 1.1
Loading...
Searching...
No Matches
gui.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#include "gui.hpp"
28
29#include "gl_helper.hpp"
30#include "screen.hpp"
31#include "texture.hpp"
32
33#ifdef USE_IMGUI
34
35using namespace brenta;
36using namespace brenta::types;
37
39{
40 IMGUI_CHECKVERSION();
41 ImGui::CreateContext();
42 ImGuiIO &io = ImGui::GetIO();
43 /* Enable Keyboard and Gamepad Controls */
44 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
45 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
46 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
47
48 /* Setup Platform/Renderer backends */
49 ImGui_ImplGlfw_InitForOpenGL(screen::get_window(), true);
50 ImGui_ImplOpenGL3_Init();
51 ImGui::SetNextWindowPos(ImVec2(0, 0));
52}
53
55{
56 ImGui_ImplOpenGL3_Shutdown();
57 ImGui_ImplGlfw_Shutdown();
58 ImGui::DestroyContext();
59}
60
62{
63 ImGui_ImplOpenGL3_NewFrame();
64 ImGui_ImplGlfw_NewFrame();
65 ImGui::NewFrame();
66
67 ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport());
68
69 // Game window ------------------------------------------
70
71 ImGui::SetNextWindowSize(ImVec2(500, 500));
72 ImGui::Begin("Game");
73
74 float window_width = ImGui::GetContentRegionAvail().x;
75 float window_height = ImGui::GetContentRegionAvail().y;
76
77 fb->rescale(window_width, window_height);
78 gl::set_viewport(0, 0, window_width, window_height);
79
80 ImGui::Image((void *) (intptr_t) fb->texture_id,
81 ImGui::GetContentRegionAvail(), ImVec2(0, 1), ImVec2(1, 0));
82
83 ImGui::End();
84}
85
87{
88 ImGui::Render();
89 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
90}
91
92#endif
static void set_viewport(int x, int y, int SCR_WIDTH, int SCR_HEIGHT)
Set Viewport.
Definition gl_helper.cpp:97
static void destroy()
Delete the gui.
Definition gui.cpp:54
static void new_frame(types::framebuffer *fb)
Start a new frame To be called at each frame before rendering.
Definition gui.cpp:61
static void render()
Render the gui To be called at each frame after rendering.
Definition gui.cpp:86
static void init()
Initialize the gui.
Definition gui.cpp:38
static GLFWwindow * get_window()
Get the window.
Definition screen.cpp:95
GLuint texture_id
Itexture ID.
void rescale(int width, int height)
Rescale the framebuffer.