#include #include "Renderer.h" #include "imgui\imgui_internal.h" Renderer* Renderer::_instance = NULL; Renderer::Renderer() : inFrame(false) { } Renderer::~Renderer() { } Renderer* Renderer::Get() { if (!_instance) { _instance = new Renderer(); } return _instance; } void Renderer::BeginScene() { ImGuiIO& io = ImGui::GetIO(); ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 0.0f, 0.0f }); ImGui::PushStyleColor(ImGuiCol_WindowBg, { 0.0f, 0.0f, 0.0f, 0.0f }); ImGui::Begin("##Backbuffer", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs); ImGui::SetWindowPos(ImVec2(0, 0), ImGuiCond_Always); ImGui::SetWindowSize(ImVec2(io.DisplaySize.x, io.DisplaySize.y), ImGuiCond_Always); inFrame = true; } void Renderer::EndScene() { ImGuiWindow* window = ImGui::GetCurrentWindow(); window->DrawList->PushClipRectFullScreen(); ImGui::End(); ImGui::PopStyleColor(); ImGui::PopStyleVar(2); inFrame = false; } void Renderer::RenderText(const std::string& text, const ImVec2& position, float size, uint32_t color, bool center) { ImGuiWindow* window = ImGui::GetCurrentWindow(); float a = (color >> 24) & 0xff; float r = (color >> 16) & 0xff; float g = (color >> 8) & 0xff; float b = (color) & 0xff; if (center) { window->DrawList->AddText(NULL, size, position, ImGui::GetColorU32({ r, g, b, a }), text.c_str()); } else { window->DrawList->AddText(NULL, size, position, ImGui::GetColorU32({ r,g,b,a }), text.c_str()); } } void Renderer::RenderLine(const ImVec2& from, const ImVec2& to, uint32_t color, float thickness) { ImGuiWindow* window = ImGui::GetCurrentWindow(); float a = (color >> 24) & 0xff; float r = (color >> 16) & 0xff; float g = (color >> 8) & 0xff; float b = (color) & 0xff; window->DrawList->AddLine(from, to, ImGui::GetColorU32({ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }), thickness); } void Renderer::RenderPolygon(const ImVec2* points, int pointCount, uint32_t color, float thickness) { for (int i = 0; i < pointCount - 1; i++) { RenderLine(points[i], points[i + 1], color, thickness); } RenderLine(points[pointCount - 1], points[0], color, thickness); } void Renderer::RenderCircle(const ImVec2& position, float radius, uint32_t color, float thickness, uint32_t segments) { ImGuiWindow* window = ImGui::GetCurrentWindow(); float a = (color >> 24) & 0xff; float r = (color >> 16) & 0xff; float g = (color >> 8) & 0xff; float b = (color) & 0xff; window->DrawList->AddCircle(position, radius, ImGui::GetColorU32({ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }), segments, thickness); } void Renderer::RenderCircleFilled(const ImVec2& position, float radius, uint32_t color, uint32_t segments) { ImGuiWindow* window = ImGui::GetCurrentWindow(); float a = (color >> 24) & 0xff; float r = (color >> 16) & 0xff; float g = (color >> 8) & 0xff; float b = (color) & 0xff; window->DrawList->AddCircleFilled(position, radius, ImGui::GetColorU32({ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }), segments); } void Renderer::RenderRect(const ImVec2& from, const ImVec2& to, uint32_t color, float rounding, uint32_t roundingCornersFlags, float thickness) { ImGuiWindow* window = ImGui::GetCurrentWindow(); float a = (color >> 24) & 0xFF; float r = (color >> 16) & 0xFF; float g = (color >> 8) & 0xFF; float b = (color) & 0xFF; window->DrawList->AddRect(from, to, ImGui::GetColorU32({ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }), rounding, roundingCornersFlags, thickness); } void Renderer::RenderRect(const ImVec2& one, const ImVec2& two, const ImVec2& three, const ImVec2& four, uint32_t color, float thickness) { ImGuiWindow* window = ImGui::GetCurrentWindow(); float a = (color >> 24) & 0xFF; float r = (color >> 16) & 0xFF; float g = (color >> 8) & 0xFF; float b = (color) & 0xFF; window->DrawList->AddLine(one, two, ImGui::GetColorU32({ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }), thickness); window->DrawList->AddLine(two, three, ImGui::GetColorU32({ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }), thickness); window->DrawList->AddLine(three, four, ImGui::GetColorU32({ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }), thickness); window->DrawList->AddLine(four, one, ImGui::GetColorU32({ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }), thickness); } void Renderer::RenderRectFilled(const ImVec2& from, const ImVec2& to, uint32_t color, float rounding, uint32_t roundingCornersFlags) { ImGuiWindow* window = ImGui::GetCurrentWindow(); float a = (color >> 24) & 0xFF; float r = (color >> 16) & 0xFF; float g = (color >> 8) & 0xFF; float b = (color) & 0xFF; window->DrawList->AddRectFilled(from, to, ImGui::GetColorU32({ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }), rounding, roundingCornersFlags); } void Renderer::DrawUnitCollisionRectangle(ImVec2 screenPositionCenter, ImVec2 collision, uint32_t color) { ImVec2 ivOne = ImVec2(screenPositionCenter.x - collision.x, screenPositionCenter.y - collision.y); ImVec2 ivTwo = ImVec2(screenPositionCenter.x + collision.x, screenPositionCenter.y - collision.y); ImVec2 ivThree = ImVec2(screenPositionCenter.x + collision.x, screenPositionCenter.y + collision.y); ImVec2 ivFour = ImVec2(screenPositionCenter.x + collision.x, screenPositionCenter.y - collision.y); Renderer::Get()->RenderRect(ivOne, ivTwo, ivThree, ivFour, color); }