1

I want to create sprite with size is relative to the screen size. I.e: Sprite size is equal to screen width * 0.2.

I use setContentSize and setScale but it gives ugly and poor graphic quality. I've read about multiple resolution supports, but it doesn't work on this case because i need sprite size adapt to the any screen size.

Testing on iPhone 7, i scale an image from 512x512 to 64x64.

This is error of cocos2d-x or anyway to archive it?

TomSawyer
  • 3,711
  • 6
  • 44
  • 79

2 Answers2

0

Scale it accordingly to a device size.

yourSprite->setScale(mainScene->getContentSize().width / yourSprite->getContentSize().width * 0.2, mainScene->getContentSize().width / yourSprite->getContentSize().width * 0.2);

And do not use setContentSize

Darvas
  • 974
  • 3
  • 14
  • 27
  • `setScale` makes same result as `setContentSize` – TomSawyer May 03 '17 at 18:17
  • @TomSawyer maybe you are putting that sprite to another sprite which have small image size? – Darvas May 04 '17 at 07:27
  • Nope, i create individual sprite. And i found out that this error only happend on real divice (i'm using iPhone 7). Using `setScale` instead of `setContentSize` can solve this trouble on simulator. – TomSawyer May 04 '17 at 10:27
  • have you experienced the same? I'm stucking here, the only way is creating multiple versions of sprite for different sizes? – TomSawyer May 16 '17 at 16:43
  • No I have not experienced issues with scalling. Can you post how you create the sprite and where you add it? – Darvas May 17 '17 at 07:56
  • I've done numerous of queries and this trouble is exist: you can see the topic here: http://discuss.cocos2d-x.org/t/sprite-border-looks-like-not-anti-aliased-after-rotation-or-downscaling/10623 http://esotericsoftware.com/forum/Aliased-Sprites-on-Scale-CCD2X-5337 down scaling creates aliasing on sprite. – TomSawyer May 17 '17 at 08:31
0

You can scale your sprite according content scale factor that calculates according to your device screen size and what ResolutionPolicy you're using.

auto sprite= Sprite::create("xyz.png")
sprite->setScale(Director::getInstance()->getContentScaleFactor());
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • I've tried with `setScale` but the result is the same with `setContentSize`. It only work if the size of image close to the target size like 150->32 is ok, but 512->32 will be really bad. – TomSawyer May 03 '17 at 18:17
  • for major variation(512->32) in size you need to create resources in different size/resolution, and set path to that resources according to device resolution. it is an ideal solution. It's obvious if we scale up or down in any amount, we are decreasing quality of resource. – Abhishek Aryan May 03 '17 at 18:22
  • Scaling up decrease graphic quality, but scaling from big to small must preserve the quality. You can see the image here: http://discuss.cocos2d-x.org/t/setcontentsize-gives-poor-graphic/36606/2 I figure out that i have this trouble only on real divice (iPhone 7), testing on iPhone 6 simulator with `setScale` and it's good. Is this a bug of cocos? – TomSawyer May 03 '17 at 18:29
  • Scaling in any way up/down, quality of image degraded according to scaling extent. In your link you scale first image `512->32` so scale factor is `.0625` and another one is `64->32` and so scale factor is `.5` . More extent of scaling(up/down) decrease more quality of that image. – Abhishek Aryan May 03 '17 at 18:39
  • I don't know what cocos does on scaling, but any resize image mechanism on any language can give a acceptable quality while scaling from bigger to smaller. Resize 2048 to 32 get the same result as 64 to 32. – TomSawyer May 03 '17 at 18:45
  • no dear I've quite good experience on LibGDX, there also facing same problem so need to apply filter on texture `myTexture.setFilter(Textue.TextureFilter.Linear, Textue.TextureFilter.Linear);` – Abhishek Aryan May 03 '17 at 18:51
  • Anyway to do it on cocos2d-x? or workaround :(? Creating numberous sprite with different sizes is not a good option i want. Resizing image is really basic thing need to be done but i don't think it's hard to archive like this. – TomSawyer May 03 '17 at 19:13
  • I've done the way to apply filter to make scaled sprite looks smooth, but it also blurry using mipmaping. Itsn't what i want. I think the best way to solve it is resize/downscale by some good image processing algorithm the image from the beginning and let sprite use it. – TomSawyer May 17 '17 at 19:12
  • From where I get such algorithms ? – Abhishek Aryan May 17 '17 at 19:19
  • http://www.imagemagick.org/Magick++/ or http://stackoverflow.com/questions/9570895/image-downscaling-algorithm . I'm finding how to import and use it inside LibGDX or Cocos2d-x – TomSawyer May 17 '17 at 19:36