Advertisement
Guest User

renderer.cpp

a guest
Sep 8th, 2018
1,423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. #include "ImGui/imgui.h"
  2. #include "ImGui/imgui_internal.h"
  3.  
  4. Renderer* Renderer::m_pInstance;
  5.  
  6. Renderer::Renderer()
  7. {
  8. }
  9.  
  10. Renderer::~Renderer()
  11. {
  12. }
  13.  
  14. void Renderer::Initialize()
  15. {
  16.     ImGuiIO& io = ImGui::GetIO();
  17.  
  18.         // This is my example of font initializing
  19.         // First font used for UI
  20.         // Second for rendering, 32.0f is pixel size, not font size.
  21.     io.Fonts->AddFontFromMemoryTTF(g_fRubik, sizeof(g_fRubik), 16.0f, NULL, io.Fonts->GetGlyphRangesCyrillic());
  22.     m_pFont = io.Fonts->AddFontFromMemoryTTF(g_fRubik, sizeof(g_fRubik), 32.0f, NULL, io.Fonts->GetGlyphRangesCyrillic());
  23. }
  24.  
  25. void Renderer::BeginScene()
  26. {
  27.     ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.0f, 0.0f, 0.0f, 0.0f));
  28.     ImGui::Begin("BackBuffer", reinterpret_cast<bool*>(true), ImVec2(0, 0), 0.0f, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs);
  29.  
  30.     ImGui::SetWindowPos(ImVec2(0, 0), ImGuiSetCond_Always);
  31.     ImGui::SetWindowSize(ImVec2(ImGui::GetIO().DisplaySize.x, ImGui::GetIO().DisplaySize.y), ImGuiSetCond_Always);
  32. }
  33.  
  34. void Renderer::DrawScene()
  35. {
  36.     ImGuiIO& io = ImGui::GetIO();
  37.  
  38.     DrawText(m_pFont,
  39.         TextFormat(META_STRING("%.0f"), io.Framerate), ImVec2(10, lastY), 32.0f, 0xFFFFFFFF);
  40. }
  41.  
  42. void Renderer::EndScene()
  43. {
  44.     ImGuiWindow* window = ImGui::GetCurrentWindow();
  45.     window->DrawList->PushClipRectFullScreen();
  46.  
  47.     ImGui::End();
  48.     ImGui::PopStyleColor();
  49. }
  50.  
  51. float Renderer::DrawText(ImFont* pFont, const std::string& text, const ImVec2& pos, float size, uint32_t color, bool center)
  52. {
  53.     ImGuiWindow* window = ImGui::GetCurrentWindow();
  54.  
  55.     float a = (color >> 24) & 0xff;
  56.     float r = (color >> 16) & 0xff;
  57.     float g = (color >> 8) & 0xff;
  58.     float b = (color) & 0xff;
  59.  
  60.     std::stringstream steam(text);
  61.     std::string line;
  62.  
  63.     float y = 0.0f;
  64.     int i = 0;
  65.  
  66.     while (std::getline(steam, line))
  67.     {
  68.         ImVec2 textSize = pFont->CalcTextSizeA(size, FLT_MAX, 0.0f, line.c_str());
  69.        
  70.         if (center)
  71.         {
  72.             window->DrawList->AddText(pFont, size, ImVec2((pos.x - textSize.x / 2.0f) + 1, (pos.y + textSize.y * i) + 1), ImGui::GetColorU32(ImVec4(0, 0, 0, a / 255)), line.c_str());
  73.             window->DrawList->AddText(pFont, size, ImVec2((pos.x - textSize.x / 2.0f) - 1, (pos.y + textSize.y * i) - 1), ImGui::GetColorU32(ImVec4(0, 0, 0, a / 255)), line.c_str());
  74.             window->DrawList->AddText(pFont, size, ImVec2((pos.x - textSize.x / 2.0f) + 1, (pos.y + textSize.y * i) - 1), ImGui::GetColorU32(ImVec4(0, 0, 0, a / 255)), line.c_str());
  75.             window->DrawList->AddText(pFont, size, ImVec2((pos.x - textSize.x / 2.0f) - 1, (pos.y + textSize.y * i) + 1), ImGui::GetColorU32(ImVec4(0, 0, 0, a / 255)), line.c_str());
  76.  
  77.             window->DrawList->AddText(pFont, size, ImVec2(pos.x - textSize.x / 2.0f, pos.y + textSize.y * i), ImGui::GetColorU32(ImVec4(r / 255, g / 255, b / 255, a / 255)), line.c_str());
  78.         }
  79.         else
  80.         {
  81.             window->DrawList->AddText(pFont, size, ImVec2((pos.x) + 1, (pos.y + textSize.y * i) + 1), ImGui::GetColorU32(ImVec4(0, 0, 0, a / 255)), line.c_str());
  82.             window->DrawList->AddText(pFont, size, ImVec2((pos.x) - 1, (pos.y + textSize.y * i) - 1), ImGui::GetColorU32(ImVec4(0, 0, 0, a / 255)), line.c_str());
  83.             window->DrawList->AddText(pFont, size, ImVec2((pos.x) + 1, (pos.y + textSize.y * i) - 1), ImGui::GetColorU32(ImVec4(0, 0, 0, a / 255)), line.c_str());
  84.             window->DrawList->AddText(pFont, size, ImVec2((pos.x) - 1, (pos.y + textSize.y * i) + 1), ImGui::GetColorU32(ImVec4(0, 0, 0, a / 255)), line.c_str());
  85.  
  86.             window->DrawList->AddText(pFont, size, ImVec2(pos.x, pos.y + textSize.y * i), ImGui::GetColorU32(ImVec4(r / 255, g / 255, b / 255, a / 255)), line.c_str());
  87.         }
  88.  
  89.         y = pos.y + textSize.y * (i + 1);
  90.         i++;
  91.     }
  92.  
  93.     return y;
  94. }
  95.  
  96. void Renderer::DrawLine(const ImVec2& from, const ImVec2& to, uint32_t color, float thickness)
  97. {
  98.     ImGuiWindow* window = ImGui::GetCurrentWindow();
  99.  
  100.     float a = (color >> 24) & 0xff;
  101.     float r = (color >> 16) & 0xff;
  102.     float g = (color >> 8) & 0xff;
  103.     float b = (color) & 0xff;
  104.  
  105.     window->DrawList->AddLine(from, to, ImGui::GetColorU32(ImVec4(r / 255, g / 255, b / 255, a / 255)), thickness);
  106. }
  107.  
  108. void Renderer::DrawCircle(const ImVec2& position, float radius, uint32_t color, float thickness)
  109. {
  110.     ImGuiWindow* window = ImGui::GetCurrentWindow();
  111.  
  112.     float a = (color >> 24) & 0xff;
  113.     float r = (color >> 16) & 0xff;
  114.     float g = (color >> 8) & 0xff;
  115.     float b = (color) & 0xff;
  116.  
  117.     window->DrawList->AddCircle(position, radius, ImGui::GetColorU32(ImVec4(r / 255, g / 255, b / 255, a / 255)), 12, thickness);
  118. }
  119.  
  120. void Renderer::DrawCircleFilled(const ImVec2& position, float radius, uint32_t color)
  121. {
  122.     ImGuiWindow* window = ImGui::GetCurrentWindow();
  123.  
  124.     float a = (color >> 24) & 0xff;
  125.     float r = (color >> 16) & 0xff;
  126.     float g = (color >> 8) & 0xff;
  127.     float b = (color) & 0xff;
  128.  
  129.     window->DrawList->AddCircleFilled(position, radius, ImGui::GetColorU32(ImVec4(r / 255, g / 255, b / 255, a / 255)), 12);
  130. }
  131.  
  132. Renderer* Renderer::GetInstance()
  133. {
  134.     if (!m_pInstance)
  135.         m_pInstance = new Renderer();
  136.  
  137.     return m_pInstance;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement