tb_renderer_batcher.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_RENDERER_BATCHER_H
  6. #define TB_RENDERER_BATCHER_H
  7. #include "../tb_renderer.h"
  8. #ifdef TB_RENDERER_BATCHER
  9. namespace tb {
  10. #define VERTEX_BATCH_SIZE 6 * 2048
  11. /** TBRendererBatcher is a helper class that implements batching of draw operations for a TBRenderer.
  12. If you do not want to do your own batching you can subclass this class instead of TBRenderer.
  13. If overriding any function in this class, make sure to call the base class too. */
  14. class TBRendererBatcher : public TBRenderer
  15. {
  16. public:
  17. /** Vertex stored in a Batch */
  18. struct Vertex
  19. {
  20. float x, y;
  21. float u, v;
  22. union {
  23. struct { unsigned char r, g, b, a; };
  24. uint32 col;
  25. };
  26. };
  27. /** A batch which should be rendered. */
  28. class Batch
  29. {
  30. public:
  31. Batch() : vertex_count(0), bitmap(nullptr), fragment(nullptr), batch_id(0), is_flushing(false) {}
  32. void Flush(TBRendererBatcher *batch_renderer);
  33. Vertex *Reserve(TBRendererBatcher *batch_renderer, int count);
  34. Vertex vertex[VERTEX_BATCH_SIZE];
  35. int vertex_count;
  36. TBBitmap *bitmap;
  37. TBBitmapFragment *fragment;
  38. uint32 batch_id;
  39. bool is_flushing;
  40. };
  41. TBRendererBatcher();
  42. virtual ~TBRendererBatcher();
  43. virtual void BeginPaint(int render_target_w, int render_target_h);
  44. virtual void EndPaint();
  45. virtual void Translate(int dx, int dy);
  46. virtual void SetOpacity(float opacity);
  47. virtual float GetOpacity();
  48. virtual TBRect SetClipRect(const TBRect &rect, bool add_to_current);
  49. virtual TBRect GetClipRect();
  50. virtual void DrawBitmap(const TBRect &dst_rect, const TBRect &src_rect, TBBitmapFragment *bitmap_fragment);
  51. virtual void DrawBitmap(const TBRect &dst_rect, const TBRect &src_rect, TBBitmap *bitmap);
  52. virtual void DrawBitmapColored(const TBRect &dst_rect, const TBRect &src_rect, const TBColor &color, TBBitmapFragment *bitmap_fragment);
  53. virtual void DrawBitmapColored(const TBRect &dst_rect, const TBRect &src_rect, const TBColor &color, TBBitmap *bitmap);
  54. virtual void DrawBitmapTile(const TBRect &dst_rect, TBBitmap *bitmap);
  55. virtual void DrawRect(const TBRect &dst_rect, const TBColor &color);
  56. virtual void DrawRectFill(const TBRect &dst_rect, const TBColor &color);
  57. virtual void FlushBitmap(TBBitmap *bitmap);
  58. virtual void FlushBitmapFragment(TBBitmapFragment *bitmap_fragment);
  59. virtual void BeginBatchHint(TBRenderer::BATCH_HINT hint) {}
  60. virtual void EndBatchHint() {}
  61. // == Methods that need implementation in subclasses ================================
  62. virtual TBBitmap *CreateBitmap(int width, int height, uint32 *data) = 0;
  63. virtual void RenderBatch(Batch *batch) = 0;
  64. virtual void SetClipRect(const TBRect &rect) = 0;
  65. protected:
  66. uint8 m_opacity;
  67. TBRect m_screen_rect;
  68. TBRect m_clip_rect;
  69. int m_translation_x;
  70. int m_translation_y;
  71. float m_u, m_v, m_uu, m_vv; ///< Some temp variables
  72. Batch batch; ///< The one and only batch. this should be improved.
  73. void AddQuadInternal(const TBRect &dst_rect, const TBRect &src_rect, uint32 color, TBBitmap *bitmap, TBBitmapFragment *fragment);
  74. void FlushAllInternal();
  75. };
  76. }; // namespace tb
  77. #endif // TB_RENDERER_BATCHER
  78. #endif // TB_RENDERER_BATCHER_H