Example #1
0
bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

    pDirector->setOpenGLView(pEGLView);
	
    // turn on display FPS
    //pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);
    
    //
    CCSize frameSize = pEGLView->getFrameSize();
    CCLOG("applicationDidFinishLaunching frameSize.width = %f  frameSize.height= %f", frameSize.width,frameSize.height);
    if(frameSize.height/frameSize.width>1.4){
        pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionFixedWidth);
    } else {
        pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionFixedHeight);
    }
    
    if(designResolutionSize.width == 720){
        pDirector->setContentScaleFactor( smallResource_tate2.size.width / designResolutionSize.width );    //ч╕ж
    } else {
        pDirector->setContentScaleFactor( smallResource_yoko.size.width / designResolutionSize.width );    //цик
    }
    
    cocos2d::CCSize visibleSize = cocos2d::CCDirector::sharedDirector()->getVisibleSize();
    cocos2d::CCPoint origin = cocos2d::CCDirector::sharedDirector()->getVisibleOrigin();
    
    CCLOG("applicationDidFinishLaunching designResolutionSize.width = %f ", designResolutionSize.width);
    CCLOG("applicationDidFinishLaunching designResolutionSize.height = %f ", designResolutionSize.height);
    CCLOG("applicationDidFinishLaunching pDirector->getContentScaleFactor() = %f ", pDirector->getContentScaleFactor() );
    CCLOG("applicationDidFinishLaunching visibleSize  width=%f height=%f ", visibleSize.width, visibleSize.height );
    CCLOG("applicationDidFinishLaunching origin  x=%f y=%f ", origin.x, origin.y );

    // create a scene. it's an autorelease object
    CCScene *pScene = HelloWorld::scene();
    //CCScene *pScene = HelloWorld2::scene();

    // run
    pDirector->runWithScene(pScene);

    return true;
}
Example #2
0
bool Assets::enableMutliResolutionSupport()
{
    /// Multi resolution support BEFORE cocos2d-x 2.0.4
    /// The correct resolution is enableds using setResourceDirectory which set the resource directory for the current device's resolution
    /// Shared/common images to all resolutions are stored in the root resources (Resources/GameResources/textures)
    /// See http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Mechanism_of_loading_resources

    /****************************************************************************************************************
     *
     *     Since 2.0.4, cocos2d-x introduced a new multi-resolution support
     *     http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Multi_resolution_support
     *     http://www.cocos2d-x.org/wiki/Detailed_explanation_of_Cocos2d-x_Multi-resolution_adaptation
     *
     *     Set the design resolution: width, height and policy
     *
     *    - kResolutionExactFit -
     *    The entire application is visible in the specified area without trying to preserve the original aspect ratio.
     *    Distortion can occur, and the application may appear stretched or compressed.
     *
     *    - kResolutionNoBorder -
     *    The entire application fills the specified area, without distortion but possibly with some cropping,
     *    while maintaining the original aspect ratio of the application.
     *
     *    - kResolutionShowAll -
     *    The entire application is visible in the specified area without distortion while maintaining the original
     *    aspect ratio of the application. Borders can appear on two sides of the application.
     ****************************************************************************************************************/

    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = pDirector->getOpenGLView();
    CCFileUtils &fileUtils = *CCFileUtils::sharedFileUtils();

    pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionExactFit);
    CCSize frameSize = pEGLView->getFrameSize();

    /// We select resource according to the frame's height.
    /// If the resource size is different from design resolution size, you need to set contentScaleFactor.
    /// We use the ratio of resource's height to the height of design resolution,
    /// this can make sure that the resource's height could fit for the height of design resolution.

    std::vector<std::string> resOrder = fileUtils.getSearchResolutionsOrder();

    /// GPU max texture size
    GLint maxTextureSize = 0;
    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
    CCLOG("GPU max texture size: %d", maxTextureSize);

    bool scaledGraphics = false;

    /// set the contentScaleFactor based on the ratio of WIDTH
    if (frameSize.width > mediumResource.size.width && maxTextureSize >= 2048) {
        CCLOG("Select LARGE (ipadhd) resource...");
        resOrder.insert(resOrder.begin(), largeResource.directory);
        /// pDirector->setContentScaleFactor(largeResource.size.width/designResolutionSize.width);
        pDirector->setContentScaleFactor(4.f);
    }
    // if the frame's height is larger than the height of small resource size, select medium resource.
    else if (frameSize.width > smallResource.size.width  && maxTextureSize >= 1024) {
        CCLOG("Select MEDIUM (hd) resource...");
        resOrder.insert(resOrder.begin(), mediumResource.directory);
        /// pDirector->setContentScaleFactor(mediumResource.size.width/designResolutionSize.width);
        pDirector->setContentScaleFactor(2.f);
        scaledGraphics = frameSize.width > mediumResource.size.width; // have scale down because of max texture size too small?
    }
    // if the frame's height is smaller than the height of medium resource size, select small resource.
    else {
        CCLOG("Select SMALL (sd) resource...");
        resOrder.insert(resOrder.begin(), smallResource.directory);
        /// pDirector->setContentScaleFactor(smallResource.size.width/designResolutionSize.width);
        pDirector->setContentScaleFactor(1.f);
        scaledGraphics = frameSize.width > smallResource.size.width; // have scale down because of max texture size too small?
    }

    /// Show an error message if we scaled down the graphics or the max texture size is too small
    if (scaledGraphics || maxTextureSize < 512) {
        CCLOGERROR("Unsupported max texture size: %d",maxTextureSize);
        CCMessageBox("We detected graphics limitation which might prevent the app to work properly or to use HD graphics (max texture too small)", "ERROR");
    }

    /// Bug fix: the resolution directory must have a trailing slash (e.g. hd/ or sd/) or
    /// CCFileUtils::getFullPathForDirectoryAndFilename() will not work correclty on iOS if
    /// both search paths and resolution order are defined (missing slash between directory and filename).
    /// We now use setSearchResolutionsOrder instead of addSearchResolutionsOrder because
    /// addSearchResolutionsOrder simply add the string to the list of the resolution order without adding the trailing slash.
    /// setSearchResolutionOrder takes care of adding the trailing slash if missing.
    /// See. http://discuss.cocos2d-x.org/t/search-paths-and-resolutions-order-issue-on-ios/14424
    fileUtils.setSearchResolutionsOrder(resOrder);

    /// Set the font sizes
    for (int i = 0; i < 4; i++)
        smFontsSize[i]  *= designResolutionSize.width / smallResource.size.width;

    ////////////////// Trace settings... /////////////////
#if COCOS2D_DEBUG > 0

    /// Redirect standard output and error stream to file
    //freopen(fileUtils.getWritablePath().append("cocos2d.log").c_str(), "a+", stdout);
    //freopen(fileUtils.getWritablePath().append("cocos2d.log").c_str(), "a+", stderr);

    /// In debug mode we change the default search path to the following.
    std::vector<std::string> paths;
    paths.push_back(fileUtils.getWritablePath()); /// First we search the writable path root for assets download on-the-fly (see command.h/cpp)
    paths.push_back(fileUtils.getWritablePath().append("lua")); /// Secondly we search in the lua folder below the writable root for lua script
    paths.push_back(""); /// Finally we look in the default resource path (empty string)

    fileUtils.setSearchPaths(paths);

    TargetPlatform platform = CCApplication::sharedApplication()->getTargetPlatform();
    if (platform == kTargetAndroid || platform == kTargetIpad || platform == kTargetIphone) {
        /// Purge the writable folder
        CCLOG("Removing files from the assets staging area (writable path)...");
        /// TODO on mac getWritablePath is "/Users/lzubiaur/Library/Caches/".
        /// So it should not be wipe out but temporary assets should be written in a subfolder
        emptyFolder(fileUtils.getWritablePath());
    }

    CCLOG("Real screen size: %.0fx%.0f", pEGLView->getFrameSize().width, pEGLView->getFrameSize().height);
    CCLOG("Design resolution size: %.0fx%.0f", pEGLView->getDesignResolutionSize().width, pEGLView->getDesignResolutionSize().height);
    CCLOG("Content scale factor: %f", pDirector->getContentScaleFactor());
    CCLOG("Scale x:%f y:%f", pEGLView->getScaleX(), pEGLView->getScaleY());
    CCLOG("Visible origin: x:%.0f y:%.0f",pEGLView->getVisibleOrigin().x, pEGLView->getVisibleOrigin().y);
    CCLOG("Visible size: %.0fx%.0f", pEGLView->getVisibleSize().width, pEGLView->getVisibleSize().height);

    /// Log the search paths
    std::string searchPath;
    for (const std::string s : fileUtils.getSearchPaths())
        searchPath.append(' ' + s);
    CCLOG("Search path: %s", searchPath.c_str());

    /// Log the search resolution order
    std::string resolutionOrder;
    for (const std::string s : fileUtils.getSearchResolutionsOrder())
        resolutionOrder.append(' ' + s);
    CCLOG("Search resolution order: %s", resolutionOrder.c_str());
    CCLOG("Writeable path: %s", fileUtils.getWritablePath().c_str());
    CCLOG("Lua version: %s",LUA_VERSION);

    /// CCLOG("Websocket version %s",lws_get_library_version());

    /*
     d("top:         %f %f", CVisibleRect::top().x,          CVisibleRect::top().y           );
     d("rightTop:    %f %f", CVisibleRect::rightTop().x,     CVisibleRect::rightTop().y      );
     d("leftTop:     %f %f", CVisibleRect::leftTop().x,      CVisibleRect::leftTop().y       );
     d("left:        %f %f", CVisibleRect::left().x,         CVisibleRect::left().y          );
     d("right:       %f %f", CVisibleRect::right().x,        CVisibleRect::right().y         );
     d("leftBottom:  %f %f", CVisibleRect::leftBottom().x,   CVisibleRect::leftBottom().y    );
     d("rightBottom: %f %f", CVisibleRect::rightBottom().x,  CVisibleRect::rightBottom().y   );
     d("center:      %f %f", CVisibleRect::center().x,       CVisibleRect::center().y        );
     d("bottom:      %f %f", CVisibleRect::bottom().x,       CVisibleRect::bottom().y        );
     */
#endif

    return true;
}
bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
	CCSize frameSize = pEGLView->getFrameSize();

    pDirector->setOpenGLView(pEGLView);

    std::vector<std::string> searchPaths;

#if MC_ADAPTIVE_RESOLUTION == 1
    // Set the design resolution
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
    pEGLView->setDesignResolutionSize(PCResource.size.width, PCResource.size.height, kResolutionExactFit);
    searchPaths.push_back(PCResource.directory);
//    pDirector->setContentScaleFactor(MIN(32 * 25 / PCResource.size.width, 32 * 15 / PCResource.size.height));
//    pDirector->setContentScaleFactor(0.75f);
    pDirector->setContentScaleFactor(MIN(32 * 25 / frameSize.width, 32 * 15 / frameSize.height));
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        // if the frame's height is larger than the height of medium resource size, select large resource.
	if (frameSize.height == smallResource.size.height) {
        pEGLView->setDesignResolutionSize(smallResource.size.width, smallResource.size.height, kResolutionNoBorder);
        searchPaths.push_back(smallResource.directory);
	}
        // if the frame's height is larger than the height of small resource size, select medium resource.
    else if (frameSize.height == mediumResource.size.height) {
        pEGLView->setDesignResolutionSize(mediumResource.size.width, mediumResource.size.height, kResolutionNoBorder);
        searchPaths.push_back(mediumResource.directory);
    }
        // if the frame's height is smaller than the height of medium resource size, select small resource.
	else {
        pEGLView->setDesignResolutionSize(largeResource.size.width, largeResource.size.height, kResolutionNoBorder);
		searchPaths.push_back(largeResource.directory);
    }
    pDirector->setContentScaleFactor(MIN(32 * 25 / frameSize.width, 32 * 15 / frameSize.height));
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    int i = 0;
    int size = sizeof(resources);
    int found = 0;
    for (; i < size; ++i) {
        if ((frameSize.width == resources[i].size.width && frameSize.height == resources[i].size.height)
            || (frameSize.width == resources[i].size.height && frameSize.height == resources[i].size.width)) {
            searchPaths.push_back(resources[i].directory);
            pEGLView->setDesignResolutionSize(resources[i].size.width, resources[i].size.height, kResolutionNoBorder);
            pDirector->setContentScaleFactor(MIN(32 * 25 / resources[i].size.width, 32 * 15 / resources[i].size.height));
            found = 1;
            break;
        }
    }
    if (found == 0) {
		searchPaths.push_back(resources[0].directory);
        pEGLView->setDesignResolutionSize(resources[0].size.width, resources[0].size.height, kResolutionNoBorder);
        pDirector->setContentScaleFactor(MIN(32 * 25 / resources[0].size.width, 32 * 15 / resources[0].size.height));
    }
#else
    // if the frame's height is larger than the height of medium resource size, select large resource.
	if (frameSize.height > mediumResource.size.height) {
        searchPaths.push_back(largeResource.directory);
        pEGLView->setDesignResolutionSize(largeResource.size.width, largeResource.size.height);
        pDirector->setContentScaleFactor(MIN(largeResource.size.height/smallResource.size.height, largeResource.size.width/smallResource.size.width));
	}
    // if the frame's height is larger than the height of small resource size, select medium resource.
    else if (frameSize.height > smallResource.size.height) {
        searchPaths.push_back(mediumResource.directory);
        pEGLView->setDesignResolutionSize(mediumResource.size.width, mediumResource.size.height);
        pDirector->setContentScaleFactor(MIN(mediumResource.size.height/smallResource.size.height, mediumResource.size.width/smallResource.size.width));
    }
    // if the frame's height is smaller than the height of medium resource size, select small resource.
	else {
		searchPaths.push_back(smallResource.directory);
        pEGLView->setDesignResolutionSize(smallResource.size.width, smallResource.size.height);
        pDirector->setContentScaleFactor(MIN(smallResource.size.height, smallResource.size.width));
    }
#endif // platforms
#else
    CCSize designSize = CCSizeMake(960, 640);
    
    searchPaths.push_back(CommonResource.directory);
    if (frameSize.height > 1280) {
        CCSize resourceSize = CCSizeMake(2048, 1536);
        std::vector<std::string> searchPaths;
//        searchPaths.push_back("hd"); //没资源
        pDirector->setContentScaleFactor(0.75 * resourceSize.height / designSize.height);
    } else {
        pDirector->setContentScaleFactor(0.75);
    }
    
    CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionFixedWidth);
#endif // MC_ADAPTIVE_RESOLUTION == 0
    CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
	
    // turn on display FPS
//    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);
    
    CCLog("%s(%d): %.0f %.0f %.0f", __FILE__, __LINE__, frameSize.width, frameSize.height, pDirector->getContentScaleFactor());
    
    // register lua engine
    CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
    CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
    
#if MC_DEBUG_SERVER == 1
    MCSimpleGameSceneContextServer *server = MCSimpleGameSceneContextServer::defaultSimpleGameSceneContextServer();
    server->setPort(kMCListeningPort);
    server->runloop();
#endif
    
    // run
//    pDirector->pushScene(MCMainMenuLayer::scene());
//    pDirector->runWithScene(MCSplashLayer::scene());
    
    pDirector->runWithScene(MCTestbed::scene());

    return true;
}
Example #4
0
bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

    pDirector->setOpenGLView(pEGLView);
	

    // マルチレゾリューション対応 START
    // Set the design resolution
    pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionShowAll);
    
	CCSize frameSize = pEGLView->getFrameSize();
    
    std::vector<std::string> searchPath;
    
    // In this demo, we select resource according to the frame's height.
    // If the resource size is different from design resolution size, you need to set contentScaleFactor.
    // We use the ratio of resource's height to the height of design resolution,
    // this can make sure that the resource's height could fit for the height of design resolution.
    
    
    cocos2d::CCLog("-------");
    cocos2d::CCLog("height:%f", frameSize.height);
    cocos2d::CCLog("width:%f", frameSize.width);
	if (frameSize.height > largeResource.size.height)
	{
        cocos2d::CCLog("xlargeResource");
        searchPath.push_back(xlargeResource.directory);
        pDirector->setContentScaleFactor(MIN(xlargeResource.size.height/designResolutionSize.height, xlargeResource.size.width/designResolutionSize.width));
	}
    else if (frameSize.height > smallResource.size.height)
    {
        cocos2d::CCLog("largeResource");
        searchPath.push_back(largeResource.directory);
        pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));
    }
	else
    {
        cocos2d::CCLog("smallResource");
        searchPath.push_back(smallResource.directory);
        pDirector->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));
    }
    
    
    // デバッグログ
    for (std::vector<std::string>::iterator itiElement = searchPath.begin(); itiElement != searchPath.end(); ++itiElement) {
        std::string iElement = *itiElement;      // itiElement を使って繰り返し、配列要素を扱う。
        cocos2d::CCLog("searchPath:%s", iElement.c_str());
    }
    float scale = pDirector->getContentScaleFactor();
    cocos2d::CCLog("scale:%f", scale);

    
    // set searching path
    CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
    // マルチレゾリューション対応 END
    
    
    // turn on display FPS
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);
    
    
    // cocos builder 読み込み
    CCNodeLoaderLibrary* ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary();
    ccNodeLoaderLibrary->registerCCNodeLoader("MenuLayer", MenuLayerLoader::loader());
    CCBReader* ccbReader = new CCBReader(ccNodeLoaderLibrary);
    CCNode* node = ccbReader->readNodeGraphFromFile("MenuLayer.ccbi");
    
    CCScene* pScene = CCScene::create();
    if (node != NULL) {
        pScene->addChild(node);
    }
    ccbReader->release();

    // run
    pDirector->runWithScene(pScene);

    return true;
}