cocos2dx源码:背景层封装类

前端之家收集整理的这篇文章主要介绍了cocos2dx源码:背景层封装类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件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;
}
原文链接:https://www.f2er.com/cocos2dx/339334.html

猜你在找的Cocos2d-x相关文章