Sprite::createWithTexture with Rect not working as expected

I’m trying to build a sprite from an image. Using Image and Texture2D class and then later creating a sprite from the texture2D.

The image I am loading is 512x512 and I expected both versions of the createWithTexture to behave the same but they don’t. Here the code:

Image* image = new Image();
image->initWithImageFile(fileName);

Texture2D* texture = new Texture2D();
 texture->initWithImage(image);

 //If used this way everything works as expected
Sprite* spr= Sprite::createWithTexture(texture);
//If used with a Rect weird result occurrs.
Sprite* spr= Sprite::createWithTexture(texture,Rect(0,0,512,512));
spr->setAnchorPoint(Vec2(0,0));
spr->setPosition(Vec2(0,0));
spr->setScale(1.0f,1.0f);
this->addChild(spr);

You can check the result of both versions at stack overflow: http://stackoverflow.com/questions/31878291/spritecreatewithtexture-with-rect-not-working-as-expected

I have noticed that cocos2d-x uses wdith/CC_CONTENT_SCALE_FACTOR() and height/CC_CONTENT_SCALE_FACTOR() to crate the Rect for the sprite creation. Why is this? Could anybody explain a bit why aren’t you using just pixels for that? Isn’t a bit weird to pass width and height / CC_CONTENT_SCALE_FACTOR() when creating a sprite from a part of a texture?

P.D. I’m using cocos2d-x 3.7

Cheers.

cocos2d-x uses a design resolution to help scaling between different resolutions and aspect ratios
http://www.cocos2d-x.org/wiki/Multi_resolution_support

The content scale factor describes how much bigger your assets are than the design resolution. This allows HD assets to be provided for HD devices which are automatically scaled down to fit the design resolution the rest of the app is working in.

Therefore the rect you pass into createWithTexture needs to also be in design resolution.

I agree this is confusing, but it allows you to swap your asset packs between resolutions without createWithTexture having to worry about what resolution asset pack is actually being used.

Hope this helps.

Thanks for the information.