Skip to content

Instantly share code, notes, and snippets.

@tks2shimizu
Last active December 11, 2015 01:29
Show Gist options
  • Save tks2shimizu/4524231 to your computer and use it in GitHub Desktop.
Save tks2shimizu/4524231 to your computer and use it in GitHub Desktop.
100匹のネコ (4)Texture Atlas画像の非同期読込み
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !CCLayer::init() )
{
return false;
}
srand((unsigned)time(NULL));
//usingSprite();
//usingSpriteFrameCache();
//usingTextureCache();
usingTextureCache2();
return true;
}
void HelloWorld::usingSprite()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
start = clock();
for (int i = 0; i < 100; i++) {
int random = rand();
// ふつうの表示
CCString* fileName = CCString::createWithFormat("cat%02d.png", i);
CCSprite* cat = CCSprite::create(fileName->getCString());
cat->setPosition(ccp(random % (int)winSize.width,
random % (int)winSize.height));
this->addChild(cat);
}
CCLog("%f", (double)(clock() - start) / CLOCKS_PER_SEC);
}
void HelloWorld::usingSpriteFrameCache()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
start = clock();
// キャッシュへ読込み
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("cats1.plist");
cache->addSpriteFramesWithFile("cats2.plist");
for (int i = 0; i < 100; i++) {
int random = rand();
// キャッシュから表示
CCString* fileName = CCString::createWithFormat("cat%02d.png", i);
CCSprite* cat = CCSprite::createWithSpriteFrameName(fileName->getCString());
cat->setPosition(ccp(random % (int)winSize.width,
random % (int)winSize.height));
this->addChild(cat);
}
CCLog("%f", (double)(clock() - start) / CLOCKS_PER_SEC);
}
void HelloWorld::usingTextureCache()
{
start = clock();
for (int i = 0; i < 100; i++)
{
CCString* fileName = CCString::createWithFormat("cat%02d.png", i);
CCTextureCache* cache = CCTextureCache::sharedTextureCache();
//キャッシュへ非同期で読込み
cache->addImageAsync(fileName->getCString(), this, callfuncO_selector(HelloWorld::loadedImage));
}
CCLog("%f", (double)(clock() - start) / CLOCKS_PER_SEC);
}
void HelloWorld::loadedImage(cocos2d::CCObject* object)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
//キャッシュから表示
CCTexture2D* texture = (CCTexture2D*)object;
int random = rand();
CCSprite* cat = CCSprite::createWithTexture(texture);
cat->setPosition(ccp(random % (int)winSize.width, random % (int)winSize.height));
this->addChild(cat);
static int i = 0;
if (++i == 100)
{
CCLog("%f", (double)(clock() - start) / CLOCKS_PER_SEC);
}
}
void HelloWorld::usingTextureCache2()
{
start = clock();
for (int i = 1; i <= 2; i++)
{
CCString* fileName = CCString::createWithFormat("cats%d.png", i);
CCTextureCache* cache = CCTextureCache::sharedTextureCache();
//キャッシュへ非同期で読込み
cache->addImageAsync(fileName->getCString(), this, callfuncO_selector(HelloWorld::loadedImage2));
}
CCLog("%f", (double)(clock() - start) / CLOCKS_PER_SEC);
}
void HelloWorld::loadedImage2(cocos2d::CCObject* object)
{
static int i = 0;
// 危険なコードなので絶対にマネしないでください
CCString* fileName = CCString::createWithFormat("cats%d.plist", i + 1);
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile(fileName->getCString(), (CCTexture2D*)object);
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
for (int j = 0; j < 50; j++)
{
int random = rand();
//キャッシュから表示
CCString* fileName = CCString::createWithFormat("cat%02d.png", j + i * 50);
CCSprite* cat = CCSprite::createWithSpriteFrameName(fileName->getCString());
cat->setPosition(ccp(random % (int)winSize.width,
random % (int)winSize.height));
this->addChild(cat);
}
if (++i == 2)
{
CCLog("%f", (double)(clock() - start) / CLOCKS_PER_SEC);
}
}
//#ifndef __HELLOWORLD_SCENE_H__
//#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
private:
clock_t start;
void usingSprite();
void usingSpriteFrameCache();
void usingTextureCache();
void loadedImage(cocos2d::CCObject* object);
void usingTextureCache2();
void loadedImage2(cocos2d::CCObject* object);
public:
virtual bool init();
static cocos2d::CCScene* scene();
CREATE_FUNC(HelloWorld);
};
//#endif // __HELLOWORLD_SCENE_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment