keybode

renderer.cpp

Nov 18th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include "renderer.hpp"
  2.  
  3. #include "../win32/input.hpp"
  4.  
  5. namespace horizon::core
  6. {
  7.  
  8. Renderer::~Renderer()
  9. {
  10.     Destroy();
  11. }
  12.  
  13. auto Renderer::Create( IDirect3DDevice9* direct_device ) -> bool
  14. {
  15.     auto& input = win32::Input::Instance();
  16.  
  17.     const auto window = input.GetWindow();
  18.  
  19.     ImGui::CreateContext();
  20.     ImGui_ImplWin32_Init( window );
  21.     ImGui_ImplDX9_Init( direct_device );
  22.     ImGui::StyleColorsDark();
  23.  
  24.     return true;
  25. }
  26.  
  27. auto Renderer::Destroy() -> void
  28. {
  29.     ImGui_ImplDX9_Shutdown();
  30.     ImGui_ImplWin32_Shutdown();
  31.     ImGui::DestroyContext();
  32. }
  33.  
  34. auto Renderer::Begin() -> bool
  35. {
  36.     ImGui_ImplDX9_NewFrame();
  37.     ImGui_ImplWin32_NewFrame();
  38.     ImGui::NewFrame();
  39.  
  40.     return true;
  41. }
  42.  
  43. auto Renderer::End() -> void
  44. {
  45.    
  46. }
  47.  
  48. auto Renderer::Present() -> void
  49. {
  50.     ImGui::EndFrame();
  51.     ImGui::Render();
  52.     ImGui_ImplDX9_RenderDrawData( ImGui::GetDrawData() );
  53. }
  54.  
  55. auto Renderer::DeviceLost() -> void
  56. {
  57.     ImGui_ImplDX9_InvalidateDeviceObjects();
  58. }
  59.  
  60. auto Renderer::DeviceReset( HRESULT result ) -> void
  61. {
  62.     if( SUCCEEDED( result ) )
  63.         ImGui_ImplDX9_CreateDeviceObjects();
  64. }
  65.  
  66. auto Renderer::DrawQuad( const ImVec2& p0, const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImColor& color ) -> void
  67. {
  68.     auto render = ImGui::GetOverlayDrawList();
  69.     render->AddQuadFilled( { p0[ 0 ], p0[ 1 ] }, { p1[ 0 ], p1[ 1 ] }, { p2[ 0 ], p2[ 1 ] }, { p3[ 0 ], p3[ 1 ] }, color );
  70. }
  71.  
  72. auto Renderer::DrawRect( const ImVec2& begin, const ImVec2& size, const ImColor& color ) -> void
  73. {
  74.     auto render = ImGui::GetOverlayDrawList();
  75.     render->AddRectFilled( { begin[ 0 ], begin[ 1 ] }, { begin[ 0 ] + size[ 0 ], begin[ 1 ] + size[ 1 ] }, color );
  76. }
  77.  
  78. auto Renderer::DrawLine( const ImVec2& begin, const ImVec2& end, const ImColor& color, float width /*= 1.f*/ ) -> void
  79. {
  80.     auto render = ImGui::GetOverlayDrawList();
  81.     render->AddLine( { begin[ 0 ], begin[ 1 ] }, { end[ 0 ], end[ 1 ] }, color );
  82. }
  83.  
  84. auto Renderer::DrawText( const ImVec2& begin, unsigned align, const ImColor& color, const char* message, ... ) -> void
  85. {
  86.     char output[ 1024 ] = { };
  87.  
  88.     va_list args;
  89.     va_start( args, message );
  90.     vsprintf_s( output, message, args );
  91.     va_end( args );
  92.  
  93.     auto coord = ImVec2( begin[ 0 ], begin[ 1 ] );
  94.     auto size = ImGui::CalcTextSize( output );
  95.  
  96.     if( align & TextRight )
  97.         coord.x -= size.x;
  98.     else if( align & TextCenterH )
  99.         coord.x -= size.x / 2.f;
  100.  
  101.     if( align & TextCenterV )
  102.         coord.y -= size.y / 2.f;
  103.  
  104.     auto render = ImGui::GetOverlayDrawList();
  105.  
  106.     auto coord_out = ImVec2{ coord.x + 1.f, coord.y + 1.f };
  107.     render->AddText( coord_out, ImColor{ 0.f, 0.f, 0.f, 1.f }, output );
  108.     render->AddText( coord, color, output );
  109. }
  110.  
  111. auto Renderer::GetTextSize( ImVec2& output, const char* message, ... ) -> void
  112. {
  113.     char format[ 1024 ] = { };
  114.  
  115.     va_list args;
  116.     va_start( args, message );
  117.     vsprintf_s( format, message, args );
  118.     va_end( args );
  119.  
  120.     auto size = ImGui::CalcTextSize( format );
  121.  
  122.     output = size;
  123. }
  124.  
  125. }
Add Comment
Please, Sign In to add comment