Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_bitmap_fragment.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_BITMAP_FRAGMENT_H
7 #define TB_BITMAP_FRAGMENT_H
8 
9 #include "tb_core.h"
10 #include "tb_geometry.h"
11 #include "tb_hashtable.h"
12 #include "tb_list.h"
13 #include "tb_id.h"
14 #include "tb_linklist.h"
15 
16 namespace tb {
17 
18 class TBBitmapFragment;
19 class TBBitmap;
20 
23 int TBGetNearestPowerOfTwo(int val);
24 
29 {
30 public:
33  static TBImageLoader *CreateFromFile(const TBStr & filename, float dpi);
34 
35  virtual ~TBImageLoader() {}
36 
38  virtual int Width() = 0;
39 
41  virtual int Height() = 0;
42 
45  virtual uint32_t *Data() = 0;
46 };
47 
50 {
51 public:
53  class Space : public TBLinkOf<Space>
54  {
55  public:
56  int x, width;
57  };
58 
59  TBSpaceAllocator(int available_space)
60  : m_available_space(available_space) { }
61 
63  bool IsAllAvailable() const { return !m_used_space_list.HasLinks(); }
64 
66  bool HasSpace(int needed_w) const;
67 
69  Space *AllocSpace(int needed_w);
70 
72  void FreeSpace(Space *space);
73 private:
74  Space *GetSmallestAvailableSpace(int needed_w);
75  int m_available_space;
76  TBLinkListAutoDeleteOf<Space> m_free_space_list;
77  TBLinkListAutoDeleteOf<Space> m_used_space_list;
78 };
79 
82 {
83 public:
84  TBFragmentSpaceAllocator(int y, int width, int height)
85  : TBSpaceAllocator(width), y(y), height(height) {}
86 
87  int y, height;
88 };
89 
92 
95 
99 };
100 
105 {
106 public:
109 
112  bool Init(int bitmap_w, int bitmap_h);
113 
116  TBBitmapFragment *CreateNewFragment(int frag_w, int frag_h, int data_stride, uint32_t *frag_data, bool add_border);
117 
120 
124 private:
125  friend class TBBitmapFragmentManager;
126  bool ValidateBitmap();
127  void DeleteBitmap();
128  void CopyData(TBBitmapFragment *frag, int data_stride, uint32_t *frag_data, int border);
130  int m_bitmap_w, m_bitmap_h;
131  uint32_t *m_bitmap_data;
132  TBBitmap *m_bitmap;
133  bool m_need_update;
134  int m_allocated_pixels;
135 };
136 
141 {
142 public:
144  int Width() const { return m_rect.w; }
145 
147  int Height() const { return m_rect.h; }
148 
152  return m_map->GetBitmap(validate_type);
153  }
154 
157  int GetAllocatedHeight() const { return m_row_height; }
158 public:
159  TBBitmapFragmentMap *m_map;
160  TBRect m_rect;
163  TBID m_id;
164  int m_row_height;
165 
168  uint32_t m_batch_id;
169 };
170 
178 {
179 public:
182 
185  void SetAddBorder(bool add_border) { m_add_border = add_border; }
186  bool GetAddBorder() const { return m_add_border; }
187 
191  TBBitmapFragment *GetFragmentFromFile(const TBStr & filename, bool dedicated_map, float dpi);
192 
194  TBBitmapFragment *GetFragment(const TBID &id) const;
195 
203  TBBitmapFragment *CreateNewFragment(const TBID &id, bool dedicated_map,
204  int data_w, int data_h, int data_stride,
205  uint32_t *data);
206 
209  void FreeFragment(TBBitmapFragment *frag);
210 
214  void Clear();
215 
217  bool ValidateBitmaps();
218 
223  void DeleteBitmaps();
224 
226  int GetNumMaps() const { return m_fragment_maps.GetNumItems(); }
227 
231  void SetNumMapsLimit(int num_maps_limit);
232 
234  void SetDefaultMapSize(int w, int h);
235 
238  int GetUseRatio() const;
239 #ifdef TB_RUNTIME_DEBUG_INFO
240 
241  void Debug();
242 #endif
243 private:
244  TBListOf<TBBitmapFragmentMap> m_fragment_maps;
246  int m_num_maps_limit;
247  bool m_add_border;
248  int m_default_map_w;
249  int m_default_map_h;
250 };
251 
252 } // namespace tb
253 
254 #endif // TB_BITMAP_FRAGMENT_H
void DeleteBitmaps()
Delete all bitmaps in all fragment maps in this manager.
Definition: tb_bitmap_fragment.cpp:521
TBBitmapFragmentManager manages loading bitmaps of arbitrary size, pack as many of them into as few T...
Definition: tb_bitmap_fragment.h:177
int GetNumMaps() const
Get number of fragment maps that is currently used.
Definition: tb_bitmap_fragment.h:226
TBBitmapFragment * GetFragmentFromFile(const TBStr &filename, bool dedicated_map, float dpi)
Get the fragment with the given image filename.
Definition: tb_bitmap_fragment.cpp:416
bool Init(int bitmap_w, int bitmap_h)
Initialize the map with the given size.
Definition: tb_bitmap_fragment.cpp:172
Space * AllocSpace(int needed_w)
Allocate the given space and return the Space, or nullptr on error.
Definition: tb_bitmap_fragment.cpp:37
int GetAllocatedHeight() const
Return the height allocated to this fragment.
Definition: tb_bitmap_fragment.h:157
TBBitmapFragment * GetFragment(const TBID &id) const
Get the fragment with the given id, or nullptr if it doesn&#39;t exist.
Definition: tb_bitmap_fragment.cpp:501
static TBImageLoader * CreateFromFile(const TBStr &filename, float dpi)
Static method used to create an image loader.
Allocates space for TBBitmapFragment in a row (used in TBBitmapFragmentMap).
Definition: tb_bitmap_fragment.h:81
TBImageloader is a class used to load skin images.
Definition: tb_bitmap_fragment.h:28
void SetAddBorder(bool add_border)
Set to true if a 1px border should be added to new fragments so stretched drawing won&#39;t get filtering...
Definition: tb_bitmap_fragment.h:185
virtual int Width()=0
Return the width of the loaded bitmap.
TB_VALIDATE_TYPE
Specify when the bitmap should be validated when calling TBBitmapFragmentMap::GetBitmap.
Definition: tb_bitmap_fragment.h:91
int GetUseRatio() const
Get the amount (in percent) of space that is currently occupied by all maps in this fragment manager...
Definition: tb_bitmap_fragment.cpp:540
void Clear()
Clear all loaded bitmaps and all created bitmap fragments and maps.
Definition: tb_bitmap_fragment.cpp:506
TBID is a wrapper for a uint32_t to be used as ID.
Definition: tb_id.h:18
void FreeFragment(TBBitmapFragment *frag)
Delete the given fragment and free the space it used in its map, so that other fragments can take its...
Definition: tb_bitmap_fragment.cpp:485
Allocator of space out of a given available space.
Definition: tb_bitmap_fragment.h:49
Only validate if the bitmap does not yet exist (Make sure there is a valid bitmap pointer...
Definition: tb_bitmap_fragment.h:98
TBBitmapFragmentMap is used to pack multiple bitmaps into a single TBBitmap.
Definition: tb_bitmap_fragment.h:104
Always validate the bitmap (The bitmap is updated if needed)
Definition: tb_bitmap_fragment.h:94
TBBitmap * GetBitmap(TB_VALIDATE_TYPE validate_type=TB_VALIDATE_ALWAYS)
Return the bitmap for this fragment.
Definition: tb_bitmap_fragment.h:151
TBBitmapFragment represents a sub part of a TBBitmap.
Definition: tb_bitmap_fragment.h:140
virtual uint32_t * Data()=0
Return the data of the loaded bitmap.
TBBitmap is a minimal interface for bitmap to be painted by TBRenderer.
Definition: tb_renderer.h:36
TBListAutoDeleteOf is a list (array) of pointers to the specified object type.
Definition: tb_list.h:118
TBBitmapFragment * CreateNewFragment(const TBID &id, bool dedicated_map, int data_w, int data_h, int data_stride, uint32_t *data)
Create a new fragment from the given data.
Definition: tb_bitmap_fragment.cpp:436
TBHashTableOf is a TBHashTable with the given class type as content.
Definition: tb_hashtable.h:99
void SetDefaultMapSize(int w, int h)
Set the default size of new fragment maps.
Definition: tb_bitmap_fragment.cpp:532
TBListOf is a list (array) of pointers to the specified object type.
Definition: tb_list.h:47
TBBitmapFragment * CreateNewFragment(int frag_w, int frag_h, int data_stride, uint32_t *frag_data, bool add_border)
Create a new fragment with the given size and data in this map.
Definition: tb_bitmap_fragment.cpp:190
TBStr is a simple string class.
Definition: tb_str.h:62
Simple rectangle class.
Definition: tb_geometry.h:25
virtual int Height()=0
Return the height of the loaded bitmap.
uint32_t m_batch_id
This uint32_t is reserved for batching renderer backends.
Definition: tb_bitmap_fragment.h:168
void SetNumMapsLimit(int num_maps_limit)
Set the number of maps (TBBitmaps) this manager should be allowed to create.
Definition: tb_bitmap_fragment.cpp:527
bool ValidateBitmaps()
Validate bitmaps on fragment maps that has changed.
Definition: tb_bitmap_fragment.cpp:512
A chunk of space.
Definition: tb_bitmap_fragment.h:53
bool HasSpace(int needed_w) const
Return true if the given width is currently available.
Definition: tb_bitmap_fragment.cpp:23
void FreeFragmentSpace(TBBitmapFragment *frag)
Free up the space used by the given fragment, so that other fragments can take its place...
Definition: tb_bitmap_fragment.cpp:289
int Width() const
Return the width of the bitmap fragment.
Definition: tb_bitmap_fragment.h:144
bool IsAllAvailable() const
Return true if no allocations are currently live using this allocator.
Definition: tb_bitmap_fragment.h:63
TBBitmap * GetBitmap(TB_VALIDATE_TYPE validate_type=TB_VALIDATE_ALWAYS)
Return the bitmap for this map.
Definition: tb_bitmap_fragment.cpp:370
int TBGetNearestPowerOfTwo(int val)
Return the nearest power of two from val.
Definition: tb_bitmap_fragment.cpp:12
void FreeSpace(Space *space)
Free the given space so it is available for new allocations.
Definition: tb_bitmap_fragment.cpp:89
int Height() const
Return the height of the bitmap fragment.
Definition: tb_bitmap_fragment.h:147