cocos2d-x: How to disable antialising for textures, atlas and bitmap fonts

Antialising of textures is a technique used to smooth images when them are shown with any transformation (scaled, rotated, etc.) respect to the original one.

By default, cocos2d-x applies this technique for the textures we use in our games, but there are situations where we want to disable this effect. For example, for games with pixelated aesthetics.

The way to disable anti alias is calling method setAliasTexParameters() on the used textures. I don’t know if there is a simple global manner to do it for the whole game, so I call to setAliasTexParameters() for every texture I use. Below it’s shown how to do it for different elements of the game.

 

Sprite

auto sprite = Sprite::create("image.png");
sprite->getTexture()->setAliasTexParameters();

 

Sprite Atlas

For the sprite atlas (.plist files) we have several ways to access textures. For example:

Get the texture through a sprite

We can access a texture from an existing sprite that uses it. When we set the alias params to the texture, this affects to all sprites that use the same texture. So it’s only neccesary to do it one time for the whole atlas:

auto sprite = Sprite::createWithSpriteFrameName("sprite_name");
sprite->getTexture()->setAliasTexParameters();

Create the sprite atlas with a pre-configured texture

This method loads a texture without smoothing.

Texture2D* loadAliasTexture(const std::string &textureFile) {
    auto img = new Image();
    img->initWithImageFile(textureFile);
 
    auto tex = new Texture2D();
    tex->initWithImage(img);
    tex->setAliasTexParameters();
    return tex;
}

Next we can load the atlas using a texture loaded with the previous method.

Texture2D* tex = loadAliasTexture("images.png");
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("imgages.plist", tex);

 

Bitmap Fonts

auto label = Label::createWithBMFont("font.fnt", "Label text");
label->getFontAtlas()->getTexture(0)->setAliasTexParameters();

Un comentario en «cocos2d-x: How to disable antialising for textures, atlas and bitmap fonts»

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*