How to disable antialising for textures, atlas and bitmap fonts

Post Reply
User avatar
admin
Site Admin
Posts: 29
Joined: Nov 24th, '23, 09:57

How to disable antialising for textures, atlas and bitmap fonts

Post by admin »

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.

Image

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

Code: Select all

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:

Code: Select all

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.

Code: Select all

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.

Code: Select all

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

Code: Select all

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

Original Article
Post Reply