void VisibleRect::lazyInit() { if (s_visibleRect.size.width == 0.0f && s_visibleRect.size.height == 0.0f) { CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); s_visibleRect.origin = pEGLView->getVisibleOrigin(); s_visibleRect.size = pEGLView->getVisibleSize(); } }
bool PPGameEndLayer::init() { bool bRet = false; do { CC_BREAK_IF(! CCLayerColor::initWithColor( ccc4(210, 205, 209, 50) ) ); setTouchEnabled(true); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); CCSize size = pEGLView->getVisibleSize(); CCSprite* p = CCSprite::create("board.png"); p->setAnchorPoint( ccp(0.5, 0.5) ); p->setPosition( ccp(size.width / 2, size.height / 2) ); addChild(p); CCActionInterval* actionFi = CCFadeIn::create(0.1f); CCSpawn* actionS = CCSpawn::create(actionFi, CCScaleTo::create(0.15, 0.1), NULL); CCScaleTo* as1 = CCScaleTo::create(0.1, 0.9); CCScaleTo* as2 = CCScaleTo::create(0.05, 1.0); CCFiniteTimeAction* action = CCSequence::create(actionS, as1, as2, NULL); p->runAction(action); bRet = true; } while (0); return bRet; }
// on "init" you need to initialize your instance bool PPMainMenuLayer::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayerColor::initWithColor( ccc4(240, 235, 239, 255) ) ); ////////////////////////////////////////////////////////////////////////// CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); CCSize size = pEGLView->getVisibleSize(); CCLabelTTF* label = CCLabelTTF::create("JELLY", "Consolas", 40); label->setColor( ccc3(0, 0, 0) ); label->setAnchorPoint( ccp(0.5, 0.5) ); label->setPosition( ccp(size.width / 2, size.height * 2 / 3) ); addChild(label); PPCube* p = new PPCube( PPCube::PPCubeType_S ); p->setAnchorPoint( ccp(0.5, 0.5) ); p->setPosition( ccp( size.width / 2, (size.height * 2 / 3 - label->getContentSize().height) ) ); addChild(p); p->release(); label = CCLabelTTF::create("Play", "Consolas", 20); label->setColor( ccc3(0, 0, 0) ); CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(PPMainMenuLayer::MainMenuCallback)); CCMenu* pMenu =CCMenu::create(pMenuItem, NULL); pMenu->setAnchorPoint( ccp(0.5, 0.5) ); pMenu->setPosition( ccp(size.width / 2, size.height / 4) ); addChild(pMenu); bRet = true; } while (0); return bRet; }
bool AppDelegate::applicationDidFinishLaunching() { // initialize director CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); pDirector->setOpenGLView(pEGLView); CCSize frameSize = pEGLView->getFrameSize(); gDesignSize.width = 480; gDesignSize.height = 320; float fResouceWH = gDesignSize.width / gDesignSize.height; float fViewWH = frameSize.width / frameSize.height; if (fViewWH >= fResouceWH) { pEGLView->setDesignResolutionSize(gDesignSize.width, gDesignSize.height, kResolutionFixedHeight); gSolutionType = kResolutionFixedHeight; } else { pEGLView->setDesignResolutionSize(gDesignSize.width, gDesignSize.height, kResolutionFixedWidth); gSolutionType = kResolutionFixedWidth; } gViewSize = pEGLView->getVisibleSize(); // 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); // create a scene. it's an autorelease object //CCScene *pScene = HelloWorld::scene(); CCScene *pScene = GameLayer::scene(); // run pDirector->runWithScene(pScene); return true; }
bool AppDelegate::applicationDidFinishLaunching() { CCLog("Finished launching"); srand(time(0)); System::_ld.init(); // initialize director CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); pDirector->setOpenGLView(pEGLView); // Set the design resolution pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionNoBorder); CCSize frameSize = pEGLView->getFrameSize(); // 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. CCSize vsize = pEGLView->getVisibleSize(); // if the frame's height is larger than the height of medium resource size, select large resource. float width_change = vsize.width / designResolutionSize.width; // if the frame's height is larger than the height of medium resource size, select large resource. CCFileUtils::sharedFileUtils()->setResourceDirectory(mediumResource.directory); pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height/width_change, mediumResource.size.width/designResolutionSize.width/width_change)); // turn on display FPS pDirector->setDisplayStats(false); // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object ScriptPtr main_script(0); IntentParams::Mode mode = IntentParams::getInstance().getMode(); if(mode == IntentParams::WakeUpMode) main_script = ScriptPtr(new WakeUpScript); else main_script = ScriptPtr(new MainScript); loadTranslation(getDeviceLang()); configureContentProviders(); // //JokesHolder::sharedHolder().updateJokes(); CCScene *pScene = ScriptScene::scene(main_script); // run pDirector->runWithScene(pScene); _is_running = true; CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("choose.wav"); return true; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSprite* pBackground = CCSprite::create("background.png"); pBackground->setPosition(ccp(size.width / 2, size.height / 2)); addChild(pBackground); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); CCPoint posBR = ccp(pEGLView->getVisibleOrigin().x + pEGLView->getVisibleSize().width, pEGLView->getVisibleOrigin().y); CCPoint posTL = ccp(pEGLView->getVisibleOrigin().x, pEGLView->getVisibleOrigin().y + pEGLView->getVisibleSize().height); // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(posBR.x - 20, posBR.y + 20) ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); CCPoint posStep = ccp(220, -150); CCPoint beginPos = ccpAdd(posTL, ccpMult(posStep, 0.5f)); int line = 0; int row = 0; for (int i = 0; i < sizeof(s_EventMenuItem)/sizeof(s_EventMenuItem[0]); i++) { CCMenuItemImage* pMenuItem = CCMenuItemImage::create(s_EventMenuItem[i].id.c_str(), s_EventMenuItem[i].id.c_str(), this, menu_selector(HelloWorld::eventMenuCallback)); pMenu->addChild(pMenuItem, 0, s_EventMenuItem[i].tag); CCPoint pos = ccpAdd(beginPos, ccp(posStep.x * row, posStep.y * line)); CCSize itemSize = pMenuItem->getContentSize(); if ((pos.x + itemSize.width / 2) > posBR.x) { line += 1; row = 0; pos = ccpAdd(beginPos, ccp(posStep.x * row, posStep.y * line)); } row += 1; pMenuItem->setPosition(pos); } CCLabelTTF* label = CCLabelTTF::create("reload all plugins", "Arial", 24); CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(HelloWorld::reloadPluginMenuCallback)); pMenuItem->setAnchorPoint(ccp(0.5f, 0)); pMenu->addChild(pMenuItem, 0); pMenuItem->setPosition( ccp(size.width / 2, 0)); return true; }
/////////////////////////////////////////// //@android: for test only void dumpCocos2dx() { string strLine = "@@------------------------------------------------------------{{\r\n"; char szInfo[1024] = ""; CCLog( strLine.c_str() ); __flog( strLine.c_str()); // dump NDDirector & CCDirector { sprintf( szInfo, "[CCDirector] size in Points (%d, %d)\r\n" "[CCDirector] size in Pixels (%d, %d)\r\n" "[CCDirector] content scale = %.1f\r\n", (int)CCDirector::sharedDirector()->getWinSize().width, (int)CCDirector::sharedDirector()->getWinSize().height, (int)CCDirector::sharedDirector()->getWinSizeInPixels().width, (int)CCDirector::sharedDirector()->getWinSizeInPixels().height, CCDirector::sharedDirector()->getContentScaleFactor() ); CCLog( szInfo ); __flog( szInfo ); } // dump EGL view { CCEGLView* pkEglView = CCDirector::sharedDirector()->getOpenGLView(); if (pkEglView) { sprintf( szInfo, "\r\n" "[EGLVIEW] frame size (%d, %d)\t\t[AKA: Screen Size]\r\n" "[EGLVIEW] designed size (%d, %d)\r\n" "[EGLVIEW] scale (%.2f, %.2f)\r\n" "\r\n" "[EGLVIEW] viewport org (%d, %d)\r\n" "[EGLVIEW] viewport size (%d, %d)\r\n" "\r\n" "[EGLVIEW] visible org (%d, %d)\r\n" "[EGLVIEW] visible size (%d, %d)\r\n" "\r\n" "[EGLVIEW] retina enabled (%d)\r\n" //"[EGLVIEW] resolution policy (%d)\r\n" , /*frame*/ (int)pkEglView->getFrameSize().width, (int)pkEglView->getFrameSize().height, /*designed*/(int)pkEglView->getSize().width, (int)pkEglView->getSize().height, /*scale*/ pkEglView->getScaleX(), pkEglView->getScaleY(), /*viewport*/(int)pkEglView->getViewPortRect().origin.x, (int)pkEglView->getViewPortRect().origin.y, /*viewport*/(int)pkEglView->getViewPortRect().size.width, (int)pkEglView->getViewPortRect().size.height, /*vis org*/ (int)pkEglView->getVisibleOrigin().x, (int)pkEglView->getVisibleOrigin().y, /*vis size*/(int)pkEglView->getVisibleSize().width, (int)pkEglView->getVisibleSize().height, /*retina*/ (int)pkEglView->isRetinaEnabled() ///*policy*/ (int)eglView->m_eResolutionPolicy ); CCLog( szInfo ); __flog( szInfo ); } } strLine = "@@------------------------------------------------------------}}\r\n"; CCLog( strLine.c_str()); __flog_flush( strLine.c_str()); }
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; }