tb_image_manager.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_IMAGE_MANAGER_H
  6. #define TB_IMAGE_MANAGER_H
  7. #include "../tb_core.h"
  8. #ifdef TB_IMAGE
  9. #include "../tb_linklist.h"
  10. #include "../tb_hashtable.h"
  11. #include "../tb_bitmap_fragment.h"
  12. #include "../tb_renderer.h"
  13. namespace tb {
  14. class TBImageManager;
  15. /** TBImageRep is the internal contents of a TBImage. Owned by reference counting from TBImage. */
  16. class TBImageRep
  17. {
  18. friend class TBImageManager;
  19. friend class TBImage;
  20. TBImageRep(TBImageManager *image_manager, TBBitmapFragment *fragment, uint32 hash_key);
  21. void IncRef();
  22. void DecRef();
  23. int ref_count;
  24. uint32 hash_key;
  25. TBImageManager *image_manager;
  26. TBBitmapFragment *fragment;
  27. };
  28. /** TBImage is a reference counting object representing a image loaded by TBImageManager.
  29. As long as there are TBImage objects for a certain image, it will be kept loaded in memory.
  30. It may be empty if the image has not yet been set, or if the TBImageManager is destroyed
  31. when the image is still alive.
  32. */
  33. class TBImage
  34. {
  35. public:
  36. TBImage() : m_image_rep(nullptr) {}
  37. TBImage(TBImageRep *rep);
  38. TBImage(const TBImage &image);
  39. ~TBImage();
  40. /** Return true if this image is empty. */
  41. bool IsEmpty() const;
  42. /** Return the width of this image, or 0 if empty. */
  43. int Width() const;
  44. /** Return the height of this image, or 0 if empty. */
  45. int Height() const;
  46. /** Return the bitmap fragment for this image, or nullptr if empty. */
  47. TBBitmapFragment *GetBitmap() const;
  48. const TBImage& operator = (const TBImage &image) { SetImageRep(image.m_image_rep); return *this; }
  49. bool operator == (const TBImage &image) const { return m_image_rep == image.m_image_rep; }
  50. bool operator != (const TBImage &image) const { return m_image_rep != image.m_image_rep; }
  51. private:
  52. void SetImageRep(TBImageRep *image_rep);
  53. TBImageRep *m_image_rep;
  54. };
  55. /** TBImageManager loads images returned as TBImage objects.
  56. It internally use a TBBitmapFragmentManager that create fragment maps for loaded images,
  57. and keeping track of which images are loaded so they are not loaded several times.
  58. Images are forgotten when there are no longer any TBImage objects for a given file.
  59. */
  60. class TBImageManager : private TBRendererListener
  61. {
  62. public:
  63. TBImageManager();
  64. ~TBImageManager();
  65. /** Return a image object for the given filename.
  66. If it fails, the returned TBImage object will be empty. */
  67. TBImage GetImage(const char *filename);
  68. #ifdef TB_RUNTIME_DEBUG_INFO
  69. /** Render the skin bitmaps on screen, to analyze fragment positioning. */
  70. void Debug() { m_frag_manager.Debug(); }
  71. #endif
  72. // Implementing TBRendererListener
  73. virtual void OnContextLost();
  74. virtual void OnContextRestored();
  75. private:
  76. TBBitmapFragmentManager m_frag_manager;
  77. TBHashTableOf<TBImageRep> m_image_rep_hash;
  78. friend class TBImageRep;
  79. void RemoveImageRep(TBImageRep *image_rep);
  80. };
  81. /** The global TBImageManager. */
  82. extern TBImageManager *g_image_manager;
  83. }; // namespace tb
  84. #endif // TB_IMAGE
  85. #endif // TB_IMAGE_MANAGER_H