renderer.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // renderer.hpp
  3. // graphics
  4. //
  5. // Created by Sam Jaffe on 5/19/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <unordered_map>
  10. #include "game/graphics/graphics_fwd.h"
  11. #include "game/math/math_fwd.hpp"
  12. #include "game/util/hash.hpp"
  13. #include "vector/vector.hpp"
  14. namespace graphics {
  15. struct renderer {
  16. virtual ~renderer() {}
  17. virtual std::shared_ptr<manager const> manager() const = 0;
  18. virtual void draw(object const & obj) = 0;
  19. virtual void draw(identity<material>, math::matr4 const &,
  20. std::vector<vertex> const &) = 0;
  21. virtual void clear() = 0;
  22. virtual void flush() = 0;
  23. };
  24. enum class driver { openGL };
  25. class direct_renderer : public renderer {
  26. public:
  27. direct_renderer(driver d);
  28. std::shared_ptr<class manager const> manager() const override;
  29. void draw(object const & obj) override;
  30. void draw(identity<material>, math::matr4 const &,
  31. std::vector<vertex> const &) override;
  32. void clear() override;
  33. void flush() override;
  34. private:
  35. struct renderer_impl * pimpl;
  36. };
  37. class batch_renderer : public renderer {
  38. public:
  39. batch_renderer(renderer * impl, std::size_t batch_size = 0);
  40. ~batch_renderer();
  41. std::shared_ptr<class manager const> manager() const override {
  42. return impl_->manager();
  43. }
  44. void draw(object const & obj) override;
  45. void draw(identity<material>, math::matr4 const &,
  46. std::vector<vertex> const &) override;
  47. void clear() override { impl_->clear(); }
  48. void flush() override;
  49. private:
  50. void check();
  51. void write();
  52. private:
  53. renderer * impl_;
  54. std::unordered_map<identity<material>, std::vector<vertex>> batches_;
  55. std::size_t batch_size_;
  56. std::size_t elements_;
  57. };
  58. }