From 041953151a7a48490d2e1ae6a4db7d972f34f762 Mon Sep 17 00:00:00 2001 From: yangws Date: Tue, 29 Mar 2011 11:41:44 +0800 Subject: [PATCH 1/5] issue #439 CCFileData and imageWithImageFile modified on win32. --- cocos2dx/cocoa/CCData.cpp | 6 +- cocos2dx/label_nodes/CCLabelBMFont.cpp | 6 +- cocos2dx/particle_nodes/CCParticleSystem.cpp | 6 +- cocos2dx/platform/CCCommon.cpp | 5 ++ cocos2dx/platform/CCCommon.h | 1 + cocos2dx/platform/CCFileUtils.cpp | 17 ++++- cocos2dx/platform/CCFileUtils.h | 41 ++++++------ cocos2dx/platform/CCImage.cpp | 62 +------------------ cocos2dx/platform/CCImage.h | 7 --- cocos2dx/platform/CCSAXParser.cpp | 6 +- cocos2dx/platform/win32/CCFileUtils_win32.cpp | 8 +++ cocos2dx/platform/win32/CCImage_win32.cpp | 5 -- cocos2dx/support/image_support/TGAlib.cpp | 6 +- cocos2dx/textures/CCTextureCache.cpp | 12 ++-- 14 files changed, 76 insertions(+), 112 deletions(-) diff --git a/cocos2dx/cocoa/CCData.cpp b/cocos2dx/cocoa/CCData.cpp index f5cd3329f9..4d258e8ed0 100644 --- a/cocos2dx/cocoa/CCData.cpp +++ b/cocos2dx/cocoa/CCData.cpp @@ -46,9 +46,9 @@ CCData::~CCData(void) CCData* CCData::dataWithContentsOfFile(const string &strPath) { - FileData data; - unsigned long nSize = 0; - unsigned char* pBuffer = data.getFileData(strPath.c_str(), "rb", &nSize); + CCFileData data(strPath.c_str(), "rb"); + unsigned long nSize = data.getSize(); + unsigned char* pBuffer = data.getBuffer(); if (! pBuffer) { diff --git a/cocos2dx/label_nodes/CCLabelBMFont.cpp b/cocos2dx/label_nodes/CCLabelBMFont.cpp index 28be090966..f2f900b7fa 100644 --- a/cocos2dx/label_nodes/CCLabelBMFont.cpp +++ b/cocos2dx/label_nodes/CCLabelBMFont.cpp @@ -125,9 +125,9 @@ namespace cocos2d{ { std::string fullpath = CCFileUtils::fullPathFromRelativePath(controlFile); - FileData data; - unsigned long nBufSize = 0; - char* pBuffer = (char*) data.getFileData(fullpath.c_str(), "r", &nBufSize); + CCFileData data(controlFile, "rb"); + unsigned long nBufSize = data.getSize(); + char* pBuffer = (char*) data.getBuffer(); CCAssert(pBuffer, "CCBMFontConfiguration::parseConfigFile | Open file error."); diff --git a/cocos2dx/particle_nodes/CCParticleSystem.cpp b/cocos2dx/particle_nodes/CCParticleSystem.cpp index 0055d3d278..18522ed190 100644 --- a/cocos2dx/particle_nodes/CCParticleSystem.cpp +++ b/cocos2dx/particle_nodes/CCParticleSystem.cpp @@ -262,12 +262,12 @@ bool CCParticleSystem::initWithDictionary(CCDictionary * if (strlen(textureName) > 0) { // set not pop-up message box when load image failed - bool bNotify = CCImage::getIsPopupNotify(); - CCImage::setIsPopupNotify(false); + bool bNotify = CCFileUtils::getIsPopupNotify(); + CCFileUtils::setIsPopupNotify(false); this->m_pTexture = CCTextureCache::sharedTextureCache()->addImage(fullpath.c_str()); // reset the value of UIImage notify - CCImage::setIsPopupNotify(bNotify); + CCFileUtils::setIsPopupNotify(bNotify); } // if it fails, try to get it from the base64-gzipped data diff --git a/cocos2dx/platform/CCCommon.cpp b/cocos2dx/platform/CCCommon.cpp index 9d0c4fd46c..5ea51d99ce 100644 --- a/cocos2dx/platform/CCCommon.cpp +++ b/cocos2dx/platform/CCCommon.cpp @@ -48,6 +48,11 @@ void CCLog(const char * pszFormat, ...) OutputDebugStringA("\n"); } +void CCMessageBox(const char * pszMsg, const char * pszTitle) +{ + MessageBoxA(NULL, pszMsg, pszTitle, MB_OK); +} + NS_CC_END; #endif // CC_PLATFORM_WIN32 diff --git a/cocos2dx/platform/CCCommon.h b/cocos2dx/platform/CCCommon.h index 7c03b2fab8..e0864560cc 100644 --- a/cocos2dx/platform/CCCommon.h +++ b/cocos2dx/platform/CCCommon.h @@ -36,6 +36,7 @@ static const int kMaxLogLen = 255; @brief Output Debug message. */ void CC_DLL CCLog(const char * pszFormat, ...); +void CC_DLL CCMessageBox(const char * pszMsg, const char * pszTitle); NS_CC_END; diff --git a/cocos2dx/platform/CCFileUtils.cpp b/cocos2dx/platform/CCFileUtils.cpp index 3316a8259d..3de5e6dbfe 100644 --- a/cocos2dx/platform/CCFileUtils.cpp +++ b/cocos2dx/platform/CCFileUtils.cpp @@ -234,6 +234,21 @@ unsigned char* CCFileUtils::getFileDataFromZip(const char* pszZipFilePath, const return pBuffer; } +////////////////////////////////////////////////////////////////////////// +// Notification support when getFileData from invalid file path. +////////////////////////////////////////////////////////////////////////// +static bool s_bPopupNotify = true; + +void CCFileUtils::setIsPopupNotify(bool bNotify) +{ + s_bPopupNotify = bNotify; +} + +bool CCFileUtils::getIsPopupNotify() +{ + return s_bPopupNotify; +} + NS_CC_END; #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) @@ -248,4 +263,4 @@ NS_CC_END; #include "android/CCFileUtils_android.cpp" #endif -#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) +#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_TARGET_PLATFORM != CC_PLATFORM_AIRPLAY) diff --git a/cocos2dx/platform/CCFileUtils.h b/cocos2dx/platform/CCFileUtils.h index 021de58239..f774ebde08 100644 --- a/cocos2dx/platform/CCFileUtils.h +++ b/cocos2dx/platform/CCFileUtils.h @@ -86,6 +86,12 @@ public: */ static CCDictionary *dictionaryWithContentsOfFile(const char *pFileName); + /** + @brief Set/Get whether pop-up a message box when the image load failed + */ + static void setIsPopupNotify(bool bNotify); + static bool getIsPopupNotify(); + /////////////////////////////////////////////////// // interfaces on wophone /////////////////////////////////////////////////// @@ -108,33 +114,30 @@ public: int ccLoadFileIntoMemory(const char *filename, unsigned char **out); }; -class FileData +class CCFileData { public: - FileData() : m_pBuffer(NULL) {} - ~FileData() + CCFileData(const char* pszFileName, const char* pszMode) + : m_pBuffer(0) + , m_uSize(0) { - if (m_pBuffer) - { - delete [] m_pBuffer; - m_pBuffer = NULL; - } + m_pBuffer = CCFileUtils::getFileData(pszFileName, pszMode, &m_uSize); + } + ~CCFileData() + { + CC_SAFE_DELETE_ARRAY(m_pBuffer); } - unsigned char* getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) + bool reset(const char* pszFileName, const char* pszMode) { - if (m_pBuffer) - { - delete [] m_pBuffer; - m_pBuffer = NULL; - } - - m_pBuffer = CCFileUtils::getFileData(pszFileName, pszMode, pSize); - return m_pBuffer; + CC_SAFE_DELETE_ARRAY(m_pBuffer); + m_uSize = 0; + m_pBuffer = CCFileUtils::getFileData(pszFileName, pszMode, &m_uSize); + return (m_pBuffer) ? true : false; } -protected: - unsigned char* m_pBuffer; + CC_SYNTHESIZE_READONLY(unsigned char *, m_pBuffer, Buffer); + CC_SYNTHESIZE_READONLY(unsigned long , m_uSize, Size); }; NS_CC_END; diff --git a/cocos2dx/platform/CCImage.cpp b/cocos2dx/platform/CCImage.cpp index 4c15f1b5c2..af26146165 100644 --- a/cocos2dx/platform/CCImage.cpp +++ b/cocos2dx/platform/CCImage.cpp @@ -26,6 +26,7 @@ THE SOFTWARE. #include "CCCommon.h" #include "CCStdC.h" +#include "CCFileUtils.h" #include "png.h" #include @@ -63,8 +64,6 @@ static void pngReadCallback(png_structp png_ptr, png_bytep data, png_size_t leng NS_CC_BEGIN; -static void CCMessageBox(const std::string& msg, const std::string& title); - ////////////////////////////////////////////////////////////////////////// // Impliment CCImage ////////////////////////////////////////////////////////////////////////// @@ -87,51 +86,8 @@ CCImage::~CCImage() bool CCImage::initWithImageFile(const char * strPath, EImageFormat eImgFmt/* = eFmtPng*/) { - bool bRet = false; - FILE *fp = 0; - unsigned char *buffer = NULL; - do - { - // open file - fp = fopen(strPath, "rb"); - CC_BREAK_IF(! fp); - - // compute the length of file - fseek(fp,0,SEEK_END); - int size = ftell(fp); - fseek(fp,0,SEEK_SET); - - // allocate enough memory to save the data of file - buffer = new unsigned char[size]; - CC_BREAK_IF(! buffer); - - // read data - size = fread(buffer, sizeof(unsigned char), size, fp); - - if (kFmtJpg == eImgFmt) - { - bRet = _initWithJpgData(buffer, size); - } - else - { - bRet = _initWithPngData(buffer, size); - } - } while (0); - - CC_SAFE_DELETE_ARRAY(buffer); - if (fp) - { - fclose(fp); - } - if (! bRet && CCImage::getIsPopupNotify()) - { - std::string title = "cocos2d-x error!"; - std::string msg = "Load "; - msg.append(strPath).append(" failed!"); - - CCMessageBox(msg, title); - } - return bRet; + CCFileData data(CCFileUtils::fullPathFromRelativePath(strPath), "rb"); + return initWithImageData(data.getBuffer(), data.getSize()); } bool CCImage::initWithImageData(void * pData, int nDataLen, EImageFormat eFmt/* = eSrcFmtPng*/) @@ -155,18 +111,6 @@ bool CCImage::initWithImageData(void * pData, int nDataLen, EImageFormat eFmt/* return bRet; } -static bool s_bPopupNotify = true; - -void CCImage::setIsPopupNotify(bool bNotify) -{ - s_bPopupNotify = bNotify; -} - -bool CCImage::getIsPopupNotify() -{ - return s_bPopupNotify; -} - bool CCImage::_initWithJpgData(void * data, int nSize) { /* these are standard libjpeg structures for reading(decompression) */ diff --git a/cocos2dx/platform/CCImage.h b/cocos2dx/platform/CCImage.h index d248965533..5f3f2adf88 100644 --- a/cocos2dx/platform/CCImage.h +++ b/cocos2dx/platform/CCImage.h @@ -107,13 +107,6 @@ public: CC_SYNTHESIZE_READONLY(short, m_nHeight, Height); CC_SYNTHESIZE_READONLY(int, m_nBitsPerComponent, BitsPerComponent); -public: - /** - @brief Set/Get whether pop-up a message box when the image load failed - */ - static void setIsPopupNotify(bool bNotify); - static bool getIsPopupNotify(); - protected: bool _initWithJpgData(void * pData, int nDatalen); bool _initWithPngData(void * pData, int nDatalen); diff --git a/cocos2dx/platform/CCSAXParser.cpp b/cocos2dx/platform/CCSAXParser.cpp index c12aaf6fb5..d666dab5aa 100644 --- a/cocos2dx/platform/CCSAXParser.cpp +++ b/cocos2dx/platform/CCSAXParser.cpp @@ -49,9 +49,9 @@ bool CCSAXParser::init(const char *pszEncoding) bool CCSAXParser::parse(const char *pszFile) { - FileData data; - unsigned long size = 0; - char *pBuffer = (char*) data.getFileData(pszFile, "r", &size); + CCFileData data(pszFile, "rt"); + unsigned long size = data.getSize(); + char *pBuffer = (char*) data.getBuffer(); if (!pBuffer) { diff --git a/cocos2dx/platform/win32/CCFileUtils_win32.cpp b/cocos2dx/platform/win32/CCFileUtils_win32.cpp index 37ace022d3..c542f4c20c 100644 --- a/cocos2dx/platform/win32/CCFileUtils_win32.cpp +++ b/cocos2dx/platform/win32/CCFileUtils_win32.cpp @@ -131,6 +131,14 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz fclose(fp); } while (0); + if (! Buffer && getIsPopupNotify()) + { + std::string title = "Notification"; + std::string msg = "Get data from file("; + msg.append(pszFileName).append(") failed!"); + + CCMessageBox(msg.c_str(), title.c_str()); + } return Buffer; } diff --git a/cocos2dx/platform/win32/CCImage_win32.cpp b/cocos2dx/platform/win32/CCImage_win32.cpp index 46a76a3181..44e11af4d1 100644 --- a/cocos2dx/platform/win32/CCImage_win32.cpp +++ b/cocos2dx/platform/win32/CCImage_win32.cpp @@ -24,11 +24,6 @@ THE SOFTWARE. NS_CC_BEGIN; -void CCMessageBox(const std::string& msg, const std::string& title) -{ - MessageBoxA(NULL, msg.c_str(), title.c_str(), MB_OK); -} - /** @brief A memory DC which uses to draw text on bitmap. */ diff --git a/cocos2dx/support/image_support/TGAlib.cpp b/cocos2dx/support/image_support/TGAlib.cpp index 33b4a823c9..120bda911a 100644 --- a/cocos2dx/support/image_support/TGAlib.cpp +++ b/cocos2dx/support/image_support/TGAlib.cpp @@ -194,9 +194,9 @@ tImageTGA * tgaLoad(const char *pszFilename) { int mode,total; tImageTGA *info = NULL; - FileData data; - unsigned long nSize = 0; - unsigned char* pBuffer = data.getFileData(pszFilename, "rb", &nSize); + CCFileData data(pszFilename, "rb"); + unsigned long nSize = data.getSize(); + unsigned char* pBuffer = data.getBuffer(); do { diff --git a/cocos2dx/textures/CCTextureCache.cpp b/cocos2dx/textures/CCTextureCache.cpp index db4bdd5dc1..63f615c928 100644 --- a/cocos2dx/textures/CCTextureCache.cpp +++ b/cocos2dx/textures/CCTextureCache.cpp @@ -209,9 +209,9 @@ CCTexture2D * CCTextureCache::addImage(const char * path) else if (std::string::npos != lowerCase.find(".jpg") || std::string::npos != lowerCase.find(".jpeg")) { CCImage * image = new CCImage(); - FileData data; - unsigned long nSize = 0; - unsigned char* pBuffer = data.getFileData(fullpath.c_str(), "rb", &nSize); + CCFileData data(fullpath.c_str(), "rb"); + unsigned long nSize = data.getSize(); + unsigned char* pBuffer = data.getBuffer(); if(! image->initWithImageData((void*)pBuffer, nSize, CCImage::kFmtJpg)) { delete image; @@ -240,9 +240,9 @@ CCTexture2D * CCTextureCache::addImage(const char * path) #else // prevents overloading the autorelease pool CCImage * image = new CCImage(); - FileData data; - unsigned long nSize = 0; - unsigned char* pBuffer = data.getFileData(fullpath.c_str(), "rb", &nSize); + CCFileData data(fullpath.c_str(), "rb"); + unsigned long nSize = data.getSize(); + unsigned char* pBuffer = data.getBuffer(); if(! image->initWithImageData((void*)pBuffer, nSize, CCImage::kFmtPng)) { delete image; From 6c5cf577b766f087854f3632317fe72447e22ef6 Mon Sep 17 00:00:00 2001 From: yangws Date: Tue, 29 Mar 2011 15:05:53 +0800 Subject: [PATCH 2/5] issue #439 wophone and android modified. --- cocos2dx/platform/CCCommon.cpp | 31 +++++++++++++++++-- .../platform/android/CCFileUtils_android.cpp | 10 +++++- cocos2dx/platform/android/CCImage_android.cpp | 6 ---- cocos2dx/platform/win32/CCFileUtils_win32.cpp | 10 +++--- .../platform/wophone/CCFileUtils_wophone.cpp | 16 +++++++++- cocos2dx/platform/wophone/CCImage_wophone.cpp | 8 ----- cocos2dx/proj.wophone/cocos2d-wophone.vcproj | 8 +++-- 7 files changed, 64 insertions(+), 25 deletions(-) diff --git a/cocos2dx/platform/CCCommon.cpp b/cocos2dx/platform/CCCommon.cpp index 5ea51d99ce..0d4b4a2ec3 100644 --- a/cocos2dx/platform/CCCommon.cpp +++ b/cocos2dx/platform/CCCommon.cpp @@ -99,6 +99,16 @@ void CCLog(const char * pszFormat, ...) #endif } +void CCMessageBox(const char * pszMsg, const char * pszTitle) +{ + TUChar tszMsg[MAX_LEN] = { 0 }; + TUChar tszTitle[MAX_LEN] = { 0 }; + TUString::StrUtf8ToStrUnicode(tszMsg,(Char*)pszMsg); + TUString::StrUtf8ToStrUnicode(tszTitle,(Char*)pszTitle); + TMessageBox box(tszMsg, tszTitle, WMB_OK); + box.Show(); +} + NS_CC_END; #endif // CC_PLATFORM_WOPHONE @@ -126,6 +136,12 @@ void CCLog(const char * pszFormat, ...) printf("\n"); } +// ios no MessageBox, use CCLog instead +void CCMessageBox(const char * pszMsg, const char * pszTitle) +{ + CCLog("%s: %s", pszTitle, pszMsg); +} + NS_CC_END; #endif // CC_PLATFORM_IOS @@ -152,6 +168,12 @@ void CCLog(const char * pszFormat, ...) __android_log_print(ANDROID_LOG_DEBUG, "cocos2d-x debug info", buf); } +// android no MessageBox, use CCLog instead +void CCMessageBox(const char * pszMsg, const char * pszTitle) +{ + CCLog("%s: %s", pszTitle, pszMsg); +} + NS_CC_END; #endif // CC_PLATFORM_ANDROID @@ -179,8 +201,13 @@ void CCLog(const char * pszFormat, ...) IwTrace(GAME, (buf)); } + +// airplay no MessageBox, use CCLog instead +void CCMessageBox(const char * pszMsg, const char * pszTitle) +{ + CCLog("%s: %s", pszTitle, pszMsg); +} + NS_CC_END; #endif // CC_PLATFORM_AIRPLAY - - diff --git a/cocos2dx/platform/android/CCFileUtils_android.cpp b/cocos2dx/platform/android/CCFileUtils_android.cpp index 665964ddfa..97773db8ce 100644 --- a/cocos2dx/platform/android/CCFileUtils_android.cpp +++ b/cocos2dx/platform/android/CCFileUtils_android.cpp @@ -82,7 +82,15 @@ const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) { string fullPath = s_strRelativePath + pszFileName; - return CCFileUtils::getFileDataFromZip(s_strResourcePath.c_str(), fullPath.c_str(), pSize); + unsigned char * pData = CCFileUtils::getFileDataFromZip(s_strResourcePath.c_str(), fullPath.c_str(), pSize); + if (! pData && getIsPopupNotify()) + { + std::string title = "Notification"; + std::string msg = "Get data from file("; + msg.append(pszFileName).append(") failed!"); + CCMessageBox(msg.c_str(), title.c_str()); + } + return pData; } void CCFileUtils::setResource(const char* pszZipFileName, const char* pszResPath) diff --git a/cocos2dx/platform/android/CCImage_android.cpp b/cocos2dx/platform/android/CCImage_android.cpp index 95f8f24d3c..d808d5f892 100644 --- a/cocos2dx/platform/android/CCImage_android.cpp +++ b/cocos2dx/platform/android/CCImage_android.cpp @@ -42,12 +42,6 @@ static int sk_atomic_dec(int *value) NS_CC_BEGIN; -// android not support -void CCMessageBox(const std::string& msg, const std::string& title) -{ - -} - class BitmapDC { public: diff --git a/cocos2dx/platform/win32/CCFileUtils_win32.cpp b/cocos2dx/platform/win32/CCFileUtils_win32.cpp index c542f4c20c..d2b40d7568 100644 --- a/cocos2dx/platform/win32/CCFileUtils_win32.cpp +++ b/cocos2dx/platform/win32/CCFileUtils_win32.cpp @@ -115,7 +115,7 @@ const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) { - unsigned char * Buffer = NULL; + unsigned char * pBuffer = NULL; do { @@ -126,12 +126,12 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz fseek(fp,0,SEEK_END); *pSize = ftell(fp); fseek(fp,0,SEEK_SET); - Buffer = new unsigned char[*pSize]; - *pSize = fread(Buffer,sizeof(unsigned char), *pSize,fp); + pBuffer = new unsigned char[*pSize]; + *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp); fclose(fp); } while (0); - if (! Buffer && getIsPopupNotify()) + if (! pBuffer && getIsPopupNotify()) { std::string title = "Notification"; std::string msg = "Get data from file("; @@ -139,7 +139,7 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz CCMessageBox(msg.c_str(), title.c_str()); } - return Buffer; + return pBuffer; } void CCFileUtils::setResource(const char* pszZipFileName, const char* pszResPath) diff --git a/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp b/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp index 7f8b98caf6..cd2035eb20 100644 --- a/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp +++ b/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp @@ -277,7 +277,7 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz do { - if (strlen(s_pszZipFilePath) != 0) + if (0 != s_pszZipFilePath[0]) { // if specify the zip file,load from it first pBuffer = getFileDataFromZip(s_pszZipFilePath, pszFileName, pSize); @@ -296,6 +296,20 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz fclose(fp); } while (0); + if (! pBuffer && getIsPopupNotify()) + { + std::string title = "Notification"; + std::string msg = "Get data from file("; + msg.append(pszFileName); + if (0 != s_pszZipFilePath[0]) + { + msg.append(") in zip archive(").append(s_pszZipFilePath); + } + msg.append(") failed!"); + + CCMessageBox(msg.c_str(), title.c_str()); + } + return pBuffer; } diff --git a/cocos2dx/platform/wophone/CCImage_wophone.cpp b/cocos2dx/platform/wophone/CCImage_wophone.cpp index 5f886b5fa2..57b5508805 100644 --- a/cocos2dx/platform/wophone/CCImage_wophone.cpp +++ b/cocos2dx/platform/wophone/CCImage_wophone.cpp @@ -29,14 +29,6 @@ NS_CC_BEGIN typedef std::basic_string stdTUString; -void CCMessageBox(const std::string& msg, const std::string& title) -{ - TUChar szText[256] = { 0 }; - TUString::StrUtf8ToStrUnicode(szText,(Char*)msg.c_str()); - TMessageBox box(szText, NULL, WMB_OK); - box.Show(); -} - class BitmapDC { public: diff --git a/cocos2dx/proj.wophone/cocos2d-wophone.vcproj b/cocos2dx/proj.wophone/cocos2d-wophone.vcproj index a41af88d3d..26ae4c8810 100644 --- a/cocos2dx/proj.wophone/cocos2d-wophone.vcproj +++ b/cocos2dx/proj.wophone/cocos2d-wophone.vcproj @@ -1065,11 +1065,15 @@ Name="makefile" > + + From 1887417472340e531203a0d07b82ffd7cfd922aa Mon Sep 17 00:00:00 2001 From: yangws Date: Tue, 29 Mar 2011 15:56:55 +0800 Subject: [PATCH 3/5] issue #439 ios and airplay midified. --- .../platform/airplay/CCFileUtils_airplay.cpp | 21 ++++++- cocos2dx/platform/airplay/CCImage_airplay.cpp | 59 +------------------ cocos2dx/platform/ios/CCFileUtils_ios.mm | 30 ++++++++-- cocos2dx/platform/ios/CCImage_ios.mm | 44 +------------- tests/test.android/build_native.sh | 4 +- 5 files changed, 53 insertions(+), 105 deletions(-) diff --git a/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp b/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp index 1660a2b67d..6dbebcdfc8 100644 --- a/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp +++ b/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp @@ -226,8 +226,10 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz s3eFile* pFile = s3eFileOpen(pszFileName, pszMode); - - IwAssertMsg(GAME, pFile, ("Open file %s Failed. s3eFileError Code : %i", pszFileName, s3eFileGetError())); + if (! pFile && getIsPopupNotify()) + { + IwAssertMsg(GAME, pFile, ("Open file %s Failed. s3eFileError Code : %i", pszFileName, s3eFileGetError())); + } int32 fileSize = s3eFileGetSize(pFile); *pSize=fileSize; @@ -264,5 +266,20 @@ std::string& CCFileUtils::ccRemoveHDSuffixFromFile(std::string& path) return path; } +/////////////////////////////////////////////////////////////////////////////// +// Notification support when getFileData from a invalid file +/////////////////////////////////////////////////////////////////////////////// +static bool s_bPopupNotify = true; + +void CCFileUtils::setIsPopupNotify(bool bNotify) +{ + s_bPopupNotify = bNotify; +} + +bool CCFileUtils::getIsPopupNotify() +{ + return s_bPopupNotify; +} + NS_CC_END; diff --git a/cocos2dx/platform/airplay/CCImage_airplay.cpp b/cocos2dx/platform/airplay/CCImage_airplay.cpp index 145bb8f005..c4e505dc47 100644 --- a/cocos2dx/platform/airplay/CCImage_airplay.cpp +++ b/cocos2dx/platform/airplay/CCImage_airplay.cpp @@ -24,6 +24,7 @@ #include "CCImage.h" #include "CCCommon.h" #include "CCStdC.h" +#include "CCFileUtils.h" #include "s3eFile.h" #include "IwImage.h" #include "IwUtil.h" @@ -61,52 +62,8 @@ CCImage::~CCImage() bool CCImage::initWithImageFile(const char * strPath, EImageFormat eImgFmt/* = eFmtPng*/) { IW_CALLSTACK("UIImage::initWithImageFile"); - - bool bRet = false; - FILE *fp = 0; - unsigned char *buffer = NULL; - do - { - // open file - fp = fopen(strPath, "rb"); - CC_BREAK_IF(! fp); - - // compute the length of file - fseek(fp,0,SEEK_END); - int size = ftell(fp); - fseek(fp,0,SEEK_SET); - - // allocate enough memory to save the data of file - buffer = new unsigned char[size]; - CC_BREAK_IF(! buffer); - - // read data - size = fread(buffer, sizeof(unsigned char), size, fp); - - if (kFmtJpg == eImgFmt) - { - bRet = _initWithJpgData(buffer, size); - } - else - { - bRet = _initWithPngData(buffer, size); - } - } while (0); - - CC_SAFE_DELETE_ARRAY(buffer); - if (fp) - { - fclose(fp); - } - if (! bRet && CCImage::getIsPopupNotify()) - { - std::string title = "cocos2d-x error!"; - std::string msg = "Load "; - msg.append(strPath).append(" failed!"); - - IwError(("cocos2d-x error! Load %s failed", strPath)); - } - return bRet; + CCFileData data(CCFileUtils::fullPathFromRelativePath(strPath), "rb"); + return initWithImageData(data.getBuffer(), data.getSize(), eImgFmt); } bool CCImage::initWithImageData(void * pData, int nDataLen, EImageFormat eFmt/* = eSrcFmtPng*/) @@ -129,16 +86,6 @@ bool CCImage::initWithImageData(void * pData, int nDataLen, EImageFormat eFmt/* } while (0); return bRet; } -static bool s_bPopupNotify = true; -void CCImage::setIsPopupNotify(bool bNotify) -{ - s_bPopupNotify = bNotify; -} - -bool CCImage::getIsPopupNotify() -{ - return s_bPopupNotify; -} bool CCImage::_initWithJpgData(void * data, int nSize) { diff --git a/cocos2dx/platform/ios/CCFileUtils_ios.mm b/cocos2dx/platform/ios/CCFileUtils_ios.mm index 34028e496b..a95f51fd76 100644 --- a/cocos2dx/platform/ios/CCFileUtils_ios.mm +++ b/cocos2dx/platform/ios/CCFileUtils_ios.mm @@ -336,7 +336,7 @@ public: } unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) { - unsigned char * Buffer = NULL; + unsigned char * pBuffer = NULL; do { @@ -347,12 +347,20 @@ public: fseek(fp,0,SEEK_END); *pSize = ftell(fp); fseek(fp,0,SEEK_SET); - Buffer = new unsigned char[*pSize]; - *pSize = fread(Buffer,sizeof(unsigned char), *pSize,fp); + pBuffer = new unsigned char[*pSize]; + *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp); fclose(fp); } while (0); - return Buffer; + if (! pBuffer && getIsPopupNotify()) + { + std::string title = "Notification"; + std::string msg = "Get data from file("; + msg.append(pszFileName).append(") failed!"); + + CCMessageBox(msg.c_str(), title.c_str()); + } + return pBuffer; } void CCFileUtils::setResource(const char* pszZipFileName, const char* pszResPath) { @@ -362,4 +370,18 @@ public: { CCAssert(0, "Have not implement!"); } + + // notification support when getFileData from a invalid file + static bool s_bPopupNotify = true; + + void CCFileUtils::setIsPopupNotify(bool bNotify) + { + s_bPopupNotify = bNotify; + } + + bool CCFileUtils::getIsPopupNotify() + { + return s_bPopupNotify; + } + }//namespace cocos2d diff --git a/cocos2dx/platform/ios/CCImage_ios.mm b/cocos2dx/platform/ios/CCImage_ios.mm index ed7e04cbf3..21b932e794 100644 --- a/cocos2dx/platform/ios/CCImage_ios.mm +++ b/cocos2dx/platform/ios/CCImage_ios.mm @@ -24,6 +24,7 @@ THE SOFTWARE. #include #include #include "CCImage.h" +#include "CCFileUtils.h" #include typedef struct @@ -448,13 +449,6 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl NS_CC_BEGIN; -static bool s_bPopupNotify = true; - -void CCMessageBox(const std::string& msg, const std::string& title) -{ - -} - CCImage::CCImage() : m_nWidth(0) , m_nHeight(0) @@ -473,30 +467,8 @@ CCImage::~CCImage() bool CCImage::initWithImageFile(const char * strPath, EImageFormat eImgFmt/* = eFmtPng*/) { - bool bRet = false; - tImageInfo info = {0}; - - switch (eImgFmt) - { - case kFmtPng: - case kFmtJpg: - bRet = _initWithFile(strPath, &info); - break; - default: - // unsupported image type - bRet = false; - break; - } - if (bRet) - { - m_nHeight = (short)info.height; - m_nWidth = (short)info.width; - m_nBitsPerComponent = info.bitsPerComponent; - m_bHasAlpha = info.hasAlpha; - m_bPreMulti = info.isPremultipliedAlpha; - m_pData = info.data; - } - return bRet; + CCFileData data(CCFileUtils::fullPathFromRelativePath(strPath), "rb"); + return initWithImageData(data.getBuffer(), data.getSize(), eImgFmt); } bool CCImage::initWithImageData(void * pData, int nDataLen, EImageFormat eFmt/* = eSrcFmtPng*/) @@ -546,15 +518,5 @@ bool CCImage::initWithString( return true; } -void CCImage::setIsPopupNotify(bool bNotify) -{ - s_bPopupNotify = bNotify; -} - -bool CCImage::getIsPopupNotify() -{ - return s_bPopupNotify; -} - NS_CC_END; diff --git a/tests/test.android/build_native.sh b/tests/test.android/build_native.sh index 5c29a9bcbf..acb8c9b15b 100644 --- a/tests/test.android/build_native.sh +++ b/tests/test.android/build_native.sh @@ -1,6 +1,6 @@ # set params -ANDROID_NDK_ROOT=/cygdrive/e/android-ndk-r4-crystax -COCOS2DX_ROOT=/cygdrive/d/Work7/cocos2d-x +ANDROID_NDK_ROOT=$ANDROID_NDK_ROOT +COCOS2DX_ROOT=/cygdrive/d/Work7/cocos2d-x.yangws TESTS_ROOT=$COCOS2DX_ROOT/tests/test.android # make sure assets is exist From 48d57b88e27a054aed17da86545afd85150f48e1 Mon Sep 17 00:00:00 2001 From: yangws Date: Tue, 29 Mar 2011 15:59:37 +0800 Subject: [PATCH 4/5] issue #439 ios and airplay midified. --- .../platform/airplay/CCFileUtils_airplay.cpp | 21 +------ cocos2dx/platform/airplay/CCImage_airplay.cpp | 59 ++++++++++++++++++- cocos2dx/platform/ios/CCFileUtils_ios.mm | 30 ++-------- cocos2dx/platform/ios/CCImage_ios.mm | 44 +++++++++++++- 4 files changed, 103 insertions(+), 51 deletions(-) diff --git a/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp b/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp index 6dbebcdfc8..1660a2b67d 100644 --- a/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp +++ b/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp @@ -226,10 +226,8 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz s3eFile* pFile = s3eFileOpen(pszFileName, pszMode); - if (! pFile && getIsPopupNotify()) - { - IwAssertMsg(GAME, pFile, ("Open file %s Failed. s3eFileError Code : %i", pszFileName, s3eFileGetError())); - } + + IwAssertMsg(GAME, pFile, ("Open file %s Failed. s3eFileError Code : %i", pszFileName, s3eFileGetError())); int32 fileSize = s3eFileGetSize(pFile); *pSize=fileSize; @@ -266,20 +264,5 @@ std::string& CCFileUtils::ccRemoveHDSuffixFromFile(std::string& path) return path; } -/////////////////////////////////////////////////////////////////////////////// -// Notification support when getFileData from a invalid file -/////////////////////////////////////////////////////////////////////////////// -static bool s_bPopupNotify = true; - -void CCFileUtils::setIsPopupNotify(bool bNotify) -{ - s_bPopupNotify = bNotify; -} - -bool CCFileUtils::getIsPopupNotify() -{ - return s_bPopupNotify; -} - NS_CC_END; diff --git a/cocos2dx/platform/airplay/CCImage_airplay.cpp b/cocos2dx/platform/airplay/CCImage_airplay.cpp index c4e505dc47..145bb8f005 100644 --- a/cocos2dx/platform/airplay/CCImage_airplay.cpp +++ b/cocos2dx/platform/airplay/CCImage_airplay.cpp @@ -24,7 +24,6 @@ #include "CCImage.h" #include "CCCommon.h" #include "CCStdC.h" -#include "CCFileUtils.h" #include "s3eFile.h" #include "IwImage.h" #include "IwUtil.h" @@ -62,8 +61,52 @@ CCImage::~CCImage() bool CCImage::initWithImageFile(const char * strPath, EImageFormat eImgFmt/* = eFmtPng*/) { IW_CALLSTACK("UIImage::initWithImageFile"); - CCFileData data(CCFileUtils::fullPathFromRelativePath(strPath), "rb"); - return initWithImageData(data.getBuffer(), data.getSize(), eImgFmt); + + bool bRet = false; + FILE *fp = 0; + unsigned char *buffer = NULL; + do + { + // open file + fp = fopen(strPath, "rb"); + CC_BREAK_IF(! fp); + + // compute the length of file + fseek(fp,0,SEEK_END); + int size = ftell(fp); + fseek(fp,0,SEEK_SET); + + // allocate enough memory to save the data of file + buffer = new unsigned char[size]; + CC_BREAK_IF(! buffer); + + // read data + size = fread(buffer, sizeof(unsigned char), size, fp); + + if (kFmtJpg == eImgFmt) + { + bRet = _initWithJpgData(buffer, size); + } + else + { + bRet = _initWithPngData(buffer, size); + } + } while (0); + + CC_SAFE_DELETE_ARRAY(buffer); + if (fp) + { + fclose(fp); + } + if (! bRet && CCImage::getIsPopupNotify()) + { + std::string title = "cocos2d-x error!"; + std::string msg = "Load "; + msg.append(strPath).append(" failed!"); + + IwError(("cocos2d-x error! Load %s failed", strPath)); + } + return bRet; } bool CCImage::initWithImageData(void * pData, int nDataLen, EImageFormat eFmt/* = eSrcFmtPng*/) @@ -86,6 +129,16 @@ bool CCImage::initWithImageData(void * pData, int nDataLen, EImageFormat eFmt/* } while (0); return bRet; } +static bool s_bPopupNotify = true; +void CCImage::setIsPopupNotify(bool bNotify) +{ + s_bPopupNotify = bNotify; +} + +bool CCImage::getIsPopupNotify() +{ + return s_bPopupNotify; +} bool CCImage::_initWithJpgData(void * data, int nSize) { diff --git a/cocos2dx/platform/ios/CCFileUtils_ios.mm b/cocos2dx/platform/ios/CCFileUtils_ios.mm index a95f51fd76..34028e496b 100644 --- a/cocos2dx/platform/ios/CCFileUtils_ios.mm +++ b/cocos2dx/platform/ios/CCFileUtils_ios.mm @@ -336,7 +336,7 @@ public: } unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) { - unsigned char * pBuffer = NULL; + unsigned char * Buffer = NULL; do { @@ -347,20 +347,12 @@ public: fseek(fp,0,SEEK_END); *pSize = ftell(fp); fseek(fp,0,SEEK_SET); - pBuffer = new unsigned char[*pSize]; - *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp); + Buffer = new unsigned char[*pSize]; + *pSize = fread(Buffer,sizeof(unsigned char), *pSize,fp); fclose(fp); } while (0); - if (! pBuffer && getIsPopupNotify()) - { - std::string title = "Notification"; - std::string msg = "Get data from file("; - msg.append(pszFileName).append(") failed!"); - - CCMessageBox(msg.c_str(), title.c_str()); - } - return pBuffer; + return Buffer; } void CCFileUtils::setResource(const char* pszZipFileName, const char* pszResPath) { @@ -370,18 +362,4 @@ public: { CCAssert(0, "Have not implement!"); } - - // notification support when getFileData from a invalid file - static bool s_bPopupNotify = true; - - void CCFileUtils::setIsPopupNotify(bool bNotify) - { - s_bPopupNotify = bNotify; - } - - bool CCFileUtils::getIsPopupNotify() - { - return s_bPopupNotify; - } - }//namespace cocos2d diff --git a/cocos2dx/platform/ios/CCImage_ios.mm b/cocos2dx/platform/ios/CCImage_ios.mm index 21b932e794..ed7e04cbf3 100644 --- a/cocos2dx/platform/ios/CCImage_ios.mm +++ b/cocos2dx/platform/ios/CCImage_ios.mm @@ -24,7 +24,6 @@ THE SOFTWARE. #include #include #include "CCImage.h" -#include "CCFileUtils.h" #include typedef struct @@ -449,6 +448,13 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl NS_CC_BEGIN; +static bool s_bPopupNotify = true; + +void CCMessageBox(const std::string& msg, const std::string& title) +{ + +} + CCImage::CCImage() : m_nWidth(0) , m_nHeight(0) @@ -467,8 +473,30 @@ CCImage::~CCImage() bool CCImage::initWithImageFile(const char * strPath, EImageFormat eImgFmt/* = eFmtPng*/) { - CCFileData data(CCFileUtils::fullPathFromRelativePath(strPath), "rb"); - return initWithImageData(data.getBuffer(), data.getSize(), eImgFmt); + bool bRet = false; + tImageInfo info = {0}; + + switch (eImgFmt) + { + case kFmtPng: + case kFmtJpg: + bRet = _initWithFile(strPath, &info); + break; + default: + // unsupported image type + bRet = false; + break; + } + if (bRet) + { + m_nHeight = (short)info.height; + m_nWidth = (short)info.width; + m_nBitsPerComponent = info.bitsPerComponent; + m_bHasAlpha = info.hasAlpha; + m_bPreMulti = info.isPremultipliedAlpha; + m_pData = info.data; + } + return bRet; } bool CCImage::initWithImageData(void * pData, int nDataLen, EImageFormat eFmt/* = eSrcFmtPng*/) @@ -518,5 +546,15 @@ bool CCImage::initWithString( return true; } +void CCImage::setIsPopupNotify(bool bNotify) +{ + s_bPopupNotify = bNotify; +} + +bool CCImage::getIsPopupNotify() +{ + return s_bPopupNotify; +} + NS_CC_END; From ba68a5b96606ad4425671c033c7be7e648b3e8a3 Mon Sep 17 00:00:00 2001 From: yangws Date: Tue, 29 Mar 2011 17:32:52 +0800 Subject: [PATCH 5/5] fixed #439 test OK. --- .../platform/airplay/CCFileUtils_airplay.cpp | 27 +- cocos2dx/platform/airplay/CCImage_airplay.cpp | 59 +-- cocos2dx/platform/ios/CCFileUtils_ios.mm | 433 +++++++++--------- cocos2dx/platform/ios/CCImage_ios.mm | 44 +- .../platform/wophone/CCFileUtils_wophone.cpp | 2 +- cocos2dx/textures/CCTextureCache.cpp | 24 +- tests/test.android/build_native.sh | 4 +- 7 files changed, 269 insertions(+), 324 deletions(-) diff --git a/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp b/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp index 1660a2b67d..5b99a43590 100644 --- a/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp +++ b/cocos2dx/platform/airplay/CCFileUtils_airplay.cpp @@ -226,9 +226,15 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz s3eFile* pFile = s3eFileOpen(pszFileName, pszMode); - - IwAssertMsg(GAME, pFile, ("Open file %s Failed. s3eFileError Code : %i", pszFileName, s3eFileGetError())); - + if (! pFile && getIsPopupNotify()) + { + IwAssertMsg(GAME, pFile, ("Open file %s Failed. s3eFileError Code : %i", pszFileName, s3eFileGetError())); + } + if (! pFile) + { + *pSize = 0; + return 0; + } int32 fileSize = s3eFileGetSize(pFile); *pSize=fileSize; @@ -264,5 +270,20 @@ std::string& CCFileUtils::ccRemoveHDSuffixFromFile(std::string& path) return path; } +/////////////////////////////////////////////////////////////////////////////// +// Notification support when getFileData from a invalid file +/////////////////////////////////////////////////////////////////////////////// +static bool s_bPopupNotify = true; + +void CCFileUtils::setIsPopupNotify(bool bNotify) +{ + s_bPopupNotify = bNotify; +} + +bool CCFileUtils::getIsPopupNotify() +{ + return s_bPopupNotify; +} + NS_CC_END; diff --git a/cocos2dx/platform/airplay/CCImage_airplay.cpp b/cocos2dx/platform/airplay/CCImage_airplay.cpp index 145bb8f005..c4e505dc47 100644 --- a/cocos2dx/platform/airplay/CCImage_airplay.cpp +++ b/cocos2dx/platform/airplay/CCImage_airplay.cpp @@ -24,6 +24,7 @@ #include "CCImage.h" #include "CCCommon.h" #include "CCStdC.h" +#include "CCFileUtils.h" #include "s3eFile.h" #include "IwImage.h" #include "IwUtil.h" @@ -61,52 +62,8 @@ CCImage::~CCImage() bool CCImage::initWithImageFile(const char * strPath, EImageFormat eImgFmt/* = eFmtPng*/) { IW_CALLSTACK("UIImage::initWithImageFile"); - - bool bRet = false; - FILE *fp = 0; - unsigned char *buffer = NULL; - do - { - // open file - fp = fopen(strPath, "rb"); - CC_BREAK_IF(! fp); - - // compute the length of file - fseek(fp,0,SEEK_END); - int size = ftell(fp); - fseek(fp,0,SEEK_SET); - - // allocate enough memory to save the data of file - buffer = new unsigned char[size]; - CC_BREAK_IF(! buffer); - - // read data - size = fread(buffer, sizeof(unsigned char), size, fp); - - if (kFmtJpg == eImgFmt) - { - bRet = _initWithJpgData(buffer, size); - } - else - { - bRet = _initWithPngData(buffer, size); - } - } while (0); - - CC_SAFE_DELETE_ARRAY(buffer); - if (fp) - { - fclose(fp); - } - if (! bRet && CCImage::getIsPopupNotify()) - { - std::string title = "cocos2d-x error!"; - std::string msg = "Load "; - msg.append(strPath).append(" failed!"); - - IwError(("cocos2d-x error! Load %s failed", strPath)); - } - return bRet; + CCFileData data(CCFileUtils::fullPathFromRelativePath(strPath), "rb"); + return initWithImageData(data.getBuffer(), data.getSize(), eImgFmt); } bool CCImage::initWithImageData(void * pData, int nDataLen, EImageFormat eFmt/* = eSrcFmtPng*/) @@ -129,16 +86,6 @@ bool CCImage::initWithImageData(void * pData, int nDataLen, EImageFormat eFmt/* } while (0); return bRet; } -static bool s_bPopupNotify = true; -void CCImage::setIsPopupNotify(bool bNotify) -{ - s_bPopupNotify = bNotify; -} - -bool CCImage::getIsPopupNotify() -{ - return s_bPopupNotify; -} bool CCImage::_initWithJpgData(void * data, int nSize) { diff --git a/cocos2dx/platform/ios/CCFileUtils_ios.mm b/cocos2dx/platform/ios/CCFileUtils_ios.mm index 34028e496b..92542a271a 100644 --- a/cocos2dx/platform/ios/CCFileUtils_ios.mm +++ b/cocos2dx/platform/ios/CCFileUtils_ios.mm @@ -38,243 +38,243 @@ THE SOFTWARE. static const char *static_ccRemoveHDSuffixFromFile( const char *pszPath) { #if CC_IS_RETINA_DISPLAY_SUPPORTED - - if(cocos2d::CC_CONTENT_SCALE_FACTOR() == 2 ) { - NSString *path = [NSString stringWithUTF8String: pszPath]; - NSString *name = [path lastPathComponent]; - NSString *suffix = [NSString stringWithUTF8String: CC_RETINA_DISPLAY_FILENAME_SUFFIX]; - - // check if path already has the suffix. - if( [name rangeOfString: suffix].location != NSNotFound ) { - - CCLOG("cocos2d: Filename(%@) contains %@ suffix. Removing it. See cocos2d issue #1040", path, CC_RETINA_DISPLAY_FILENAME_SUFFIX); - - NSString *newLastname = [name stringByReplacingOccurrencesOfString: suffix withString:@""]; - - NSString *pathWithoutLastname = [path stringByDeletingLastPathComponent]; - return [[pathWithoutLastname stringByAppendingPathComponent:newLastname] UTF8String]; - } - } - + + if(cocos2d::CC_CONTENT_SCALE_FACTOR() == 2 ) { + NSString *path = [NSString stringWithUTF8String: pszPath]; + NSString *name = [path lastPathComponent]; + NSString *suffix = [NSString stringWithUTF8String: CC_RETINA_DISPLAY_FILENAME_SUFFIX]; + + // check if path already has the suffix. + if( [name rangeOfString: suffix].location != NSNotFound ) { + + CCLOG("cocos2d: Filename(%@) contains %@ suffix. Removing it. See cocos2d issue #1040", path, CC_RETINA_DISPLAY_FILENAME_SUFFIX); + + NSString *newLastname = [name stringByReplacingOccurrencesOfString: suffix withString:@""]; + + NSString *pathWithoutLastname = [path stringByDeletingLastPathComponent]; + return [[pathWithoutLastname stringByAppendingPathComponent:newLastname] UTF8String]; + } + } + #endif // CC_IS_RETINA_DISPLAY_SUPPORTED - - return pszPath; + + return pszPath; } static NSString* getDoubleResolutionImage(NSString* path) { #if CC_IS_RETINA_DISPLAY_SUPPORTED - - if( cocos2d::CC_CONTENT_SCALE_FACTOR() == 2 ) - { - - NSString *pathWithoutExtension = [path stringByDeletingPathExtension]; - NSString *name = [pathWithoutExtension lastPathComponent]; - NSString *suffix = [NSString stringWithUTF8String: CC_RETINA_DISPLAY_FILENAME_SUFFIX]; - - // check if path already has the suffix. - if( [name rangeOfString: suffix].location != NSNotFound ) { - - CCLOG("cocos2d: WARNING Filename(%@) already has the suffix %@. Using it.", name, CC_RETINA_DISPLAY_FILENAME_SUFFIX); - return path; - } - - - NSString *extension = [path pathExtension]; - - if( [extension isEqualToString:@"ccz"] || [extension isEqualToString:@"gz"] ) - { - // All ccz / gz files should be in the format filename.xxx.ccz - // so we need to pull off the .xxx part of the extension as well - extension = [NSString stringWithFormat:@"%@.%@", [pathWithoutExtension pathExtension], extension]; - pathWithoutExtension = [pathWithoutExtension stringByDeletingPathExtension]; - } - - - NSString *retinaName = [pathWithoutExtension stringByAppendingString: suffix]; - retinaName = [retinaName stringByAppendingPathExtension:extension]; - - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; - if( [fileManager fileExistsAtPath:retinaName] ) - return retinaName; - - CCLOG("cocos2d: CCFileUtils: Warning HD file not found: %@", [retinaName lastPathComponent] ); - } - + + if( cocos2d::CC_CONTENT_SCALE_FACTOR() == 2 ) + { + + NSString *pathWithoutExtension = [path stringByDeletingPathExtension]; + NSString *name = [pathWithoutExtension lastPathComponent]; + NSString *suffix = [NSString stringWithUTF8String: CC_RETINA_DISPLAY_FILENAME_SUFFIX]; + + // check if path already has the suffix. + if( [name rangeOfString: suffix].location != NSNotFound ) { + + CCLOG("cocos2d: WARNING Filename(%@) already has the suffix %@. Using it.", name, CC_RETINA_DISPLAY_FILENAME_SUFFIX); + return path; + } + + + NSString *extension = [path pathExtension]; + + if( [extension isEqualToString:@"ccz"] || [extension isEqualToString:@"gz"] ) + { + // All ccz / gz files should be in the format filename.xxx.ccz + // so we need to pull off the .xxx part of the extension as well + extension = [NSString stringWithFormat:@"%@.%@", [pathWithoutExtension pathExtension], extension]; + pathWithoutExtension = [pathWithoutExtension stringByDeletingPathExtension]; + } + + + NSString *retinaName = [pathWithoutExtension stringByAppendingString: suffix]; + retinaName = [retinaName stringByAppendingPathExtension:extension]; + + NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; + if( [fileManager fileExistsAtPath:retinaName] ) + return retinaName; + + CCLOG("cocos2d: CCFileUtils: Warning HD file not found: %@", [retinaName lastPathComponent] ); + } + #endif // CC_IS_RETINA_DISPLAY_SUPPORTED - - return path; + + return path; } static const char* static_fullPathFromRelativePath(const char *pszRelativePath) { - - // NSAssert(pszRelativePath != nil, @"CCFileUtils: Invalid path"); - - // do not convert an absolute path (starting with '/') - NSString *relPath = [NSString stringWithUTF8String: pszRelativePath]; - NSString *fullpath = nil; - - // only if it is not an absolute path - if( ! [relPath isAbsolutePath] ) - { - NSString *file = [relPath lastPathComponent]; - NSString *imageDirectory = [relPath stringByDeletingLastPathComponent]; - - fullpath = [[NSBundle mainBundle] pathForResource:file - ofType:nil - inDirectory:imageDirectory]; - } - - if (fullpath == nil) - fullpath = relPath; - - fullpath = getDoubleResolutionImage(fullpath); - - return [fullpath UTF8String]; + + // NSAssert(pszRelativePath != nil, @"CCFileUtils: Invalid path"); + + // do not convert an absolute path (starting with '/') + NSString *relPath = [NSString stringWithUTF8String: pszRelativePath]; + NSString *fullpath = nil; + + // only if it is not an absolute path + if( ! [relPath isAbsolutePath] ) + { + NSString *file = [relPath lastPathComponent]; + NSString *imageDirectory = [relPath stringByDeletingLastPathComponent]; + + fullpath = [[NSBundle mainBundle] pathForResource:file +ofType:nil +inDirectory:imageDirectory]; + } + + if (fullpath == nil) + fullpath = relPath; + + fullpath = getDoubleResolutionImage(fullpath); + + return [fullpath UTF8String]; } namespace cocos2d { -typedef enum -{ - SAX_NONE = 0, - SAX_KEY, - SAX_DICT, - SAX_INT, - SAX_REAL, - SAX_STRING -}CCSAXState; + typedef enum + { + SAX_NONE = 0, + SAX_KEY, + SAX_DICT, + SAX_INT, + SAX_REAL, + SAX_STRING + }CCSAXState; -class CCDictMaker : public CCSAXDelegator -{ -public: - CCDictionary *m_pRootDict; - CCDictionary *m_pCurDict; - std::stack*> m_tDictStack; - std::string m_sCurKey;///< parsed key - CCSAXState m_tState; -public: - CCDictMaker() + class CCDictMaker : public CCSAXDelegator { - m_pRootDict = NULL; - m_pCurDict = NULL; - m_tState = SAX_NONE; - } - ~CCDictMaker() - { - } - CCDictionary *dictionaryWithContentsOfFile(const char *pFileName) - { - CCSAXParser parser; - - if (false == parser.init("UTF-8")) + public: + CCDictionary *m_pRootDict; + CCDictionary *m_pCurDict; + std::stack*> m_tDictStack; + std::string m_sCurKey;///< parsed key + CCSAXState m_tState; + public: + CCDictMaker() { - return NULL; + m_pRootDict = NULL; + m_pCurDict = NULL; + m_tState = SAX_NONE; } - parser.setDelegator(this); - - parser.parse(pFileName); - return m_pRootDict; - } - - void startElement(void *ctx, const char *name, const char **atts) - { - std::string sName((char*)name); - if( sName == "dict" ) + ~CCDictMaker() { - CCDictionary *pNewDict = new CCDictionary(); - if(! m_pRootDict) + } + CCDictionary *dictionaryWithContentsOfFile(const char *pFileName) + { + CCSAXParser parser; + + if (false == parser.init("UTF-8")) { - m_pRootDict = pNewDict; - pNewDict->autorelease(); + return NULL; + } + parser.setDelegator(this); + + parser.parse(pFileName); + return m_pRootDict; + } + + void startElement(void *ctx, const char *name, const char **atts) + { + std::string sName((char*)name); + if( sName == "dict" ) + { + CCDictionary *pNewDict = new CCDictionary(); + if(! m_pRootDict) + { + m_pRootDict = pNewDict; + pNewDict->autorelease(); + } + else + { + CCAssert(m_pCurDict && !m_sCurKey.empty(), ""); + m_pCurDict->setObject(pNewDict, m_sCurKey); + pNewDict->release(); + m_sCurKey.clear(); + } + m_pCurDict = pNewDict; + m_tDictStack.push(m_pCurDict); + m_tState = SAX_DICT; + } + else if(sName == "key") + { + m_tState = SAX_KEY; + } + else if(sName == "integer") + { + m_tState = SAX_INT; + } + else if(sName == "real") + { + m_tState = SAX_REAL; + } + else if(sName == "string") + { + m_tState = SAX_STRING; } else { - CCAssert(m_pCurDict && !m_sCurKey.empty(), ""); - m_pCurDict->setObject(pNewDict, m_sCurKey); - pNewDict->release(); - m_sCurKey.clear(); + m_tState = SAX_NONE; } - m_pCurDict = pNewDict; - m_tDictStack.push(m_pCurDict); - m_tState = SAX_DICT; } - else if(sName == "key") - { - m_tState = SAX_KEY; - } - else if(sName == "integer") - { - m_tState = SAX_INT; - } - else if(sName == "real") - { - m_tState = SAX_REAL; - } - else if(sName == "string") - { - m_tState = SAX_STRING; - } - else + + void endElement(void *ctx, const char *name) { + std::string sName((char*)name); + if( sName == "dict" ) + { + m_tDictStack.pop(); + if ( !m_tDictStack.empty() ) + { + m_pCurDict = (CCDictionary*)(m_tDictStack.top()); + } + } m_tState = SAX_NONE; } - } - void endElement(void *ctx, const char *name) - { - std::string sName((char*)name); - if( sName == "dict" ) + void textHandler(void *ctx, const char *ch, int len) { - m_tDictStack.pop(); - if ( !m_tDictStack.empty() ) + if (m_tState == SAX_NONE) { - m_pCurDict = (CCDictionary*)(m_tDictStack.top()); + return; } - } - m_tState = SAX_NONE; - } + CCString *pText = new CCString(); + pText->m_sString = std::string((char*)ch,0,len); - void textHandler(void *ctx, const char *ch, int len) - { - if (m_tState == SAX_NONE) - { - return; - } - CCString *pText = new CCString(); - pText->m_sString = std::string((char*)ch,0,len); - - switch(m_tState) - { - case SAX_KEY: - m_sCurKey = pText->m_sString; - break; - case SAX_INT: - case SAX_REAL: - case SAX_STRING: + switch(m_tState) { - CCAssert(!m_sCurKey.empty(), "not found key : "); - m_pCurDict->setObject(pText, m_sCurKey); + case SAX_KEY: + m_sCurKey = pText->m_sString; break; + case SAX_INT: + case SAX_REAL: + case SAX_STRING: + { + CCAssert(!m_sCurKey.empty(), "not found key : "); + m_pCurDict->setObject(pText, m_sCurKey); + break; + } } + pText->release(); } - pText->release(); - } -}; - + }; + // record the resource path static char s_pszResourcePath[MAX_PATH] = {0}; - + void CCFileUtils::setResourcePath(const char *pszResourcePath) { - // NSAssert(pszResourcePath != NULL, "[FileUtils setResourcePath] -- wrong resource path"); - // NSAssert(strlen(pszResourcePath) <= MAX_PATH, "[FileUtils setResourcePath] -- resource path too long"); - + // NSAssert(pszResourcePath != NULL, "[FileUtils setResourcePath] -- wrong resource path"); + // NSAssert(strlen(pszResourcePath) <= MAX_PATH, "[FileUtils setResourcePath] -- resource path too long"); + strcpy(s_pszResourcePath, pszResourcePath); } - + const char* CCFileUtils::getResourcePath() { return s_pszResourcePath; @@ -284,18 +284,18 @@ public: { assert( out ); assert( &*out ); - + int size = 0; FILE *f = fopen(filename, "rb"); if( !f ) { *out = NULL; return -1; } - + fseek(f, 0, SEEK_END); size = ftell(f); fseek(f, 0, SEEK_SET); - + *out = (unsigned char*)malloc(size); int read = fread(*out, 1, size, f); if( read != size ) { @@ -303,23 +303,23 @@ public: *out = NULL; return -1; } - + fclose(f); - + return size; } - + std::string& CCFileUtils::ccRemoveHDSuffixFromFile(std::string& path ) { path = static_ccRemoveHDSuffixFromFile(path.c_str()); return path; } - + const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) { return static_fullPathFromRelativePath(pszRelativePath); } - + const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile) { std::string relativeFile = fullPathFromRelativePath(pszRelativeFile); @@ -336,7 +336,7 @@ public: } unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) { - unsigned char * Buffer = NULL; + unsigned char * pBuffer = NULL; do { @@ -347,12 +347,20 @@ public: fseek(fp,0,SEEK_END); *pSize = ftell(fp); fseek(fp,0,SEEK_SET); - Buffer = new unsigned char[*pSize]; - *pSize = fread(Buffer,sizeof(unsigned char), *pSize,fp); + pBuffer = new unsigned char[*pSize]; + *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp); fclose(fp); } while (0); - return Buffer; + if (! pBuffer && getIsPopupNotify()) + { + std::string title = "Notification"; + std::string msg = "Get data from file("; + msg.append(pszFileName).append(") failed!"); + + CCMessageBox(msg.c_str(), title.c_str()); + } + return pBuffer; } void CCFileUtils::setResource(const char* pszZipFileName, const char* pszResPath) { @@ -362,4 +370,19 @@ public: { CCAssert(0, "Have not implement!"); } -}//namespace cocos2d + + // notification support when getFileData from a invalid file + static bool s_bPopupNotify = true; + + void CCFileUtils::setIsPopupNotify(bool bNotify) + { + s_bPopupNotify = bNotify; + } + + bool CCFileUtils::getIsPopupNotify() + { + return s_bPopupNotify; + } + +}//namespace cocos2d + diff --git a/cocos2dx/platform/ios/CCImage_ios.mm b/cocos2dx/platform/ios/CCImage_ios.mm index ed7e04cbf3..21b932e794 100644 --- a/cocos2dx/platform/ios/CCImage_ios.mm +++ b/cocos2dx/platform/ios/CCImage_ios.mm @@ -24,6 +24,7 @@ THE SOFTWARE. #include #include #include "CCImage.h" +#include "CCFileUtils.h" #include typedef struct @@ -448,13 +449,6 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl NS_CC_BEGIN; -static bool s_bPopupNotify = true; - -void CCMessageBox(const std::string& msg, const std::string& title) -{ - -} - CCImage::CCImage() : m_nWidth(0) , m_nHeight(0) @@ -473,30 +467,8 @@ CCImage::~CCImage() bool CCImage::initWithImageFile(const char * strPath, EImageFormat eImgFmt/* = eFmtPng*/) { - bool bRet = false; - tImageInfo info = {0}; - - switch (eImgFmt) - { - case kFmtPng: - case kFmtJpg: - bRet = _initWithFile(strPath, &info); - break; - default: - // unsupported image type - bRet = false; - break; - } - if (bRet) - { - m_nHeight = (short)info.height; - m_nWidth = (short)info.width; - m_nBitsPerComponent = info.bitsPerComponent; - m_bHasAlpha = info.hasAlpha; - m_bPreMulti = info.isPremultipliedAlpha; - m_pData = info.data; - } - return bRet; + CCFileData data(CCFileUtils::fullPathFromRelativePath(strPath), "rb"); + return initWithImageData(data.getBuffer(), data.getSize(), eImgFmt); } bool CCImage::initWithImageData(void * pData, int nDataLen, EImageFormat eFmt/* = eSrcFmtPng*/) @@ -546,15 +518,5 @@ bool CCImage::initWithString( return true; } -void CCImage::setIsPopupNotify(bool bNotify) -{ - s_bPopupNotify = bNotify; -} - -bool CCImage::getIsPopupNotify() -{ - return s_bPopupNotify; -} - NS_CC_END; diff --git a/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp b/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp index cd2035eb20..38f64e5f3e 100644 --- a/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp +++ b/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp @@ -292,7 +292,7 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz *pSize = ftell(fp); fseek(fp,0,SEEK_SET); pBuffer = new unsigned char[*pSize]; - fread(pBuffer,sizeof(unsigned char), *pSize,fp); + *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp); fclose(fp); } while (0); diff --git a/cocos2dx/textures/CCTextureCache.cpp b/cocos2dx/textures/CCTextureCache.cpp index 63f615c928..64c8d2f72c 100644 --- a/cocos2dx/textures/CCTextureCache.cpp +++ b/cocos2dx/textures/CCTextureCache.cpp @@ -208,18 +208,14 @@ CCTexture2D * CCTextureCache::addImage(const char * path) // Issue #886: TEMPORARY FIX FOR TRANSPARENT JPEGS IN IOS4 else if (std::string::npos != lowerCase.find(".jpg") || std::string::npos != lowerCase.find(".jpeg")) { - CCImage * image = new CCImage(); + CCImage image; CCFileData data(fullpath.c_str(), "rb"); unsigned long nSize = data.getSize(); unsigned char* pBuffer = data.getBuffer(); - if(! image->initWithImageData((void*)pBuffer, nSize, CCImage::kFmtJpg)) - { - delete image; - break; - } + CC_BREAK_IF(! image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtJpg)); + texture = new CCTexture2D(); - texture->initWithImage(image); - CC_SAFE_DELETE(image);// image->release(); + texture->initWithImage(&image); if( texture ) { @@ -239,18 +235,14 @@ CCTexture2D * CCTextureCache::addImage(const char * path) tex = [ [CCTexture2D alloc] initWithImage: image ]; #else // prevents overloading the autorelease pool - CCImage * image = new CCImage(); + CCImage image; CCFileData data(fullpath.c_str(), "rb"); unsigned long nSize = data.getSize(); unsigned char* pBuffer = data.getBuffer(); - if(! image->initWithImageData((void*)pBuffer, nSize, CCImage::kFmtPng)) - { - delete image; - break; - } + CC_BREAK_IF(! image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtPng)); + texture = new CCTexture2D(); - texture->initWithImage(image); - CC_SAFE_DELETE(image);// image->release(); + texture->initWithImage(&image); #endif if( texture ) { diff --git a/tests/test.android/build_native.sh b/tests/test.android/build_native.sh index acb8c9b15b..5c29a9bcbf 100644 --- a/tests/test.android/build_native.sh +++ b/tests/test.android/build_native.sh @@ -1,6 +1,6 @@ # set params -ANDROID_NDK_ROOT=$ANDROID_NDK_ROOT -COCOS2DX_ROOT=/cygdrive/d/Work7/cocos2d-x.yangws +ANDROID_NDK_ROOT=/cygdrive/e/android-ndk-r4-crystax +COCOS2DX_ROOT=/cygdrive/d/Work7/cocos2d-x TESTS_ROOT=$COCOS2DX_ROOT/tests/test.android # make sure assets is exist