Repeat sprite's texture to fill whole rect

Hi, i need some solution to one thing. Lets say i have an box2d object with 90*90 dimensions and i have a picture “pic.png”, that is of 30*30 dimensions. I initialize it this way:

CCSprite *sprite = CCSprite::spriteWithFile("pic.png", CCRectMake(0, 0, 90, 90));

And of course that picture doesn’t fill the whole rect. So, do you have any thought/if it’s possible to repeat that texture to fill whole rect? Thank you.

could you use sprite.setScale()?

I do it by settings the sprite’s texture parameters to repeat like this:

@
CCTexture2D texture = CCTextureCache::sharedTextureCache~~>addImage;
ccTexParams params = ;
texture~~>setTexParameters;
@
Then use spriteWithTexture instead of spriteWithFile like this:
@
CCSprite
sprite = CCSprite::spriteWithTexture(texture, CCRectMake(0, 0, 90, 90));
@

Hope that helps!

  • Rob

If I’m not mistaken, GL_REPEAT requires that the texture size be a power of 2.

Rob Merrell, thank you, that’s exactly what i was looking for. Works like a charm!

Alex Zhd, just realized that you were also right, GL_REPEAT repeats nicely only those texture, which has dimensions of 16x16, 32x32, 64x64 etc. Is there any way to repeat nicely e.g. 100x10 texture?

As far as I can tell there is no way to repeat a NPOT texture, it is an OpenGL ES constraint. If you have a repeatable texture it should be easy to stitch your 100x10 image into a power of two image using photoshop then use that image for repeating.

Thanks, again.

Žygis Buzzy wrote:

Hi, i need some solution to one thing. Lets say i have an box2d object with 90*90 dimensions and i have a picture “pic.png”, that is of 30*30 dimensions. I initialize it this way:
>
CCSprite *sprite = CCSprite::spriteWithFile("pic.png", CCRectMake(0, 0, 90, 90));
>
And of course that picture doesn’t fill the whole rect. So, do you have any thought/if it’s possible to repeat that texture to fill whole rect? Thank you.

Thanks! I solved my problem.

I ran across this pesky GL_REPEAT problem in my Cocos2d-iPhone project and made a little handy generalized tiling class that should be no trouble to port to Cocos2d-x:

http://www.nolithius.com/game-development/cocos2d-iphone-repeating-sprite

Jump straight to the source code here:

Let me know if this helps and/or if you end up porting it over to Cocos2d-x!

1 Like