text: #include "carbon/graphics/renderer.h"
  #include "carbon/platform/platform.h"
  
  #include "carbon/debugging/cr_assert.h"
  #include "carbon/debugging/cr_log.h"
  
  namespace CR {
  
  RenderAPI Renderer::api_ = RenderAPI::kNone;
  Context* Renderer::context_ = nullptr;
  
  void Renderer::SetAPI(RenderAPI api) {
    // Check whether the renderer is supported and if not, roll back to OpenGL
    switch (api) {
    case RenderAPI::kOpenGL:
      api_ = api;
      break;
  
    case RenderAPI::kDirectX11:
  #ifdef CR_PLATFORM_SUPPORTS_DIRECTX11
      api_ = api;
  #else
      CR_LOG("Platform doesn't support %s, falling back to OpenGL\n", GetAPIstr(api));
      api_ = RenderAPI::kOpenGL;
  #endif // CO_PLATFORM_SUPPORTS_DIRECTX11
      break;
  
    case RenderAPI::kVulkan:
  #ifdef CR_PLATFORM_SUPPORTS_VULKAN
      api_ = api;
  #else
      CR_LOG("Platform doesn't support %s, falling back to OpenGL\n", GetAPIstr(api));
      api_ = RenderAPI::kOpenGL;
  #endif // CO_PLATFORM_SUPPORTS_VULKAN
      break;
  
    case RenderAPI::kMetal:
  #ifdef CR_PLATFORM_SUPPORTS_METAL
      api_ = api;
  #else
      CR_LOG("Platform doesn't support %s, falling back to OpenGL\n", GetAPIstr(api));
      api_ = RenderAPI::kOpenGL;
  #endif // CO_PLATFORM_SUPPORTS_METAL
      break;
  
    case RenderAPI::kNone:
      CR_ASSERT(false, "Render API not set");
      break;
    }
  
    CR_LOG("Renderer initialised for %s\n", GetAPIstr(api_));
  }
  
  void Renderer::LoadShaders() {
    
  }
  
  RenderAPI Renderer::API() { return api_; }
  
  void Renderer::SetContext(Context* context) { context_ = context; }
  
  void* Renderer::GetContext() { return context_; }
  
  void Renderer::SetClearColour(float r, float g, float b, float a)  { context_->SetClearColour(r, g, b, a); }
  void Renderer::BeginRenderPass() { context_->BeginRenderPass(); }
  void Renderer::BindShader(const std::shared_ptr<Shader>& shader) { context_->BindShader(shader); };
  void Renderer::EndRenderPass() { context_->EndRenderPass(); }
  
  void Renderer::SwapBuffers() { context_->SwapBuffers(); }
  
  void Renderer::Render(const std::shared_ptr<VertexArray>& vao) { context_->Render(vao); }
  
  #ifdef CR_BUILD_DEBUG
  const char* Renderer::GetAPIstr(RenderAPI api) {
    switch (api) {
    case RenderAPI::kOpenGL:    return "OpenGL";
    case RenderAPI::kDirectX11: return "DirectX11";
    case RenderAPI::kVulkan:    return "Vulkan";
    case RenderAPI::kMetal:     return "Metal";
    default: return "Unknown Rendering API";
    }
  }
  #endif
  
  } // CR
  