bool FontAtlas::prepareLetterDefinitions(const std::u16string& utf16Text) { if (_fontFreeType == nullptr) { return false; } std::unordered_map<unsigned short, unsigned short> codeMapOfNewChar; findNewCharacters(utf16Text, codeMapOfNewChar); if (codeMapOfNewChar.empty()) { return false; } int adjustForDistanceMap = _letterPadding / 2; int adjustForExtend = _letterEdgeExtend / 2; long bitmapWidth; long bitmapHeight; Rect tempRect; FontLetterDefinition tempDef; auto scaleFactor = CC_CONTENT_SCALE_FACTOR(); auto pixelFormat = _fontFreeType->getOutlineSize() > 0 ? CCTexture2DPixelFormat::kCCTexture2DPixelFormat_AI88 : CCTexture2DPixelFormat::kCCTexture2DPixelFormat_A8; float startY = _currentPageOrigY; for (auto&& it : codeMapOfNewChar) { auto bitmap = _fontFreeType->getGlyphBitmap(it.second, bitmapWidth, bitmapHeight, tempRect, tempDef.xAdvance); if (bitmap && bitmapWidth > 0 && bitmapHeight > 0) { tempDef.validDefinition = true; tempDef.width = tempRect.size.width + _letterPadding + _letterEdgeExtend; tempDef.height = tempRect.size.height + _letterPadding + _letterEdgeExtend; tempDef.offsetX = tempRect.origin.x + adjustForDistanceMap + adjustForExtend; tempDef.offsetY = _fontAscender + tempRect.origin.y - adjustForDistanceMap - adjustForExtend; if (bitmapHeight > _currLineHeight) { _currLineHeight = static_cast<int>(bitmapHeight) + _letterPadding + _letterEdgeExtend + 1; } if (_currentPageOrigX + tempDef.width > CacheTextureWidth) { _currentPageOrigY += _currLineHeight; _currLineHeight = 0; _currentPageOrigX = 0; if (_currentPageOrigY + _lineHeight >= CacheTextureHeight) { unsigned char *data = nullptr; if (pixelFormat == CCTexture2DPixelFormat::kCCTexture2DPixelFormat_AI88) { data = _currentPageData + CacheTextureWidth * (int)startY * 2; } else { data = _currentPageData + CacheTextureWidth * (int)startY; } _atlasTextures[_currentPage]->updateWithData(data, 0, startY, CacheTextureWidth, CacheTextureHeight - startY); startY = 0.0f; _currentPageOrigY = 0; memset(_currentPageData, 0, _currentPageDataSize); _currentPage++; auto tex = new (std::nothrow) Texture2D; if (_antialiasEnabled) { tex->setAntiAliasTexParameters(); } else { tex->setAliasTexParameters(); } tex->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth, CacheTextureHeight)); addTexture(tex, _currentPage); tex->release(); } } _fontFreeType->renderCharAt(_currentPageData, _currentPageOrigX + adjustForExtend, _currentPageOrigY + adjustForExtend, bitmap, bitmapWidth, bitmapHeight); tempDef.U = _currentPageOrigX; tempDef.V = _currentPageOrigY; tempDef.textureID = _currentPage; _currentPageOrigX += tempDef.width + 1; // take from pixels to points tempDef.width = tempDef.width / scaleFactor; tempDef.height = tempDef.height / scaleFactor; tempDef.U = tempDef.U / scaleFactor; tempDef.V = tempDef.V / scaleFactor; } else{ if (tempDef.xAdvance) tempDef.validDefinition = true; else tempDef.validDefinition = false; tempDef.width = 0; tempDef.height = 0; tempDef.U = 0; tempDef.V = 0; tempDef.offsetX = 0; tempDef.offsetY = 0; tempDef.textureID = 0; _currentPageOrigX += 1; } _letterDefinitions[it.first] = tempDef; } unsigned char *data = nullptr; if (pixelFormat == CCTexture2DPixelFormat::kCCTexture2DPixelFormat_AI88) { data = _currentPageData + CacheTextureWidth * (int)startY * 2; } else { data = _currentPageData + CacheTextureWidth * (int)startY; } _atlasTextures[_currentPage]->updateWithData(data, 0, startY, CacheTextureWidth, _currentPageOrigY - startY + _lineHeight); return true; }
bool FontAtlas::prepareLetterDefinitions(const std::u16string& utf16String) { if (_fontFreeType == nullptr) { return false; } std::unordered_map<unsigned short, unsigned short> newCharsMap; findNewCharacters(utf16String, newCharsMap); if (newCharsMap.empty()) { return false; } float offsetAdjust = _letterPadding / 2; long bitmapWidth; long bitmapHeight; Rect tempRect; FontLetterDefinition tempDef; auto scaleFactor = CC_CONTENT_SCALE_FACTOR(); auto pixelFormat = _fontFreeType->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; int bottomHeight = _commonLineHeight - _fontAscender; float startY = _currentPageOrigY; for (auto&& it : newCharsMap) { auto bitmap = _fontFreeType->getGlyphBitmap(it.second, bitmapWidth, bitmapHeight, tempRect, tempDef.xAdvance); if (bitmap) { tempDef.validDefinition = true; tempDef.letteCharUTF16 = it.first; tempDef.width = tempRect.size.width + _letterPadding; tempDef.height = tempRect.size.height + _letterPadding; tempDef.offsetX = tempRect.origin.x + offsetAdjust; tempDef.offsetY = _fontAscender + tempRect.origin.y - offsetAdjust; tempDef.clipBottom = bottomHeight - (tempDef.height + tempRect.origin.y + offsetAdjust); if (_currentPageOrigX + tempDef.width > CacheTextureWidth) { _currentPageOrigY += _commonLineHeight; _currentPageOrigX = 0; if (_currentPageOrigY + _commonLineHeight >= CacheTextureHeight) { unsigned char *data = nullptr; if (pixelFormat == Texture2D::PixelFormat::AI88) { data = _currentPageData + CacheTextureWidth * (int)startY * 2; } else { data = _currentPageData + CacheTextureWidth * (int)startY; } _atlasTextures[_currentPage]->updateWithData(data, 0, startY, CacheTextureWidth, CacheTextureHeight - startY); startY = 0.0f; _currentPageOrigY = 0; memset(_currentPageData, 0, _currentPageDataSize); _currentPage++; auto tex = new (std::nothrow) Texture2D; if (_antialiasEnabled) { tex->setAntiAliasTexParameters(); } else { tex->setAliasTexParameters(); } tex->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth, CacheTextureHeight)); addTexture(tex, _currentPage); tex->release(); } } _fontFreeType->renderCharAt(_currentPageData, _currentPageOrigX, _currentPageOrigY, bitmap, bitmapWidth, bitmapHeight); tempDef.U = _currentPageOrigX; tempDef.V = _currentPageOrigY; tempDef.textureID = _currentPage; _currentPageOrigX += tempDef.width + 1; // take from pixels to points tempDef.width = tempDef.width / scaleFactor; tempDef.height = tempDef.height / scaleFactor; tempDef.U = tempDef.U / scaleFactor; tempDef.V = tempDef.V / scaleFactor; } else{ if (tempDef.xAdvance) tempDef.validDefinition = true; else tempDef.validDefinition = false; tempDef.letteCharUTF16 = it.first; tempDef.width = 0; tempDef.height = 0; tempDef.U = 0; tempDef.V = 0; tempDef.offsetX = 0; tempDef.offsetY = 0; tempDef.textureID = 0; tempDef.clipBottom = 0; _currentPageOrigX += 1; } _fontLetterDefinitions[tempDef.letteCharUTF16] = tempDef; } unsigned char *data = nullptr; if (pixelFormat == Texture2D::PixelFormat::AI88) { data = _currentPageData + CacheTextureWidth * (int)startY * 2; } else { data = _currentPageData + CacheTextureWidth * (int)startY; } _atlasTextures[_currentPage]->updateWithData(data, 0, startY, CacheTextureWidth, _currentPageOrigY - startY + _commonLineHeight); return true; }
bool FontAtlas::prepareLetterDefinitions(const std::u16string& utf16String) { FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font); if(fontTTf == nullptr) return false; size_t length = utf16String.length(); float offsetAdjust = _letterPadding / 2; long bitmapWidth; long bitmapHeight; Rect tempRect; FontLetterDefinition tempDef; auto scaleFactor = CC_CONTENT_SCALE_FACTOR(); auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; bool existNewLetter = false; int bottomHeight = _commonLineHeight - _fontAscender; float startY = _currentPageOrigY; for (size_t i = 0; i < length; ++i) { auto outIterator = _fontLetterDefinitions.find(utf16String[i]); if (outIterator == _fontLetterDefinitions.end()) { existNewLetter = true; auto bitmap = fontTTf->getGlyphBitmap(utf16String[i],bitmapWidth,bitmapHeight,tempRect,tempDef.xAdvance); if (bitmap) { tempDef.validDefinition = true; tempDef.letteCharUTF16 = utf16String[i]; tempDef.width = tempRect.size.width + _letterPadding; tempDef.height = tempRect.size.height + _letterPadding; tempDef.offsetX = tempRect.origin.x + offsetAdjust; tempDef.offsetY = _fontAscender + tempRect.origin.y - offsetAdjust; tempDef.clipBottom = bottomHeight - (tempDef.height + tempRect.origin.y + offsetAdjust); if (_currentPageOrigX + tempDef.width > CacheTextureWidth) { _currentPageOrigY += _commonLineHeight; _currentPageOrigX = 0; if(_currentPageOrigY + _commonLineHeight >= CacheTextureHeight) { unsigned char *data = nullptr; if(pixelFormat == Texture2D::PixelFormat::AI88) { data = _currentPageData + CacheTextureWidth * (int)startY * 2; } else { data = _currentPageData + CacheTextureWidth * (int)startY; } _atlasTextures[_currentPage]->updateWithData(data, 0, startY, CacheTextureWidth, CacheTextureHeight - startY); startY = 0.0f; _currentPageOrigY = 0; memset(_currentPageData, 0, _currentPageDataSize); _currentPage++; auto tex = new (std::nothrow) Texture2D; if (_antialiasEnabled) { tex->setAntiAliasTexParameters(); } else { tex->setAliasTexParameters(); } tex->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) ); addTexture(tex,_currentPage); tex->release(); } } fontTTf->renderCharAt(_currentPageData,_currentPageOrigX,_currentPageOrigY,bitmap,bitmapWidth,bitmapHeight); tempDef.U = _currentPageOrigX; tempDef.V = _currentPageOrigY; tempDef.textureID = _currentPage; _currentPageOrigX += tempDef.width + 1; // take from pixels to points tempDef.width = tempDef.width / scaleFactor; tempDef.height = tempDef.height / scaleFactor; tempDef.U = tempDef.U / scaleFactor; tempDef.V = tempDef.V / scaleFactor; } else{ if(tempDef.xAdvance) tempDef.validDefinition = true; else tempDef.validDefinition = false; tempDef.letteCharUTF16 = utf16String[i]; tempDef.width = 0; tempDef.height = 0; tempDef.U = 0; tempDef.V = 0; tempDef.offsetX = 0; tempDef.offsetY = 0; tempDef.textureID = 0; tempDef.clipBottom = 0; _currentPageOrigX += 1; } _fontLetterDefinitions[tempDef.letteCharUTF16] = tempDef; } } if(existNewLetter) { if (_rendererRecreate) { _atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) ); } else { unsigned char *data = nullptr; if(pixelFormat == Texture2D::PixelFormat::AI88) { data = _currentPageData + CacheTextureWidth * (int)startY * 2; } else { data = _currentPageData + CacheTextureWidth * (int)startY; } _atlasTextures[_currentPage]->updateWithData(data, 0, startY, CacheTextureWidth, _currentPageOrigY - startY + _commonLineHeight); } } return true; }
ProgressBar::ProgressBar( Beatup* beatup, std::string front_sprite_path, std::string back_sprite_path, std::string front_sprite_frame_path, std::string back_sprite_frame_path ) { this->target_percentage = 100.0f; this->beatup = beatup; auto get_sprite = [](std::string sprite_path, std::string frame_path) { Sprite* result; if (sprite_path == "") { if (frame_path != "") result = Sprite::createWithSpriteFrameName(frame_path); else result = Sprite::create(); } else { result = Sprite::create(sprite_path); }; return result; }; Sprite* front_sprite = get_sprite(front_sprite_path, front_sprite_frame_path); this->front_timer = cocos2d::ProgressTimer::create(front_sprite); Sprite* slider_sprite = get_sprite("whitebar.png", ""); this->back_timer = cocos2d::ProgressTimer::create(slider_sprite); Sprite* back_sprite = get_sprite(back_sprite_path, back_sprite_frame_path); this->background = back_sprite; beatup->addChild(this->background); this->lbl = Label::createWithTTF("", DEFAULT_FONT, 40); this->lbl->setScale(0.25f); this->lbl->setAnchorPoint(Vec2(0.5f, 0.5f)); this->lbl->getTexture()->setAliasTexParameters(); auto front_size = this->front_timer->getContentSize(); this->lbl->setPosition(Vec2( front_size.width/2.0f, front_size.height/2.0f )); this->lbl->setTextColor(Color4B::BLACK); this->front_timer->addChild(this->lbl); this->background->setLocalZOrder(99); this->back_timer->setLocalZOrder(100); //prog bars on top of everything this->front_timer->setLocalZOrder(101); this->lbl->setLocalZOrder(102); this->beatup->addChild(this->back_timer); this->beatup->addChild(this->front_timer); this->color_layer = LayerColor::create(Color4B(255, 255, 255, 25)); this->front_timer->addChild(this->color_layer); this->color_layer->setContentSize(this->front_timer->getContentSize()); this->wait_to_clear = true; for (ProgressTimer* timer : {this->front_timer, this->back_timer}) { timer->setType(ProgressTimerType::BAR); timer->setBarChangeRate(Vec2(1, 0)); timer->setAnchorPoint(Vec2(0, 0)); timer->setPosition(0, 0); timer->setVisible(true); timer->setPercentage(100); auto t_sprite = timer->getSprite(); if (t_sprite) { auto t_tex = t_sprite->getTexture(); t_tex->setAliasTexParameters(); }; timer->setMidpoint(Vec2(0, 0)); }; this->do_finish_bump = false; this->setScale(sx(4)); };