Esempio n. 1
0
void LightEffect::prepareForRender(Sprite *sprite, Texture2D *normalmap)
{
    auto gl = getGLProgramState();

    gl->setUniformVec2("u_contentSize", sprite->getContentSize());

    Point posRelToSprite = PointApplyAffineTransform(Point(_lightPos.x, _lightPos.y), sprite->getWorldToNodeAffineTransform());
    gl->setUniformVec3("u_lightPos", Vec3(posRelToSprite.x, posRelToSprite.y, _lightPos.z));

    gl->setUniformTexture("u_normals", normalmap);

	int opacity = sprite->getOpacity();
    SpriteFrame *frame = sprite->getSpriteFrame();
    Size untrimmedSize = frame->getOriginalSize();
    Size trimmedSize = frame->getRect().size;
    Vec2 framePos = frame->getRect().origin;
    Size texSize = frame->getTexture()->getContentSize();
    
    // set sprite position in sheet
    gl->setUniformVec2("u_spritePosInSheet", Vec2(framePos.x / texSize.width, framePos.y / texSize.height));
    gl->setUniformVec2("u_spriteSizeRelToSheet", Vec2(untrimmedSize.width / texSize.width, untrimmedSize.height / texSize.height));
    gl->setUniformInt("u_spriteRotated", frame->isRotated());
	gl->setUniformFloat("u_spriteOpacity", (float)opacity / 255.0f);
    
    // set offset of trimmed sprite
    Vec2 bottomLeft = frame->getOffset() + (untrimmedSize - trimmedSize) / 2;
    Vec2 cornerOffset = frame->isRotated() ? Vec2(bottomLeft.y, bottomLeft.x)
                                           : Vec2(bottomLeft.x, untrimmedSize.height - trimmedSize.height - bottomLeft.y);
    gl->setUniformVec2("u_spriteOffset", cornerOffset);

}
void SpriteFrameCache::removeUnusedSpriteFrames()
{
    bool removed = false;
    std::vector<std::string> toRemoveFrames;
    
    for (auto iter = _spriteFrames.begin(); iter != _spriteFrames.end(); ++iter)
    {
        SpriteFrame* spriteFrame = iter->second;
        if( spriteFrame->getReferenceCount() == 1 )
        {
            toRemoveFrames.push_back(iter->first);
            spriteFrame->getTexture()->removeSpriteFrameCapInset(spriteFrame);
            CCLOG("cocos2d: SpriteFrameCache: removing unused frame: %s", iter->first.c_str());
            removed = true;
        }
    }

    _spriteFrames.erase(toRemoveFrames);

    // FIXME:. Since we don't know the .plist file that originated the frame, we must remove all .plist from the cache
    if( removed )
    {
        _loadedFileNames->clear();
    }
}
void LightsHolder::prepareForRender(Sprite *_sprite, Texture2D *normalmap)
{
    int idx=0;
    for(auto &ef : _effectsArray) {
     
        if (ef != nullptr)
        {
            Point posRelToSprite = PointApplyAffineTransform(Point(ef->getLightPos().x, ef->getLightPos().y),_sprite->getWorldToNodeAffineTransform());
            _lightPos[idx]=Vec3(posRelToSprite.x, posRelToSprite.y, ef->getLightPos().z);
            brightness[idx]=ef->getBrightness();
    
        }
        idx++;
    }
    
    auto gl = getGLProgramState();

    gl->setUniformVec2("u_contentSize", _sprite->getContentSize());


 

    gl->setUniformVec3v("u_lightPos",_lightsNumber,_lightPos);

    gl->setUniformTexture("u_normals", normalmap);

    SpriteFrame *frame = _sprite->getSpriteFrame();
    Size untrimmedSize = frame->getOriginalSize();
    Size trimmedSize = frame->getRect().size;
    Vec2 framePos = frame->getRect().origin;
    Size texSize = frame->getTexture()->getContentSize();
    
    // set sprite position in sheet
    gl->setUniformVec2("u_spritePosInSheet", Vec2(framePos.x / texSize.width, framePos.y / texSize.height));
    gl->setUniformVec2("u_spriteSizeRelToSheet", Vec2(untrimmedSize.width / texSize.width, untrimmedSize.height / texSize.height));
    
    gl->setUniformFloatv("u_brightness",_lightsNumber, brightness);
    
    gl->setUniformInt("u_spriteRotated", frame->isRotated());
    gl->setUniformInt("u_lightsNumber", _lightsNumber);
    
    // set offset of trimmed sprite
    Vec2 bottomLeft = frame->getOffset() + (untrimmedSize - trimmedSize) / 2;
    Vec2 cornerOffset = frame->isRotated() ? Vec2(bottomLeft.y, bottomLeft.x)
                                           : Vec2(bottomLeft.x, untrimmedSize.height - trimmedSize.height - bottomLeft.y);
    gl->setUniformVec2("u_spriteOffset", cornerOffset);

}
Esempio n. 4
0
void SpriteFrameCache::removeSpriteFramesFromTexture(Texture2D* texture)
{
    std::vector<std::string> keysToRemove;

    for (auto iter = _spriteFrames.cbegin(); iter != _spriteFrames.cend(); ++iter)
    {
        std::string key = iter->first;
        SpriteFrame* frame = _spriteFrames.at(key);
        if (frame && (frame->getTexture() == texture))
        {
            keysToRemove.push_back(key);
        }
    }

    _spriteFrames.erase(keysToRemove);
}
Esempio n. 5
0
void SpriteFrameCachePixelFormatTest::loadSpriteFrames(const std::string &file, cocos2d::Texture2D::PixelFormat expectedFormat)
{
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file);
    SpriteFrame *spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName("grossini.png");
    Texture2D *texture = spriteFrame->getTexture();
    const ssize_t bitsPerKB = 8 * 1024;
    const double memorySize = 1.0 * texture->getBitsPerPixelForFormat() * texture->getContentSizeInPixels().width * texture->getContentSizeInPixels().height / bitsPerKB;
    //FIXME: texture->getPixelFormat() != expectedFormat all the time, such as Texture2D::PixelFormat::PVRTC2A
//    CC_ASSERT(texture->getPixelFormat() == expectedFormat);
    
    const std::string textureInfo = StringUtils::format("%s: %.2f KB\r\n", texture->getStringForFormat(), memorySize);
    infoLabel->setString(infoLabel->getString() + textureInfo);
    
    SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(file);
    Director::getInstance()->getTextureCache()->removeTexture(texture);
}
Texture2D * ParticleSystemQuadLoader::parsePropTypeTexture(Node * pNode, Node * pParent, CCBReader * ccbReader) {
        
    std::string spriteFile = ccbReader->readCachedString();
    memset(&_textureRect, 0, sizeof(_textureRect));
        
    if (spriteFile.length() > 0)
    {
        Texture2D* texture = Director::getInstance()->getTextureCache()->addImage(ccbReader->getCCBRootPath() + spriteFile.c_str());
        if (texture == NULL) {
                
            SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFile);
            texture = spriteFrame->getTexture();
            _textureRect.origin = spriteFrame->getOffsetInPixels();
            _textureRect.size = spriteFrame->getOriginalSizeInPixels();
        }
            
        return  texture;
    }
    else
    {
        return NULL;
    }
}
Esempio n. 7
0
void Block::initBlock() {

    // 빌딩과 루프에 대한 스프라이트 프레임 레퍼런스 보관
    _tile1 = CCSpriteFrameCache::getInstance()->getSpriteFrameByName ("building_1.png");
    _tile2 = CCSpriteFrameCache::getInstance()->getSpriteFrameByName ("building_2.png");
    _tile3 = CCSpriteFrameCache::getInstance()->getSpriteFrameByName ("building_3.png");
    _tile4 = CCSpriteFrameCache::getInstance()->getSpriteFrameByName ("building_4.png");
    _tile1->getTexture()->setAliasTexParameters();
    _tile2->getTexture()->setAliasTexParameters();
    _tile3->getTexture()->setAliasTexParameters();
    _tile4->getTexture()->setAliasTexParameters();
    
    _roof1 = CCSpriteFrameCache::getInstance()->getSpriteFrameByName ("roof_1.png");
    _roof2 = CCSpriteFrameCache::getInstance()->getSpriteFrameByName ("roof_2.png");
    _roof1->getTexture()->setAliasTexParameters();
    _roof2->getTexture()->setAliasTexParameters();
    

    //create tiles
    int i;
    
    // 벽은 20개 보관
    _wallTiles.reserve(20);
    //= CCArray::createWithCapacity(20);
//    _wallTiles.retain();

    // 지붕은 5개 보관
    _roofTiles.reserve(5);
  //  _roofTiles = CCArray::createWithCapacity(5);
  //  _roofTiles->retain();

    
    Sprite * tile;
    
    // 미리 roof_1 스프라이트를 5장 준비하여 보관, x는 5장 옆으로 y는 0, 크기는 138, 즉 -138까지 지붕
    // 미리 building_1 스프라이트를 20장 준비하여 보관, x는 5장 옆으로 y는 47, -120정도 부터 배치
    // 즉 눈에 보이지 않는 화면 아래에 5장 정도로 깔아둔다.
    for (i = 0; i < 5; i++) {
        tile = CCSprite::createWithSpriteFrameName("roof_1.png");
        tile->getTexture()->setAliasTexParameters();
        tile->setAnchorPoint(Point(0, 1));
        tile->setPosition(Point(i * _tileWidth, 0));
        tile->setVisible(false);
        this->addChild(tile, kMiddleground, kRoofTile);
        _roofTiles.pushBack(tile);
 //       _roofTiles->addObject(tile);
        
        for (int j = 0; j < 4; j++) {
            tile = Sprite::createWithSpriteFrameName("building_1.png");
            tile->getTexture()->setAliasTexParameters();
            tile->setAnchorPoint(Point(0, 1));
            tile->setPosition(Point(i * _tileWidth, -1 * (_tileHeight * 0.47f + j * _tileHeight)));
            tile->setVisible(false);
            this->addChild(tile, kBackground, kWallTile);
            _wallTiles.pushBack(tile);
 //           _wallTiles->addObject(tile);
        }
        
    }
    
    _chimneys.reserve(5);
//    _chimneys = CCArray::createWithCapacity(5);
//    _chimneys->retain();
    
    
    Sprite * chimney;
    Sprite * puff;
    
    // 타일을 옆으로 5장이니 굴뚝도 5장인가?
    for (i = 0; i < 5; i++) {
        chimney = Sprite::createWithSpriteFrameName("chimney.png");
        chimney->getTexture()->setAliasTexParameters();
        chimney->setVisible(false);
        this->addChild(chimney, kForeground, kChimney);
 
        _chimneys.pushBack(chimney);
//        _chimneys->addObject(chimney);
        
        for (int j = 0; j < TOTAL_PUFFS; j++) {
            puff = Sprite::createWithSpriteFrameName("puff_1.png");
            puff->getTexture()->setAliasTexParameters();
            puff->setAnchorPoint(Point(0,-0.5));
            puff->setVisible(false);
            chimney->addChild(puff, -1, j);
        }
        
    }
    
    
    Animation* animation;
    animation = Animation::create();
    SpriteFrame * frame;

    for(i = 1; i <= 4; i++) {
        char szName[100] = {0};
        sprintf(szName, "puff_%i.png", i);
        frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(szName);
        frame->getTexture()->setAliasTexParameters();
        animation->addSpriteFrame(frame);
    }

    animation->setDelayPerUnit(0.75f / 4.0f);
    animation->setRestoreOriginalFrame(false);
    animation->setLoops(-1);
    _puffAnimation = CCAnimate::create(animation);
    _puffAnimation->retain();

    // docgkill
    _puffSpawn = Repeat::create(Sequence::create(DelayTime::create(0.5f),
                                                 CallFuncN::create( CC_CALLBACK_0(Block::createPuff, this) ),
                                                 NULL
                                                 ),
                                TOTAL_PUFFS
                                );
    _puffSpawn->retain();
    
 //   this->runAction(_puffSpawn->clone());

    _puffMove = MoveBy::create(1.0f, Point(-100,80));
    _puffMove->retain();
    _puffFade = CCFadeOut::create(2.0f);
    _puffFade->retain();
    _puffScale = CCScaleBy::create(1.5f, 1.5);
    _puffScale->retain();
    
    _puffIndex = 0;
}
Esempio n. 8
0
Node * MapObj::use(Vec2 index,MapInfo * info){
    if (_is_using) {
        return NULL;
    }
    _is_using = true;
    _index = index;
    
    int _tmp_x = _index.x;
    int _tmp_y = info->getTileCountY() - 1 - _index.y;
    
    int _tmp_x_d = _tmp_x / info->getTilePerArea();
    int _tmp_y_d = _tmp_y / info->getTilePerArea();
    
    int area = _tmp_x_d + _tmp_y_d * info->getTileAreaCount() +1 ; // 地域
    
    _tmp_x = _tmp_x - _tmp_x_d * info->getTilePerArea();
    _tmp_y = _tmp_y - _tmp_y_d * info->getTilePerArea();
    
    _tmp_x_d = _tmp_x / info->getTilePerFeild();
    _tmp_y_d = _tmp_y / info->getTilePerFeild();
    
    int feild = _tmp_x_d + _tmp_y_d * info->getTileFeildCount() + 1; // 场景
    
    _tmp_x = _tmp_x - _tmp_x_d * info->getTilePerFeild();
    _tmp_y = _tmp_y - _tmp_y_d * info->getTilePerFeild();
    
    int map_index = _tmp_x + _tmp_y * info->getTileIndexCount() + 1; // 索引
    
    // 生成大图索引
    memset(_filename, 0, FILE_NAME_SIZE);
    sprintf(_filename, "map_%d_%d.plist",area,feild);
    _batchnode_name = _filename;
    // 生成小图文件名
    memset(_filename, 0, FILE_NAME_SIZE);
    sprintf(_filename, "map_%02d_%02d_%02d.png",area,feild,map_index);
    SpriteFrame *frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(_filename);
    if (!frame) {
        cout << "area : " <<area << " : " << feild << " : " << map_index << endl;
    }
    // 如果已经创建的sp则直接设置frame 否则创建一个新的sp
    if(_sp){
        std::string f = _filename;
        
        if (frame->getTexture()->getName() != _sp->getTexture()->getName()) {
            // 如果不是同一个大图的,那就这能重新创建了 
            _sp->removeFromParentAndCleanup(true);
            _sp = Sprite::createWithSpriteFrame(frame);
            _sp->setAnchorPoint(Vec2::ZERO);
        }
        else{
            _sp->setSpriteFrame(frame);
        }
    }
    else{
        _sp = Sprite::createWithSpriteFrame(frame);
        _sp->setAnchorPoint(Vec2::ZERO);
    }
    
    _sp->setPosition(Vec2(index.x * info->getTileWidth(), index.y * info->getTileHeight()));
    return _sp;
}