Example CCControlSwitchSprite usage?

All,

I’m new to Cocos2dx and am trying to get a toggle switch going using CCControlSwitchSprite. I have searched and Googled but can’t find any examples on how to use it?
Can anyone post a snippet or link to get CCControlSwitchSprite - and come to think of it, the other GUI CCControl* extensions as well!? (a good description of the capabilities of each one would be nice :slight_smile:

Thanks!

You are welcome!

Thanks Stanislav - that is exactly what I was looking for!

I’ve been studying your Cocos2D examples, and have the demos working via XCode and the simulators.

I’m trying to get a CCControlSwitch working in Cocos2D-x using the GUI extension library, within the Marmalade SDK.

I am creating a new instance of CCControlSwitch and then calling create with (in the first instance) the sprites used from your demo.
However, i’m getting an exception in needsLayout() when it gets around to calling visit(); (CCControlSwitch.cpp line 219)

CCRenderTexture *rt = CCRenderTexture::create((int)m_pMaskTexture~~>getContentSize.width, m_pMaskTexture~~>getContentSize().height);

rt~~>begin;
m_pOnSprite~~>visit(); <—— exception here

Delving a bit deeper, in CCNode.cpp, within visit(), its calling this->draw(); (line 849). This is where it is going wrong.
My console has the following message:

cocos2d-x debug info [Assert failed: If CCSprite is being rendered by CCSpriteBatchNode, CCSprite#draw SHOULD NOT be called]

Do you know why this may be?

Looks like you are not able to interfere in render process of CCSprite which child of CCSpriteBatchNode.

My bad, had a copy/paste error to do with my sprite definition.

I have it displaying now, but i’m getting an OpenGL error and the sprites aren’t located correctly.
See the attached screenshot.

I notice that your demo files have shaders included and the Cocos2dx versions don’t? Could the problems I’m seeing be something to do with that?

Nearly there :slight_smile:

The positioning of the sprites being incorrect is because I was loading my sprites via a sprite sheet output from Texture Packer. I changed to use raw .png files and things are in the correct position now. This is a secondary problem that I have yet to look into.

The main problem is with the mask and its associated shader. It’s throwing an exception on line 170 of CCControlSwitch.cpp

glUniform1i(m_uMaskLocation, 1);

The exception is:

28/07/13 14:53:44.038: [0xfa0] DEBUG: s3eDebugAssertShow 0x00000000 type=2 ‘IwAssert failure:
Channel: IW_GL
File: IwGL.cpp
Line: 344
Expression: err == 0
Message: GL Error GL_INVALID_OPERATION in function glUniform1i(1, 1)

I have absolutely no experience with shaders, so would appreciate any help with this one?

EDIT:

In CCControlSwitchSprite::initWithMaskSprite two variables are setup, m_uTextureLocation and m_uMaskLocation (0 & 1 in my case respectively)
However, when these are used in draw() to pass to glUniform1i the locations they refer to seem to be invalid? I checked this by calling:
@
m_uTextureLocation = glGetUniformLocation( getShaderProgram()>getProgram, “u_texture”);
m_uMaskLocation = glGetUniformLocation
>getProgram, “u_mask”);
@
from within draw and both return as1
So, in summary, it’s the mask texture/shader that doesn’t seem to be working properly for me :
(

Ding,I meet the same issue . Waiting somebody correct it.

Tao Yinqing wrote:

Ding,I meet the same issue . Waiting somebody correct it.

Tao, thats good it’s not just me, and it would be nice if somebody with more knowledge of shaders could take a look (Stanislav?), but I think we might have to fix this ourselves :frowning:

OK, fixed it.

Turns out that during the draw, the shader program didn’t exist anymore (it was being destroyed after it was initialised?)
Anyhow, the attached updated file seems to work, hopefully this will help someone, is there a way of getting changes like this updated into the mainline?

Darren Yeates wrote:

OK, fixed it.
>
Turns out that during the draw, the shader program didn’t exist anymore (it was being destroyed after it was initialised?)
Anyhow, the attached updated file seems to work, hopefully this will help someone, is there a way of getting changes like this updated into the mainline?

year It change the shader program when reset the texture in ccsprite.

@
void CCControlSwitchSprite::needsLayout()
{
m_pOnSprite~~>setPosition.width / 2 + m_fSliderXPosition,
m_pOnSprite~~>getContentSize().height / 2));
m_pOffSprite~~>setPosition.width + m_pOffSprite~~>getContentSize().width / 2 + m_fSliderXPosition,
m_pOffSprite~~>getContentSize.height / 2));
m_ThumbSprite~~>setPosition(ccp(m_pOnSprite~~>getContentSize.width + m_fSliderXPosition,
m_pMaskTexture~~>getContentSize().height / 2));

if (m_pOnLabel)
{
m_pOnLabel~~>setPosition.x~~ m_ThumbSprite~~>getContentSize.width / 6,
m_pOnSprite~~>getContentSize().height / 2));
}
if (m_pOffLabel)
{
m_pOffLabel~~>setPosition.x + m_ThumbSprite~~>getContentSize().width / 6,
m_pOffSprite~~>getContentSize.height / 2));
}
CCRenderTexture *rt = CCRenderTexture::createm_pMaskTexture~~>getContentSize().width, (int)m_pMaskTexture~~>getContentSize.height);
rt~~>begin();
m_pOnSprite~~>visit;
m_pOffSprite~~>visit();

if (m_pOnLabel)
{
m_pOnLabel~~>visit;
}
if
{
m_pOffLabel~~>visit();
}

rt->end();

setTexture(rt->getSprite()->getTexture());
setFlipY(true);
}
@

and the setTexture do like this :
@
void CCSprite::setTexture(CCTexture2D *texture)
{
// If batchnode, then texture id should be the same
CCAssert(! m_pobBatchNode || texture->getName() m_pobBatchNode->getTexture()->getName(), “CCSprite: Batched sprites should use the same texture as the batchnode”);
// accept texturenil as argument
CCAssert( !texture || dynamic_cast<CCTexture2D*>(texture), “setTexture expects a CCTexture2D. Invalid argument”);

// shader program
if (texture)
{
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
}
else
{
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
}

if (= texture)
{
CC_SAFE_RETAIN(texture);
CC_SAFE_RELEASE(m_pobTexture);
m_pobTexture = texture;
updateBlendFunc();
}
}
@