Search code examples
cocos2d-x

cocos2d-x What is the best way to use device orientation portrait and landscape


I scanned the forum and also in the test examples with no strict answer to the question how can i support game portrait and landscape device orientation. on iOS first is there any kind of example or source or something i can see and learn thanks !


Solution

  • You can refer this : http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Device_Orientation

    For ios : in viewcontroller.m

    bool shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    

    // for Landscape

    return UIInterfaceOrientationIsLandscape( interfaceOrientation );
    

    // for Potrait

    return UIInterfaceOrientationIsPotrait( interfaceOrientation );
    

    }

    if multiple :

    bool shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    

    {

    return UIInterfaceOrientationIsPortrait(interfaceOrientation)||UIInterfaceOrientationIsLandscape(interfaceOrientation) ;
    

    }

    Scaling Contents according to your Orientations :

    void didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    
    CGSize s;
    if (UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication.statusBarOrientation))
    {
    s = CGSizeMake(std::max<float>(UIScreen.mainScreen.bounds.size.width,    UIScreen.mainScreen.bounds.size.height), 
                             std::min<float>(UIScreen.mainScreen.bounds.size.width,  UIScreen.mainScreen.bounds.size.height));
    } 
    else
    {
    s = CGSizeMake(std::min<float>(UIScreen.mainScreen.bounds.size.width,     UIScreen.mainScreen.bounds.size.height),  
                             std::max<float>(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height));
    }
    
    CCDirector* director = cocos2d::CCDirector::sharedDirector();
    director->enableRetinaDisplay(false);
    director->getOpenGLView()->setFrameSize(s.width, s.height);
    director->getOpenGLView()->setDesignResolutionSize(s.width, s.height, kResolutionShowAll);
    director->enableRetinaDisplay(true);