Made CCBIReaderTest finally work completely. =) Also added support for having a root path when loading a ccb-file (i.e. when you have the two directories, one for the ccb files, one for the spritesheets. <- With a common ancestor.)

This commit is contained in:
Nicolas Gramlich 2012-06-11 18:31:11 -07:00
parent 4c8351f304
commit 1f2652a8d7
10 changed files with 97 additions and 57 deletions

View File

@ -59,6 +59,10 @@ CCBReader::CCBReader(CCBReader * pCCBReader) {
this->mCCBSelectorResolver = pCCBReader->mCCBSelectorResolver;
}
std::string CCBReader::getCCBRootPath() {
return this->mCCBRootPath;
}
CCBMemberVariableAssigner * CCBReader::getCCBMemberVariableAssigner() {
return this->mCCBMemberVariableAssigner;
}
@ -71,12 +75,18 @@ float CCBReader::getResolutionScale() {
return this->mResolutionScale;
}
CCNode * CCBReader::readNodeGraphFromFile(const char * pCCBFileName, CCObject * pOwner) {
return this->readNodeGraphFromFile(pCCBFileName, pOwner, CCDirector::sharedDirector()->getWinSize());
CCNode * CCBReader::readNodeGraphFromFile(const char * pCCBRootPath, const char * pCCBFileName, CCObject * pOwner) {
return this->readNodeGraphFromFile(pCCBRootPath, pCCBFileName, pOwner, CCDirector::sharedDirector()->getWinSize());
}
CCNode * CCBReader::readNodeGraphFromFile(const char * pCCBFileName, CCObject * pOwner, CCSize pRootContainerSize) {
const char * path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(pCCBFileName);
CCNode * CCBReader::readNodeGraphFromFile(const char * pCCBRootPath, const char * pCCBFileName, CCObject * pOwner, CCSize pRootContainerSize) {
this->mCCBRootPath = pCCBRootPath;
char ccbFullFilePath[strlen(pCCBRootPath) + strlen(pCCBFileName) + 1];
strcpy(ccbFullFilePath, pCCBRootPath);
strcat(ccbFullFilePath, pCCBFileName);
const char * path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(ccbFullFilePath);
unsigned long size = 0;
this->mBytes = CCFileUtils::sharedFileUtils()->getFileData(path, "r", &size);
@ -240,19 +250,19 @@ void CCBReader::alignBits() {
}
}
const char * CCBReader::readCachedString() {
std::string CCBReader::readCachedString() {
int i = this->readInt(false);
return this->mStringCache[i].c_str();
return this->mStringCache[i];
}
CCNode * CCBReader::readNodeGraph(CCNode * pParent) {
/* Read class name. */
const char * className = this->readCachedString();
const char * className = this->readCachedString().c_str();
int memberVarAssignmentType = this->readInt(false);
const char * memberVarAssignmentName;
if(memberVarAssignmentType != kCCBTargetTypeNone) {
memberVarAssignmentName = this->readCachedString();
memberVarAssignmentName = this->readCachedString().c_str();
}
CCNodeLoader * ccNodeLoader = this->mCCNodeLoaderLibrary->getCCNodeLoader(className);
@ -324,28 +334,36 @@ void CCBReader::addLoadedSpriteSheet(const char * pSpriteSheet) {
this->mLoadedSpriteSheets.insert(pSpriteSheet);
}
const char * CCBReader::lastPathComponent(const char * pPath) {
std::string CCBReader::lastPathComponent(const char * pPath) {
std::string path(pPath);
int slashPos = path.find_last_of("/");
if(slashPos != std::string::npos) {
return path.substr(slashPos + 1, path.length() - slashPos).c_str();
return path.substr(slashPos + 1, path.length() - slashPos);
}
return pPath;
return path;
}
const char * CCBReader::deletePathExtension(const char * pPath) {
std::string CCBReader::deletePathExtension(const char * pPath) {
std::string path(pPath);
int dotPos = path.find_last_of(".");
if(dotPos != std::string::npos) {
return path.substr(0, dotPos).c_str();
return path.substr(0, dotPos);
}
return pPath;
return path;
}
const char * CCBReader::toLowerCase(const char * pString) {
std::string CCBReader::toLowerCase(const char * pString) {
std::string copy(pString);
std::transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
return copy.c_str();
return copy;
}
std::string CCBReader::concat(const char * pStringA, const char * pStringB) {
std::string stringA(pStringA);
std::string stringB(pStringB);
std::string string = stringA + stringB;
return string;
}
bool CCBReader::endsWith(const char * pString, const char * pEnding) {

View File

@ -83,6 +83,7 @@ class CCNodeLoaderLibrary;
*/
class CC_DLL CCBReader : public CCObject {
private:
std::string mCCBRootPath;
bool mRootCCBReader;
unsigned char * mBytes;
@ -107,11 +108,12 @@ class CC_DLL CCBReader : public CCObject {
/* Destructor. */
~CCBReader();
CCNode * readNodeGraphFromFile(const char * pCCBFileName, CCObject * pOwner = NULL);
CCNode * readNodeGraphFromFile(const char * pCCBFileName, CCObject * pOwner, CCSize pRootContainerSize);
CCNode * readNodeGraphFromFile(const char * pCCBRootPath, const char * pCCBFileName, CCObject * pOwner = NULL);
CCNode * readNodeGraphFromFile(const char * pCCBRootPath, const char * pCCBFileName, CCObject * pOwner, CCSize pRootContainerSize);
CCBMemberVariableAssigner * getCCBMemberVariableAssigner();
CCBSelectorResolver * getCCBSelectorResolver();
std::string getCCBRootPath();
CCObject * getOwner();
CCNode * getRootNode();
CCSize getContainerSize(CCNode *);
@ -121,17 +123,18 @@ class CC_DLL CCBReader : public CCObject {
void addLoadedSpriteSheet(const char *);
/* Utility methods. */
const char * lastPathComponent(const char *);
const char * deletePathExtension(const char *);
const char * toLowerCase(const char *);
std::string lastPathComponent(const char *);
std::string deletePathExtension(const char *);
std::string toLowerCase(const char *);
bool endsWith(const char *, const char *);
std::string concat(const char *, const char *);
/* Parse methods. */
int readInt(bool pSign);
unsigned char readByte();
bool readBool();
float readFloat();
const char * readCachedString();
std::string readCachedString();
private:
bool readHeader();

View File

@ -39,7 +39,7 @@ void CCLabelBMFontLoader::onHandlePropTypeBlendFunc(CCNode * pNode, CCNode * pPa
void CCLabelBMFontLoader::onHandlePropTypeFntFile(CCNode * pNode, CCNode * pParent, const char * pPropertyName, const char * pFntFile, CCBReader * pCCBReader) {
if(strcmp(pPropertyName, PROPERTY_FNTFILE) == 0) {
((CCLabelBMFont *)pNode)->setFntFile(pFntFile);
((CCLabelBMFont *)pNode)->setFntFile(pCCBReader->concat(pCCBReader->getCCBRootPath().c_str(), pFntFile).c_str());
} else {
CCNodeLoader::onHandlePropTypeFntFile(pNode, pParent, pPropertyName, pFntFile, pCCBReader);
}

View File

@ -31,7 +31,7 @@ void CCNodeLoader::parseProperties(CCNode * pNode, CCNode * pParent, CCBReader *
int propertyCount = pCCBReader->readInt(false);
for(int i = 0; i < propertyCount; i++) {
int type = pCCBReader->readInt(false);
const char * propertyName = pCCBReader->readCachedString();
const char * propertyName = pCCBReader->readCachedString().c_str();
// Check if the property can be set for this platform
bool setProp = false;
@ -404,24 +404,29 @@ bool CCNodeLoader::parsePropTypeCheck(CCNode * pNode, CCNode * pParent, CCBReade
CCSpriteFrame * CCNodeLoader::parsePropTypeSpriteFrame(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
const char * spriteSheet = pCCBReader->readCachedString();
const char * spriteFile = pCCBReader->readCachedString();
const char * spriteSheet = pCCBReader->readCachedString().c_str();
const char * spriteFile = pCCBReader->readCachedString().c_str();
CCSpriteFrame * spriteFrame;
if(strcmp(spriteSheet, "") == 0) {
if(strcmp(spriteFile, "") == 0) {
return NULL;
}
CCTexture2D * texture = CCTextureCache::sharedTextureCache()->addImage(spriteFile);
const char * spriteFilePath = pCCBReader->concat(pCCBReader->getCCBRootPath().c_str(), spriteFile).c_str();
CCTexture2D * texture = CCTextureCache::sharedTextureCache()->addImage(spriteFilePath);
CCRect bounds = CCRect::CCRect(0, 0, texture->getContentSize().width, texture->getContentSize().height);
spriteFrame = CCSpriteFrame::frameWithTexture(texture, bounds);
} else {
CCSpriteFrameCache * frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
const char * spriteSheetPath = pCCBReader->concat(pCCBReader->getCCBRootPath().c_str(), spriteSheet).c_str();
/* Load the sprite sheet only if it is not loaded. */
if(!pCCBReader->isSpriteSheetLoaded(spriteSheet)) {
frameCache->addSpriteFramesWithFile(spriteSheet);
pCCBReader->addLoadedSpriteSheet(spriteSheet);
if(!pCCBReader->isSpriteSheetLoaded(spriteSheetPath)) {
frameCache->addSpriteFramesWithFile(spriteSheetPath);
pCCBReader->addLoadedSpriteSheet(spriteSheetPath);
}
spriteFrame = frameCache->spriteFrameByName(spriteFile);
@ -430,8 +435,8 @@ CCSpriteFrame * CCNodeLoader::parsePropTypeSpriteFrame(CCNode * pNode, CCNode *
}
CCAnimation * CCNodeLoader::parsePropTypeAnimation(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
const char * animationFile = pCCBReader->readCachedString();
const char * animation = pCCBReader->readCachedString();
const char * animationFile = pCCBReader->readCachedString().c_str();
const char * animation = pCCBReader->readCachedString().c_str();
CCAnimation * ccAnimation = NULL;
@ -440,8 +445,8 @@ CCAnimation * CCNodeLoader::parsePropTypeAnimation(CCNode * pNode, CCNode * pPar
// Eventually this should be handled by a client side asset manager
// interface which figured out what resources to load.
// TODO Does this problem exist in C++?
animation = pCCBReader->lastPathComponent(animation);
animationFile = pCCBReader->lastPathComponent(animationFile);
animation = pCCBReader->lastPathComponent(animation).c_str();
animationFile = pCCBReader->lastPathComponent(animationFile).c_str();
if(strcmp(animation, "") != 0) {
CCAnimationCache * animationCache = CCAnimationCache::sharedAnimationCache();
@ -453,7 +458,7 @@ CCAnimation * CCNodeLoader::parsePropTypeAnimation(CCNode * pNode, CCNode * pPar
}
CCTexture2D * CCNodeLoader::parsePropTypeTexture(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
const char * spriteFile = pCCBReader->readCachedString();
const char * spriteFile = pCCBReader->concat(pCCBReader->getCCBRootPath().c_str(), pCCBReader->readCachedString().c_str()).c_str();
return CCTextureCache::sharedTextureCache()->addImage(spriteFile);
}
@ -518,31 +523,31 @@ ccBlendFunc CCNodeLoader::parsePropTypeBlendFunc(CCNode * pNode, CCNode * pParen
}
const char * CCNodeLoader::parsePropTypeFntFile(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readCachedString();
return pCCBReader->readCachedString().c_str();
}
const char * CCNodeLoader::parsePropTypeString(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readCachedString();
return pCCBReader->readCachedString().c_str();
}
const char * CCNodeLoader::parsePropTypeText(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readCachedString();
return pCCBReader->readCachedString().c_str();
}
const char * CCNodeLoader::parsePropTypeFontTTF(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
const char * fnt = pCCBReader->readCachedString();
const char * fnt = pCCBReader->readCachedString().c_str();
const char * ttfEnding("ttf");
if(pCCBReader->endsWith(pCCBReader->toLowerCase(fnt), ttfEnding)){
fnt = pCCBReader->deletePathExtension(pCCBReader->lastPathComponent(fnt));
if(pCCBReader->endsWith(pCCBReader->toLowerCase(fnt).c_str(), ttfEnding)){
fnt = pCCBReader->deletePathExtension(pCCBReader->lastPathComponent(fnt).c_str()).c_str();
}
return fnt;
}
BlockData * CCNodeLoader::parsePropTypeBlock(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
const char * selectorName = pCCBReader->readCachedString();
const char * selectorName = pCCBReader->readCachedString().c_str();
int selectorTarget = pCCBReader->readInt(false);
if(selectorTarget != kCCBTargetTypeNone) {
@ -578,7 +583,7 @@ BlockData * CCNodeLoader::parsePropTypeBlock(CCNode * pNode, CCNode * pParent, C
}
BlockCCControlData * CCNodeLoader::parsePropTypeBlockCCControl(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
const char * selectorName = pCCBReader->readCachedString();
const char * selectorName = pCCBReader->readCachedString().c_str();
int selectorTarget = pCCBReader->readInt(false);
int controlEvents = pCCBReader->readInt(false);
@ -616,20 +621,16 @@ BlockCCControlData * CCNodeLoader::parsePropTypeBlockCCControl(CCNode * pNode, C
}
CCNode * CCNodeLoader::parsePropTypeCCBFile(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
const char * ccbFileName = pCCBReader->readCachedString();
const char * ccbFileName = pCCBReader->readCachedString().c_str();
/* Change path extension to .ccbi. */
const char * ccbFileWithoutPathExtension = pCCBReader->deletePathExtension(ccbFileName);
int ccbiFileNameLenght = strlen(ccbFileWithoutPathExtension) + strlen(".ccbi");
char ccbiFileName[ccbiFileNameLenght + 1];
strcpy(ccbiFileName, ccbFileWithoutPathExtension);
strcat(ccbiFileName, ".ccbi");
ccbiFileName[ccbiFileNameLenght] = '\0';
const char * ccbFileWithoutPathExtension = pCCBReader->deletePathExtension(ccbFileName).c_str();
const char * ccbiFileName = pCCBReader->concat(ccbFileWithoutPathExtension, ".ccbi").c_str();
CCBReader * ccbReader = new CCBReader(pCCBReader);
ccbReader->autorelease();
CCNode * ccbFileNode = ccbReader->readNodeGraphFromFile(ccbiFileName, pCCBReader->getOwner(), pParent->getContentSize());
CCNode * ccbFileNode = ccbReader->readNodeGraphFromFile(pCCBReader->getCCBRootPath().c_str(), ccbiFileName, pCCBReader->getOwner(), pParent->getContentSize());
return ccbFileNode;
}

View File

@ -1 +1 @@
f42fb181945ec91cdc8569191c5810959798ba15
2183a27683dff4601536660a9a71e35cdfc1ce34

View File

@ -5,6 +5,21 @@
using namespace cocos2d;
using namespace cocos2d::extension;
CCBIReaderLayer * CCBIReaderLayer::node() {
CCBIReaderLayer *pRet = new CCBIReaderLayer();
if (pRet && pRet->init()) {
pRet->autorelease();
return pRet;
} else {
CC_SAFE_DELETE(pRet);
return NULL;
}
}
bool CCBIReaderLayer::init() {
return CCLayer::init();
}
void CCBIReaderLayer::menuCloseCallback(CCObject* pSender) {
CCDirector::sharedDirector()->end();

View File

@ -7,6 +7,8 @@
class CCBIReaderLayer : public cocos2d::CCLayer, public cocos2d::extension::CCBMemberVariableAssigner, public cocos2d::extension::CCBSelectorResolver {
public:
static CCBIReaderLayer * node();
virtual bool init();
virtual void menuCloseCallback(CCObject * pSender);

View File

@ -25,7 +25,6 @@
#include "CCBIReaderTest.h"
#include "../../testResource.h"
#include "extensions/CCBIReader/CCBReader.h"
#include "HelloCocosBuilder.h"
#include "extensions/CCBIReader/CCNodeLoaderLibrary.h"
#include "CCBIReaderLayer.h"
@ -33,7 +32,7 @@ using namespace cocos2d;
using namespace cocos2d::extension;
void CCBIReaderTestScene::runThisTest() {
CCBIReaderLayer * ccbiReaderLayer = (CCBIReaderLayer *)CCBIReaderLayer::node();
CCBIReaderLayer * ccbiReaderLayer = CCBIReaderLayer::node();
/* Create an autorelease CCNodeLoaderLibrary. */
CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary();
@ -43,11 +42,13 @@ void CCBIReaderTestScene::runThisTest() {
ccbReader->autorelease();
/* Read a ccbi file. */
CCNode * node = ccbReader->readNodeGraphFromFile("ccb/pub/ccb/test.ccbi", ccbiReaderLayer);
CCNode * node = ccbReader->readNodeGraphFromFile("ccb/pub/", "ccb/test.ccbi", ccbiReaderLayer);
if(node != NULL) {
this->addChild(node);
ccbiReaderLayer->addChild(node);
}
this->addChild(ccbiReaderLayer);
CCDirector::sharedDirector()->replaceScene(this);
}