Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_renderer_batcher.h
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 
6 #ifndef TB_RENDERER_BATCHER_H
7 #define TB_RENDERER_BATCHER_H
8 
9 #include "tb_renderer.h"
10 
11 #ifdef TB_RENDERER_BATCHER
12 
13 namespace tb {
14 
15 #define VERTEX_BATCH_SIZE 6 * 2048
16 
20 class TBRendererBatcher : public TBRenderer
21 {
22 public:
24  struct Vertex
25  {
26  float x, y;
27  float u, v;
28  union {
29  struct { unsigned char r, g, b, a; };
30  uint32_t col;
31  };
32  };
34  class Batch
35  {
36  public:
37  Batch() : vertex_count(0), bitmap(nullptr), fragment(nullptr), batch_id(0), is_flushing(false) {}
38  void Flush(TBRendererBatcher *batch_renderer);
39  Vertex *Reserve(TBRendererBatcher *batch_renderer, int count);
40 
41  Vertex vertex[VERTEX_BATCH_SIZE];
42  int vertex_count;
43 
44  TBBitmap *bitmap;
45  TBBitmapFragment *fragment;
46 
47  uint32_t batch_id;
48  bool is_flushing;
49  };
50 
51  TBRendererBatcher();
52  virtual ~TBRendererBatcher();
53 
54  virtual void BeginPaint(int render_target_w, int render_target_h);
55  virtual void EndPaint();
56 
57  virtual void Translate(int dx, int dy);
58 
59  virtual void SetOpacity(float opacity);
60  virtual float GetOpacity();
61 
62  virtual TBRect SetClipRect(const TBRect &rect, bool add_to_current);
63  virtual TBRect GetClipRect();
64 
65  virtual void DrawBitmap(const TBRect &dst_rect, const TBRect &src_rect, TBBitmapFragment *bitmap_fragment);
66  virtual void DrawBitmap(const TBRect &dst_rect, const TBRect &src_rect, TBBitmap *bitmap);
67  virtual void DrawBitmapColored(const TBRect &dst_rect, const TBRect &src_rect, const TBColor &color, TBBitmapFragment *bitmap_fragment);
68  virtual void DrawBitmapColored(const TBRect &dst_rect, const TBRect &src_rect, const TBColor &color, TBBitmap *bitmap);
69  virtual void DrawBitmapTile(const TBRect &dst_rect, TBBitmap *bitmap);
70  virtual void DrawBitmapTileColored(const TBRect &dst_rect, const TBColor &color, TBBitmap *bitmap);
71  virtual void FlushBitmap(TBBitmap *bitmap);
72  virtual void FlushBitmapFragment(TBBitmapFragment *bitmap_fragment);
73 
74  virtual void BeginBatchHint(TBRenderer::BATCH_HINT /*hint*/) {}
75  virtual void EndBatchHint() {}
76 
77  // == Methods that need implementation in subclasses ================================
78  virtual TBBitmap *CreateBitmap(int width, int height, uint32_t *data) = 0;
79  virtual void RenderBatch(Batch *batch) = 0;
80  virtual void SetClipRect(const TBRect &rect) = 0;
81 protected:
82  uint8_t m_opacity;
83  TBRect m_screen_rect;
84  TBRect m_clip_rect;
85  int m_translation_x;
86  int m_translation_y;
87 
88  float m_u, m_v, m_uu, m_vv;
89  Batch batch;
90 
91  void AddQuadInternal(const TBRect &dst_rect, const TBRect &src_rect, uint32_t color, TBBitmap *bitmap, TBBitmapFragment *fragment);
92  void FlushAllInternal();
93 };
94 
95 } // namespace tb
96 
97 #endif // TB_RENDERER_BATCHER
98 
99 #endif // TB_RENDERER_BATCHER_H
BATCH_HINT
Defines the hint given to BeginBatchHint.
Definition: tb_renderer.h:131