diff --git a/cocos2dx/include/CCParallaxNode.h b/cocos2dx/include/CCParallaxNode.h index 630ea0a3e0..117fc95475 100644 --- a/cocos2dx/include/CCParallaxNode.h +++ b/cocos2dx/include/CCParallaxNode.h @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2009-2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org @@ -49,9 +50,9 @@ namespace cocos2d { CCParallaxNode(); virtual ~CCParallaxNode(); static CCParallaxNode * node(); - virtual void addChild(CCNode * child, int z, CCPoint parallaxRatio, CCPoint positionOffset); + virtual void addChild(CCNode * child, unsigned int z, CCPoint parallaxRatio, CCPoint positionOffset); // super methods - virtual void addChild(CCNode * child, int zOrder, int tag); + virtual void addChild(CCNode * child, unsigned int zOrder, int tag); virtual void removeChild(CCNode* child, bool cleanup); virtual void removeAllChildrenWithCleanup(bool cleanup); virtual void visit(void); diff --git a/cocos2dx/include/CCTMXLayer.h b/cocos2dx/include/CCTMXLayer.h index 722b4781a4..780d89dd3f 100644 --- a/cocos2dx/include/CCTMXLayer.h +++ b/cocos2dx/include/CCTMXLayer.h @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2009-2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org @@ -70,7 +71,7 @@ namespace cocos2d { /** Tilset information for the layer */ CC_PROPERTY(CCTMXTilesetInfo*, m_pTileSet, TileSet); /** Layer orientation, which is the same as the map orientation */ - CC_SYNTHESIZE(int, m_nLayerOrientation, LayerOrientation); + CC_SYNTHESIZE(unsigned int, m_uLayerOrientation, LayerOrientation); /** properties from the layer. They can be added using Tiled */ CC_PROPERTY(CCStringToStringDictionary*, m_pProperties, Properties); public: diff --git a/cocos2dx/include/CCTMXObjectGroup.h b/cocos2dx/include/CCTMXObjectGroup.h index 6ab4f3af43..09380dad09 100644 --- a/cocos2dx/include/CCTMXObjectGroup.h +++ b/cocos2dx/include/CCTMXObjectGroup.h @@ -1,7 +1,8 @@ /**************************************************************************** Copyright (c) 2010-2011 cocos2d-x.org -Copyright (c) 2010 Ricardo Quesada Copyright (c) 2010 Neophit +Copyright (c) 2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org diff --git a/cocos2dx/include/CCTMXTiledMap.h b/cocos2dx/include/CCTMXTiledMap.h index fb9df41a21..4447a2f3d3 100644 --- a/cocos2dx/include/CCTMXTiledMap.h +++ b/cocos2dx/include/CCTMXTiledMap.h @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2009-2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org diff --git a/cocos2dx/include/CCTMXXMLParser.h b/cocos2dx/include/CCTMXXMLParser.h index d5fdd50dc0..7c8159e069 100644 --- a/cocos2dx/include/CCTMXXMLParser.h +++ b/cocos2dx/include/CCTMXXMLParser.h @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2009-2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org @@ -48,6 +49,7 @@ namespace cocos2d { TMXLayerAttribNone = 1 << 0, TMXLayerAttribBase64 = 1 << 1, TMXLayerAttribGzip = 1 << 2, + TMXLayerAttribZlib = 1 << 3, }; enum { diff --git a/cocos2dx/include/CCTileMapAtlas.h b/cocos2dx/include/CCTileMapAtlas.h index 240ca9bedc..cc60bdb8ae 100644 --- a/cocos2dx/include/CCTileMapAtlas.h +++ b/cocos2dx/include/CCTileMapAtlas.h @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2008-2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org @@ -75,7 +76,7 @@ namespace cocos2d { private: void loadTGAfile(const char *file); void calculateItemsToRender(); - void updateAtlasValueAt(ccGridSize pos, ccColor3B value, int index); + void updateAtlasValueAt(ccGridSize pos, ccColor3B value, unsigned int index); void updateAtlasValues(); protected: diff --git a/cocos2dx/support/zip_support/ZipUtils.cpp b/cocos2dx/support/zip_support/ZipUtils.cpp index 298de45534..96983e99b4 100644 --- a/cocos2dx/support/zip_support/ZipUtils.cpp +++ b/cocos2dx/support/zip_support/ZipUtils.cpp @@ -34,14 +34,13 @@ namespace cocos2d // Should buffer factor be 1.5 instead of 2 ? #define BUFFER_INC_FACTOR (2) - int ZipUtils::inflateMemory_(unsigned char *in, unsigned int inLength, unsigned char **out, unsigned int *outLength) + int ZipUtils::ccInflateMemoryWithHint(unsigned char *in, unsigned int inLength, unsigned char **out, unsigned int *outLength, unsigned int outLenghtHint) { /* ret value */ int err = Z_OK; - /* 256k initial decompress buffer */ - int bufferSize = 256 * 1024; - *out = new unsigned char[bufferSize]; + int bufferSize = outLenghtHint; + *out = (unsigned char*) malloc(bufferSize); z_stream d_stream; /* decompression stream */ d_stream.zalloc = (alloc_func)0; @@ -55,11 +54,10 @@ namespace cocos2d /* window size to hold 256k */ if( (err = inflateInit2(&d_stream, 15 + 32)) != Z_OK ) - { return err; - } - for (;;) { + for (;;) + { err = inflate(&d_stream, Z_NO_FLUSH); if (err == Z_STREAM_END) @@ -67,28 +65,30 @@ namespace cocos2d break; } - switch (err) { + switch (err) + { case Z_NEED_DICT: err = Z_DATA_ERROR; case Z_DATA_ERROR: case Z_MEM_ERROR: inflateEnd(&d_stream); return err; - } + } // not enough memory ? if (err != Z_STREAM_END) { - delete [] *out; - *out = new unsigned char[bufferSize * BUFFER_INC_FACTOR]; + unsigned char *tmp = (unsigned char*)realloc(*out, bufferSize * BUFFER_INC_FACTOR); /* not enough memory, ouch */ - if (! *out ) + if (! tmp ) { CCLOG("cocos2d: ZipUtils: realloc failed"); inflateEnd(&d_stream); return Z_MEM_ERROR; } + /* only assign to *out if tmp is valid. it's not guaranteed that realloc will reuse the memory */ + *out = tmp; d_stream.next_out = *out + bufferSize; d_stream.avail_out = bufferSize; @@ -96,16 +96,15 @@ namespace cocos2d } } - *outLength = bufferSize - d_stream.avail_out; err = inflateEnd(&d_stream); return err; } - int ZipUtils::ccInflateMemory(unsigned char *in, unsigned int inLength, unsigned char **out) + int ZipUtils::ccInflateMemoryWithHint(unsigned char *in, unsigned int inLength, unsigned char **out, unsigned int outLengthHint) { unsigned int outLength = 0; - int err = inflateMemory_(in, inLength, out, &outLength); + int err = ccInflateMemoryWithHint(in, inLength, out, &outLength, outLengthHint); if (err != Z_OK || *out == NULL) { if (err == Z_MEM_ERROR) @@ -133,6 +132,12 @@ namespace cocos2d return outLength; } + int ZipUtils::ccInflateMemory(unsigned char *in, unsigned int inLength, unsigned char **out) + { + // 256k for hint + return ccInflateMemoryWithHint(in, inLength, out, 256 * 1024); + } + int ZipUtils::ccInflateGZipFile(const char *path, unsigned char **out) { int len; diff --git a/cocos2dx/support/zip_support/ZipUtils.h b/cocos2dx/support/zip_support/ZipUtils.h index 98ee6b8e6b..133315db43 100644 --- a/cocos2dx/support/zip_support/ZipUtils.h +++ b/cocos2dx/support/zip_support/ZipUtils.h @@ -51,12 +51,25 @@ namespace cocos2d * Inflates either zlib or gzip deflated memory. The inflated memory is * expected to be freed by the caller. * + * It will allocate 256k for the destination buffer. If it is not enought it will multiply the previous buffer size per 2, until there is enough memory. * @returns the length of the deflated buffer * @since v0.8.1 */ static int ccInflateMemory(unsigned char *in, unsigned int inLength, unsigned char **out); + /** + * Inflates either zlib or gzip deflated memory. The inflated memory is + * expected to be freed by the caller. + * + * outLenghtHint is assumed to be the needed room to allocate the inflated buffer. + * + * @returns the length of the deflated buffer + * + @since v1.0.0 + */ + static int ccInflateMemoryWithHint(unsigned char *in, unsigned int inLength, unsigned char **out, unsigned int outLenghtHint); + /** inflates a GZip file into memory * * @returns the length of the deflated buffer @@ -74,7 +87,8 @@ namespace cocos2d static int ccInflateCCZFile(const char *filename, unsigned char **out); private: - static int inflateMemory_(unsigned char *in, unsigned int inLength, unsigned char **out, unsigned int *outLengh); + static int ccInflateMemoryWithHint(unsigned char *in, unsigned int inLength, unsigned char **out, unsigned int *outLength, + unsigned int outLenghtHint); }; } // end of namespace cocos2d diff --git a/cocos2dx/tileMap_parallax_nodes/CCParallaxNode.cpp b/cocos2dx/tileMap_parallax_nodes/CCParallaxNode.cpp index 533ff00122..93b5b8b9b4 100644 --- a/cocos2dx/tileMap_parallax_nodes/CCParallaxNode.cpp +++ b/cocos2dx/tileMap_parallax_nodes/CCParallaxNode.cpp @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2009-2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org @@ -69,14 +70,14 @@ namespace cocos2d { pRet->autorelease(); return pRet; } - void CCParallaxNode::addChild(CCNode * child, int zOrder, int tag) + void CCParallaxNode::addChild(CCNode * child, unsigned int zOrder, int tag) { CC_UNUSED_PARAM(zOrder); CC_UNUSED_PARAM(child); CC_UNUSED_PARAM(tag); CCAssert(0,"ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead"); } - void CCParallaxNode::addChild(CCNode *child, int z, CCPoint ratio, CCPoint offset) + void CCParallaxNode::addChild(CCNode *child, unsigned int z, CCPoint ratio, CCPoint offset) { CCAssert( child != NULL, "Argument must be non-nil"); CCPointObject *obj = CCPointObject::pointWithCCPoint(ratio, offset); diff --git a/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp b/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp index 88d4b03d9e..b70310a7ef 100644 --- a/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp +++ b/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2009-2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org @@ -74,7 +75,7 @@ namespace cocos2d { // mapInfo m_tMapTileSize = mapInfo->getTileSize(); - m_nLayerOrientation = mapInfo->getOrientation(); + m_uLayerOrientation = mapInfo->getOrientation(); // offset (after layer orientation is set); CCPoint offset = this->calculateLayerOffset(layerInfo->m_tOffset); @@ -519,7 +520,7 @@ namespace cocos2d { CCPoint CCTMXLayer::calculateLayerOffset(CCPoint pos) { CCPoint ret = CCPointZero; - switch( m_nLayerOrientation ) + switch( m_uLayerOrientation ) { case CCTMXOrientationOrtho: ret = ccp( pos.x * m_tMapTileSize.width, -pos.y *m_tMapTileSize.height); @@ -537,7 +538,7 @@ namespace cocos2d { CCPoint CCTMXLayer::positionAt(CCPoint pos) { CCPoint ret = CCPointZero; - switch( m_nLayerOrientation ) + switch( m_uLayerOrientation ) { case CCTMXOrientationOrtho: ret = positionForOrthoAt(pos); @@ -581,7 +582,7 @@ namespace cocos2d { unsigned int maxVal = 0; if( m_bUseAutomaticVertexZ ) { - switch( m_nLayerOrientation ) + switch( m_uLayerOrientation ) { case CCTMXOrientationIso: maxVal = (unsigned int)(m_tLayerSize.width + m_tLayerSize.height); diff --git a/cocos2dx/tileMap_parallax_nodes/CCTMXObjectGroup.cpp b/cocos2dx/tileMap_parallax_nodes/CCTMXObjectGroup.cpp index a3cbd0ce5d..baa57fafcf 100644 --- a/cocos2dx/tileMap_parallax_nodes/CCTMXObjectGroup.cpp +++ b/cocos2dx/tileMap_parallax_nodes/CCTMXObjectGroup.cpp @@ -1,7 +1,8 @@ /**************************************************************************** Copyright (c) 2010-2011 cocos2d-x.org -Copyright (c) 2010 Ricardo Quesada Copyright (c) 2010 Neophit +Copyright (c) 2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org diff --git a/cocos2dx/tileMap_parallax_nodes/CCTMXTiledMap.cpp b/cocos2dx/tileMap_parallax_nodes/CCTMXTiledMap.cpp index 0aaff71dbf..3d0e6e18dc 100644 --- a/cocos2dx/tileMap_parallax_nodes/CCTMXTiledMap.cpp +++ b/cocos2dx/tileMap_parallax_nodes/CCTMXTiledMap.cpp @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2009-2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org @@ -154,9 +155,6 @@ namespace cocos2d{ CCTMXTilesetInfo * CCTMXTiledMap::tilesetForLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo) { - CCTMXTilesetInfo *tileset = NULL; - //CFByteOrder o = CFByteOrderGetCurrent(); - CCSize size = layerInfo->m_tLayerSize; CCMutableArray* tilesets = mapInfo->getTilesets(); if (tilesets && tilesets->count()>0) @@ -197,7 +195,7 @@ namespace cocos2d{ // If all the tiles are 0, return empty tileset CCLOG("cocos2d: Warning: TMX Layer '%@' has no tiles", layerInfo->m_sName.c_str()); - return tileset; + return NULL; } diff --git a/cocos2dx/tileMap_parallax_nodes/CCTMXXMLParser.cpp b/cocos2dx/tileMap_parallax_nodes/CCTMXXMLParser.cpp index ee6f456b23..510bf591b2 100644 --- a/cocos2dx/tileMap_parallax_nodes/CCTMXXMLParser.cpp +++ b/cocos2dx/tileMap_parallax_nodes/CCTMXXMLParser.cpp @@ -2,6 +2,7 @@ Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2011 Максим Аксенов Copyright (c) 2009-2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org @@ -391,10 +392,15 @@ namespace cocos2d { { layerAttribs = pTMXMapInfo->getLayerAttribs(); pTMXMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribGzip); + } else + if (compression == "zip") + { + layerAttribs = pTMXMapInfo->getLayerAttribs(); + pTMXMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribZlib); } - CCAssert( compression == "" || compression == "gzip", "TMX: unsupported compression method" ); + CCAssert( compression == "" || compression == "gzip" || compression == "zlib", "TMX: unsupported compression method" ); } - CCAssert( pTMXMapInfo->getLayerAttribs() != TMXLayerAttribNone, "TMX tile map: Only base64 and/or gzip maps are supported" ); + CCAssert( pTMXMapInfo->getLayerAttribs() != TMXLayerAttribNone, "TMX tile map: Only base64 and/or gzip/zlib maps are supported" ); } else if(elementName == "object") @@ -534,17 +540,25 @@ namespace cocos2d { std::string currentString = pTMXMapInfo->getCurrentString(); unsigned char *buffer; - len = base64Decode((unsigned char*)currentString.c_str(), currentString.length(), &buffer); + len = base64Decode((unsigned char*)currentString.c_str(), (unsigned int)currentString.length(), &buffer); if( ! buffer ) { CCLOG("cocos2d: TiledMap: decode data error"); return; } - if( pTMXMapInfo->getLayerAttribs() & TMXLayerAttribGzip ) + if( pTMXMapInfo->getLayerAttribs() & (TMXLayerAttribGzip | TMXLayerAttribZlib) ) { unsigned char *deflated; - ZipUtils::ccInflateMemory(buffer, len, &deflated); + CCSize s = layer->m_tLayerSize; + // int sizeHint = s.width * s.height * sizeof(uint32_t); + int sizeHint = (int)(s.width * s.height * sizeof(unsigned int)); + + int inflatedLen = ZipUtils::ccInflateMemoryWithHint(buffer, len, &deflated, sizeHint); + assert(inflatedLen == sizeHint); + + inflatedLen = (int)&inflatedLen; // XXX: to avoid warings in compiler + delete [] buffer; buffer = NULL; diff --git a/cocos2dx/tileMap_parallax_nodes/CCTileMapAtlas.cpp b/cocos2dx/tileMap_parallax_nodes/CCTileMapAtlas.cpp index 872f84af2b..3330e69a79 100644 --- a/cocos2dx/tileMap_parallax_nodes/CCTileMapAtlas.cpp +++ b/cocos2dx/tileMap_parallax_nodes/CCTileMapAtlas.cpp @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2008-2010 Ricardo Quesada +Copyright (c) 2011 Zynga Inc. http://www.cocos2d-x.org @@ -174,7 +175,7 @@ namespace cocos2d { return value; } - void CCTileMapAtlas::updateAtlasValueAt(ccGridSize pos, ccColor3B value, int index) + void CCTileMapAtlas::updateAtlasValueAt(ccGridSize pos, ccColor3B value, unsigned int index) { ccV3F_C4B_T2F_Quad quad;