Migrate Cocos2d to latest device support

34 views Asked by At

Hi I am trying to run my very old code(year 2011) to new Xcode12. I am able to run it but my offset mess the UI. My SCALEX, SCALEY, OFFSETX, OFFSETY make the mess as that time device available is only iPhone4s and standard iPad. How to change those to Support all device avail today. Here is my old constant.

// Scale

#define SW ([[CCDirector sharedDirector] winSize].width)
#define SH ([[CCDirector sharedDirector] winSize].height)
#define SCALEX                                  (1600.0f / 3.0f) / 800.0f
#define SCALEY                                  320.0f / 480.0f
#define OFFSETX                                 ((1600.0f / 3.0f) - 480.0f) / 2
#define OFFSETY                                 (320.0f - 320.0f) / 2
1

There are 1 answers

0
Md. Ibrahim Hassan On

I am not very sure about how SCALEX and OFFSETX is being calculated, but it seems the 480.0f and 320.0f can be substituted with

#define DEVICE_HEIGHT [[[[UIApplication sharedApplication] keyWindow] rootViewController].view convertRect:[[UIScreen mainScreen] bounds] fromView:nil].size.height

and

#define DEVICE_WIDTH [[[[UIApplication sharedApplication] keyWindow] rootViewController].view convertRect:[[UIScreen mainScreen] bounds] fromView:nil].size.width

The final expression would be

    #define SCALEX                                  (1600.0f / 3.0f) / 800.0f
    #define SCALEY                                  DEVICE_WIDTH / DEVICE_HEIGHT
    #define OFFSETX                                 ((1600.0f / 3.0f) - DEVICE_HEIGHT) / 2
    #define OFFSETY                                 (DEVICE_WIDTH - DEVICE_WIDTH) / 2

Please let me know if it produces better results, if we can know what 1600 and 800 values we can improve the soultion.