// Initialize director
Director * pDirector = Director: getInstance ();
EGLView * pEGLView = EGLView: getInstance ();
PDirector-> setOpenGLView (pEGLView );
Size win_size = pDirector-> getWinSize (); // screen Size
Int w = win_size.width;
Int h = win_size.height;
Int design W = 1280;
Int design_h = 720;
Float s = w/(float) h;
Float scale_w = w/(float) design_w; // width scaling ratio
Float scale_h = h/(float) design_h; // height scaling ratio
Float scale = 1;
// Perform branch processing (based on width)
If (w> design_w) {// the screen size is larger than the design size and must be enlarged.
If (scale_w> scale_h) {// The width amplification ratio is greater than the height amplification ratio. First, determine the size (I .e. the height) with a small proportion.
Design _ H = 720;
Design_w = (int) (design_h/s );
} Else {// The width-to-width ratio is smaller than the height-to-height ratio. First, determine the small size, that is, the width.
Designing W = 1280;
Design_h = (int) (design_w/s );
}
} Elseif (w
If (scale_w
Designing W = 1280;
Design_h = (int) (design_w/s );
} Else {// The width-to-width ratio is smaller than the height-to-height ratio. First, determine the small size, that is, the width.
Design _ H = 720;
Design_w = (int) (design_h * s );
}
} Else {
Designing W = 1280;
Design_h = (int) (design_w/s );
}
// Achieve full screen display according to this size
PEGLView-> setDesignResolutionSize (design_w, design_h, ResolutionPolicy: SHOW_ALL );
Refer:
Starting from Cocos2d-x 2.0.4, Cocos2d-x proposed its multi-resolution support scheme, abandoned the previous retina related setting interface, and proposed the design resolution concept.
The following interfaces are available:
- CCEGLView: sharedOpenGLView ()-> setDesignResolutionSize () // design the resolution size and Mode
- CCDirector: sharedDirector ()-> setContentScaleFactor () // content Scaling Factor
- CCFileUtils: sharedFileUtils ()-> setResourceDirectory () // deprecated
- CCFileUtils: sharedFileUtils ()-> setSearchPaths () // resource search path
- CCEGLView: Export dopenglview ()-> getFrameSize () // screen resolution
- CCDirector: sharedDirector ()-> getWinSize () // design resolution
- CCDirector: sharedDirector ()-> getVisibleSize () // design resolution visible area size
- CCDirector: sharedDirector ()-> getVisibleOrigin () // design resolution visible region start from cocos2d-2.1beta3-x-2.1.1,CCFileUtils: sharedFileUtils ()-> setResourceDirectory ()Replaced by the new interface CCFileUtils: sharedFileUtils ()-> setSearchPaths (searchPath) starting from Cocos2d-x 2.1.3, two new resolutionpolicies (kResolutionFixedHeight, kResolutionFixedWidth) are added, in a total of 5 middle modes. Multi_resolution_support and mechanic ism_of_loading_resources are officially introduced. This paper analyzes the multi-resolution adaptation technology of Cocos2d-x from the perspective of engine users.From Retina to design resolutionBefore Cocos2d-x 2.0.4, there was a Retina concept, which was a concept from cocos2d-iphone. To support Retina iphone devices, cocos2d-iphone uses extensions such as-hd to differentiate iphone and Retine iphone image resources. When designing a game, use the point coordinate system instead of the real pixel coordinate system. This is the same as the point concept proposed by iOS native application development. without modifying the code, you can run the previous 640 × 960 program on a 320 × 480 device, but the picture will look blurred, once the @ 2x image is added, iOS automatically loads the @ 2x image to support the Retna iphone. Point coordinate system, which can solve the problem of multi-resolution support within a certain range. However, when iPhone 5 and ipad 3 come out, iOS has a total of five resolutions to support. If it is a universal program, it is quite painful. The point coordinate system cannot completely solve the problem, and the resolution on android is more complicated. Design resolution should be a concept evolved from the point coordinate system to shield device resolutions. The genie coordinates are all arranged on design resolution, but it is not easy to achieve this goal. Cocos2d-x provides a set of related interfaces and five resolution adaptation policies, which is what we need, below we explore together. Reference: http://www.cocoachina.com/applenews/devnews/2013/0809/6767.html
The cocos2d-x adds a new method that makes cocos2d adaptive easily based on the size of the screen.
Method Name:
virtual void setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy);
In the parameters passed by this method, the first two parameters, width and height, refer to the developer's default size when designing the interface. For example, if the developer takes the iPhone 5 as the standard, the width and height are 568*320. If the developer uses the iPhone 4 as the standard, the width and height are 480*320, and so on.
The third parameter resolutionPolicy refers to the rules that cocos2d needs to follow during scaling. The ResolutionPolicy custom structure is as follows:
KResolutionExactFit means that when cocos2d is scaled, image distortion is not considered, and the entire screen is filled with the screen.
KResolutionNoBorder means to maximize the screen when cocos2d is scaled. If there is a part beyond the screen, the excess part will be removed (for example: if the design size is 568*320, when the screen size is 480*320, the excess width is removed)
KResolutionShowAll indicates that all images are displayed when cocos2d is scaled. black bars are displayed if any screen is left blank.
The three rules have their own advantages and disadvantages. Which of the Rules should be used to consider the needs of the project and make a reasonable choice.
However,Note that the setDesignResolutionSize and enableRetina methods cannot be used simultaneously. This means that the HD version cannot be used when self-adaptation is used.
If you look at the cocos2d-x source code, you can see a code in the setDesignResolutionSize method:
CCAssert(m_bIsRetinaEnabled == false, "can not enable retina while set design resolution size!");
This is because the resources in the high-definition mode are reduced, and all coordinates are defined points rather than pixel. Therefore, there may be some conflicts between the HD mode and self-adaptation. (The above is just the author's guess, not to blame)
In short, if you want to use self-adaptation, you must give up HD at the beginning of the project. If you want to use the high-definition version, you can only set the setContentScaleFactor based on the ratio of the screen size. The setting method of this part will not be expanded here.
In addition, when I first started using the self-adaptation function, I mistakenly thought that the coordinates and sizes of the UI elements should be written as a proportion to the screen size during development. For example:
CCSprite* avatarSprite = CCSprite::create("avatar.png"); avatarSprite->setPosition(ccp(winSize.width * 0.000878, winSize.height * 0.653125)); addChild(avatarSprite);But in fact this is not necessary. The auto-adaptation function is to directly adjust the size of the entire CCDirector canvas. Therefore, any element added to CCDirector will be scaled to the appropriate proportion, and there is no need to write the coordinates as the relative ratio of the screen.
Reference: http://www.cnblogs.com/eagley/archive/2013/05/31/3110785.html