Cocos2d-x Multi-Resolution Support

Source: Internet
Author: User

I recently encountered multi-resolution support problems, so I checked some information. Share some of the gains for future reference by yourself and other friends who need them.

If I want to build a cocos2d-x project, my goal is to support iphone3G (480,320), iphone4/4S (960,640), iphone5 (1136,640), ipad1/2 (1024,768 ), ipad 3 (2048,153 6) and all android devices (which resolution may be (800,480) and other resolutions )? How should I plan? But do you know before planning how cocos2d-x is designed for multi-resolution support? So first let's take a look at the basic support that cocos2d-x provides. Since version 2.0.2, Cocos2d-x does not use-hd,-ipd,-ipdhd and other file name suffix, because its goal is to support the expansion platform, no longer just IOS system. Use setResourceDirectory () (2.0.4) or setSearchPaths () (2.1.1) in the ccfileuiti class ). We only need to create our own directory for each system under the resource Directory and put the corresponding resources in (refer to the http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Mechanism_of_loading_resources for details ). Since version 2.0.4, The Cocos2d-x has removed the setRetinaEnable () function because the cocos2d-x will enable retina by default (if the device supports it) and bring a new concept. You need to understand how designSize and contentScaleFactor map to the window. I will briefly explain: designSize: literally, it refers to the design size. Why does the word "design" mean? For example, if we leave the resolution of different devices aside, we assume that there is only one standard interface to hide the differences between different devices. This standard interface is designSize. The Cocos2d-x will also "align" to the standard interface, and once we set the designSize, The getWinSize in the cocos2d-x will become our designSize. We usually set it to (480,320) "classic" size, but if your initial resource is (1024,768) size, we can use the (512,384) design interface, ensure that the resource aspect ratio is the same as the designed aspect ratio to ensure full screen resource usage. ContentScaleFactor: literally, the content scaling factor, which is our resource (which I understand ). In general, contentScaleFactor = resourceHeight/designSize. Height; why is this? Because we just talked about the design size, we set it to (480,320), and our winSize will become the design size later, but we still use the background image (960,640) in iPhone 4, in this case, the screen size is 4 times larger. With the zoom factor, we can program the background image (960,640) (480,320 ). In this way, the original full-screen resource is still full-screen. How does designSize map to "real window": This process maps our "Standard Interface" to a real device. The size of our real devices may change a lot. (480,320) How do I map the design interface? If I say (1136,640? There are three types of kResourceExactFit, kResourceNoBorder, and kResourceShowAll. The first method is completely adaptive, that is, no matter how strange the device resolution is, it will paste the design interface exactly. The seemingly "perfect" solution also has flaws, that is, deformation. Your character may be "fat" or "thin". The second method is borderless, that is, there is no boundary restriction. If your "Design Interface" is (480,320), the target device is (1136,640 ). If the aspect ratio of the target device is different from the aspect ratio of the design interface, this mode is stretched beyond the boundaries of the target device, that is, the design interface is fully adapted to one side, and the other side is exceeded. In our current assumption, it is to stretch the width of the design interface to 1136, and then enlarge the height ratio. In this case, we will find that the width is just "full screen", but it is superb. This is the so-called "benefit" without border restrictions. The third is full display, that is, full display. If your "Design Interface" is (480,320), the target device is (1136,640 ). This mode ensures that all the design interfaces are displayed. That is to say, if the aspect ratio of the target device is different from the aspect ratio of the design, this mode is stretched to the target device boundary. In our current assumption, it is to stretch the height of the design interface to 640, and then enlarge the width and equality ratio. In this case, we will find that the height is "full screen", but the width is "crowded" (black borders are left on both sides ). This is the "benefit" of full display. (Specific reference http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Multi_resolution_support) and cocos2d-x has a "weird" feature (at least in cocos2d-x 2.1.1), that is when you turn on [iphone Retina 4-inch, if you do not set the start cover, the winSize will still be (960,640 ). Therefore, you must set the start cover to obtain the winSize (1136,640 ). Knowing the cocos2d-x's support for resolution, we can better understand how our project can support multi-resolution. If you use CocosBuilder (if you still don't use it, please refer to my [CocosBuilder tutorial series]), how can we make CocosBuilder support multi-resolution? Because the CocosBuilder file reading library (that is, CCBReader reads ccbi files) is also loaded using CCTexture, therefore, the Directory Search rules we mentioned earlier will be automatically used to load ccbi files. Although CocosBuilder has the same promise as Cocos2d "-hd,-ipd,-ipdhd", these have no effect on the cocos2d-x. Since we are targeting multiple resolutions, we must use the percentage mode in CocosBuilder to set coordinates and sizes. Otherwise, the coordinates you set will not automatically adapt to the corresponding device resolution. For example, if you set a login box, you should not set the coordinates to (512,384), because the coordinates are not directly visible on the (480,320) iPhone 3G or android device, the relative distance from other elements is also messy. Therefore, the percentage (50%, 50%) can be used to ensure the center on any device. As described above, you should be able to find your own multi-resolution support solution. A set of resources, two sets, three sets can be, the main is to see each game their own needs Article Source: http://www.2cto.com/kf/201303/195840.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.