Example #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 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);

}
bool StartLayer::init() {
    if (!Layer::init()) {
        return false;
    }
    Size screenSize = Director::getInstance()->getVisibleSize();

    SCALE_DENSITY = screenSize.width / 640;
    this->setScale(SCALE_DENSITY);
    
    Sprite *bgSprite = Sprite::create("loading.png");
    bgSprite->setPosition(Point(screenSize.width/2, screenSize.height/2));
    this->addChild(bgSprite);

    Sprite* card = Sprite::create();
    Animation* animation = Animation::create();

    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("card.plist");

    int cardheight;
    for(int i=0;i<13;i++){
        char framename[64];
        sprintf(framename, "card%d.png",i+1);

        SpriteFrame *sf = SpriteFrameCache::getInstance()->getSpriteFrameByName(framename);
        Rect rect = sf->getRect();
        cardheight = rect.size.height;
        animation->addSpriteFrame(sf);
    }
    animation->setDelayPerUnit(0.1f);

    Animate* cardAni = Animate::create(animation);
    RepeatForever* rf = RepeatForever::create(cardAni);
    card->setPosition(Point(screenSize.width/2,screenSize.height/2 + cardheight));
    this->addChild(card,1);
    card->runAction(rf);

    
    // リクエスト生成
    HttpRequest* request = new HttpRequest();
    request->setUrl("http://httpbin.org/post");
    request->setRequestType(HttpRequest::Type::POST);
    request->setResponseCallback(this, httpresponse_selector(StartLayer::onHttpRequestCompleted));
    request->setTag("POST test1");

    // POSTデータ生成
    string postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
    request->setRequestData(postData.c_str(), postData.length());

    // リクエスト送信
    HttpClient::getInstance()->send(request);
    request->release();

    return true;
}