axmol/cocos2dx/extensions/CCBIReader/CCBReader.cpp

360 lines
9.1 KiB
C++
Raw Normal View History

2012-05-31 07:51:33 +08:00
#include "CCBReader.h"
#include "CCNodeLoader.h"
#include "CCNodeLoaderLibrary.h"
2012-06-05 07:16:42 +08:00
#ifdef __CC_PLATFORM_IOS
#import <UIKit/UIDevice.h>
#endif
using namespace cocos2d;
using namespace cocos2d::extension;
CCBReader::CCBReader(CCNodeLoaderLibrary * pCCNodeLoaderLibrary, CCBMemberVariableAssigner * pCCBMemberVariableAssigner, CCBSelectorResolver * pCCBSelectorResolver) {
this->mRootNode = NULL;
this->mRootCCBReader = true;
this->mCCNodeLoaderLibrary = pCCNodeLoaderLibrary;
this->mCCBMemberVariableAssigner = pCCBMemberVariableAssigner;
this->mCCBSelectorResolver = pCCBSelectorResolver;
2012-06-05 07:16:42 +08:00
this->mResolutionScale = 1;
2012-06-05 07:16:42 +08:00
#ifdef __CC_PLATFORM_IOS
/* iPad */
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
this->mResolutionScale = 2;
}
#endif
}
CCBReader::~CCBReader() {
if(this->mBytes != NULL) {
delete this->mBytes;
this->mBytes = NULL;
}
/* Clear string cache. */
this->mStringCache.clear();
if(this->mRootCCBReader) {
/* Clear loaded spritesheets. */
this->mLoadedSpriteSheets.clear();
}
if(this->mRootNode != NULL) {
this->mRootNode->release();
}
}
CCBReader::CCBReader(CCBReader * pCCBReader) {
this->mRootNode = NULL;
this->mRootCCBReader = false;
/* Borrow data from the 'parent' CCBReader. */
2012-06-05 07:16:42 +08:00
this->mResolutionScale = pCCBReader->mResolutionScale;
this->mLoadedSpriteSheets = pCCBReader->mLoadedSpriteSheets;
this->mCCNodeLoaderLibrary = pCCBReader->mCCNodeLoaderLibrary;
this->mCCBMemberVariableAssigner = pCCBReader->mCCBMemberVariableAssigner;
this->mCCBSelectorResolver = pCCBReader->mCCBSelectorResolver;
}
CCBMemberVariableAssigner * CCBReader::getCCBMemberVariableAssigner() {
return this->mCCBMemberVariableAssigner;
}
CCBSelectorResolver * CCBReader::getCCBSelectorResolver() {
return this->mCCBSelectorResolver;
}
2012-06-05 07:16:42 +08:00
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 * pCCBFileName, CCObject * pOwner, CCSize pRootContainerSize) {
const char * path = CCFileUtils::fullPathFromRelativePath(pCCBFileName);
unsigned long size = 0;
this->mBytes = CCFileUtils::getFileData(path, "r", &size);
this->mCurrentByte = 0;
this->mCurrentBit = 0;
this->mOwner = pOwner;
this->mRootContainerSize = pRootContainerSize;
if(!this->readHeader()) {
return NULL;
}
if(!this->readStringCache()) {
return NULL;
}
return this->readNodeGraph();
}
bool CCBReader::readHeader() {
/* If no bytes loaded, don't crash about it. */
if(this->mBytes == NULL) {
return false;
}
/* Read magic bytes */
int magicBytes = *((int*)(this->mBytes + this->mCurrentByte));
this->mCurrentByte += 4;
if(magicBytes != 'ccbi') {
return false;
}
/* Read version. */
int version = this->readInt(false);
if(version != kCCBVersion) {
CCLog("WARNING! Incompatible ccbi file version (file: %d reader: %d)", version, kCCBVersion);
return false;
}
return true;
}
bool CCBReader::readStringCache() {
int numStrings = this->readInt(false);
for(int i = 0; i < numStrings; i++) {
this->readStringCacheEntry();
}
return true;
}
void CCBReader::readStringCacheEntry() {
int b0 = this->readByte();
int b1 = this->readByte();
int numBytes = b0 << 8 | b1;
const char * src = (const char *) (this->mBytes + this->mCurrentByte);
std::string string(src, numBytes);
this->mCurrentByte += numBytes;
this->mStringCache.push_back(string);
}
unsigned char CCBReader::readByte() {
unsigned char byte = this->mBytes[this->mCurrentByte];
this->mCurrentByte++;
return byte;
}
bool CCBReader::readBool() {
return this->readByte();
}
int CCBReader::readInt(bool pSign) {
int numBits = 0;
while(!this->getBit()) {
numBits++;
}
long long current = 0;
for(int a = numBits - 1; a >= 0; a--) {
if(this->getBit()) {
current |= 1 << a;
}
}
current |= 1 << numBits;
int num;
if(pSign) {
int s = current % 2;
if(s) {
num = (int)(current / 2);
} else {
num = (int)(-current / 2);
}
} else {
num = current - 1;
}
this->alignBits();
return num;
}
float CCBReader::readFloat() {
unsigned char type = this->readByte();
switch (type) {
case kCCBFloat0:
return 0;
case kCCBFloat1:
return 1;
case kCCBFloatMinus1:
return -1;
case kCCBFloat05:
return 0.5f;
case kCCBFloatInteger:
return this->readInt(true);
default:
/* using a memcpy since the compiler isn't
* doing the float ptr math correctly on device.
* TODO still applies in C++ ? */
float * pF = (float*)(this->mBytes + this->mCurrentByte);
float f = 0;
memcpy(&f, pF, sizeof(float));
this->mCurrentByte += 4;
return f;
}
}
bool CCBReader::getBit() {
bool bit;
unsigned char byte = *(this->mBytes + this->mCurrentByte);
if(byte & (1 << this->mCurrentBit)) {
bit = true;
} else {
bit = false;
}
this->mCurrentBit++;
if(this->mCurrentBit >= 8) {
this->mCurrentBit = 0;
this->mCurrentByte++;
}
return bit;
}
void CCBReader::alignBits() {
if(this->mCurrentBit) {
this->mCurrentBit = 0;
this->mCurrentByte++;
}
}
const char * CCBReader::readCachedString() {
int i = this->readInt(false);
return this->mStringCache[i].c_str();
}
CCNode * CCBReader::readNodeGraph(CCNode * pParent) {
/* Read class name. */
const char * className = this->readCachedString();
int memberVarAssignmentType = this->readInt(false);
const char * memberVarAssignmentName;
if(memberVarAssignmentType != kCCBTargetTypeNone) {
memberVarAssignmentName = this->readCachedString();
}
CCNodeLoader * ccNodeLoader = this->mCCNodeLoaderLibrary->getCCNodeLoader(className);
CCNode * node = ccNodeLoader->loadCCNode(pParent, this);
/* Set root node, if not set yet. */
if(this->mRootNode == NULL) {
this->mRootNode = node;
this->mRootNode->retain();
}
if(memberVarAssignmentType != kCCBTargetTypeNone) {
CCObject * target = NULL;
if(memberVarAssignmentType == kCCBTargetTypeDocumentRoot) {
target = this->mRootNode;
} else if(memberVarAssignmentType == kCCBTargetTypeOwner) {
target = this->mOwner;
}
if(target != NULL) {
if(this->mCCBMemberVariableAssigner != NULL) {
this->mCCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName, node);
}
}
}
/* Read and add children. */
int numChildren = this->readInt(false);
for(int i = 0; i < numChildren; i++) {
CCNode * child = this->readNodeGraph(node);
node->addChild(child);
}
// TODO
/*
if([node respondsToSelector:@selector(didLoadFromCCB)]) {
[node performSelector:@selector(didLoadFromCCB)];
}
*/
return node;
}
CCNode * CCBReader::readNodeGraph() {
return this->readNodeGraph(NULL);
}
CCObject * CCBReader::getOwner() {
return this->mOwner;
}
CCNode * CCBReader::getRootNode() {
return this->mRootNode;
}
CCSize CCBReader::getContainerSize(CCNode * pNode) {
if(pNode) {
return pNode->getContentSize();
} else {
return this->mRootContainerSize;
}
}
bool CCBReader::isSpriteSheetLoaded(const char * pSpriteSheet) {
return this->mLoadedSpriteSheets.find(pSpriteSheet) != this->mLoadedSpriteSheets.end();
}
void CCBReader::addLoadedSpriteSheet(const char * pSpriteSheet) {
this->mLoadedSpriteSheets.insert(pSpriteSheet);
}
const char * 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 pPath;
}
const char * 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 pPath;
}
const char * CCBReader::toLowerCase(const char * pString) {
std::string copy(pString);
std::transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
return copy.c_str();
}
bool CCBReader::endsWith(const char * pString, const char * pEnding) {
std::string string(pString);
std::string ending(pEnding);
if(string.length() >= ending.length()) {
return (string.compare(string.length() - ending.length(), ending.length(), ending) == 0);
} else {
return false;
}
}