Tidy, update programState texture at attachProgramState

This commit is contained in:
halx99 2020-09-09 15:29:56 +08:00
parent 6c2939d235
commit df8631582b
8 changed files with 88 additions and 69 deletions

View File

@ -42,38 +42,6 @@ NS_CC_BEGIN
// AtlasNode - Creation & Init
AtlasNode::AtlasNode()
{
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
attachProgramState(new (std::nothrow) backend::ProgramState(program));
pipelineDescriptor.programState = _programState;
_mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture");
auto vertexLayout = _programState->getVertexLayout();
//a_position
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
_programState->getAttributeLocation(backend::Attribute::POSITION),
backend::VertexFormat::FLOAT3,
0,
false);
//a_texCoord
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD,
_programState->getAttributeLocation(backend::Attribute::TEXCOORD),
backend::VertexFormat::FLOAT2, offsetof(V3F_C4B_T2F, texCoords),
false);
//a_color
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR,
_programState->getAttributeLocation(backend::Attribute::COLOR),
backend::VertexFormat::UBYTE4, offsetof(V3F_C4B_T2F, colors),
true);
vertexLayout->setLayout(sizeof(V3F_C4B_T2F));
}
AtlasNode::~AtlasNode()
{
CC_SAFE_RELEASE(_textureAtlas);
@ -117,8 +85,9 @@ bool AtlasNode::initWithTexture(Texture2D* texture, int tileWidth, int tileHeigh
}
_textureAtlas->initWithTexture(texture, itemsToRender);
updateProgramStateTexture(texture);
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, texture);
this->updateBlendFunc();
this->updateOpacityModifyRGB();
@ -129,6 +98,41 @@ bool AtlasNode::initWithTexture(Texture2D* texture, int tileWidth, int tileHeigh
return true;
}
bool AtlasNode::attachProgramState(backend::ProgramState* programState)
{
if (Node::attachProgramState(programState))
{
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
pipelineDescriptor.programState = _programState;
_mvpMatrixLocation = _programState->getUniformLocation("u_MVPMatrix");
auto vertexLayout = _programState->getVertexLayout();
//a_position
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
_programState->getAttributeLocation(backend::Attribute::POSITION),
backend::VertexFormat::FLOAT3,
0,
false);
//a_texCoord
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD,
_programState->getAttributeLocation(backend::Attribute::TEXCOORD),
backend::VertexFormat::FLOAT2, offsetof(V3F_C4B_T2F, texCoords),
false);
//a_color
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR,
_programState->getAttributeLocation(backend::Attribute::COLOR),
backend::VertexFormat::UBYTE4, offsetof(V3F_C4B_T2F, colors),
true);
vertexLayout->setLayout(sizeof(V3F_C4B_T2F));
updateProgramStateTexture(_textureAtlas->getTexture());
return true;
}
return false;
}
// AtlasNode - Atlas generation

View File

@ -99,8 +99,10 @@ public:
void setQuadsToDraw(ssize_t quadsToDraw);
size_t getQuadsToDraw() const;
bool attachProgramState(backend::ProgramState* programState) override;
CC_CONSTRUCTOR_ACCESS:
AtlasNode();
AtlasNode() = default;
virtual ~AtlasNode();
/** Initializes an AtlasNode with an Atlas file the width and height of each item and the quantity of items to render*/
@ -141,7 +143,6 @@ protected:
bool _ignoreContentScaleFactor = false;
QuadCommand _quadCommand;
backend::UniformLocation _textureLocation;
backend::UniformLocation _mvpMatrixLocation;
private:

View File

@ -40,20 +40,7 @@ NS_CC_BEGIN
//CCLabelAtlas - Creation & Init
LabelAtlas* LabelAtlas::create()
{
LabelAtlas* ret = new (std::nothrow) LabelAtlas();
if (ret)
{
ret->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(ret);
}
return ret;
}
LabelAtlas* LabelAtlas::create(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
{
@ -102,6 +89,24 @@ LabelAtlas* LabelAtlas::create(const std::string& string, const std::string& fnt
return ret;
}
LabelAtlas* LabelAtlas::create(const std::string& string, Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{
LabelAtlas* ret = new (std::nothrow) LabelAtlas();
if (ret)
{
if (ret->initWithString(string, texture, itemWidth, itemHeight, startCharMap))
{
ret->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(ret);
}
}
return ret;
}
bool LabelAtlas::initWithString(const std::string& theString, const std::string& fntFile)
{
std::string pathStr = FileUtils::getInstance()->fullPathForFilename(fntFile);

View File

@ -56,12 +56,6 @@ NS_CC_BEGIN
class CC_DLL LabelAtlas : public AtlasNode, public LabelProtocol
{
public:
/**
* Creates an empty LabelAtlas.
* User need to call initWithString(...) later to make this object work properly.
*/
static LabelAtlas* create();
/** Creates the LabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas. */
static LabelAtlas* create(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
@ -71,6 +65,11 @@ public:
*/
static LabelAtlas* create(const std::string& string, const std::string& fntFile);
/**
* Creates the LabelAtlas with a string, a texture, the width and height of each element and the starting char of the atlas.
*/
static LabelAtlas* create(const std::string& string, Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
/** Initializes the LabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas. */
bool initWithString(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);

View File

@ -281,8 +281,7 @@ MenuItemAtlasFont * MenuItemAtlasFont::create(const std::string& value, const st
bool MenuItemAtlasFont::initWithString(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback)
{
CCASSERT( value.size() != 0, "value length must be greater than 0");
LabelAtlas *label = LabelAtlas::create();
label->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap);
LabelAtlas *label = LabelAtlas::create(value, charMapFile, itemWidth, itemHeight, startCharMap);
if (MenuItemLabel::initWithLabel(label, callback))
{
// do something ?

View File

@ -211,9 +211,6 @@ void MotionStreak::setTexture(Texture2D *texture)
_texture = texture;
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, _texture);
if (_texture)
_programState->setTexture(_texture->getBackendTexture());
}
}
@ -245,6 +242,8 @@ bool MotionStreak::attachProgramState(backend::ProgramState* programState)
vertexLayout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4, 4 * sizeof(float), true);
}
vertexLayout->setLayout(4 * sizeof(float) + 4 * sizeof(uint8_t));
updateProgramStateTexture(_texture);
return true;
}
return false;

View File

@ -103,7 +103,6 @@ bool SpriteBatchNode::initWithTexture(Texture2D *tex, ssize_t capacity/* = DEFAU
_textureAtlas->initWithTexture(tex, capacity);
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, tex);
updateProgramStateTexture(_textureAtlas->getTexture());
updateBlendFunc();
@ -156,6 +155,7 @@ bool SpriteBatchNode::attachProgramState(backend::ProgramState *programState)
pipelineDescriptor.programState = _programState;
setVertexLayout();
updateProgramStateTexture(_textureAtlas->getTexture());
setUniformLocation();
return true;
}
@ -697,7 +697,6 @@ void SpriteBatchNode::setTexture(Texture2D *texture)
{
_textureAtlas->setTexture(texture);
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, texture);
updateProgramStateTexture(texture);
updateBlendFunc();
}

View File

@ -1228,6 +1228,22 @@ void Director::getFPSImageData(unsigned char** datapointer, ssize_t* length)
*length = cc_fps_images_len();
}
template <typename T, typename F, typename... Ts>
static T* newInstance2(std::function<bool(T*, Ts&&... args)> pFunc, Ts&&... args)
{
T* pRet = new(std::nothrow) T();
if (pRet && std::mem_fn(memf)(pRet, std::forward<Ts>(args)...))
{
return pRet;
}
else
{
delete pRet;
pRet = nullptr;
return nullptr;
}
}
void Director::createStatsLabel()
{
Texture2D *texture = nullptr;
@ -1275,22 +1291,19 @@ void Director::createStatsLabel()
*/
float scaleFactor = 1 / CC_CONTENT_SCALE_FACTOR();
_FPSLabel = LabelAtlas::create();
_FPSLabel = LabelAtlas::create(fpsString, texture, 12, 32, '.');
_FPSLabel->retain();
_FPSLabel->setIgnoreContentScaleFactor(true);
_FPSLabel->initWithString(fpsString, texture, 12, 32 , '.');
_FPSLabel->setScale(scaleFactor);
_drawnBatchesLabel = LabelAtlas::create();
_drawnBatchesLabel = LabelAtlas::create(drawBatchString, texture, 12, 32, '.');
_drawnBatchesLabel->retain();
_drawnBatchesLabel->setIgnoreContentScaleFactor(true);
_drawnBatchesLabel->initWithString(drawBatchString, texture, 12, 32, '.');
_drawnBatchesLabel->setScale(scaleFactor);
_drawnVerticesLabel = LabelAtlas::create();
_drawnVerticesLabel = LabelAtlas::create(drawVerticesString, texture, 12, 32, '.');
_drawnVerticesLabel->retain();
_drawnVerticesLabel->setIgnoreContentScaleFactor(true);
_drawnVerticesLabel->initWithString(drawVerticesString, texture, 12, 32, '.');
_drawnVerticesLabel->setScale(scaleFactor);