issue #19: fixed runtime error

This commit is contained in:
Ming 2010-07-30 10:17:13 +00:00
parent 458ca18148
commit 9945867be7
3 changed files with 69 additions and 33 deletions

View File

@ -27,6 +27,7 @@ THE SOFTWARE.
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include <tg3.h>
#include "CCFileUtils.h"
#include "Cocos2dDefine.h"
@ -179,11 +180,14 @@ char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
}
// get the user data path and append relativepath to it
char *pszUserPath = (char*)EOS_GetSpecialPath(EOS_FILE_SPECIAL_PATH_USER_DATA);
const TUChar *pszTmp = EOS_GetSpecialPath(EOS_FILE_SPECIAL_PATH_USER_DATA);
char *pszUserPath = new char[TUString::StrLen(pszTmp) + 1];
TUString::StrUnicodeToStrUtf8((Char*)pszUserPath, pszTmp);
char *pszRet;
INT32 nLen = strlen(pszRelativePath) + strlen(pszUserPath) + 1;
pszRet = new char[nLen];
memset(pszRet, 0, nLen);
strncat(pszRet, pszUserPath, strlen(pszUserPath));
strncat(pszRet, pszRelativePath, strlen(pszRelativePath));

View File

@ -40,6 +40,10 @@ UIImage::UIImage(int nX, int nY, void *buffer)
UIImage::~UIImage(void)
{
if (m_pBitmap)
{
m_pBitmap->Destroy();
}
}
bool UIImage::initWithContentsOfFile(const string &strPath)
@ -49,10 +53,27 @@ bool UIImage::initWithContentsOfFile(const string &strPath)
return false;
}
m_pBitmap = TBitmap::CreateFromFile((TUChar *)(strPath.c_str()));
// load the image
ImageLoader obImgLoader;
Image obImg;
TUChar pszPath[strPath.size() + 1];
TUString::StrGBToUnicode(pszPath, (const Char *) strPath.c_str());
// check if the loading action is successful
if (! obImgLoader.loadImage(obImg, pszPath, IT_LOAD_FMT_UNKNOWN))
{
return false;
}
// init bitmap
m_pBitmap = (TBitmap *) obImg.GetTBitmap();
m_pBitmap = m_pBitmap->DupBitmapTo32();
// the hight is 0??
if (m_pBitmap && m_pBitmap->GetHeight() == 0)
{
m_pBitmap->Destroy();
m_pBitmap = NULL;
return false;
@ -169,7 +190,7 @@ UINT8* UIImage::getRGBA8888Data(void)
nH = pBitmap->GetHeight();
// alloc memory and store the bitmap data
pBufferRet = new UINT8(nW * nH * 4);
pBufferRet = new UINT8[nW * nH * 4];
memcpy(pBufferRet, pBitmap->GetDataPtr(), nW * nH * 4);
} while(0);

View File

@ -353,6 +353,7 @@ void CCSprite::setTextureRect(CGRect rect, CGSize size)
m_obRect = rect;
setContentSize(size);
updateTextureCoords(rect);
// rendering using SpriteSheet
if (m_bUsesSpriteSheet)
@ -380,36 +381,39 @@ void CCSprite::setTextureRect(CGRect rect, CGSize size)
void CCSprite::updateTextureCoords(CGRect rect)
{
float atlasWidth = (float)m_pobTexture->getPixelsWide();
float atlasHeight = (float)m_pobTexture->getPixelsHigh();
float left = m_obRect.origin.x / atlasWidth;
float right = (m_obRect.origin.x + m_obRect.size.width) / atlasWidth;
float top = m_obRect.origin.y / atlasHeight;
float bottom = (m_obRect.origin.y + m_obRect.size.height) / atlasHeight;
if (m_bFlipX)
if (m_pobTexture)
{
float tmp = left;
left = right;
right = tmp;
}
float atlasWidth = (float)m_pobTexture->getPixelsWide();
float atlasHeight = (float)m_pobTexture->getPixelsHigh();
if (m_bFlipY)
{
float tmp = top;
top = bottom;
bottom = tmp;
}
float left = m_obRect.origin.x / atlasWidth;
float right = (m_obRect.origin.x + m_obRect.size.width) / atlasWidth;
float top = m_obRect.origin.y / atlasHeight;
float bottom = (m_obRect.origin.y + m_obRect.size.height) / atlasHeight;
m_sQuad.bl.texCoords.u = left;
m_sQuad.bl.texCoords.v = bottom;
m_sQuad.br.texCoords.u = right;
m_sQuad.br.texCoords.v = bottom;
m_sQuad.tl.texCoords.u = left;
m_sQuad.tl.texCoords.v = top;
m_sQuad.tr.texCoords.u = right;
m_sQuad.tr.texCoords.v = top;
if (m_bFlipX)
{
float tmp = left;
left = right;
right = tmp;
}
if (m_bFlipY)
{
float tmp = top;
top = bottom;
bottom = tmp;
}
m_sQuad.bl.texCoords.u = left;
m_sQuad.bl.texCoords.v = bottom;
m_sQuad.br.texCoords.u = right;
m_sQuad.br.texCoords.v = bottom;
m_sQuad.tl.texCoords.u = left;
m_sQuad.tl.texCoords.v = top;
m_sQuad.tr.texCoords.u = right;
m_sQuad.tr.texCoords.v = top;
}
}
void CCSprite::updateTransform(void)
@ -978,11 +982,18 @@ void CCSprite::setTexture(CCTexture2D *texture)
assert(! m_bUsesSpriteSheet);
// // accept texture==nil as argument
assert(! m_pobTexture);
assert((! texture) || dynamic_cast<CCTexture2D*>(texture));
if (m_pobTexture)
{
m_pobTexture->release();
}
m_pobTexture->release();
m_pobTexture = texture;
texture->retain();
if (texture)
{
texture->retain();
}
updateBlendFunc();
}