public int AtlasIndexForChild(CCSprite pobSprite, int nZ) { CCRawList <CCNode> pBrothers = pobSprite.Parent.Children; int uChildIndex = pBrothers.IndexOf(pobSprite); // ignore parent Z if parent is spriteSheet bool bIgnoreParent = (pobSprite.Parent == this); CCSprite pPrevious = null; if (uChildIndex > 0) { pPrevious = (CCSprite)pBrothers[uChildIndex - 1]; } // first child of the sprite sheet if (bIgnoreParent) { if (uChildIndex == 0) { return(0); } return(HighestAtlasIndexInChild(pPrevious) + 1); } // parent is a CCSprite, so, it must be taken into account // first child of an CCSprite ? if (uChildIndex == 0) { var p = (CCSprite)pobSprite.Parent; // less than parent and brothers if (nZ < 0) { return(p.AtlasIndex); } else { return(p.AtlasIndex + 1); } } else { // previous & sprite belong to the same branch if ((pPrevious.ZOrder < 0 && nZ < 0) || (pPrevious.ZOrder >= 0 && nZ >= 0)) { return(HighestAtlasIndexInChild(pPrevious) + 1); } // else (previous < 0 and sprite >= 0 ) var p = (CCSprite)pobSprite.Parent; return(p.AtlasIndex + 1); } }
private CCBMFontConfiguration InitializeFont(string fontName, float fontSize, string charset) { if (m_pData == null) { InitializeTTFAtlas(1024, 1024); } if (String.IsNullOrEmpty(charset)) { charset = " "; } var chars = new CCRawList <char>(); var fontKey = GetFontKey(fontName, fontSize); CCBMFontConfiguration fontConfig; if (!fontConfigurations.TryGetValue(fontKey, out fontConfig)) { fontConfig = new CCBMFontConfiguration(); fontConfigurations.Add(fontKey, fontConfig); } for (int i = 0; i < charset.Length; i++) { var ch = charset[i]; if (!fontConfig.Glyphs.ContainsKey(ch) && chars.IndexOf(ch) == -1) { chars.Add(ch); } } if (chars.Count == 0) { return(fontConfig); } CreateFont(fontName, fontSize, chars); fontConfig.CommonHeight = (int)Math.Ceiling(GetFontHeight()); int[] data = null; for (int i = 0; i < chars.Count; i++) { var s = chars[i].ToString(); var charSize = GetMeasureString(s); int w = (int)Math.Ceiling(charSize.Width + 2); int h = (int)Math.Ceiling(charSize.Height + 2); if (data == null || data.Length < (w * h)) { data = new int[w * h]; } unsafe { int stride; byte *pBase = GetBitmapData(s, out stride); int minX = w; int maxX = 0; int minY = h; int maxY = 0; for (int y = 0; y < h; y++) { var row = (int *)(pBase + y * stride); for (int x = 0; x < w; x++) { if (row[x] != 0) { minX = Math.Min(minX, x); maxX = Math.Max(maxX, x); minY = Math.Min(minY, y); maxY = Math.Max(maxY, y); } } } w = Math.Max(maxX - minX + 1, 1); h = Math.Max(maxY - minY + 1, 1); //maxX = minX + w; //maxY = minY + h; int index = 0; for (int y = minY; y <= maxY; y++) { var row = (int *)(pBase + y * stride); for (int x = minX; x <= maxX; x++) { data[index] = row[x]; index++; } } var region = AllocateRegion(w, h); if (region.x >= 0) { SetRegionData(region, data, w); var info = GetKerningInfo(chars[i]); var fontDef = new CCBMFontConfiguration.CCBMGlyphDef() { Character = chars[i], Subrect = new CCRect(region.x, region.y, region.width, region.height), XOffset = minX, // + (int)Math.Ceiling(info.A), YOffset = minY, XAdvance = (int)Math.Ceiling(info.A + info.B + info.C) }; fontConfig.CharacterSet.Add(chars[i]); fontConfig.Glyphs.Add(chars[i], fontDef); } else { CCLog.Log("Texture atlas is full"); } } } isTextureDirty = true; return(fontConfig); }