Game Development Reference
In-Depth Information
Figure 7-7 . The result: an infinitely scrolling parallax background
Repeat, Repeat, Repeat
Another neat trick deserves mention. You can set any texture to repeat over a certain
rectangular area. If you make this area big enough, you can have this texture repeat
nearly endlessly. At least several thousand pixels or dozens of screen areas can be
covered with a repeating texture, with no penalty to memory usage.
The trick is to use the GL_REPEAT texture parameter supported by OpenGL. But it
only works with square images that are exactly a power of 2, like 32 × 32 or 512 × 512
pixels. Listing 7-7 shows the code.
Listing 7-7. Repeating Background with GL_REPEAT
CGRect repeatRect = CGRectMake(−5000, -5000, 5000, 5000);
CCSprite* sprite = [CCSprite spriteWithFile:@"square.png" rect:repeatRect];
ccTexParams params =
GL_LINEAR, // texture minifying function
GL_LINEAR, // texture magnification function
GL_REPEAT, // how texture should wrap along X coordinates
GL_REPEAT // how texture should wrap along Y coordinates
[sprite.texture setTexParameters:&params];
Search Nedrilad ::

Custom Search