{"id":863,"date":"2018-02-10T12:05:19","date_gmt":"2018-02-10T11:05:19","guid":{"rendered":"http:\/\/www.pixnbgames.com\/blog\/?p=863"},"modified":"2018-02-10T12:05:19","modified_gmt":"2018-02-10T11:05:19","slug":"cocos2d-x-how-to-disable-antialising-for-textures-atlas-and-bitmap-fonts","status":"publish","type":"post","link":"https:\/\/www.pixnbgames.com\/blog\/cocos2d-x\/cocos2d-x-how-to-disable-antialising-for-textures-atlas-and-bitmap-fonts\/","title":{"rendered":"cocos2d-x: How to disable antialising for textures, atlas and bitmap fonts"},"content":{"rendered":"<p><a href=\"https:\/\/es.wikipedia.org\/wiki\/Antialiasing\" target=\"_blank\" rel=\"noopener\">Antialising<\/a> of textures is a technique used to smooth images when them are shown with any transformation (scaled, rotated, etc.) respect to the original one.<\/p>\n<p>By default, <a href=\"http:\/\/www.cocos2d-x.org\/\" target=\"_blank\" rel=\"noopener\">cocos2d-x<\/a> 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.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-853\" src=\"http:\/\/www.pixnbgames.com\/blog\/wp-content\/uploads\/2018\/02\/texture_aliasing.png\" alt=\"\" width=\"645\" height=\"400\" srcset=\"https:\/\/www.pixnbgames.com\/blog\/wp-content\/uploads\/2018\/02\/texture_aliasing.png 645w, https:\/\/www.pixnbgames.com\/blog\/wp-content\/uploads\/2018\/02\/texture_aliasing-300x186.png 300w\" sizes=\"(max-width: 645px) 100vw, 645px\" \/><\/p>\n<p>The way to disable anti alias is calling method <code>setAliasTexParameters()<\/code> on the used textures. I don&#8217;t know if there is a simple global manner to do it for the whole game, so I call to <code>setAliasTexParameters()<\/code> for every texture I use. Below it&#8217;s shown how to do it for different elements of the game.<\/p>\n<p>&nbsp;<\/p>\n<h3>Sprite<\/h3>\n<pre lang=\"cpp\">auto sprite = Sprite::create(\"image.png\");\r\nsprite->getTexture()->setAliasTexParameters();\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Sprite Atlas<\/h3>\n<p>For the sprite atlas (.plist files) we have several ways to access textures. For example:<\/p>\n<h4>Get the texture through a sprite<\/h4>\n<p>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&#8217;s only neccesary to do it one time for the whole atlas:<\/p>\n<pre lang=\"cpp\">auto sprite = Sprite::createWithSpriteFrameName(\"sprite_name\");\r\nsprite->getTexture()->setAliasTexParameters();\r\n<\/pre>\n<h4>Create the sprite atlas with a pre-configured texture<\/h4>\n<p>This method loads a texture without smoothing.<\/p>\n<pre lang=\"cpp\">Texture2D* loadAliasTexture(const std::string &textureFile) {\r\n    auto img = new Image();\r\n    img->initWithImageFile(textureFile);\r\n\r\n    auto tex = new Texture2D();\r\n    tex->initWithImage(img);\r\n    tex->setAliasTexParameters();\r\n    return tex;\r\n}\r\n<\/pre>\n<p>Next we can load the atlas using a texture loaded with the previous method.<\/p>\n<pre lang=\"cpp\">Texture2D* tex = loadAliasTexture(\"images.png\");\r\nSpriteFrameCache::getInstance()->addSpriteFramesWithFile(\"imgages.plist\", tex);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Bitmap Fonts<\/h3>\n<pre lang=\"cpp\">auto label = Label::createWithBMFont(\"font.fnt\", \"Label text\");\r\nlabel->getFontAtlas()->getTexture(0)->setAliasTexParameters();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 <a class=\"more-link\" href=\"https:\/\/www.pixnbgames.com\/blog\/cocos2d-x\/cocos2d-x-how-to-disable-antialising-for-textures-atlas-and-bitmap-fonts\/\">Seguir leyendo <span class=\"screen-reader-text\">  cocos2d-x: How to disable antialising for textures, atlas and bitmap fonts<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":853,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29],"tags":[32],"language":[4],"_links":{"self":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/863"}],"collection":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/comments?post=863"}],"version-history":[{"count":2,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/863\/revisions"}],"predecessor-version":[{"id":865,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/863\/revisions\/865"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/media\/853"}],"wp:attachment":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/media?parent=863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/categories?post=863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/tags?post=863"},{"taxonomy":"language","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/language?post=863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}