Search code examples
iosobjective-ccocos2d-iphone

How to fill texture in cocos2d?


I have a shape and texture image.. (shape.png, texture.png)

I would like to paint a shape as texture.png pattern in cocos2d. (shape size is pretty bigger than texture image. so automatically fill texture pattern in entire shape.

I trying to know the way.

Can't find..

someone have a solution to solve this problem?


Solution

  • sprite = [[CCSprite alloc] initWithFile:@"texture.png"];
    
    ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
    [sprite.texture setTexParameters:&params];
    [sprite setTextureRect: CGRectMake(0.0, 0.0, w, h)];
    

    This code repeats a texture in both the x and the y (or s, t in texture terms). The only limitation is that your texture must be a power of two (ie. 64 * 128, 128 * 128, 1024 * 1024) etc.