Search code examples
ioscocos2d-iphonescreeniphone-x

Cocos2d-iPhone and iPhone X resolution


Can you believe we are still using Cocos2d-iPhone?

Does anyone have a fix for making it work with the newest iPad's and iPhone X's?

Our mode is landscape.


Solution

    1. If cocos2d version 1.0 - 2.1 then update it to Cocos2d 2.2
    2. Add iPhoneX splash screen in Images.xcassets
    3. In CCConfiguration.m file updated below function. In this we are handling contentScaleFactor 3.

      -(NSInteger) runningDevice
      {
          NSInteger ret=-1;
      
            #ifdef __CC_PLATFORM_IOS
      
          if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
          {
              ret = (CC_CONTENT_SCALE_FACTOR() == 2) ? kCCDeviceiPadRetinaDisplay : kCCDeviceiPad;
          }
          else if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone )
          {
              // From http://stackoverflow.com/a/12535566
              BOOL isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136));
      
              if( CC_CONTENT_SCALE_FACTOR() == 2 ) {
                  ret = isiPhone5 ? kCCDeviceiPhone5RetinaDisplay : kCCDeviceiPhoneRetinaDisplay;
      
                  // Guru - Handle iPhone Plus device, iPhoneX
                  // - - - - -  - - --  -- - - - -- - -  - - -
                  if([[UIScreen mainScreen] scale]==3)
                  {
                      ret = kCCDeviceiPhone5RetinaDisplay;
                  }
              }
              else
              {
                  // Guru - Handle iPhone Plus device, iPhoneX
                  // - - - - -  - - --  -- - - - -- - -  - - -
                  if([[UIScreen mainScreen] scale]==3)
                  {
                      ret = kCCDeviceiPhone5RetinaDisplay;
                  }
                  else
                  // - - - - -  - - --  -- - - - -- - -  - - -
                  ret = isiPhone5 ? kCCDeviceiPhone5 : kCCDeviceiPhone;
              }
          }
      
          #elif defined(__CC_PLATFORM_MAC)
      
          // XXX: Add here support for Mac Retina Display
          ret = kCCDeviceMac;
      
           #endif // __CC_PLATFORM_MAC
      
            return ret; 
      

      }

      1. Use buttons and other image from iPhone5HD and manually load background for iPhone X

        CCSprite *bg ;
        if([App isIphoneX])
        {
           bg = [CCSprite spriteWithFile:@“Background-iphoneX.png"]; // 1624X750 image size
        }
        else
        {
           bg = [CCSprite spriteWithFile:@"Background.png"]; // make sure -hd,-ipad,-ipadhd, -iphone5hd there
        }
        

    OR just scale bg image

    #define SW ([[CCDirector sharedDirector] winSize].width)
    #define SH ([[CCDirector sharedDirector] winSize].height)
    
    CCSprite *bg ;
    if([App isIphoneX])
    {
          bg = [CCSprite spriteWithFile:@"Background.png"];
          bg.scaleX = SW/bg.contentSize.width;
           bg.scaleY = SH/bg.contentSize.height;
    
    } 
    

    App Delegate Code:

    -(bool)isIphoneX
    {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            CGSize screenSize = [[UIScreen mainScreen] bounds].size;
            if (screenSize.width == 812) // portrait height = 812
            {
                return true;
            }
            if([self isIphoneXR]) // Now in this game iPhoneXR, iPhoneXS Max = iPhoneX,
            {
                return true;
            }
        }
        return false;
    }
    -(bool)isIphoneXR
    {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            CGSize screenSize = [[UIScreen mainScreen] bounds].size;
            if (screenSize.width == 896 && screenSize.height == 414)
            {
                return true;
            }
        }
        return false;
    }
    
    1. Don't add iPhone6,iPhone7, iPhoneXR,iPhoneXSMax splash screen..system auto scale iphone5 to iPhone6,iPhone7 etc and iPhoneX to iPhoneXR,iPhoneXSMax. You must use iPhone 5, iPhoneX splash

    NOTE: I just supported iPhoneX and all device support for my old game...Apple recently approved this change...successfully added iPhoneX support to cocos2d 2.2