ui.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "ui.hpp"
  2. #include <imgui.h>
  3. #include <imgui_impl_sdl3.h>
  4. #include <imgui_impl_sdlrenderer3.h>
  5. namespace saura::ui {
  6. Context::Context(std::weak_ptr<os::Window_Context> window_ctx) {
  7. this->window_ctx = window_ctx;
  8. }
  9. void Context::init() {
  10. IMGUI_CHECKVERSION();
  11. ImGui::CreateContext();
  12. ImGuiIO &io = ImGui::GetIO();
  13. io.Fonts->AddFontFromFileTTF("res/fonts/InterRegular.ttf", 18, nullptr,
  14. io.Fonts->GetGlyphRangesCyrillic());
  15. (void)io;
  16. io.ConfigFlags |=
  17. ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  18. io.ConfigFlags |=
  19. ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
  20. // Setup Dear ImGui style
  21. ImGui::StyleColorsDark();
  22. // Setup Platform/Renderer backends
  23. ImGui_ImplSDL3_InitForSDLRenderer(this->window_ctx.lock()->handle,
  24. this->window_ctx.lock()->renderer);
  25. ImGui_ImplSDLRenderer3_Init(this->window_ctx.lock()->renderer);
  26. }
  27. void Context::deinit() {
  28. ImGui_ImplSDLRenderer3_Shutdown();
  29. ImGui_ImplSDL3_Shutdown();
  30. ImGui::DestroyContext();
  31. }
  32. void Context::update_events(SDL_Event &event) {
  33. ImGui_ImplSDL3_ProcessEvent(&event);
  34. }
  35. void Context::begin() {
  36. ImGui_ImplSDLRenderer3_NewFrame();
  37. ImGui_ImplSDL3_NewFrame();
  38. ImGui::NewFrame();
  39. }
  40. void Context::end() {
  41. ImVec4 clear_color = {0.15f, 0.15f, 0.15f, 0.0f};
  42. ImGuiIO &io = ImGui::GetIO();
  43. (void)io;
  44. ImGui::Render();
  45. SDL_SetRenderScale(this->window_ctx.lock()->renderer, io.DisplayFramebufferScale.x,
  46. io.DisplayFramebufferScale.y);
  47. SDL_SetRenderDrawColorFloat(this->window_ctx.lock()->renderer, clear_color.x,
  48. clear_color.y, clear_color.z, clear_color.w);
  49. SDL_RenderClear(this->window_ctx.lock()->renderer);
  50. ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(),
  51. this->window_ctx.lock()->renderer);
  52. SDL_RenderPresent(this->window_ctx.lock()->renderer);
  53. }
  54. } // namespace saura::ui