对象池简单实现

前端之家收集整理的这篇文章主要介绍了对象池简单实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

每帧都会有贴图变化,尝试使用对象池用于优化贴图的创建和删除

ObjPool.h

  1. #ifndef _OBJPOOL_H_
  2. #define _OBJPOOL_H_
  3.  
  4. #include <string>
  5. #include <list>
  6.  
  7. #include <map>
  8.  
  9.  
  10. typedef struct _tagSPRITE
  11. {
  12. std::string name;
  13. }iSprite;
  14.  
  15. typedef struct _tagSpriteInUse
  16. {
  17. iSprite* pSprite;
  18. bool bInUse;
  19.  
  20. _tagSpriteInUse()
  21. {
  22. pSprite = NULL;
  23. bInUse = true;
  24. }
  25. }SpriteInUse;
  26.  
  27.  
  28. typedef std::map<std::string,std::list<SpriteInUse*> > MapSprite;
  29.  
  30.  
  31.  
  32. class ObjPool
  33. {
  34. public:
  35. static ObjPool* Instance();
  36. static void Release();
  37.  
  38. //add obj
  39. iSprite* CreateObject(std::string& name);
  40. //releaSEObj
  41. void ReleaSEObj(std::string& name,iSprite* pSprite);
  42.  
  43. //gc
  44. void RemoveUnusedObjects();
  45.  
  46. protected:
  47. ObjPool(void);
  48. ~ObjPool(void);
  49.  
  50. void ReleaseAllData();
  51.  
  52.  
  53. SpriteInUse* CreateSpriteInUse(std::string& name);
  54. private:
  55. MapSprite _mapSprite;
  56. static ObjPool* m_pObjPool;
  57.  
  58. };
  59.  
  60. #endif

ObjPool.cpp

  1. #include "ObjPool.h"
  2. ObjPool* ObjPool::m_pObjPool = NULL;
  3.  
  4. ObjPool::ObjPool(void)
  5. {
  6. _mapSprite.clear();
  7. }
  8.  
  9.  
  10. ObjPool::~ObjPool(void)
  11. {
  12. ReleaseAllData();
  13. }
  14.  
  15. ObjPool* ObjPool::Instance()
  16. {
  17. if( !m_pObjPool )
  18. {
  19. m_pObjPool = new ObjPool();
  20. }
  21. return m_pObjPool;
  22. }
  23.  
  24. void ObjPool::Release()
  25. {
  26. m_pObjPool->ReleaseAllData();
  27. SAFE_DELETE( m_pObjPool );
  28. }
  29.  
  30. void ObjPool::ReleaseAllData()
  31. {
  32. MapSprite::iterator it = _mapSprite.begin();
  33. MapSprite::iterator itEnd = _mapSprite.begin();
  34.  
  35. for( ; it != itEnd; ++it )
  36. {
  37. std::list<SpriteInUse*>& vec = it->second;
  38. std::list<SpriteInUse*>::iterator iter = vec.begin();
  39. std::list<SpriteInUse*>::iterator iterend = vec.end();
  40.  
  41. while(iter != iterend )
  42. {
  43. SAFE_DELETE( (*iter)->pSprite );
  44. iter++;
  45. }
  46. }
  47.  
  48. _mapSprite.clear();
  49. }
  50.  
  51. //add obj
  52. iSprite* ObjPool::CreateObject(std::string& name)
  53. {
  54. MapSprite::iterator it = _mapSprite.find(name);
  55. if( it != _mapSprite.end() )
  56. {
  57. //found
  58. std::list<SpriteInUse*>& vec = it->second;
  59. std::list<SpriteInUse*>::iterator iter = vec.begin();
  60. std::list<SpriteInUse*>::iterator iterend = vec.end();
  61.  
  62. for ( ; iter != iterend; ++iter )
  63. {
  64. if( !(*iter)->bInUse )
  65. {
  66. (*iter)->bInUse = true;
  67. (*iter)->pSprite->SetEnable(iTRUE);
  68. return (*iter)->pSprite;
  69. }
  70. }
  71. }
  72.  
  73. //not found or found but all inuse
  74. //create new iSprite
  75. SpriteInUse* pSIU = CreateSpriteInUse(name);
  76. if( it != _mapSprite.end() )
  77. {
  78. std::list<SpriteInUse*>& vec = it->second;
  79. vec.push_back(pSIU);
  80. }
  81. else
  82. {
  83. std::list<SpriteInUse*> vecSpriteInUse;
  84. vecSpriteInUse.push_back(pSIU);
  85. _mapSprite.insert(std::make_pair(name,vecSpriteInUse));
  86. }
  87.  
  88. return pSIU->pSprite;
  89. }
  90.  
  91. SpriteInUse* I2DObjPool::CreateSpriteInUse(std::string& name)
  92. {
  93. iSprite* pSprite = new iSprite();
  94. pSprite->name = name;
  95.  
  96. SpriteInUse* pSpriteInUse = myNew SpriteInUse();
  97. pSpriteInUse->pSprite = pSprite;
  98. pSpriteInUse->bInUse = true;
  99.  
  100. return pSpriteInUse;
  101. }
  102.  
  103. void ObjPool::ReleaSEObj(std::string& name,iSprite* pSprite)
  104. {
  105. MapSprite::iterator it = _mapSprite.find(name);
  106. if( it != _mapSprite.end() )
  107. {
  108. std::list<SpriteInUse*>& vec = it->second;
  109. std::list<SpriteInUse*>::iterator iter = vec.begin();
  110. std::list<SpriteInUse*>::iterator iterend = vec.end();
  111.  
  112. for ( ; iter != iterend; ++iter )
  113. {
  114. if( (*iter)->pSprite == pSprite )
  115. {
  116. (*iter)->bInUse = false;
  117. pSprite->SetEnable(iFALSE);
  118. return;
  119. }
  120. }
  121.  
  122. }
  123.  
  124. int x = 0; //something wrong
  125. }
  126.  
  127.  
  128. //gc
  129. void ObjPool::RemoveUnusedObjects()
  130. {
  131. MapSprite::iterator it = _mapSprite.begin();
  132. MapSprite::iterator itEnd = _mapSprite.begin();
  133.  
  134. for( ; it != itEnd; ++it )
  135. {
  136. std::list<SpriteInUse*>& vec = it->second;
  137. std::list<SpriteInUse*>::iterator iter = vec.begin();
  138. std::list<SpriteInUse*>::iterator iterend = vec.end();
  139.  
  140. while(iter != iterend )
  141. {
  142. if( !(*iter)->bInUse )
  143. {
  144. SAFE_DELETE( (*iter)->pSprite );
  145. iter = vec.erase(iter);
  146. }
  147. else
  148. {
  149. ++iter;
  150. }
  151. }
  152. }
  153. }

猜你在找的Cocos2d-x相关文章