示例#1
0
        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);
            }
        }
示例#2
0
        public void RemoveSpriteFromAtlas(CCSprite pobSprite)
        {
            // remove from TextureAtlas
            m_pobTextureAtlas.RemoveQuadAtIndex(pobSprite.AtlasIndex);

            // Cleanup sprite. It might be reused (issue #569)
            pobSprite.BatchNode = null;

            int uIndex = m_pobDescendants.IndexOf(pobSprite);

            if (uIndex >= 0)
            {
                m_pobDescendants.RemoveAt(uIndex);

                // update all sprites beyond this one
                int        count    = m_pobDescendants.count;
                CCSprite[] elements = m_pobDescendants.Elements;

                for (; uIndex < count; ++uIndex)
                {
                    elements[uIndex].AtlasIndex--;
                }
            }

            // remove children recursively
            CCRawList <CCNode> pChildren = pobSprite.Children;

            if (pChildren != null && pChildren.count > 0)
            {
                CCNode[] elements = pChildren.Elements;
                for (int i = 0, count = pChildren.count; i < count; i++)
                {
                    RemoveSpriteFromAtlas((CCSprite)elements[i]);
                }
            }
        }
示例#3
0
        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 (!s_pConfigurations.TryGetValue(fontKey, out fontConfig))
            {
                fontConfig = new CCBMFontConfiguration();
                s_pConfigurations.Add(fontKey, fontConfig);
            }

            for (int i = 0; i < charset.Length; i++)
            {
                var ch = charset[i];
                if (!fontConfig.m_pFontDefDictionary.ContainsKey(ch) && chars.IndexOf(ch) == -1)
                {
                    chars.Add(ch);
                }
            }

            if (chars.Count == 0)
            {
                return(fontConfig);
            }

            CreateFont(fontName, fontSize * CCMacros.CCContentScaleFactor(), chars);

            fontConfig.m_nCommonHeight = (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.CCBMFontDef()
                        {
                            charID   = chars[i],
                            rect     = 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.m_pFontDefDictionary.Add(chars[i], fontDef);
                    }
                    else
                    {
                        CCLog.Log("Texture atlas is full");
                    }
                }
            }

            m_bTextureDirty = true;

            return(fontConfig);
        }