satdump/src-ui/backend.cpp

100 lines
2.3 KiB
C++
Raw Permalink Normal View History

2023-12-20 14:11:57 -05:00
#include <GLFW/glfw3.h>
#include "core/style.h"
2023-12-20 14:11:57 -05:00
#include "backend.h"
extern GLFWwindow* window;
2023-12-31 11:11:36 -05:00
extern bool fallback_gl;
2023-12-20 14:11:57 -05:00
float funcDeviceScale()
{
float display_scale;
#if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3)
glfwGetWindowContentScale(window, &display_scale, nullptr);
display_scale /= style::macos_framebuffer_scale();
#else
display_scale = 1.0f;
#endif
return display_scale;
}
void funcRebuildFonts()
{
#ifndef IMGUI_IMPL_OPENGL_ES2
if (fallback_gl)
{
ImGui_ImplOpenGL2_DestroyFontsTexture();
ImGui_ImplOpenGL2_CreateFontsTexture();
}
else
#endif
{
ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture();
}
}
2023-12-20 14:11:57 -05:00
void funcSetMousePos(int x, int y)
{
glfwSetCursorPos(window, x, y);
ImGui_ImplGlfw_CursorPosCallback(window, x, y);
}
2023-12-31 11:11:36 -05:00
std::pair<int, int> funcBeginFrame()
{
// Start the Dear ImGui frame
#ifndef IMGUI_IMPL_OPENGL_ES2
if (fallback_gl)
ImGui_ImplOpenGL2_NewFrame();
else
#endif
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
int wwidth, wheight;
glfwGetWindowSize(window, &wwidth, &wheight);
return {wwidth, wheight};
}
void funcEndFrame()
{
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
2024-01-21 14:57:41 -05:00
glClearColor(style::theme.frame_bg.Value.x, style::theme.frame_bg.Value.y, style::theme.frame_bg.Value.z, 1.0f);
2023-12-31 11:11:36 -05:00
glClear(GL_COLOR_BUFFER_BIT);
#ifndef IMGUI_IMPL_OPENGL_ES2
if (fallback_gl)
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
else
#endif
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
glfwPollEvents();
}
2023-12-31 11:46:40 -05:00
void funcSetIcon(uint8_t *image, int w, int h)
2023-12-31 11:11:36 -05:00
{
#ifndef _WIN32
GLFWimage img;
img.pixels = image;
2023-12-31 11:46:40 -05:00
img.width = w;
img.height = h;
2023-12-31 11:11:36 -05:00
glfwSetWindowIcon(window, 1, &img);
#endif
}
2023-12-20 14:11:57 -05:00
void bindBackendFunctions()
{
backend::device_scale = funcDeviceScale();
backend::rebuildFonts = funcRebuildFonts;
2023-12-31 11:46:40 -05:00
backend::setMousePos = funcSetMousePos;
backend::beginFrame = funcBeginFrame;
backend::endFrame = funcEndFrame;
2023-12-31 11:11:36 -05:00
backend::setIcon = funcSetIcon;
2023-12-31 11:46:40 -05:00
}