Merge pull request #15794 from TheCodez/cachetexture

UIScrollbar and UIPageViewIndicator cache base64 Texture now #15780
This commit is contained in:
子龙山人 2016-06-13 17:31:12 +08:00 committed by GitHub
commit da2ea7e08b
4 changed files with 43 additions and 7 deletions

View File

@ -33,6 +33,7 @@ THE SOFTWARE.
#include "base/base64.h"
#include "renderer/CCCustomCommand.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCTextureCache.h"
#include "platform/CCImage.h"
#include "platform/CCFileUtils.h"
@ -311,10 +312,33 @@ Rect getCascadeBoundingBox(Node *node)
return cbb;
}
Sprite* createSpriteFromBase64Cached(const char* base64String, const char* key)
{
Texture2D* texture = Director::getInstance()->getTextureCache()->getTextureForKey(key);
if (texture == nullptr)
{
unsigned char* decoded;
int length = base64Decode((const unsigned char*)base64String, (unsigned int)strlen(base64String), &decoded);
Image *image = new (std::nothrow) Image();
bool imageResult = image->initWithImageData(decoded, length);
CCASSERT(imageResult, "Failed to create image from base64!");
free(decoded);
texture = Director::getInstance()->getTextureCache()->addImage(image, key);
image->release();
}
Sprite* sprite = Sprite::createWithTexture(texture);
return sprite;
}
Sprite* createSpriteFromBase64(const char* base64String)
{
unsigned char* decoded;
int length = base64Decode((const unsigned char*) base64String, (unsigned int) strlen(base64String), &decoded);
int length = base64Decode((const unsigned char*)base64String, (unsigned int)strlen(base64String), &decoded);
Image *image = new (std::nothrow) Image();
bool imageResult = image->initWithImageData(decoded, length);
@ -328,7 +352,7 @@ Sprite* createSpriteFromBase64(const char* base64String)
Sprite* sprite = Sprite::createWithTexture(texture);
texture->release();
return sprite;
}

View File

@ -114,12 +114,20 @@ namespace utils
CC_DLL Rect getCascadeBoundingBox(Node *node);
/**
* Create a sprite instance from base64 encoded image.
* Create a sprite instance from base64 encoded image and adds the texture to the Texture Cache.
* @return Returns an instance of sprite
*/
CC_DLL Sprite* createSpriteFromBase64Cached(const char* base64String, const char* key);
/**
* Create a sprite instance from base64 encoded image.
* @return Returns an instance of sprite
*/
CC_DLL Sprite* createSpriteFromBase64(const char* base64String);
/**
* Find a child by name recursively

View File

@ -27,6 +27,7 @@ THE SOFTWARE.
#include "base/ccUtils.h"
static const char* CIRCLE_IMAGE = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAQAAADZc7J/AAAA8ElEQVRIx62VyRGCQBBF+6gWRCEmYDIQkhiBCgHhSclC8YqWzOV5oVzKAYZp3r1/9fpbxAIBMTsKrjx5cqVgR0wgLhCRUWOjJiPqD56xoaGPhpRZV/iSEy6crHmw5oIrF9b/lVeMofrJgjlnxlIy/wik+JB+mme8BExbBhm+5CJC2LE2LtSEQoyGWDioBA5CoRIohJtK4CYDxzNEM4GAugR1E9VjVC+SZpXvhCJCrjomESLvc17pDGX7bWmlh6UtpjPVCWy9zaJ0TD7qfm3pwERMz2trRVZk3K3BD/L34AY+dEDCniMVBkPFkT2J/b2/AIV+dRpFLOYoAAAAAElFTkSuQmCC";
static const char* CIRCLE_IMAGE_KEY = "/__circleImage";
NS_CC_BEGIN
@ -64,7 +65,7 @@ PageViewIndicator::~PageViewIndicator()
bool PageViewIndicator::init()
{
_currentIndexNode = utils::createSpriteFromBase64(CIRCLE_IMAGE);
_currentIndexNode = utils::createSpriteFromBase64Cached(CIRCLE_IMAGE, CIRCLE_IMAGE_KEY);
_currentIndexNode->setVisible(false);
addProtectedChild(_currentIndexNode, 1);
return true;

View File

@ -34,6 +34,9 @@ namespace ui {
static const char* HALF_CIRCLE_IMAGE = "iVBORw0KGgoAAAANSUhEUgAAAAwAAAAGCAMAAADAMI+zAAAAJ1BMVEX///////////////////////////////////////////////////9Ruv0SAAAADHRSTlMABgcbbW7Hz9Dz+PmlcJP5AAAAMElEQVR4AUXHwQ2AQAhFwYcLH1H6r1djzDK3ASxUpTBeK/uTCyz7dx54b44m4p5cD1MwAooEJyk3AAAAAElFTkSuQmCC";
static const char* BODY_IMAGE_1_PIXEL_HEIGHT = "iVBORw0KGgoAAAANSUhEUgAAAAwAAAABCAMAAADdNb8LAAAAA1BMVEX///+nxBvIAAAACklEQVR4AWNABgAADQABYc2cpAAAAABJRU5ErkJggg==";
static const char* HALF_CIRCLE_IMAGE_KEY = "/__halfCircleImage";
static const char* BODY_IMAGE_1_PIXEL_HEIGHT_KEY = "/__bodyImage";
static const Color3B DEFAULT_COLOR(52, 65, 87);
static const float DEFAULT_MARGIN = 20;
static const float DEFAULT_AUTO_HIDE_TIME = 0.2f;
@ -82,7 +85,7 @@ bool ScrollViewBar::init()
return false;
}
_upperHalfCircle = utils::createSpriteFromBase64(HALF_CIRCLE_IMAGE);
_upperHalfCircle = utils::createSpriteFromBase64Cached(HALF_CIRCLE_IMAGE, HALF_CIRCLE_IMAGE_KEY);
_upperHalfCircle->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
addProtectedChild(_upperHalfCircle);
@ -91,7 +94,7 @@ bool ScrollViewBar::init()
_lowerHalfCircle->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
addProtectedChild(_lowerHalfCircle);
_body = utils::createSpriteFromBase64(BODY_IMAGE_1_PIXEL_HEIGHT);
_body = utils::createSpriteFromBase64Cached(BODY_IMAGE_1_PIXEL_HEIGHT, BODY_IMAGE_1_PIXEL_HEIGHT_KEY);
_body->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
addProtectedChild(_body);