1

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Alistair
  • 101
  • 10

2 Answers2

3
  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

Guru
  • 21,652
  • 10
  • 63
  • 102
  • Hello, did you have any shader issues? I'm getting an enormous amount of warrnings with OpenGLES API being deprecated for iOS 12 – Mark Nov 27 '18 at 04:44
  • In my game shaders not used. Checkout you forgot to replace GLES-Render.mm from cocos2d 2.2 checkout this..may be useful https://stackoverflow.com/questions/11662770/cocos2d-2-0-zillions-of-opengl-errors/11663486#11663486 – Guru Nov 27 '18 at 06:40
  • Thanks for your timely reply. Unfortunately that didn't help. But with your help so far I'm actually get the app to launch, though it crashes early. There's an error compiling shaders in the file CCGLProgram.m, so I'll look into that further – Mark Nov 27 '18 at 06:51
  • Just found this that replaces a method in CCGLProgram.m. I didn't use the accepted answer, but the answer below it. https://stackoverflow.com/questions/30864055/coco2d-2-1-and-xcode-7-ios-9-crash-ccshader. I believe it works as my app now proceeds further than before but I'm having IAP errors which I'll probably remove from my app. – Mark Nov 27 '18 at 06:57
  • I have inApp purchases in Cocos2d 2.2 game and its working fine...you may need to replace your deprecated inApp code...better post your other problem in separate question...its not related to Cocos2d. – Guru Nov 27 '18 at 13:10
0

Guru's solution is incomplete. His solution loads iPhone X images in a scale of 2 (1624x750) instead of 3 (2436×1125). So you need to set the scale of the device to 3. In setContentScaleFactor from CCDirectiorIOS.m you have to add:

if([UIScreen mainScreen].scale == 3.0f)
        __ccContentScaleFactor = 3;