CCTexture2D* MapTexturePool::addImage(const char* path, bool keep) { NDAsssert(path != NULL); CCTexture2D* pkTexture = NULL; // MUTEX: // Needed since addImageAsync calls this method from a different thread pkTexture = (CCTexture2D*)m_pkDict->objectForKey(path); if (!pkTexture) { CCImage image; if (image.initWithImageFile(path)) { //todo //tex = [ [CCTexture2D alloc] initWithImage:image keepData:keep]; } if (pkTexture) { m_pkDict->setObject(pkTexture, path); pkTexture->release(); } } return pkTexture; }
void CCLabelTTF::setString(const char *label) { if (strlen(label)==0) { return; } m_sString = string(label); CCTexture2D *texture; if( CGSize::CGSizeEqualToSize( m_tDimensions, CGSizeZero ) ) { texture = new CCTexture2D(); texture->initWithString(label, m_sFontName.c_str(), m_fFontSize); } else { texture = new CCTexture2D(); texture->initWithString(label, m_tDimensions, m_eAlignment, m_sFontName.c_str(), m_fFontSize); } this->setTexture(texture); texture->release(); CGRect rect = CGRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); }
void SuperAnimSprite::SetTexture(CCTexture2D *theTexture, CCRect theTextureRect) { if (theTexture == NULL) { return; } if (mTexture != NULL) { mTexture->release(); mTexture = NULL; } // retain this texture in case removed by removeUnusedTextures(); theTexture->retain(); mTexture = theTexture; // Set Texture coordinates CCRect theTexturePixelRect = CC_RECT_POINTS_TO_PIXELS(theTextureRect); float aTextureWidth = (float)mTexture->getPixelsWide(); float aTextureHeight = (float)mTexture->getPixelsHigh(); float aLeft, aRight, aTop, aBottom; aLeft = theTexturePixelRect.origin.x / aTextureWidth; aRight = (theTexturePixelRect.origin.x + theTexturePixelRect.size.width) / aTextureWidth; aTop = theTexturePixelRect.origin.y / aTextureHeight; aBottom = (theTexturePixelRect.origin.y + theTexturePixelRect.size.height) / aTextureHeight; mQuad.bl.texCoords.u = aLeft; mQuad.bl.texCoords.v = aBottom; mQuad.br.texCoords.u = aRight; mQuad.br.texCoords.v = aBottom; mQuad.tl.texCoords.u = aLeft; mQuad.tl.texCoords.v = aTop; mQuad.tr.texCoords.u = aRight; mQuad.tr.texCoords.v = aTop; // Set position //float x1 = 0; //float y1 = 0; //float x2 = x1 + theTextureRect.size.width; //float y2 = y1 + theTextureRect.size.height; float x1 = theTexturePixelRect.size.width * -0.5f; float y1 = theTexturePixelRect.size.height * -0.5f; float x2 = theTexturePixelRect.size.width * 0.5f; float y2 = theTexturePixelRect.size.height * 0.5f; mQuad.bl.vertices = vertex3(x1, y1, 0); mQuad.br.vertices = vertex3(x2, y1, 0); mQuad.tl.vertices = vertex3(x1, y2, 0); mQuad.tr.vertices = vertex3(x2, y2, 0); // Set color ccColor4B aDefaultColor = {255, 255, 255, 255}; mQuad.bl.colors = aDefaultColor; mQuad.br.colors = aDefaultColor; mQuad.tl.colors = aDefaultColor; mQuad.tr.colors = aDefaultColor; }
// Helper bool CCLabelTTF::updateTexture() { CCTexture2D *tex; // let system compute label's width or height when its value is 0 // refer to cocos2d-x issue #1430 tex = new CCTexture2D(); if (!tex) { return false; } tex->initWithString( m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment); this->setTexture(tex); tex->release(); CCRect rect = CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); return true; }
void CCLabelTTF::setString(const char *label) { if (m_pString) { delete m_pString; m_pString = NULL; } m_pString = new std::string(label); CCTexture2D *texture; if( CCSize::CCSizeEqualToSize( m_tDimensions, CCSizeZero ) ) { texture = new CCTexture2D(); texture->initWithString(label, m_pFontName->c_str(), m_fFontSize); } else { texture = new CCTexture2D(); texture->initWithString(label, m_tDimensions, m_eAlignment, m_pFontName->c_str(), m_fFontSize); } this->setTexture(texture); texture->release(); CCRect rect = CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); }
// Helper void CCLabelTTF::updateTexture() { CCTexture2D *tex; if (m_tDimensions.width == 0 || m_tDimensions.height == 0) { tex = new CCTexture2D(); tex->initWithString(m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR()) ; } else { tex = new CCTexture2D(); tex->initWithString(m_string.c_str(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment, m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR()); } // iPad ? //if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) { if (CCApplication::sharedApplication().isIpad()) { if (CC_CONTENT_SCALE_FACTOR() == 2) { tex->setResolutionType(kCCResolutioniPadRetinaDisplay); } else { tex->setResolutionType(kCCResolutioniPad); } } // iPhone ? else { if (CC_CONTENT_SCALE_FACTOR() == 2) { tex->setResolutionType(kCCResolutioniPhoneRetinaDisplay); } else { tex->setResolutionType(kCCResolutioniPhone); } } this->setTexture(tex); tex->release(); CCRect rect = CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); }
void EaseSpriteDemo::onEnter() { CCLayer::onEnter(); // Example: // You can create a sprite using a Texture2D CCTexture2D* tex = new CCTexture2D(); UIImage* pImage = new UIImage(); if(pImage->initWithContentsOfFile(CCFileUtils::fullPathFromRelativePath(s_pPathGrossini))) { tex->initWithImage(pImage); m_grossini = CCSprite::spriteWithTexture(tex); m_grossini->retain(); } tex->release(); delete pImage; // Example: // Or you can create an sprite using a filename. PNG and BMP files are supported. Probably TIFF too m_tamara = CCSprite::spriteWithFile(s_pPathSister1); m_tamara->retain(); m_kathia = CCSprite::spriteWithFile(s_pPathSister2); m_kathia->retain(); addChild( m_grossini, 3); addChild( m_kathia, 2); addChild( m_tamara, 1); CGSize s = CCDirector::sharedDirector()->getWinSize(); m_grossini->setPosition( CGPointMake(60, 50) ); m_kathia->setPosition( CGPointMake(60, 150) ); m_tamara->setPosition( CGPointMake(60, 250) ); CCLabelTTF* label = CCLabelTTF::labelWithString(title().c_str(), "Arial", 32); addChild(label); label->setPosition( CGPointMake(s.width/2, s.height-50) ); CCMenuItemImage *item1 = CCMenuItemImage::itemFromNormalImage(s_pPathB1, s_pPathB2, this, menu_selector(EaseSpriteDemo::backCallback) ); CCMenuItemImage *item2 = CCMenuItemImage::itemFromNormalImage(s_pPathR1, s_pPathR2, this, menu_selector(EaseSpriteDemo::restartCallback) ); CCMenuItemImage *item3 = CCMenuItemImage::itemFromNormalImage(s_pPathF1, s_pPathF2, this, menu_selector(EaseSpriteDemo::nextCallback) ); CCMenu *menu = CCMenu::menuWithItems(item1, item2, item3, NULL); menu->setPosition( CGPointZero ); item1->setPosition( CGPointMake( s.width/2 - 100,30) ); item2->setPosition( CGPointMake( s.width/2, 30) ); item3->setPosition( CGPointMake( s.width/2 + 100,30) ); addChild(menu, 1); }
void CCLabelTTF::setString(const char *label) { if (m_pString) { delete m_pString; m_pString = NULL; } m_pString = new std::string(label); CCTexture2D *texture; if( CCSize::CCSizeEqualToSize( m_tDimensions, CCSizeZero ) ) { texture = new CCTexture2D(); texture->initWithString(label, m_pFontName->c_str(), m_fFontSize); } else { texture = new CCTexture2D(); texture->initWithString(label, m_tDimensions, m_eAlignment, m_pFontName->c_str(), m_fFontSize); } // TODO // #ifdef __CC_PLATFORM_IOS // // iPad ? // if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) { // if( CC_CONTENT_SCALE_FACTOR() == 2 ) // [tex setResolutionType:kCCResolutioniPadRetinaDisplay]; // else // [tex setResolutionType:kCCResolutioniPad]; // } // // iPhone ? // else // { // if( CC_CONTENT_SCALE_FACTOR() == 2 ) // [tex setResolutionType:kCCResolutioniPhoneRetinaDisplay]; // else // [tex setResolutionType:kCCResolutioniPhone]; // } // #end this->setTexture(texture); texture->release(); CCRect rect = CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); }
void UITextInputField::CreateWhiteBack() { if(m_pWhiteBack != NULL) { m_pWhiteBack->removeFromParentAndCleanup(true); m_pWhiteBack = NULL; } if(m_pTextInputField == NULL) { return; } CCSize dimension = m_pTextInputField->getDimensions(); int dimensionWidth = dimension.width; int dimensionHeight = dimension.height; if(dimensionWidth != 0 && dimensionHeight != 0) { int *pixels = new int[(dimensionWidth + 8) * (dimensionHeight + 8)]; for (int i=0; i<dimensionWidth+8; ++i) { for (int j=0; j<dimensionHeight+8; ++j) { //pixels[i][j] = 0xffffffff; pixels[i*4+j] = 0xffffffff; } } CCTexture2D *texture = new CCTexture2D(); texture->initWithData(pixels, kCCTexture2DPixelFormat_RGB888, 1, 1, CCSizeMake(dimensionWidth + 8, dimensionHeight + 8) ); delete[] pixels; m_pWhiteBack = CCSprite::createWithTexture(texture); texture->release(); } }
//------------------------------------------------------------------------- bool FKCW_UI_ProgressIndicator::initWithMessage(const string& message) { if(!CCLayerColor::initWithColor(m_tagDimColor)) { return false; } // 保存消息 m_strMessage = message; // 创建Label m_pMsgLabel = CCLabelTTF::create(m_strMessage.c_str(), "Helvetica", 28 / CC_CONTENT_SCALE_FACTOR()); // 提示器 CCImage* image = new CCImage(); image->initWithImageData((void*)s_SpinnerPng, 1043); CCTexture2D* tex = new CCTexture2D(); tex->initWithImage(image); m_pIndicator = CCSprite::createWithTexture(tex); CC_SAFE_RETAIN(m_pIndicator); image->release(); tex->release(); _RelayOut(); addChild(m_pIndicator); addChild(m_pMsgLabel); // 开启事件 setTouchEnabled(true); setTouchMode(kCCTouchesOneByOne); setTouchPriority(-MAX_INT); setKeypadEnabled(true); // 开启帧更新 scheduleUpdate(); return true; }
CCSize CCtrlEdit::GetTextSize(const char * text) { CCSize textSize(0.0f,0.0f); if (NULL == text) { return textSize; } do { CCTexture2D *tex = new CCTexture2D(); CC_BREAK_IF(!tex); float fContentScaleFactor= CCDirector::sharedDirector()->getContentScaleFactor(); bool bRet = tex->initWithString( text, m_strFontName.c_str(), m_nFontSize * fContentScaleFactor, CCSizeZero, m_pTextField->getHorizontalAlignment(), m_pTextField->getVerticalAlignment()); if (bRet) { float fOffsetX = tex->getPixelsWide(); fOffsetX /= fContentScaleFactor; textSize.width += fOffsetX; float fOffsetY = tex->getPixelsHigh(); fOffsetY /= fContentScaleFactor; textSize.height += fOffsetY; } tex->release(); }while(false); return textSize; }
// Helper bool CCLabelTTF::updateTexture() { CCTexture2D *tex; tex = new CCTexture2D(); if (!tex) return false; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) ccFontDefinition texDef = _prepareTextDefinition(true); tex->initWithString( m_string.c_str(), &texDef ); #else tex->initWithString( m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment); #endif // set the texture this->setTexture(tex); // release it tex->release(); // set the size in the sprite CCRect rect =CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); //ok return true; }
bool CCGridBase::initWithSize(ccGridSize gridSize) { CCDirector *pDirector = CCDirector::sharedDirector(); CGSize s = pDirector->getWinSizeInPixels(); unsigned int POTWide = ccNextPOT((unsigned int)s.width); unsigned int POTHigh = ccNextPOT((unsigned int)s.height); // on mac, it use kCCTexture2DPixelFormat_RGBA8888 CCTexture2DPixelFormat format = kCCTexture2DPixelFormat_RGBA8888; void *data = calloc((int)(POTWide * POTHigh * 4), 1); if (! data) { CCLOG("cocos2d: CCGrid: not enough memory."); this->release(); return false; } CCTexture2D *pTexture = new CCTexture2D(); pTexture->initWithData(data, format, POTWide, POTHigh, s); free(data); if (! pTexture) { CCLOG("cocos2d: CCGrid: error creating texture"); delete this; return false; } initWithSize(gridSize, pTexture, false); pTexture->release(); return true; }
bool CCGridBase::initWithSize(const ccGridSize& gridSize) { CCDirector *pDirector = CCDirector::sharedDirector(); CCSize s = pDirector->getWinSizeInPixels(); unsigned int POTWide = (unsigned int) s.width; unsigned int POTHigh = (unsigned int) s.height; bool needPOT = !g_Render->isRenderTargetNPOTSupported(); if (needPOT) { POTWide = PixelFormat::calcNextPot(POTWide); POTHigh = PixelFormat::calcNextPot(POTHigh); } // we only use rgba8888 CCTexture2DPixelFormat format = kCCTexture2DPixelFormat_RGBA8888; CCTexture2D *pTexture = new CCTexture2D(); if (! pTexture) { CCLOG("cocos2d: CCGrid: error creating texture"); delete this; return false; } pTexture->initWithTexture( new GLESTexture(POTWide, POTHigh, CCTexture2D::ToNitPixelFormat(format, false))); initWithSize(gridSize, pTexture, false); pTexture->release(); return true; }
void ActionsDemo::onEnter() { CCLayer::onEnter(); // Example: // You can create a sprite using a Texture2D CCTexture2D *tex = new CCTexture2D(); UIImage* pImage = new UIImage(); std::string fullpath( CCFileUtils::fullPathFromRelativePath(s_pPathGrossini)); pImage->initWithContentsOfFile(fullpath); tex->initWithImage( pImage ); m_grossini = CCSprite::spriteWithTexture(tex); m_grossini->retain(); delete pImage; tex->release(); // Example: // Or you can create an sprite using a filename. only PNG is supported now. Probably TIFF too m_tamara = CCSprite::spriteWithFile(s_pPathSister1); m_tamara->retain(); m_kathia = CCSprite::spriteWithFile(s_pPathSister2); m_kathia->retain(); addChild(m_grossini, 1); addChild(m_tamara, 2); addChild(m_kathia, 3); CGSize s = CCDirector::sharedDirector()->getWinSize(); m_grossini->setPosition( CGPointMake(s.width/2, s.height/3)); m_tamara->setPosition( CGPointMake(s.width/2, 2*s.height/3)); m_kathia->setPosition( CGPointMake(s.width/2, s.height/2)); // add title and subtitle std::string str = title(); const char * pTitle = str.c_str(); CCLabelTTF* label = CCLabelTTF::labelWithString(pTitle, "Arial", 18); addChild(label, 1); label->setPosition( CGPointMake(s.width/2, s.height - 30) ); std::string strSubtitle = subtitle(); if( ! strSubtitle.empty() ) { CCLabelTTF* l = CCLabelTTF::labelWithString(strSubtitle.c_str(), "Thonburi", 22); addChild(l, 1); l->setPosition( CGPointMake(s.width/2, s.height - 60) ); } // add menu CCMenuItemImage *item1 = CCMenuItemImage::itemFromNormalImage(s_pPathB1, s_pPathB2, this, menu_selector(ActionsDemo::backCallback) ); CCMenuItemImage *item2 = CCMenuItemImage::itemFromNormalImage(s_pPathR1, s_pPathR2, this, menu_selector(ActionsDemo::restartCallback) ); CCMenuItemImage *item3 = CCMenuItemImage::itemFromNormalImage(s_pPathF1, s_pPathF2, this, menu_selector(ActionsDemo::nextCallback) ); CCMenu *menu = CCMenu::menuWithItems(item1, item2, item3, NULL); menu->setPosition( CGPointZero ); item1->setPosition( CGPointMake( s.width/2 - 100,30) ); item2->setPosition( CGPointMake( s.width/2, 30) ); item3->setPosition( CGPointMake( s.width/2 + 100,30) ); addChild(menu, 1); }
// Helper bool CCLabelTTF::updateTexture() { //if(this->isFlipY()) // this->setFlipY(false); CCTexture2D *tex; tex = new CCTexture2D(); if (!tex) return false; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) ccFontDefinition texDef = _prepareTextDefinition(true); tex->initWithString( m_string.c_str(), &texDef ); #else tex->initWithString( m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment); #endif // set the texture this->setTexture(tex); // release it tex->release(); // set the size in the sprite CCRect rect =CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); /***************************给文字描黑边begin*********************************/ CCPoint prePos = this->getPosition(); ccColor3B color1 = this->getColor(); float strokeValue = 2; CCSize textureSize = this->getContentSize(); textureSize.width += 2 * strokeValue; textureSize.height += 2 * strokeValue; /* 监测OpenGl的错误状态 */ glGetError(); /* 创建一张纹理画布 */ CCRenderTexture *rt = CCRenderTexture::create(textureSize.width, textureSize.height); if(!rt) { CCLog("create render texture failed !!!!"); //addChild(label); return 0; } /* 设置描边的颜色 */ this->setColor(ccBLACK); /* *拿到源文字的混色机制,存储以备恢复,并设置新的目标混色机制 *混色机制设为:源颜色透明度(影响亮度)和目标颜色(影响颜色) */ ccBlendFunc originalBlend = this->getBlendFunc(); ccBlendFunc func = { GL_SRC_ALPHA, GL_ONE}; this->setBlendFunc(func); CCPoint anchPoint = this->getAnchorPoint(); /* 这是自定义的一些调整,倾斜了一点 */ this->setAnchorPoint(CCPoint(0.5, 0.5)); //label->setRotationX(15); /* 张开画布,开始绘画 */ rt->begin(); this->setFlipY(true); for(int i = 0; i < 360; i += 5)//每变化5度绘制一张 { float r = CC_DEGREES_TO_RADIANS(i); //度数格式的转换 this->setPosition(CCPoint(textureSize.width * 0.5f + sin(r) * strokeValue,textureSize.height * 0.5f + cos(r) * strokeValue)); /* CCRenderTexture的用法,在begin和end之间visit的纹理,都会画在CCRenderTexture里面 */ this->visit();//画了一次该label } /* 恢复原始的label并绘制在最上层 */ this->setColor(color1); this->setBlendFunc(originalBlend); this->setPosition(CCPoint(textureSize.width * 0.5f, textureSize.height * 0.5f)); this->visit(); this->setFlipY(false); /* 在画布上绘制结束,此时会生成一张纹理 */ rt->end(); /* 取出生成的纹理,添加抗锯齿打磨,并返回 */ CCTexture2D *texture = rt->getSprite()->getTexture(); texture->setAntiAliasTexParameters();// setAliasTexParameters(); this->setTexture(texture); // release it rt->release(); //set the size in the sprite rect =CCRectZero; rect.size = m_pobTexture->getContentSize(); //this->setFlipY(true); //this->setFlipX(true); this->setTextureRect(rect); //if(!this->isFlipY()) // this->setFlipY(true); this->setAnchorPoint(anchPoint); this->setColor(color1); this->setPosition(prePos); /***************************给文字描黑边end*********************************/ //ok return true; }
CCTexture2D * CCTextureCache::addImage(const char * path) { CCAssert(path != NULL, "TextureCache: fileimage MUST not be NULL"); CCTexture2D * texture = NULL; // Split up directory and filename // MUTEX: // Needed since addImageAsync calls this method from a different thread m_pDictLock->lock(); // remove possible -HD suffix to prevent caching the same image twice (issue #1040) std::string pathKey = path; CCFileUtils::ccRemoveHDSuffixFromFile(pathKey); pathKey = CCFileUtils::fullPathFromRelativePath(pathKey.c_str()); texture = m_pTextures->objectForKey(pathKey); std::string fullpath = pathKey; // (CCFileUtils::fullPathFromRelativePath(path)); if( ! texture ) { std::string lowerCase(path); for (unsigned int i = 0; i < lowerCase.length(); ++i) { lowerCase[i] = tolower(lowerCase[i]); } // all images are handled by UIImage except PVR extension that is handled by our own handler // if ( [[path lowercaseString] hasSuffix:@".pvr"] ) do { if (std::string::npos != lowerCase.find(".pvr")) { #ifdef _POWERVR_SUPPORT_ texture = this->addPVRTCImage(fullpath.c_str()); #endif } // Issue #886: TEMPORARY FIX FOR TRANSPARENT JPEGS IN IOS4 else if (std::string::npos != lowerCase.find(".jpg") || std::string::npos != lowerCase.find(".jpeg")) { CCImage image; CCFileData data(fullpath.c_str(), "rb"); unsigned long nSize = data.getSize(); unsigned char* pBuffer = data.getBuffer(); CC_BREAK_IF(! image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtJpg)); texture = new CCTexture2D(); texture->initWithImage(&image); if( texture ) { #if CC_ENABLE_CACHE_TEXTTURE_DATA // cache the texture file name VolatileTexture::addImageTexture(texture, fullpath.c_str(), CCImage::kFmtJpg); #endif m_pTextures->setObject(texture, pathKey); texture->release(); } else { CCLOG("cocos2d: Couldn't add image:%s in CCTextureCache", path); } } else { // prevents overloading the autorelease pool CCImage image; CCFileData data(fullpath.c_str(), "rb"); unsigned long nSize = data.getSize(); unsigned char* pBuffer = data.getBuffer(); CC_BREAK_IF(! image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtPng)); texture = new CCTexture2D(); texture->initWithImage(&image); if( texture ) { #if CC_ENABLE_CACHE_TEXTTURE_DATA // cache the texture file name VolatileTexture::addImageTexture(texture, fullpath.c_str(), CCImage::kFmtPng); #endif m_pTextures->setObject(texture, pathKey); texture->release(); } else { CCLOG("cocos2d: Couldn't add image:%s in CCTextureCache", path); } } } while (0); } m_pDictLock->unlock(); return texture; }
void HelloWorld::update(float dt) { bool bComputeAgain = false; if (m_bSpeedUp) m_fElapsedTime = 1.0; else m_fElapsedTime += dt * 0.5; //m_fScale = pow(2.0f, m_fElapsedTime); if (m_fElapsedTime >= 1.0) //if (m_fScale >= 2.0) { //m_fScale /= 2.0; //m_fElapsedTime = 0; m_fElapsedTime = fmod(m_fElapsedTime, 1.0f); bComputeAgain = true; } //static int off = 0; // RGBA raw data 세팅함. /* float c1,c2,c3,c4,c5,c6,v; int indexColor; int offset = 0; c1=(float)(rand()%5+1); c2=(float)(rand()%5+1); c3=(float)(rand()%5+1); c4=(float)(rand()%10+1); c5=(float)(rand()%512+1); c6=(float)(rand()%512+1); for (int i=0; i<IMAGE_HEIGHT; i++) { for (int j=0; j<IMAGE_WIDTH; j++) { v = cos((float)(j-256+c5)/(cos((float)i/c1)+c3))+ cos((float)(j)*(float)(i/c2)+c6)+ cos((float)(j/c3)*(float)(i/c4)+c5); // 최종 결과물 = 높이값 indexColor = v*10.5+64; if (indexColor<0) indexColor = 0; else if (indexColor>255) indexColor=255; //(indexColor-64)*8; m_pIndexm_pIndexBuffer[offset++] = indexColor; } } */ int i, j; int x_q1, x_q3; int y_q1, y_q3; if (bComputeAgain) { // 각각 25%, 75% 지점의 좌표를 구한다. x_q1 = IMAGE_WIDTH/4; x_q3 = IMAGE_WIDTH - IMAGE_WIDTH/4 -1; y_q1 = IMAGE_HEIGHT/4; y_q3 = IMAGE_HEIGHT - IMAGE_HEIGHT/4 -1; for (i=0; i<IMAGE_HEIGHT/4; i++) { for (j=0; j<IMAGE_WIDTH/4; j++) { m_pIndexBuffer[i*2*IMAGE_WIDTH+j*2] = GetBuffer((y_q1+i)*IMAGE_WIDTH+x_q1+j,-1,-1); m_pIndexBuffer[i*2*IMAGE_WIDTH+j*2+1]= GetBuffer((y_q1+i)*IMAGE_WIDTH+x_q1+j,0,-1); m_pIndexBuffer[i*2*IMAGE_WIDTH+IMAGE_WIDTH-j*2-1]=GetBuffer((y_q1+i)*IMAGE_WIDTH+x_q3-j,1,-1); m_pIndexBuffer[i*2*IMAGE_WIDTH+IMAGE_WIDTH-j*2-2]=GetBuffer((y_q1+i)*IMAGE_WIDTH+x_q3-j,0,-1); m_pIndexBuffer[(i*2+1)*IMAGE_WIDTH+j*2]=GetBuffer((y_q1+i)*IMAGE_WIDTH+x_q1+j,-1,0); m_pIndexBuffer[(i*2+1)*IMAGE_WIDTH+j*2+1]=GetBuffer((y_q1+i)*IMAGE_WIDTH+x_q1+j,0,0); m_pIndexBuffer[(i*2+2)*IMAGE_WIDTH-j*2-1]=GetBuffer((y_q1+i)*IMAGE_WIDTH+x_q3-j,1,0); m_pIndexBuffer[(i*2+2)*IMAGE_WIDTH-j*2-2]=GetBuffer((y_q1+i)*IMAGE_WIDTH+x_q3-j,0,0); m_pIndexBuffer[(IMAGE_HEIGHT-1-i*2)*IMAGE_WIDTH+j*2]=GetBuffer((y_q3-i)*IMAGE_WIDTH+x_q1+j,-1,1); m_pIndexBuffer[(IMAGE_HEIGHT-1-i*2)*IMAGE_WIDTH+j*2+1]=GetBuffer((y_q3-i)*IMAGE_WIDTH+x_q1+j,0,1); m_pIndexBuffer[(IMAGE_HEIGHT-1-i*2+1)*IMAGE_WIDTH-j*2-1]=GetBuffer((y_q3-i)*IMAGE_WIDTH+x_q3-j,1,1); m_pIndexBuffer[(IMAGE_HEIGHT-1-i*2+1)*IMAGE_WIDTH-j*2-2]=GetBuffer((y_q3-i)*IMAGE_WIDTH+x_q3-j,0,1); m_pIndexBuffer[(IMAGE_HEIGHT-2-i*2)*IMAGE_WIDTH+j*2]=GetBuffer((y_q3-i)*IMAGE_WIDTH+x_q1+j,-1,0); m_pIndexBuffer[(IMAGE_HEIGHT-2-i*2)*IMAGE_WIDTH+j*2+1]=GetBuffer((y_q3-i)*IMAGE_WIDTH+x_q1+j,0,0); m_pIndexBuffer[(IMAGE_HEIGHT-2-i*2+1)*IMAGE_WIDTH-j*2-1]=GetBuffer((y_q3-i)*IMAGE_WIDTH+x_q3-j,1,0); m_pIndexBuffer[(IMAGE_HEIGHT-2-i*2+1)*IMAGE_WIDTH-j*2-2]=GetBuffer((y_q3-i)*IMAGE_WIDTH+x_q3-j,0,0); } } // 가운데 4 pixel에 랜덤값을 더함. m_pIndexBuffer[(IMAGE_HEIGHT/2-1)*IMAGE_WIDTH+(IMAGE_WIDTH/2-1)]+=rand()%6-2; m_pIndexBuffer[(IMAGE_HEIGHT/2-1)*IMAGE_WIDTH+(IMAGE_WIDTH/2)]+=rand()%5-2; m_pIndexBuffer[(IMAGE_HEIGHT/2)*IMAGE_WIDTH+(IMAGE_WIDTH/2-1)]+=rand()%5-2; m_pIndexBuffer[(IMAGE_HEIGHT/2)*IMAGE_WIDTH+(IMAGE_WIDTH/2)]+=rand()%5-2; // 인덱스 -> 이미지 변환 IndexToImage(m_pIndexBuffer, m_pImageBuffer, m_pPaletteBuffer, IMAGE_WIDTH*IMAGE_HEIGHT); //off++; m_pBitmap = new CCImage(); m_pBitmap->initWithImageData(m_pImageBuffer, IMAGE_WIDTH*IMAGE_HEIGHT*4, CCImage::kFmtRawData, IMAGE_WIDTH, IMAGE_HEIGHT, 8); CCTexture2D *pTex = new CCTexture2D(); pTex->initWithImage(m_pBitmap); //m_pScreen->removeFromParentAndCleanup(true); //m_pScreen = CCSprite::createWithTexture(pTex); m_pScreen->setTextureRect(CCRectMake(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT)); m_pScreen->setTexture(pTex); pTex->release(); m_pBitmap->release(); } /* // position the sprite on the center of the screen CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); m_pScreen->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(m_pScreen, 0); */ // 스프라이트의 크기를 조정 m_pScreen->setScale(1.0+m_fElapsedTime); //m_pScreen->setScale(1.0+m_fScale); }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); m_fElapsedTime = 0; m_fScale = 1.0; m_bSpeedUp = false; ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // 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(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Map Maker", "Arial", 24); // position the label on the center of the screen pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the label as a child to this layer this->addChild(pLabel, 1); // 버퍼 생성 및 팔레트 로딩 m_pPaletteBuffer = new unsigned char[NUM_PALETTES * 4]; // RGBA * 256 인덱스 컬러 m_pIndexBuffer = new unsigned char[IMAGE_WIDTH * IMAGE_HEIGHT]; // 인덱스 컬러로 된 이미지 버퍼 m_pImageBuffer = new unsigned char[IMAGE_WIDTH * IMAGE_HEIGHT*4]; // RGBA * 512 * 512 /* // LAND.COL로 부터 팔레트 로딩 std::string sFilePath = std::string(""); sFilePath += CCFileUtils::sharedFileUtils()->fullPathForFilename("LAND.COL"); FILE *pal_file = fopen(sFilePath.c_str(), "rb"); if (pal_file) { fread(&head, sizeof(ColHeader), 1, pal_file); //if (head.num_cols > NUM_PALETTES) // head.num_cols = NUM_PALETTES; fread(palette, 256*3, 1, pal_file); fclose(pal_file); // 팔레트 변환 RGB -> RGBA BuildPal(palette, m_pPaletteBuffer); } */ // PNG로부터 팔레트 로딩 GetPaletteFromFile("mapheight.png", m_pPaletteBuffer, 256); // add generating "Map" screen" // 인덱스 컬러로 된 이미지에 랜덤 값을 채운다. for (int i = 0; i < IMAGE_WIDTH*IMAGE_HEIGHT; i++) m_pIndexBuffer[i] = rand()%NUM_PALETTES; // 인덱스 -> 이미지 변환 IndexToImage(m_pIndexBuffer, m_pImageBuffer, m_pPaletteBuffer, IMAGE_WIDTH*IMAGE_HEIGHT); m_pBitmap = new CCImage(); m_pBitmap->initWithImageData(m_pImageBuffer, IMAGE_WIDTH*IMAGE_HEIGHT*4, CCImage::kFmtRawData, IMAGE_WIDTH, IMAGE_HEIGHT, 8); CCTexture2D *pTex = new CCTexture2D(); pTex->initWithImage(m_pBitmap); m_pScreen = CCSprite::createWithTexture(pTex); pTex->release(); m_pBitmap->release(); // position the sprite on the center of the screen m_pScreen->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); m_pScreen->setScale( 2.0 ); // add the sprite as a child to this layer this->addChild(m_pScreen, 0); setTouchEnabled(true); this->scheduleUpdate(); return true; }
CCSprite* CCColorUtil::graylightWithCCSprite(CCSprite* oldSprite,bool isLight) { //CCSprite转成CCimage CCPoint ptOldAnchor = oldSprite->getAnchorPoint(); CCPoint ptOld = oldSprite->getPosition(); oldSprite->setAnchorPoint(ccp(0,0)); oldSprite->setPosition(ccp(0,0)); CCRenderTexture *outTexture = CCRenderTexture::create((int)oldSprite->getContentSize().width*1,(int)oldSprite->getContentSize().height*1); outTexture->begin(); oldSprite->visit(); outTexture->end(); oldSprite->setAnchorPoint(ptOldAnchor); oldSprite->setPosition(ptOld); CCImage* finalImage = outTexture->newCCImage(); unsigned char *pData = finalImage->getData(); int iIndex = 0; if(isLight) { for (int i = 0; i < finalImage->getHeight(); i ++) { for (int j = 0; j < finalImage->getWidth(); j ++) { // highlight int iHightlightPlus = 50; int iBPos = iIndex; unsigned int iB = pData[iIndex]; iIndex ++; unsigned int iG = pData[iIndex]; iIndex ++; unsigned int iR = pData[iIndex]; iIndex ++; //unsigned int o = pData[iIndex]; iIndex ++; //原来的示例缺少 iB = (iB + iHightlightPlus > 255 ? 255 : iB + iHightlightPlus); iG = (iG + iHightlightPlus > 255 ? 255 : iG + iHightlightPlus); iR = (iR + iHightlightPlus > 255 ? 255 : iR + iHightlightPlus); // iR = (iR < 0 ? 0 : iR); // iG = (iG < 0 ? 0 : iG); // iB = (iB < 0 ? 0 : iB); pData[iBPos] = (unsigned char)iB; pData[iBPos + 1] = (unsigned char)iG; pData[iBPos + 2] = (unsigned char)iR; } } }else{ for (int i = 0; i < finalImage->getHeight(); i ++) { for (int j = 0; j < finalImage->getWidth(); j ++) { // gray int iBPos = iIndex; unsigned int iB = pData[iIndex]; iIndex ++; unsigned int iG = pData[iIndex]; iIndex ++; unsigned int iR = pData[iIndex]; iIndex ++; //unsigned int o = pData[iIndex]; iIndex ++; //原来的示例缺少 unsigned int iGray = 0.3 * iR + 0.4 * iG + 0.2 * iB; pData[iBPos] = pData[iBPos + 1] = pData[iBPos + 2] = (unsigned char)iGray; } } } CCTexture2D *texture = new CCTexture2D; texture->initWithImage(finalImage); CCSprite* newSprite = CCSprite::createWithTexture(texture); delete finalImage; texture->release(); return newSprite; }