PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/SlotGames/SlotGamesSource/FrameworkSource/cocos2dx-wrap/GameObjectCreation.cpp

https://gitlab.com/BGCX261/zjh-dev-svn-to-git
C++ | 648 lines | 395 code | 86 blank | 167 comment | 119 complexity | 4aaa3c9d09e5fd031777f955673bd6a9 MD5 | raw file
  1. #include <GTBaseDef.h>
  2. #include "GameObjectCreation.h"
  3. #include <GTLog/GTLogManager.h>
  4. #include <MapManager.h>
  5. #include <TagManager.h>
  6. #include "GSpriteCocos2dx.h"
  7. #include "Cocos2dxGeneral.h"
  8. #include "GSpriteAnimationCocos2dx.h"
  9. #include "SpriteAtlasCocos2dx.h"
  10. #include <GTPropertyFile.h>
  11. #include <GameGeneral.h>
  12. #include <UI/UILabelBMFont.h>
  13. #include "UILabelBMFontCocos2dx.h"
  14. #include "ResourceManager.h"
  15. #ifdef GT_DEBUG
  16. #include <GTNew.h>
  17. #endif // GT_DEBUG
  18. // Create a normal sprite without animations, just an image
  19. //
  20. // szFileName = <Sprite Image File Name>.png
  21. // nZOrder for this sprite
  22. GSprite* GameObjectCreation::CreateStaticSprite(const tchar* szFileName, const int32 nZOrder)
  23. {
  24. if(NULL == szFileName)
  25. return NULL;
  26. if(Cocos2dxGeneral::GetInstance()->GetDefaultLayer() == NULL)
  27. {
  28. GTLogManager::GetInstance()->LogError(CTEXT("Cocos2dxGeneral::GetInstance()->GetDefaultLayer() == NULL"));
  29. return NULL;
  30. }
  31. // Create new sprite object
  32. GSpriteCocos2dx *pSprite = new GSpriteCocos2dx();
  33. if(NULL == pSprite)
  34. return NULL;
  35. // Save the key
  36. pSprite->m_strKey = szFileName;
  37. pSprite->m_pSprite = cocos2d::CCSprite::create(szFileName);
  38. if(NULL == pSprite)
  39. return NULL;
  40. // Add the sprite as a child to the specified layer with z-order
  41. //pSprite->m_nZOrder = nZOrder;
  42. Cocos2dxGeneral::GetInstance()->GetDefaultLayer()->addChild(
  43. pSprite->m_pSprite,
  44. nZOrder//pSprite->m_nZOrder
  45. );
  46. pSprite->m_pLayer = Cocos2dxGeneral::GetInstance()->GetDefaultLayer();
  47. return pSprite;
  48. }
  49. // szFileName = <Sprite Image File Name>.png
  50. SpriteAtlas* GameObjectCreation::CreateSpriteAtlas(const tchar* szFileName, const int32 nZOrder)
  51. {
  52. if(NULL == szFileName)
  53. return NULL;
  54. // Create new sprite object
  55. SpriteAtlas* pSpriteAtlas = new SpriteAtlasCocos2dx();
  56. if(NULL == pSpriteAtlas)
  57. return NULL;
  58. if(false == pSpriteAtlas->Initialize(szFileName, nZOrder))
  59. return NULL;
  60. return pSpriteAtlas;
  61. }
  62. // szFileName = <Sprite Image File Name>.sprite
  63. GSprite* GameObjectCreation::CreateStaticSpriteWithSpriteFile(const tchar* szFileName, const int32 nZOrder)
  64. {
  65. if(NULL == szFileName)
  66. return NULL;
  67. // Load the sprite file
  68. GTTreeNode* pTreeNode = GTPropertyFile::Load(szFileName);
  69. if(pTreeNode == NULL)
  70. return NULL;
  71. // Get image node
  72. GTTreeNode* pImageNode = pTreeNode->GetChild(GTPropertyFile::TagImage);
  73. if(NULL == pImageNode)
  74. return NULL;
  75. // Create the sprite
  76. GSprite *pSprite = CreateStaticSprite(pImageNode->GetValue().ToCharString(), nZOrder);
  77. if(NULL == pSprite)
  78. GTLogManager::GetInstance()->LogFormatError(CTEXT("Failed to call CreateStaticSprite for '%s'"), szFileName);
  79. if(pTreeNode)
  80. {
  81. pTreeNode->Release();
  82. pTreeNode = NULL;
  83. }
  84. return pSprite;
  85. }
  86. // Clone a static sprite
  87. GSprite* GameObjectCreation::CloneStaticSprite(GSprite* pSpriteSource, const int32 nZOrder)
  88. {
  89. if(NULL == pSpriteSource)
  90. return NULL;
  91. GSpriteCocos2dx* pSpriteSourceCC = (GSpriteCocos2dx*)pSpriteSource;
  92. if(NULL == pSpriteSourceCC || NULL == pSpriteSourceCC->m_pSprite)
  93. return NULL;
  94. if(Cocos2dxGeneral::GetInstance()->GetDefaultLayer() == NULL)
  95. {
  96. GTLogManager::GetInstance()->LogError(CTEXT("Cocos2dxGeneral::GetInstance()->GetDefaultLayer() == NULL"));
  97. return NULL;
  98. }
  99. // Create new sprite object
  100. GSpriteCocos2dx *pSprite = new GSpriteCocos2dx();
  101. if(NULL == pSprite)
  102. return NULL;
  103. // Save the key
  104. pSprite->m_strKey = pSpriteSourceCC->m_strKey;
  105. // Create new sprite from the texture
  106. pSprite->m_pSprite = cocos2d::CCSprite::createWithTexture(
  107. pSpriteSourceCC->m_pSprite->getTexture(),
  108. pSpriteSourceCC->m_pSprite->getTextureRect()
  109. );
  110. if(NULL == pSprite->m_pSprite)
  111. return NULL;
  112. // Add the sprite as a child to the specified layer with z-order
  113. //pSprite->m_nZOrder = GameGeneral::GetInstance()->GetZOrderIdSprite();
  114. Cocos2dxGeneral::GetInstance()->GetDefaultLayer()->addChild(
  115. pSprite->m_pSprite,
  116. nZOrder//pSprite->m_nZOrder
  117. );
  118. pSprite->m_pLayer = Cocos2dxGeneral::GetInstance()->GetDefaultLayer();
  119. return pSprite;
  120. }
  121. // Create an animated sprite from GTTreeNode
  122. GSprite* GameObjectCreation::CreateAnimatedSpriteFromTreeNode(GTTreeNode* pTreeNode, const int32 nZOrder)
  123. {
  124. if(NULL == pTreeNode)
  125. return NULL;
  126. // Get Image node
  127. GTTreeNode* pNodeImage = pTreeNode->FindChild(GTPropertyFile::TagImage);
  128. if(pNodeImage == NULL)
  129. GTLogManager::GetInstance()->LogFormatError(CTEXT("[%s] tag not found"), GTPropertyFile::TagImage);
  130. // Get plist node
  131. GTTreeNode* pNodePlist = pTreeNode->FindChild(GTPropertyFile::TagPlist);
  132. if(pNodePlist == NULL)
  133. GTLogManager::GetInstance()->LogFormatError(CTEXT("[%s] tag not found"), GTPropertyFile::TagPlist);
  134. // Get animation node
  135. GTTreeNode* pNodeAnimation = pTreeNode->FindChild(GTPropertyFile::TagAnimation);
  136. if(pNodeAnimation == NULL)
  137. GTLogManager::GetInstance()->LogFormatError(CTEXT("[%s] tag not found"), GTPropertyFile::TagAnimation);
  138. // Get animation clip count
  139. const int32 nAnimationClipCount = pNodeAnimation->GetChildCount();
  140. if(nAnimationClipCount == 0)
  141. GTLogManager::GetInstance()->LogError(CTEXT("nAnimationClipCount == 0"));
  142. if(Cocos2dxGeneral::GetInstance()->GetDefaultLayer() == NULL)
  143. {
  144. GTLogManager::GetInstance()->LogError(CTEXT("Cocos2dxGeneral::GetInstance()->GetDefaultLayer() == NULL"));
  145. return NULL;
  146. }
  147. // Create new sprite object
  148. GSpriteCocos2dx *pSprite = new GSpriteCocos2dx();
  149. if(NULL == pSprite)
  150. return NULL;
  151. // Load this sprite into the cache
  152. cocos2d::CCSpriteFrameCache* cacheSpriteFrame = cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache();
  153. if(cacheSpriteFrame == NULL)
  154. return NULL;
  155. cacheSpriteFrame->addSpriteFramesWithFile(pNodePlist->Value().ToCharString());
  156. /*
  157. // Initialize animations
  158. if(!pSprite->InitializeAnimation())
  159. {
  160. pSprite->Release();
  161. pSprite = NULL;
  162. GTLogManager::GetInstance()->LogError(CTEXT("Failed to call pSprite->InitializeAnimation()"));
  163. return NULL;
  164. }
  165. GSpriteAnimationCocos2dx* pSpriteAnimaton = (GSpriteAnimationCocos2dx*)(pSprite->GetAnimation());
  166. // Initialize the animations
  167. if(!pSpriteAnimaton->Initialize(nAnimationClipCount))
  168. {
  169. pSprite->Release();
  170. pSprite = NULL;
  171. GTLogManager::GetInstance()->LogError(CTEXT("Failed to call pSpriteAnimaton->Initialize(nAnimationClipCount)"));
  172. return NULL;
  173. }
  174. */
  175. // Check if animation object is loaded already
  176. GTString strPlistName = pNodePlist->Value();
  177. strPlistName.ToLower();
  178. GSpriteAnimationCocos2dx* pAnimationNew = ResourceManager::Inst()->FindAnimation(strPlistName);
  179. if(pAnimationNew == NULL)
  180. {
  181. // The animation is not loaded yet, load it into resource manager
  182. pAnimationNew = (GSpriteAnimationCocos2dx*)GameObjectCreation::CreateAnimationFromTreeNode(pTreeNode);
  183. // Add this new animation object into resource manager
  184. ResourceManager::Inst()->AddAnimation(strPlistName, pAnimationNew);
  185. }
  186. else
  187. {
  188. // The animation object is loaded alreay, add reference.
  189. pAnimationNew->AddReference();
  190. }
  191. pSprite->m_pAnimation = pAnimationNew;
  192. if(NULL == pSprite->m_pAnimation)
  193. {
  194. pSprite->Release();
  195. pSprite = NULL;
  196. GTLogManager::GetInstance()->LogError(CTEXT("pSprite->m_pAnimation == NULL : Failed to call GameObjectCreation::CreateAnimationFromTreeNode(pTreeNode)"));
  197. return NULL;
  198. }
  199. // Setup first animatin clip as default animation for sprite object
  200. GSpriteAnimationClipCocos2dx* pAnimationClip = (GSpriteAnimationClipCocos2dx*)(pSprite->m_pAnimation->GetClipByIndex(0));
  201. if(pAnimationClip)
  202. {
  203. cocos2d::CCSpriteFrame* pFirstSpriteFrame = pAnimationClip->GetSpriteFrameByIndex(0);
  204. if(pFirstSpriteFrame)
  205. {
  206. pSprite->m_pSprite = cocos2d::CCSprite::createWithSpriteFrame(pFirstSpriteFrame);
  207. }
  208. else
  209. {
  210. GTLogManager::GetInstance()->LogError(CTEXT("Failed to call pAnimationClip->GetSpriteFrameByIndex(0)"));
  211. }
  212. }
  213. else
  214. {
  215. GTLogManager::GetInstance()->LogError(CTEXT("Failed to call pSpriteAnimaton->GetAnimationClipByIndex(0)"));
  216. }
  217. // Add the sprite as a child to the specified layer with z-order
  218. //pSprite->m_nZOrder = nZOrder;
  219. Cocos2dxGeneral::GetInstance()->GetDefaultLayer()->addChild(
  220. pSprite->m_pSprite,
  221. nZOrder//pSprite->m_nZOrder
  222. );
  223. pSprite->m_pLayer = Cocos2dxGeneral::GetInstance()->GetDefaultLayer();
  224. return pSprite;
  225. }
  226. // Create an animated sprite
  227. //
  228. // szFileName = <Sprte Data File>.sprite
  229. GSprite* GameObjectCreation::CreateAnimatedSprite(const tchar* szFileName, const int32 nZOrder)
  230. {
  231. if(NULL == szFileName)
  232. return NULL;
  233. GTTreeNode* pTreeNode = GTPropertyFile::Load(szFileName);
  234. if(pTreeNode == NULL)
  235. return NULL;
  236. GSprite *pSprite = CreateAnimatedSpriteFromTreeNode(pTreeNode, nZOrder);
  237. if(NULL == pSprite)
  238. GTLogManager::GetInstance()->LogFormatError(CTEXT("Failed to call CreateAnimatedSpriteFromTreeNode for '%s'"), szFileName);
  239. // Save the key
  240. ((GSpriteCocos2dx*)pSprite)->m_strKey = szFileName;
  241. if(pTreeNode)
  242. {
  243. pTreeNode->Release();
  244. pTreeNode = NULL;
  245. }
  246. return pSprite;
  247. }
  248. // Clone an animated sprite
  249. GSprite* GameObjectCreation::CloneAnimatedSprite(GSprite* pSpriteSource, const int32 nZOrder)
  250. {
  251. if(NULL == pSpriteSource)
  252. return NULL;
  253. // Get animation object
  254. GSpriteAnimation* pAnimObjectSrc = pSpriteSource->GetAnimation();
  255. if(NULL == pAnimObjectSrc)
  256. return NULL;
  257. // Get animation clip count
  258. const int32 nAnimationClipCountSrc = pAnimObjectSrc->GetClipCount();
  259. if(nAnimationClipCountSrc == 0)
  260. GTLogManager::GetInstance()->LogError(CTEXT("nAnimationClipCount == 0"));
  261. if(Cocos2dxGeneral::GetInstance()->GetDefaultLayer() == NULL)
  262. {
  263. GTLogManager::GetInstance()->LogError(CTEXT("Cocos2dxGeneral::GetInstance()->GetDefaultLayer() == NULL"));
  264. return NULL;
  265. }
  266. // Create new sprite object
  267. GSpriteCocos2dx *pSpriteNew = new GSpriteCocos2dx();
  268. if(NULL == pSpriteNew)
  269. return NULL;
  270. // Save the key
  271. pSpriteNew->m_strKey = ((GSpriteCocos2dx*)pSpriteSource)->m_strKey;
  272. // Reference animation instance
  273. pSpriteNew->m_pAnimation = ((GSpriteCocos2dx*)pSpriteSource)->m_pAnimation;
  274. if(NULL == pSpriteNew->m_pAnimation)
  275. {
  276. pSpriteNew->Release();
  277. pSpriteNew = NULL;
  278. GTLogManager::GetInstance()->LogError(CTEXT("pSpriteSource->m_pAnimation == NULL"));
  279. return NULL;
  280. }
  281. // Add object reference
  282. GSpriteAnimationCocos2dx* pSpriteAnimatonNew = (GSpriteAnimationCocos2dx*)(pSpriteNew->m_pAnimation);
  283. pSpriteAnimatonNew->AddReference();
  284. /*
  285. // Initialize animations
  286. if(!pSpriteNew->InitializeAnimation())
  287. {
  288. pSpriteNew->Release();
  289. pSpriteNew = NULL;
  290. GTLogManager::GetInstance()->LogError(CTEXT("Failed to call pSprite->InitializeAnimation()"));
  291. return NULL;
  292. }
  293. GSpriteAnimationCocos2dx* pSpriteAnimatonNew = (GSpriteAnimationCocos2dx*)(pSpriteNew->GetAnimation());
  294. // Initialize the animations
  295. if(!pSpriteAnimatonNew->Initialize(nAnimationClipCountSrc))
  296. {
  297. pSpriteNew->Release();
  298. pSpriteNew = NULL;
  299. GTLogManager::GetInstance()->LogError(CTEXT("Failed to call pSpriteAnimaton->Initialize(nAnimationClipCount)"));
  300. return NULL;
  301. }
  302. GSpriteAnimationCocos2dx* pSpriteAnimatonSrc = (GSpriteAnimationCocos2dx*)pAnimObjectSrc;
  303. // Build up sprite frames for each animaiton clip
  304. for(int32 nClipIndex = 0; nClipIndex<nAnimationClipCountSrc; nClipIndex++)
  305. {
  306. GSpriteAnimationClipCocos2dx* pClipSrc = (GSpriteAnimationClipCocos2dx*)(pSpriteAnimatonSrc->GetClipByIndex(nClipIndex));
  307. GSpriteAnimationClipCocos2dx* pClipNew = (GSpriteAnimationClipCocos2dx*)(pSpriteAnimatonNew->GetClipByIndex(nClipIndex));
  308. if(pClipSrc && pClipNew)
  309. {
  310. // Get frame count of the clip
  311. const uint32 nClipFrameCountSrc = pClipSrc->GetFrameCount();
  312. // Initialize clip frames
  313. if(pClipNew->InitializeFrame(nClipFrameCountSrc))
  314. {
  315. for(uint32 frame=0; frame<nClipFrameCountSrc; frame++)
  316. {
  317. // Get a frame
  318. pClipNew->m_pAnimFrames->addObject(pClipSrc->m_pAnimFrames->objectAtIndex(frame));
  319. }
  320. // Setup frame delay time
  321. pClipNew->SetFrameDelayTime(pClipSrc->GetFrameDelayTime());
  322. // Initialize the animation
  323. if(pClipNew->InitializeAnimation())
  324. {
  325. // Setup the animation clip name
  326. pClipNew->SetClipName(pClipSrc->GetClipName());
  327. }
  328. else
  329. {
  330. pSpriteNew->Release();
  331. pSpriteNew = NULL;
  332. GTLogManager::GetInstance()->LogError(CTEXT("Failed to call pAnimationClip->InitializeAnimation()"));
  333. return NULL;
  334. }
  335. }
  336. else
  337. {
  338. pSpriteNew->Release();
  339. pSpriteNew = NULL;
  340. GTLogManager::GetInstance()->LogFormatError(CTEXT("Failed to call pAnimationClip->InitializeFrame(). [nFrameCount = %d]"), nClipFrameCountSrc);
  341. return NULL;
  342. }
  343. }
  344. }
  345. */
  346. // Setup first animatin clip as default animation for sprite object
  347. GSpriteAnimationClipCocos2dx* pAnimationClip = (GSpriteAnimationClipCocos2dx*)(pSpriteAnimatonNew->GetClipByIndex(0));
  348. if(pAnimationClip)
  349. {
  350. cocos2d::CCSpriteFrame* pFirstSpriteFrame = pAnimationClip->GetSpriteFrameByIndex(0);
  351. if(pFirstSpriteFrame)
  352. {
  353. pSpriteNew->m_pSprite = cocos2d::CCSprite::createWithSpriteFrame(pFirstSpriteFrame);
  354. }
  355. else
  356. {
  357. GTLogManager::GetInstance()->LogError(CTEXT("Failed to call pAnimationClip->GetSpriteFrameByIndex(0)"));
  358. }
  359. }
  360. else
  361. {
  362. GTLogManager::GetInstance()->LogError(CTEXT("Failed to call pSpriteAnimaton->GetAnimationClipByIndex(0)"));
  363. }
  364. // Add the sprite as a child to the specified layer with z-order
  365. //pSpriteNew->m_nZOrder = nZOrder;
  366. Cocos2dxGeneral::GetInstance()->GetDefaultLayer()->addChild(
  367. pSpriteNew->m_pSprite,
  368. nZOrder//pSpriteNew->m_nZOrder
  369. );
  370. pSpriteNew->m_pLayer = Cocos2dxGeneral::GetInstance()->GetDefaultLayer();
  371. return pSpriteNew;
  372. }
  373. // Create an animation object from specified GTTreeNode object
  374. GSpriteAnimation* GameObjectCreation::CreateAnimationFromTreeNode(GTTreeNode* pTreeNode)
  375. {
  376. if(NULL == pTreeNode)
  377. return NULL;
  378. // Get animation node
  379. GTTreeNode* pNodeAnimation = pTreeNode->FindChild(GTPropertyFile::TagAnimation);
  380. if(pNodeAnimation == NULL)
  381. GTLogManager::GetInstance()->LogFormatError(CTEXT("GameObjectCreation.[%s] tag not found"), GTPropertyFile::TagAnimation);
  382. // Get animation clip count
  383. const int32 nAnimationClipCount = pNodeAnimation->GetChildCount();
  384. if(nAnimationClipCount == 0)
  385. GTLogManager::GetInstance()->LogError(CTEXT("GameObjectCreation.nAnimationClipCount == 0"));
  386. // Get cocos2dx sprite cache object
  387. cocos2d::CCSpriteFrameCache* cacheSpriteFrame = cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache();
  388. if(cacheSpriteFrame == NULL)
  389. return NULL;
  390. // Get Plist node
  391. GTTreeNode* pNodePlist = pTreeNode->FindChild(GTPropertyFile::TagPlist);
  392. // Initialize animations
  393. GSpriteAnimationCocos2dx* pNewAnimation = new GSpriteAnimationCocos2dx();
  394. if(pNewAnimation == NULL)
  395. {
  396. GTLogManager::GetInstance()->LogError("GSpriteCocos2dx.InitializeAnimation: m_pAnimaton == NULL");
  397. return NULL;
  398. }
  399. pNewAnimation->m_strKey = (pNodePlist != NULL) ? pNodePlist->Value() : CTEXT("no-anim-key");
  400. // Initialize the animations
  401. if(!pNewAnimation->Initialize(nAnimationClipCount))
  402. {
  403. pNewAnimation->Release();
  404. pNewAnimation = NULL;
  405. GTLogManager::GetInstance()->LogError(CTEXT("Failed to call pNewAnimation->Initialize(nAnimationClipCount)"));
  406. return NULL;
  407. }
  408. // Build up sprite frames for each animaiton clip
  409. tchar buf[128];
  410. for(int32 animation = 0; animation<nAnimationClipCount; animation++)
  411. {
  412. GTTreeNode* pNodeClip = pNodeAnimation->GetChild(animation);
  413. if(pNodeClip)
  414. {
  415. // Get clip information
  416. const float fDelayTime = pNodeClip->GetChild(GTPropertyFile::TagDelayTime)->Value().ToFloat();
  417. const tchar* szPrefixName = pNodeClip->GetChild(GTPropertyFile::TagPrefix)->Value().ToCharString();
  418. const int32 nStartFrame = pNodeClip->GetChild(GTPropertyFile::TagStartFrame)->Value().ToInt();
  419. const int32 nEndFrame = pNodeClip->GetChild(GTPropertyFile::TagEndFrame)->Value().ToInt();
  420. const int32 nFrameCount = nEndFrame - nStartFrame + 1;
  421. GSpriteAnimationClipCocos2dx* pAnimationClip = (GSpriteAnimationClipCocos2dx*)(pNewAnimation->GetClipByIndex(animation));
  422. if(pAnimationClip)
  423. {
  424. // Initialize animation frames
  425. if(pAnimationClip->InitializeFrame(nFrameCount))
  426. {
  427. for(int32 frame=nStartFrame; frame<=nEndFrame; frame++)
  428. {
  429. tsprintf(buf, GSpriteAnimation::m_sAnimationClipNameFormat, szPrefixName, frame);
  430. // Get a CCSpriteFrame object by its name that is already loaded by CCSpriteFrameCache::addSpriteFramesWithFile
  431. cocos2d::CCSpriteFrame* spriteFrame = cacheSpriteFrame->spriteFrameByName(buf);
  432. if(spriteFrame)
  433. {
  434. pAnimationClip->m_pAnimFrames->addObject(spriteFrame);
  435. }
  436. else
  437. {
  438. GTLogManager::GetInstance()->LogFormatError(CTEXT("Failed to call cacheSpriteFrame->spriteFrameByName: %s"), buf);
  439. }
  440. }
  441. // Build the animation
  442. // Setup frame delay time
  443. pAnimationClip->SetFrameDelayTime(fDelayTime);
  444. // Initialize the animation
  445. if(pAnimationClip->InitializeAnimation())
  446. {
  447. // Setup the animation clip name
  448. pAnimationClip->SetClipName(pNodeClip->Value().ToCharString());
  449. }
  450. else
  451. {
  452. pNewAnimation->Release();
  453. pNewAnimation = NULL;
  454. GTLogManager::GetInstance()->LogError(CTEXT("Failed to call pAnimationClip->InitializeAnimation()"));
  455. return NULL;
  456. }
  457. }
  458. else
  459. {
  460. pNewAnimation->Release();
  461. pNewAnimation = NULL;
  462. GTLogManager::GetInstance()->LogFormatError(CTEXT("Failed to call pAnimationClip->InitializeFrame(). [nFrameCount = %d]"), nFrameCount);
  463. return NULL;
  464. }
  465. }
  466. }
  467. }
  468. return pNewAnimation;
  469. }
  470. // Create an animation object from specified .sprite file
  471. GSpriteAnimation* GameObjectCreation::CreateAnimationFromSpriteFile(const tchar* szFileName)
  472. {
  473. if(NULL == szFileName)
  474. return NULL;
  475. // Load .sprite file
  476. GTTreeNode* pTreeNode = GTPropertyFile::Load(szFileName);
  477. if(pTreeNode == NULL)
  478. return NULL;
  479. GSpriteAnimation* pNewAnimation = GameObjectCreation::CreateAnimationFromTreeNode(pTreeNode);
  480. if(pTreeNode)
  481. {
  482. pTreeNode->Release();
  483. pTreeNode = NULL;
  484. }
  485. return pNewAnimation;
  486. }
  487. // Release a sprite object created by CreateSprite or CreateAnimatedSprite
  488. bool GameObjectCreation::ReleaseSprite(GSprite** ppSprite)
  489. {
  490. if(NULL == ppSprite || NULL == (*ppSprite))
  491. {
  492. return false;
  493. }
  494. GSpriteCocos2dx* pSprite = (GSpriteCocos2dx*)(*ppSprite);
  495. if(NULL == pSprite)
  496. {
  497. return false;
  498. }
  499. pSprite->Release();
  500. pSprite = NULL;
  501. return true;
  502. }
  503. // Load a map from the name specified
  504. cocos2d::CCTMXTiledMap* GameObjectCreation::LoadMap(const tchar* szName)
  505. {
  506. if(szName == NULL)
  507. {
  508. return NULL;
  509. }
  510. if(Cocos2dxGeneral::GetInstance()->GetDefaultLayer() == NULL)
  511. {
  512. GTLogManager::GetInstance()->LogError(CTEXT("Cocos2dxGeneral::GetInstance()->GetDefaultLayer() == NULL"));
  513. return NULL;
  514. }
  515. cocos2d::CCTMXTiledMap *pMap = cocos2d::CCTMXTiledMap::create(szName);
  516. if(NULL == pMap)
  517. {
  518. GTLogManager::GetInstance()->LogError(CTEXT("Failed to load the map"));
  519. return NULL;
  520. }
  521. // Add this map object as the child of the layer
  522. Cocos2dxGeneral::GetInstance()->GetDefaultLayer()->addChild(
  523. pMap,
  524. GameGeneral::GetInstance()->GetZOrderIdMap(),
  525. TagManager::GetInstance()->GetTagMainMap()
  526. );
  527. // Set default position
  528. pMap->setPosition(cocos2d::CCPoint(0, 0));
  529. // Save the instance
  530. //MapManager::GetInstance()->SetMap(pMap);
  531. // Set obstacle layer invisible
  532. //cocos2d::CCTMXLayer *pObstacleLayer = MapManager::GetInstance()->GetObstacleLayer();
  533. return pMap;
  534. }
  535. // UILabelBMFont object
  536. UILabelBMFont* GameObjectCreation::CreateUILabelBMFont(void)
  537. {
  538. return new UILabelBMFontCocos2dx();
  539. }
  540. // Release a UILabelBMFont object
  541. bool GameObjectCreation::ReleaseUILabelBMFont(UILabelBMFont** pUILabelBMFont)
  542. {
  543. if(NULL == pUILabelBMFont || NULL == (*pUILabelBMFont))
  544. return false;
  545. /*
  546. UILabelBMFontCocos2dx* pLabel = (UILabelBMFontCocos2dx*)(*pUILabelBMFont);
  547. if(NULL == pLabel)
  548. return false;
  549. pLabel->Release();
  550. delete pLabel;
  551. (*pUILabelBMFont) = NULL;
  552. */
  553. if((*pUILabelBMFont))
  554. {
  555. (*pUILabelBMFont)->Release();
  556. (*pUILabelBMFont) = NULL;
  557. }
  558. return true;
  559. }