加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx源码:背景层封装类

发布时间:2020-12-14 17:09:52 所属栏目:百科 来源:网络整理
导读:头文件BackgroundLayer.h #ifndef __BBACKGROUNDLAYER_H__ # define __BBACKGROUNDLAYER_H__ #include "cocos2d.h" NS_CC_BEGINclass BackgroundLayer : public LayerColor{ protected : GLuint m_wrapS; GLuint m_wrapT; public : static BackgroundLayer*

头文件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 wrapT = 0);

    virtual bool init(const char* pszFileName,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 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 wrapT)
{
    BackgroundLayer* pobLayer = new BackgroundLayer();
    Size winSize = Director::getInstance()->getWinSize();
    if (pobLayer && pobLayer->init(pszFileName,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,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,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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读