Chapter 6. Creativity with Textures

In this chapter, we will take a leap into a new direction and take things to the next level. You will learn how to programmatically generate textures and bind them to shapes so that they can be used in the game. Cocos2d-x is so flexible that it allows us to run our own rendering commands and we will take advantage of this fact.

In this chapter, you'll learn:

  • How to use the CCRenderTexture class to draw into a texture
  • How to apply various graphical effects to a texture to add aesthetics
  • How to create a curved Box2D terrain
  • How to apply a texture to a curve

An introduction to Penguins Can Fly

In our sixth game, Penguins Can Fly, we lead a cute little penguin through a wide expanse of vibrant, colorful, and breath-taking hills. The controls are quite simple, but the game takes some time to master. The player must simply tap the screen. Tapping the screen pulls the penguin towards the hill. Thus, the game becomes a skill-based judgment game where the player must learn the right moment to release the tap.

Quite a bit of code in this chapter has been ported to Cocos2d-x from a wonderful demo project (https://github.com/haqu/tiny-wings) in Cocos2d-iphone written by Sergey Tikhonov (http://haqu.net/). This demonstrates how some of the challenging features of the game are implemented. A lot of this chapter's explanation on the above demo project is inspired from an excellent tutorial written by Ali Hafizji on http://www.raywenderlich.com/. Please head out to these links if you want to take a look at the original. The original needs some understanding of Objective-C:

We will discuss the following entities in this chapter:

  • The Terrain class
    • Drawing into a texture using CCRenderTexture
    • Rendering the stripes, gradient, highlights, and noise
    • Generating a set of vertices to represent a hill
    • Binding a Box2D body to the vertices of the hill
    • Rendering, scrolling, and scaling the hill
  • The Penguin class
    • The diving mechanism
    • Detecting perfect slides and triggering fevers
    • Leaping from one hill to the next
  • The GameWorld class
    • Bringing it all together
    • The main update loop
    • The PreSolve contact event

This is what we will achieve by the end of this chapter:

An introduction to Penguins Can Fly
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.114.85