LoginSignup
19

More than 5 years have passed since last update.

【Cocos2dx】画像の非同期読み込み

Last updated at Posted at 2014-08-13

画像を非同期で読みこんで描画時に固まることを避ける

サンプルで40個の画像を
・キャッシュありで描画
・キャッシュなしで描画
してみる。

ResourceLoadScene.h
#ifndef __Sample__ResourceLoadScene__
#define __Sample__ResourceLoadScene__

#include <iostream>
#include "cocos2d.h"

using namespace cocos2d;

class ResourceLoadScene : public Layer
{
public:
    static Scene* createScene();
    virtual bool init();
    CREATE_FUNC(ResourceLoadScene);
    ResourceLoadScene();
    ~ResourceLoadScene();
private:
    int _loadCount;
    double getSec();
    void loadResource();
    void didLoadResource(Texture2D *texture);
    void removeResourceCache();
    void drawSprite();
};
#endif
ResourceLoadScene.h
#include "ResourceLoadScene.h"
USING_NS_CC;
#define IMAGE_COUNT 40

Scene* ResourceLoadScene::createScene()
{
    auto scene = Scene::create();
    auto layer = ResourceLoadScene::create();
    scene->addChild(layer);
    return scene;
}

ResourceLoadScene::ResourceLoadScene()
{
    this->_loadCount = IMAGE_COUNT;
}

ResourceLoadScene::~ResourceLoadScene()
{
}

bool ResourceLoadScene::init()
{
    if(!Layer::init()){
        return false;
    }
    this->loadResource();
    return true;
}

//=======================================================
/// 秒数取得
//=======================================================
double ResourceLoadScene::getSec(){
    timeval val;
    gettimeofday(&val, nullptr);
    return (val.tv_sec) + (val.tv_usec) * 1e-6;
}

//=======================================================
/// テクスチャ非同期読み込み
//=======================================================
void ResourceLoadScene::loadResource(){
    TextureCache *cache = Director::getInstance()->getTextureCache();
    for(int i = 0; i < IMAGE_COUNT; i++){
        cache->addImageAsync(StringUtils::format("%d.png", i).c_str(), CC_CALLBACK_1(ResourceLoadScene::didLoadResource, this));
    }
}
//=======================================================
/// テクスチャ非同期読み込み完了
//=======================================================
void ResourceLoadScene::didLoadResource(Texture2D *texture){
    this->_loadCount--;
    if(this->_loadCount <= 0){
        // キャッシュから描画
        this->drawSprite();
        // キャッシュ削除
        this->removeAllChildren();
        this->removeResourceCache();
        // 普通に描画
        this->drawSprite();
    }
}
//=======================================================
/// スプライト描画
//=======================================================
void ResourceLoadScene::drawSprite(){
    log("START drawSprite --------");
    double tStart = getSec();
    for(int i = 0; i < IMAGE_COUNT; i++){
        Sprite *sp = Sprite::create(StringUtils::format("%d.png", i).c_str());
        this->addChild(sp);
    }
    double tEnd = getSec();
    log(" END  drawSprite -------- %f", tEnd - tStart);
}
//=======================================================
/// テクスチャのキャッシュ削除
//=======================================================
void ResourceLoadScene::removeResourceCache(){
    TextureCache *cache = Director::getInstance()->getTextureCache();
    for(int i = 0; i < IMAGE_COUNT; i++){
        cache->removeTextureForKey(StringUtils::format("%d.png", i).c_str());
    }
}

結果は

cocos2d: START drawSprite --------
cocos2d: END drawSprite -------- 0.000136
cocos2d: START drawSprite --------
cocos2d: END drawSprite -------- 0.399950

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19