Пример #1
0
        internal void DrawQuads(CCRawList <CCV3F_C4B_T2F_Quad> quads, int start, int n)
        {
            if (n == 0)
            {
                return;
            }

            ApplyEffectParams();

            // We unbox our quads into our vertex array that is passed onto MonoGame
            // Our vertex array is of a fixed size, so split up into multiple draw calls if required
            while (n > 0)
            {
                int nIteration = Math.Min(n, MaxNumQuads);

                int i4 = 0;
                for (int i = start, N = start + nIteration; i < N; i++)
                {
                    quadsVertices[i4 + 0] = quads[i].TopLeft;
                    quadsVertices[i4 + 1] = quads[i].BottomLeft;
                    quadsVertices[i4 + 2] = quads[i].TopRight;
                    quadsVertices[i4 + 3] = quads[i].BottomRight;

                    i4 += 4;
                }

                EffectPassCollection passes = currentEffect.CurrentTechnique.Passes;
                for (int i = 0; i < passes.Count; i++)
                {
                    passes[i].Apply();
                    graphicsDevice.DrawUserIndexedPrimitives(
                        PrimitiveType.TriangleList, quadsVertices, 0, nIteration * NumOfVerticesPerQuad, quadsIndices, 0, nIteration * 2);
                }

                n     -= nIteration;
                start += nIteration;
            }
        }
Пример #2
0
        public int RebuildIndexInOrder(CCSprite pobParent, int uIndex)
        {
            CCRawList <CCNode> pChildren = pobParent.Children;

            if (pChildren != null && pChildren.Count > 0)
            {
                CCNode[] elements = pChildren.Elements;
                for (int i = 0, count = pChildren.Count; i < count; i++)
                {
                    if (elements[i].ZOrder < 0)
                    {
                        uIndex = RebuildIndexInOrder((CCSprite)pChildren[i], uIndex);
                    }
                }
            }

            // ignore self (batch node)
            if (!pobParent.Equals(this))
            {
                pobParent.AtlasIndex = uIndex;
                uIndex++;
            }

            if (pChildren != null && pChildren.Count > 0)
            {
                CCNode[] elements = pChildren.Elements;
                for (int i = 0, count = pChildren.Count; i < count; i++)
                {
                    if (elements[i].ZOrder >= 0)
                    {
                        uIndex = RebuildIndexInOrder((CCSprite)elements[i], uIndex);
                    }
                }
            }

            return(uIndex);
        }
Пример #3
0
        public void RemoveSpriteFromAtlas(CCSprite sprite)
        {
            // remove from TextureAtlas
            TextureAtlas.RemoveQuadAtIndex(sprite.AtlasIndex);

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

            int uIndex = Descendants.IndexOf(sprite);

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

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

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

            // remove children recursively
            CCRawList <CCNode> pChildren = sprite.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]);
                }
            }
        }
Пример #4
0
        private string CreateFont(string fontName, float fontSize, CCRawList <char> charset)
        {
            if (Factory2D == null)
            {
                Factory2D      = new SharpDX.Direct2D1.Factory();
                FactoryDWrite  = new SharpDX.DirectWrite.Factory();
                FactoryImaging = new SharpDX.WIC.ImagingFactory();

                dpi      = Factory2D.DesktopDpi;
                dpiScale = dpi.Height / 72f;
            }

            if (_defaultFont == null)
            {
                _defaultFont = GenericSanSerif();
                //_defaultDIP = ConvertPointSizeToDIP(_defaultFontSizeEm);
            }

            FontFamily fontFamily = GetFontFamily(fontName);


            if (!_fontFamilyCache.TryGetValue(fontName, out fontFamily))
            {
                var ext = Path.GetExtension(fontName);

                _currentFont = _defaultFont;

                if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf")
                {
                    //var appPath = AppDomain.CurrentDomain.BaseDirectory;
                    //var contentPath = Path.Combine(appPath, CCApplication.SharedApplication.Content.RootDirectory);
                    //var fontPath = Path.Combine(contentPath, fontName);

                    //if (File.Exists(fontPath))
                    //{
                    // try
                    //{
                    //var fontFileReference = new FontCollection(
                    //_loadedFonts.AddFontFile(fontPath);

                    //        //fontFamily = _loadedFonts.Families[_loadedFonts.Families.Length - 1];

                    //        //_currentFont = new Font(fontFamily, fontSize);
                    //    }
                    //    catch
                    //    {
                    //        _currentFont = _defaultFont;
                    //    }
                    //}
                    //else
                    //{
                    _currentFont       = _defaultFont;
                    _currentFontSizeEm = fontSize;
                    _currentDIP        = ConvertPointSizeToDIP(fontSize);
                    //}
                }
                else
                {
                    _currentFont       = GetFont(fontName, fontSize);
                    _currentFontSizeEm = fontSize;
                    _currentDIP        = ConvertPointSizeToDIP(fontSize);
                }

                _fontFamilyCache.Add(fontName, _currentFont.FontFamily);
            }
            else
            {
                _currentFont       = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal);
                _currentFontSizeEm = fontSize;
                _currentDIP        = ConvertPointSizeToDIP(fontSize);
            }
            fontName   = _currentFont.FontFamily.FamilyNames.GetString(0);
            textFormat = new TextFormat(FactoryDWrite, fontName, _currentDIP);

            GetKerningInfo(charset);

            return(_currentFont.ToString());
        }
Пример #5
0
        protected void InitSpriteFont(string theString, string fntFile, float fontSize, CCSize dimensions, CCLabelFormat labelFormat,
                                      CCPoint imageOffset, CCTexture2D texture)
        {
            Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null),
                         "Invalid params for CCLabel SpriteFont");

            if (!String.IsNullOrEmpty(fntFile))
            {
                try
                {
                    FontAtlas = CCFontAtlasCache.GetFontAtlasSpriteFont(fntFile, fontSize, imageOffset);
                }
                catch {}

                if (FontAtlas == null)
                {
                    CCLog.Log("SpriteFont CCLabel: Impossible to create font. Please check file: '{0}'", fntFile);
                    return;
                }
            }

            AnchorPoint = CCPoint.AnchorMiddle;

            currentLabelType = CCLabelType.SpriteFont;

            if (String.IsNullOrEmpty(theString))
            {
                theString = String.Empty;
            }

            // Initialize the TextureAtlas along with children.
            var capacity = theString.Length;

            BlendFunc = CCBlendFunc.AlphaBlend;

            if (capacity == 0)
            {
                capacity = defaultSpriteBatchCapacity;
            }

            UpdateBlendFunc();

            // no lazy alloc in this node
            Children    = new CCRawList <CCNode>(capacity);
            Descendants = new CCRawList <CCSprite>(capacity);

            this.labelDimensions = dimensions;

            horzAlignment = labelFormat.Alignment;
            vertAlignment = labelFormat.LineAlignment;

            IsOpacityCascaded = true;

            ContentSize = CCSize.Zero;

            IsColorModifiedByOpacity = TextureAtlas.Texture.HasPremultipliedAlpha;
            AnchorPoint = CCPoint.AnchorMiddle;

            ImageOffset = imageOffset;

            Text = theString;
        }
Пример #6
0
 private void CreateFont(string fontName, float fontSize, CCRawList <char> charset)
 {
     throw(new NotImplementedException("PSM support for CCLabel is not implemented yet."));
 }
Пример #7
0
        protected void UpdateLabel()
        {
            SetString(labelInitialText, false);

            if (string.IsNullOrEmpty(labelText))
            {
                return;
            }

            if (labelDimensions.Width > 0)
            {
                // Step 1: Make multiline
                string str_whole        = labelText;
                int    stringLength     = str_whole.Length;
                var    multiline_string = new StringBuilder(stringLength);
                var    last_word        = new StringBuilder(stringLength);

                int   line = 1, i = 0;
                bool  start_line = false, start_word = false;
                float startOfLine = -1, startOfWord = -1;
                int   skip = 0;

                CCRawList <CCNode> children = Children;
                for (int j = 0; j < children.Count; j++)
                {
                    CCSprite characterSprite;
                    int      justSkipped = 0;

                    while ((characterSprite = (CCSprite)this[(j + skip + justSkipped)]) == null)
                    {
                        justSkipped++;
                    }

                    skip += justSkipped;

                    if (!characterSprite.Visible)
                    {
                        continue;
                    }

                    if (i >= stringLength)
                    {
                        break;
                    }

                    char character = str_whole[i];

                    if (!start_word)
                    {
                        startOfWord = GetLetterPosXLeft(characterSprite);
                        start_word  = true;
                    }
                    if (!start_line)
                    {
                        startOfLine = startOfWord;
                        start_line  = true;
                    }

                    // Newline.
                    if (character == '\n')
                    {
                        int len = last_word.Length;
                        while (len > 0 && Char.IsWhiteSpace(last_word[len - 1]))
                        {
                            len--;
                            last_word.Remove(len, 1);
                        }

                        multiline_string.Append(last_word);
                        multiline_string.Append('\n');

                        last_word.Clear();

                        start_word  = false;
                        start_line  = false;
                        startOfWord = -1;
                        startOfLine = -1;
                        i          += justSkipped;
                        line++;

                        if (i >= stringLength)
                        {
                            break;
                        }

                        character = str_whole[i];

                        if (startOfWord == 0)
                        {
                            startOfWord = GetLetterPosXLeft(characterSprite);
                            start_word  = true;
                        }
                        if (startOfLine == 0)
                        {
                            startOfLine = startOfWord;
                            start_line  = true;
                        }
                    }

                    // Whitespace.
                    if (Char.IsWhiteSpace(character))
                    {
                        last_word.Append(character);
                        multiline_string.Append(last_word);
                        last_word.Clear();
                        start_word  = false;
                        startOfWord = -1;
                        i++;
                        continue;
                    }

                    // Out of bounds.
                    if (GetLetterPosXRight(characterSprite) - startOfLine > labelDimensions.Width)
                    {
                        if (!lineBreakWithoutSpaces)
                        {
                            last_word.Append(character);

                            int len = multiline_string.Length;
                            while (len > 0 && Char.IsWhiteSpace(multiline_string[len - 1]))
                            {
                                len--;
                                multiline_string.Remove(len, 1);
                            }

                            if (multiline_string.Length > 0)
                            {
                                multiline_string.Append('\n');
                            }

                            line++;
                            start_line  = false;
                            startOfLine = -1;
                            i++;
                        }
                        else
                        {
                            int len = last_word.Length;
                            while (len > 0 && Char.IsWhiteSpace(last_word[len - 1]))
                            {
                                len--;
                                last_word.Remove(len, 1);
                            }

                            multiline_string.Append(last_word);
                            multiline_string.Append('\n');

                            last_word.Clear();

                            start_word  = false;
                            start_line  = false;
                            startOfWord = -1;
                            startOfLine = -1;
                            line++;

                            if (i >= stringLength)
                            {
                                break;
                            }

                            if (startOfWord == 0)
                            {
                                startOfWord = GetLetterPosXLeft(characterSprite);
                                start_word  = true;
                            }
                            if (startOfLine == 0)
                            {
                                startOfLine = startOfWord;
                                start_line  = true;
                            }

                            j--;
                        }

                        continue;
                    }
                    else
                    {
                        // Character is normal.
                        last_word.Append(character);
                        i++;
                        continue;
                    }
                }

                multiline_string.Append(last_word);

                SetString(multiline_string.ToString(), false);
            }

            // Step 2: Make alignment
            if (horzAlignment != CCTextAlignment.Left)
            {
                int i = 0;

                int lineNumber = 0;
                int str_len    = labelText.Length;
                var last_line  = new CCRawList <char>();
                // If label dim is 0, then we need to use the content size width instead
                float maxLabelWidth = labelDimensions.Width > 0 ? labelDimensions.Width : ContentSize.Width;
                for (int ctr = 0; ctr <= str_len; ++ctr)
                {
                    if (ctr == str_len || labelText[ctr] == '\n')
                    {
                        float lineWidth   = 0.0f;
                        int   line_length = last_line.Count;
                        // if last line is empty we must just increase lineNumber and work with next line
                        if (line_length == 0)
                        {
                            lineNumber++;
                            continue;
                        }
                        int index = i + line_length - 1 + lineNumber;
                        if (index < 0)
                        {
                            continue;
                        }

                        var lastChar = (CCSprite)this[index];
                        if (lastChar == null)
                        {
                            continue;
                        }

                        lineWidth = lastChar.Position.X + lastChar.ContentSize.Width;

                        var shift = maxLabelWidth - lineWidth;
                        if (horzAlignment == CCTextAlignment.Center)
                        {
                            shift /= 2;
                        }

                        for (int j = 0; j < line_length; j++)
                        {
                            index = i + j + lineNumber;
                            if (index < 0)
                            {
                                continue;
                            }

                            var characterSprite = this[index];
                            characterSprite.PositionX += shift;
                        }

                        i += line_length;
                        lineNumber++;

                        last_line.Clear();
                        continue;
                    }

                    last_line.Add(labelText[ctr]);
                }
            }

            if (vertAlignment != CCVerticalTextAlignment.Bottom && labelDimensions.Height > 0)
            {
                int lineNumber = 1;
                int str_len    = labelText.Length;
                for (int ctr = 0; ctr < str_len; ++ctr)
                {
                    if (labelText[ctr] == '\n')
                    {
                        lineNumber++;
                    }
                }

                float yOffset = labelDimensions.Height - FontConfiguration.CommonHeight * lineNumber;

                if (vertAlignment == CCVerticalTextAlignment.Center)
                {
                    yOffset /= 2f;
                }

                for (int i = 0; i < str_len; i++)
                {
                    var characterSprite = this[i] as CCSprite;
                    if (characterSprite != null && characterSprite.Visible)
                    {
                        characterSprite.PositionY += yOffset;
                    }
                }
            }
        }
Пример #8
0
        private string CreateFont(string fontName, float fontSize, CCRawList <char> charset)
        {
            if (Factory2D == null)
            {
                Factory2D      = new SharpDX.Direct2D1.Factory();
                FactoryDWrite  = new SharpDX.DirectWrite.Factory();
                FactoryImaging = new SharpDX.WIC.ImagingFactory();

                dpi      = Factory2D.DesktopDpi;
                dpiScale = dpi.Height / 72f;
            }

            _currentFontCollection = FactoryDWrite.GetSystemFontCollection(true);

            if (_defaultFont == null)
            {
                _defaultFont = GenericSanSerif();
                //_defaultDIP = ConvertPointSizeToDIP(_defaultFontSizeEm);
            }

            FontFamily fontFamily = GetFontFamily(fontName);


            if (!_fontFamilyCache.TryGetValue(fontName, out fontFamily))
            {
                var ext = Path.GetExtension(fontName);

                _currentFont = _defaultFont;

                //if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf")
                //{

                //    //var appPath = AppDomain.CurrentDomain.BaseDirectory;
                //    //var contentPath = Path.Combine(appPath, CCContentManager.SharedContentManager.RootDirectory);
                //    //var fontPath = Path.Combine(contentPath, fontName);

                //    //if (File.Exists(fontPath))
                //    //{
                //    //    try
                //    //    {
                //    //        if (privateFontLoader == null)
                //    //        {
                //    //            privateFontLoader = new PrivateFontLoader(FactoryDWrite, fontName);
                //    //            privateFontCollection = new FontCollection(FactoryDWrite, privateFontLoader, privateFontLoader.Key);
                //    //        }

                //    //        _currentFontCollection = privateFontCollection;

                //    //        var family = _currentFontCollection.GetFontFamily(0);
                //    //        // This is generic right now.  We should be able to handle different styles in the future
                //    //        var font = family.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal);
                //    //        _currentFont = font;
                //    //        _currentFontSizeEm = fontSize;
                //    //        _currentDIP = ConvertPointSizeToDIP(fontSize);
                //    //    }
                //    //    catch
                //    //    {
                //    //        _currentFont = _defaultFont;
                //    //        _currentFontSizeEm = fontSize;
                //    //        _currentDIP = ConvertPointSizeToDIP(fontSize);
                //    //    }
                //    //}

                //    //else
                //    //{
                //    //    _currentFont = _defaultFont;
                //    //    _currentFontSizeEm = fontSize;
                //    //    _currentDIP = ConvertPointSizeToDIP(fontSize);
                //    //}
                //}
                //else
                //{
                _currentFont       = GetFont(fontName, fontSize);
                _currentFontSizeEm = fontSize;
                _currentDIP        = ConvertPointSizeToDIP(fontSize);
                //}

                _fontFamilyCache.Add(fontName, _currentFont.FontFamily);
            }
            else
            {
                _currentFont       = fontFamily.GetFirstMatchingFont(FontWeight.Regular, FontStretch.Normal, FontStyle.Normal);
                _currentFontSizeEm = fontSize;
                _currentDIP        = ConvertPointSizeToDIP(fontSize);
            }

            fontName   = _currentFont.FontFamily.FamilyNames.GetString(0);
            textFormat = new TextFormat(FactoryDWrite, fontName,
                                        _currentFontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, _currentDIP);

            GetKerningInfo(charset);

            return(_currentFont.ToString());
        }
Пример #9
0
        void UpdateAtlasIndex(CCSprite sprite, ref int curIndex)
        {
            int count = 0;
            CCRawList <CCNode> pArray = sprite.Children;

            if (pArray != null)
            {
                count = pArray.Count;
            }

            int oldIndex = 0;

            if (count == 0)
            {
                oldIndex              = sprite.AtlasIndex;
                sprite.AtlasIndex     = curIndex;
                sprite.OrderOfArrival = 0;
                if (oldIndex != curIndex)
                {
                    Swap(oldIndex, curIndex);
                }
                curIndex++;
            }
            else
            {
                bool needNewIndex = true;

                if (pArray.Elements[0].ZOrder >= 0)
                {
                    //all children are in front of the parent
                    oldIndex              = sprite.AtlasIndex;
                    sprite.AtlasIndex     = curIndex;
                    sprite.OrderOfArrival = 0;
                    if (oldIndex != curIndex)
                    {
                        Swap(oldIndex, curIndex);
                    }
                    curIndex++;

                    needNewIndex = false;
                }

                for (int i = 0; i < count; i++)
                {
                    var child = (CCSprite)pArray.Elements[i];
                    if (needNewIndex && child.ZOrder >= 0)
                    {
                        oldIndex              = sprite.AtlasIndex;
                        sprite.AtlasIndex     = curIndex;
                        sprite.OrderOfArrival = 0;
                        if (oldIndex != curIndex)
                        {
                            Swap(oldIndex, curIndex);
                        }
                        curIndex++;
                        needNewIndex = false;
                    }

                    UpdateAtlasIndex(child, ref curIndex);
                }

                if (needNewIndex)
                {
                    //all children have a zOrder < 0)
                    oldIndex              = sprite.AtlasIndex;
                    sprite.AtlasIndex     = curIndex;
                    sprite.OrderOfArrival = 0;
                    if (oldIndex != curIndex)
                    {
                        Swap(oldIndex, curIndex);
                    }
                    curIndex++;
                }
            }
        }
Пример #10
0
 public CCIndexBuffer(int indexCount, BufferUsage usage)
 {
     data       = new CCRawList <T>(indexCount);
     this.usage = usage;
     ReinitResource();
 }
Пример #11
0
 public CCVertexBuffer(int vertexCount, CCBufferUsage usage)
 {
     data       = new CCRawList <T>(vertexCount);
     this.usage = usage;
     ReinitResource();
 }
Пример #12
0
 public CCGeometryBatch(int bufferSize = DefaultBufferSize)
 {
     indicesArray  = new CCRawList <short>(bufferSize * 2);
     verticesArray = new CCRawList <CCV3F_C4B_T2F>(bufferSize);
     batchItemList = new CCRawList <CCGeometryInstance>(bufferSize);
 }
Пример #13
0
        protected void InitBMFont(string theString, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment,
                                  CCPoint imageOffset, CCTexture2D texture)
        {
            Debug.Assert(FontConfiguration == null, "re-init is no longer supported");
            Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null),
                         "Invalid params for CCLabelBMFont");

            if (!String.IsNullOrEmpty(fntFile))
            {
                try
                {
                    FontAtlas = CCFontAtlasCache.GetFontAtlasFNT(fntFile, imageOffset);
                }
                catch {}

                if (FontAtlas == null)
                {
                    CCLog.Log("Bitmap Font CCLabel: Impossible to create font. Please check file: '{0}'", fntFile);
                    return;
                }
            }

            AnchorPoint = CCPoint.AnchorMiddle;

            FontConfiguration = CCBMFontConfiguration.FontConfigurationWithFile(fntFile);

            LabelType = CCLabelType.BitMapFont;

            if (String.IsNullOrEmpty(theString))
            {
                theString = String.Empty;
            }

            // Initialize the TextureAtlas along with children.
            var capacity = theString.Length;

            BlendFunc = CCBlendFunc.AlphaBlend;

            if (capacity == 0)
            {
                capacity = defaultSpriteBatchCapacity;
            }

            UpdateBlendFunc();

            // no lazy alloc in this node
            Children    = new CCRawList <CCNode>(capacity);
            Descendants = new CCRawList <CCSprite>(capacity);

            this.labelDimensions = dimensions;

            horzAlignment = hAlignment;
            vertAlignment = vAlignment;

            IsOpacityCascaded = true;

            // We use base here so we do not trigger an update internally.
            base.ContentSize = CCSize.Zero;

            IsColorModifiedByOpacity = TextureAtlas.Texture.HasPremultipliedAlpha;
            AnchorPoint = CCPoint.AnchorMiddle;

            ImageOffset = imageOffset;

            Text = theString;
        }
Пример #14
0
 ///<summary>
 /// Constructs a new enumerator.
 ///</summary>
 ///<param name="list"></param>
 public Enumerator(CCRawList <T> list)
 {
     index     = -1;
     this.list = list;
 }
Пример #15
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 (!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);
        }
Пример #16
0
        private static void GetKerningInfo(CCRawList <char> charset)
        {
            _abcValues.Clear();

            var fontFace = new FontFace(_currentFont);

            var value = new ABCFloat[1];

            var glyphRun = new GlyphRun();

            glyphRun.FontFace = fontFace;
            glyphRun.FontSize = _currentDIP;

            var BrushColor = SharpDX.Color.White;

            /*
             * SharpDX.DirectWrite.Matrix mtrx = new SharpDX.DirectWrite.Matrix();
             * mtrx.M11 = 1F;
             * mtrx.M12 = 0;
             * mtrx.M21 = 0;
             * mtrx.M22 = 1F;
             * mtrx.Dx = 0;
             * mtrx.Dy = 0;
             */
            //GlyphMetrics[] metrics = fontFace.GetGdiCompatibleGlyphMetrics(23, 1, mtrx, false, glyphIndices, false);

            //FontMetrics metr = fontFace.GetGdiCompatibleMetrics(23, 1, new SharpDX.DirectWrite.Matrix());
            //_pRenderTarget.DrawGlyphRun(new SharpDX.DrawingPointF(left, top), glyphRun, new SharpDX.Direct2D1.SolidColorBrush(_pRenderTarget, BrushColor), MeasuringMode.GdiClassic);
            int[] codePoints = new int[1];
            var   unitsPerEm = fontFace.Metrics.DesignUnitsPerEm;
            var   familyName = _currentFont.ToString();


            for (int i = 0; i < charset.Count; i++)
            {
                var ch = charset[i];
                if (!_abcValues.ContainsKey(ch))
                {
                    var textLayout = new TextLayout(FactoryDWrite, ch.ToString(), textFormat, unitsPerEm, unitsPerEm);

                    var tlMetrics = textLayout.Metrics;
                    var tlmWidth  = tlMetrics.Width;
                    var tllWidth  = tlMetrics.LayoutWidth;

                    codePoints[0] = (int)ch;
                    short[] glyphIndices = fontFace.GetGlyphIndices(codePoints);
                    glyphRun.Indices = glyphIndices;

                    var metrics = fontFace.GetDesignGlyphMetrics(glyphIndices, false);

                    //var width = metrics[0].AdvanceWidth + metrics[0].LeftSideBearing + metrics[0].RightSideBearing;
                    //var glyphWidth = _currentFontSizeEm * (float)metrics[0].AdvanceWidth / unitsPerEm;
                    //var abcWidth = _currentDIP * (float)width / unitsPerEm;

                    //value[0].abcfA = _currentFontSizeEm * (float)metrics[0].LeftSideBearing / unitsPerEm;
                    //value[0].abcfB = _currentFontSizeEm * (float)metrics[0].AdvanceWidth / unitsPerEm;
                    //value[0].abcfC = _currentFontSizeEm * (float)metrics[0].RightSideBearing / unitsPerEm;

                    // The A and C values are throwing the spacing off
                    //value[0].abcfA = _currentDIP * (float)metrics[0].LeftSideBearing / unitsPerEm;
                    value[0].abcfB = _currentDIP * (float)metrics[0].AdvanceWidth / unitsPerEm;
                    //value[0].abcfC = _currentDIP * (float)metrics[0].RightSideBearing / unitsPerEm;

                    _abcValues.Add(
                        ch,
                        new KerningInfo()
                    {
                        A = value[0].abcfA,
                        B = value[0].abcfB,
                        C = value[0].abcfC
                    });
                }
            }
        }
Пример #17
0
 public CCDrawNode()
 {
     BlendFunc        = CCBlendFunc.AlphaBlend;
     triangleVertices = new CCRawList <CCV3F_C4B>(DefaultBufferSize);
     lineVertices     = new CCRawList <CCV3F_C4B>(DefaultBufferSize);
 }
 void InitRenderCommand()
 {
     quads = new CCRawList <CCV3F_C4B_T2F_Quad> (TotalParticles);
     renderParticlesCommand = new CCCustomCommand(RenderParticles);
 }
Пример #19
0
 public CCParticleBatchNode(CCTexture2D tex, int capacity = ParticleDefaultCapacity)
 {
     BlendFunc    = CCBlendFunc.AlphaBlend;
     TextureAtlas = new CCTextureAtlas(tex, capacity);
     Children     = new CCRawList <CCNode>(capacity);
 }
Пример #20
0
 public CCDrawNode()
 {
     BlendFunc        = CCBlendFunc.AlphaBlend;
     triangleVertices = new CCRawList <VertexPositionColor>(DefaultBufferSize);
     lineVertices     = new CCRawList <VertexPositionColor>(DefaultBufferSize);
 }