cocos2dx原始碼:背景層封裝類

峻峰飛陽發表於2016-08-03

標頭檔案BackgroundLayer.h

#ifndef __BBACKGROUNDLAYER_H__
#define __BBACKGROUNDLAYER_H__
#include "cocos2d.h"

NS_CC_BEGIN

class BackgroundLayer : public LayerColor
{
protected:
    GLuint m_wrapS;
    GLuint m_wrapT;

public:
    static BackgroundLayer* create(const Color4B& color);
    static BackgroundLayer* create(const char* pszFileName, Size &winSize, GLuint wrapS = 0, GLuint wrapT = 0);
    static BackgroundLayer* create(const char* pszFileName, GLuint wrapS = 0, GLuint wrapT = 0);

    virtual bool init(const char* pszFileName, Size &winSize, GLuint wrapS, GLuint wrapT);
};

NS_CC_END
#endif

原始檔BackgroundLayer.cpp

#include "BackgroundLayer.h"
USING_NS_CC;

BackgroundLayer* BackgroundLayer::create(const char* pszFileName, Size &winSize, GLuint wrapS, GLuint wrapT)
{
    BackgroundLayer* pobLayer = new BackgroundLayer();
    if (pobLayer && pobLayer->init(pszFileName, winSize, wrapS, wrapT))
    {
        pobLayer->autorelease();
        return pobLayer;
    }
    else
    {
        CC_SAFE_DELETE(pobLayer);
        return NULL;
    }
}

BackgroundLayer* BackgroundLayer::create(const char* pszFileName, GLuint wrapS, GLuint wrapT)
{
    BackgroundLayer* pobLayer = new BackgroundLayer();
    Size winSize = Director::getInstance()->getWinSize();
    if (pobLayer && pobLayer->init(pszFileName, winSize, wrapS, wrapT))
    {
        pobLayer->autorelease();
        return pobLayer;
    }
    else
    {
        CC_SAFE_DELETE(pobLayer);
        return NULL;
    }
}

BackgroundLayer* BackgroundLayer::create(const Color4B& color)
{
    BackgroundLayer* pobLayer = new BackgroundLayer();
    if (pobLayer && pobLayer->initWithColor(color))
    {
        pobLayer->autorelease();
        return pobLayer;
    }
    else
    {
        CC_SAFE_DELETE(pobLayer);
        return NULL;
    }
}

bool BackgroundLayer::init(const char* pszFileName, Size &winSize, GLuint wrapS, GLuint wrapT)
{
    if (LayerColor::init())
    {
        if (!wrapS && !wrapT)
        {
            Sprite* bgImage = Sprite::create(pszFileName);
            if (bgImage)
            {
                Size bgImageSize = bgImage->getContentSize();
                bgImage->setScale(winSize.width / bgImageSize.width, winSize.height / bgImageSize.height);
                this->addChild(bgImage);
                bgImage->setPosition(Point(winSize.width/2, winSize.height/2));
                this->setContentSize(winSize);
                return true;
            }
        }
        else
        {
            Rect winRect(0, 0, winSize.width, winSize.height);
            Sprite* bgImage = Sprite::create(pszFileName, winRect);
            if (bgImage)
            {
                Texture2D::TexParams tp = {GL_LINEAR, GL_LINEAR, wrapS, wrapT};
                bgImage->getTexture()->setTexParameters(tp);
                Size bgImageSize = bgImage->getContentSize();
                this->addChild(bgImage);
                bgImage->setPosition(Point(winSize.width/2, winSize.height/2));
                this->setContentSize(winSize);
                return true;
            }
        }
    }
    return false;
}

相關文章