Exemplo n.º 1
0
        /* optimization methos */

        private CCSprite AppendTileForGID(uint gid, CCPoint pos)
        {
            CCRect rect = m_pTileSet.RectForGID(gid);

            rect = CCMacros.CCRectanglePixelsToPoints(rect);

            var z = (int)(pos.X + pos.Y * m_tLayerSize.Width);

            CCSprite tile = ReusedTileWithRect(rect);

            SetupTileSprite(tile, pos, gid);

            // optimization:
            // The difference between appendTileForGID and insertTileforGID is that append is faster, since
            // it appends the tile at the end of the texture atlas
            int indexForZ = m_pAtlasIndexArray.Count;

            // don't add it using the "standard" way.
            AddQuadFromSprite(tile, indexForZ);

            // append should be after addQuadFromSprite since it modifies the quantity values
            m_pAtlasIndexArray.Insert(indexForZ, z);

            return(tile);
        }
Exemplo n.º 2
0
        protected virtual bool InitWithTextureFilename(String filename, CCRect rect, bool rotated, CCPoint offset,
                                                       CCSize originalSize)
        {
            m_pobTexture             = null;
            m_strTextureFilename     = filename;
            m_obRectInPixels         = rect;
            m_obRect                 = CCMacros.CCRectanglePixelsToPoints(rect);
            m_obOffsetInPixels       = offset;
            m_obOffset               = CCMacros.CCPointPixelsToPoints(m_obOffsetInPixels);
            m_obOriginalSizeInPixels = originalSize;
            m_obOriginalSize         = CCMacros.CCSizePixelsToPoints(m_obOriginalSizeInPixels);
            m_bRotated               = rotated;

            return(true);
        }
Exemplo n.º 3
0
        protected virtual bool InitWithTexture(CCTexture2D pobTexture, CCRect rect, bool rotated, CCPoint offset,
                                               CCSize originalSize)
        {
            m_pobTexture = pobTexture;

            m_obRectInPixels         = rect;
            m_obRect                 = CCMacros.CCRectanglePixelsToPoints(rect);
            m_obOffsetInPixels       = offset;
            m_obOffset               = CCMacros.CCPointPixelsToPoints(m_obOffsetInPixels);
            m_obOriginalSizeInPixels = originalSize;
            m_obOriginalSize         = CCMacros.CCSizePixelsToPoints(m_obOriginalSizeInPixels);
            m_bRotated               = rotated;

            return(true);
        }
Exemplo n.º 4
0
        /** sets the tile gid (gid = tile global id) at a given tile coordinate.
         * The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor . Tileset Mgr +1.
         * If a tile is already placed at that position, then it will be removed.
         *
         * Use withFlags if the tile flags need to be changed as well
         */

        public void SetTileGID(uint gid, CCPoint pos, uint flags)
        {
            Debug.Assert(pos.X < m_tLayerSize.Width && pos.Y < m_tLayerSize.Height && pos.X >= 0 && pos.Y >= 0, "TMXLayer: invalid position");
            Debug.Assert(m_pTiles != null && m_pAtlasIndexArray != null, "TMXLayer: the tiles map has been released");
            Debug.Assert(gid == 0 || gid >= m_pTileSet.m_uFirstGid, "TMXLayer: invalid gid");

            uint currentFlags;
            uint currentGID = TileGIDAt(pos, out currentFlags);

            if (currentGID != gid || currentFlags != flags)
            {
                uint gidAndFlags = gid | flags;

                // setting gid=0 is equal to remove the tile
                if (gid == 0)
                {
                    RemoveTileAt(pos);
                }
                // empty tile. create a new one
                else if (currentGID == 0)
                {
                    InsertTileForGID(gidAndFlags, pos);
                }
                // modifying an existing tile with a non-empty tile
                else
                {
                    var z      = (int)(pos.X + pos.Y * m_tLayerSize.Width);
                    var sprite = (CCSprite)GetChildByTag(z);
                    if (sprite != null)
                    {
                        CCRect rect = m_pTileSet.RectForGID(gid);
                        rect = CCMacros.CCRectanglePixelsToPoints(rect);

                        sprite.SetTextureRect(rect, false, rect.Size);
                        if (flags != 0)
                        {
                            SetupTileSprite(sprite, sprite.Position, gidAndFlags);
                        }
                        m_pTiles[z] = gidAndFlags;
                    }
                    else
                    {
                        UpdateTileForGID(gidAndFlags, pos);
                    }
                }
            }
        }
Exemplo n.º 5
0
        private CCSprite InsertTileForGID(uint gid, CCPoint pos)
        {
            CCRect rect = m_pTileSet.RectForGID(gid);

            rect = CCMacros.CCRectanglePixelsToPoints(rect);

            var z = (int)(pos.X + pos.Y * m_tLayerSize.Width);

            CCSprite tile = ReusedTileWithRect(rect);

            SetupTileSprite(tile, pos, gid);

            // get atlas index
            int indexForZ = AtlasIndexForNewZ(z);

            // Optimization: add the quad without adding a child
            AddQuadFromSprite(tile, indexForZ);

            // insert it into the local atlasindex array
            m_pAtlasIndexArray.Insert(indexForZ, z);

            // update possible children
            if (m_pChildren != null && m_pChildren.count > 0)
            {
                CCNode[] elements = m_pChildren.Elements;
                int      count    = m_pChildren.count;

                for (int i = 0; i < count; i++)
                {
                    var sprite = (CCSprite)elements[i];
                    int ai     = sprite.AtlasIndex;
                    if (ai >= indexForZ)
                    {
                        sprite.AtlasIndex = ai + 1;
                    }
                }
            }
            m_pTiles[z] = gid;
            return(tile);
        }
Exemplo n.º 6
0
        /** returns the tile (CCSprite) at a given a tile coordinate.
         * The returned CCSprite will be already added to the CCTMXLayer. Don't add it again.
         * The CCSprite can be treated like any other CCSprite: rotated, scaled, translated, opacity, color, etc.
         * You can remove either by calling:
         * - layer.removeChild(sprite, cleanup);
         * - or layer.removeTileAt(ccp(x,y));
         */

        public CCSprite TileAt(CCPoint pos)
        {
            Debug.Assert(pos.X < m_tLayerSize.Width && pos.Y < m_tLayerSize.Height && pos.X >= 0 && pos.Y >= 0, "TMXLayer: invalid position");
            Debug.Assert(m_pTiles != null && m_pAtlasIndexArray != null, "TMXLayer: the tiles map has been released");

            CCSprite tile = null;
            uint     gid  = TileGIDAt(pos);

            // if GID == 0, then no tile is present
            if (gid != 0)
            {
                var z = (int)(pos.X + pos.Y * m_tLayerSize.Width);
                tile = (CCSprite)GetChildByTag(z);

                // tile not created yet. create it
                if (tile == null)
                {
                    CCRect rect = m_pTileSet.RectForGID(gid);
                    rect = CCMacros.CCRectanglePixelsToPoints(rect);

                    tile = new CCSprite(Texture, rect);
                    //
                    // do the init AFTER the batch node is set so that the tile is set to
                    // draw in batch mode instead of self draw mode.
                    //
                    tile.BatchNode   = this;
                    tile.Position    = PositionAt(pos);
                    tile.VertexZ     = VertexZForPos(pos);
                    tile.AnchorPoint = CCPoint.Zero;
                    tile.Opacity     = m_cOpacity;
//                    tile.InitWithTexture(Texture, rect);
//                    tile.BatchNode = this;

                    int indexForZ = AtlasIndexForExistantZ(z);
                    AddSpriteWithoutQuad(tile, indexForZ, z);
                }
            }

            return(tile);
        }
Exemplo n.º 7
0
        public void CreateFontChars()
        {
            int nextFontPositionX = 0;
            int nextFontPositionY = 0;
            //unsigned short prev = -1;
            int kerningAmount = 0;

            CCSize tmpSize = CCSize.Zero;

            int longestLine = 0;
            int totalHeight = 0;

            int quantityOfLines = 1;

            int stringLen = m_sString.Length;

            if (stringLen == 0)
            {
                return;
            }

            for (int i = 0; i < stringLen - 1; ++i)
            {
                if (m_sString[i] == '\n')
                {
                    quantityOfLines++;
                }
            }

            totalHeight       = m_pConfiguration.m_nCommonHeight * quantityOfLines;
            nextFontPositionY = 0 - (m_pConfiguration.m_nCommonHeight - m_pConfiguration.m_nCommonHeight * quantityOfLines);

            for (int i = 0; i < stringLen; i++)
            {
                char c = m_sString[i];

                if (c == '\n')
                {
                    nextFontPositionX  = 0;
                    nextFontPositionY -= m_pConfiguration.m_nCommonHeight;
                    continue;
                }

                // unichar is a short, and an int is needed on HASH_FIND_INT
                CCBMFontConfiguration.ccBMFontDef fontDef = m_pConfiguration.m_pFontDefDictionary[c];

                CCRect rect = fontDef.rect;
                rect = CCMacros.CCRectanglePixelsToPoints(rect);

                rect.Origin.X += m_tImageOffset.X;
                rect.Origin.Y += m_tImageOffset.Y;

                CCSprite fontChar;

                fontChar = (CCSprite)(GetChildByTag(i));
                if (fontChar == null)
                {
                    fontChar = new CCSprite(m_pobTextureAtlas.Texture, rect);
                    AddChild(fontChar, 0, i);
                }
                else
                {
                    // reusing fonts
                    fontChar.SetTextureRect(rect, false, rect.Size);

                    // restore to default in case they were modified
                    fontChar.Visible = true;
                    fontChar.Opacity = 255;
                }

                // See issue 1343. cast( signed short + unsigned integer ) == unsigned integer (sign is lost!)
                int yOffset = m_pConfiguration.m_nCommonHeight - fontDef.yOffset;
                var fontPos = new CCPoint((float)nextFontPositionX + fontDef.xOffset + fontDef.rect.Size.Width * 0.5f + kerningAmount,
                                          (float)nextFontPositionY + yOffset - rect.Size.Height * 0.5f * CCMacros.CCContentScaleFactor());
                fontChar.Position = CCMacros.CCPointPixelsToPoints(fontPos);

                // update kerning
                nextFontPositionX += fontDef.xAdvance + kerningAmount;
                //prev = c;

                // Apply label properties
                fontChar.IsOpacityModifyRGB = m_bIsOpacityModifyRGB;
                // Color MUST be set before opacity, since opacity might change color if OpacityModifyRGB is on
                fontChar.Color = m_tColor;

                // only apply opacity if it is different than 255 )
                // to prevent modifying the color too (issue #610)
                if (m_cOpacity != 255)
                {
                    fontChar.Opacity = m_cOpacity;
                }

                if (longestLine < nextFontPositionX)
                {
                    longestLine = nextFontPositionX;
                }
            }

            tmpSize.Width  = longestLine;
            tmpSize.Height = totalHeight;

            ContentSize = CCMacros.CCSizePixelsToPoints(tmpSize);
        }
Exemplo n.º 8
0
        public void CreateFontChars()
        {
            int  nextFontPositionX = 0;
            int  nextFontPositionY = 0;
            char prev          = (char)255;
            int  kerningAmount = 0;

            CCSize tmpSize = CCSize.Zero;

            int longestLine = 0;
            int totalHeight = 0;

            int quantityOfLines = 1;

            if (String.IsNullOrEmpty(m_sString))
            {
                return;
            }

            int stringLen = m_sString.Length;

            var charSet = m_pConfiguration.CharacterSet;

            if (charSet.Count == 0)
            {
                throw (new InvalidOperationException(
                           "Can not compute the size of the font because the character set is empty."));
            }

            for (int i = 0; i < stringLen - 1; ++i)
            {
                if (m_sString[i] == '\n')
                {
                    quantityOfLines++;
                }
            }

            totalHeight       = m_pConfiguration.m_nCommonHeight * quantityOfLines;
            nextFontPositionY = 0 -
                                (m_pConfiguration.m_nCommonHeight - m_pConfiguration.m_nCommonHeight * quantityOfLines);

            CCBMFontConfiguration.CCBMFontDef fontDef = null;
            CCRect rect;

            for (int i = 0; i < stringLen; i++)
            {
                char c = m_sString[i];

                if (c == '\n')
                {
                    nextFontPositionX  = 0;
                    nextFontPositionY -= m_pConfiguration.m_nCommonHeight;
                    continue;
                }

                if (charSet.IndexOf(c) == -1)
                {
                    CCLog.Log("Cocos2D.CCLabelBMFont: Attempted to use character not defined in this bitmap: {0}",
                              (int)c);
                    continue;
                }

                kerningAmount = this.KerningAmountForFirst(prev, c);

                // unichar is a short, and an int is needed on HASH_FIND_INT
                if (!m_pConfiguration.m_pFontDefDictionary.TryGetValue(c, out fontDef))
                {
                    CCLog.Log("cocos2d::CCLabelBMFont: characer not found {0}", (int)c);
                    continue;
                }

                rect = fontDef.rect;
                rect = CCMacros.CCRectanglePixelsToPoints(rect);

                rect.Origin.X += m_tImageOffset.X;
                rect.Origin.Y += m_tImageOffset.Y;

                CCSprite fontChar;

                //bool hasSprite = true;
                fontChar = (CCSprite)(GetChildByTag(i));
                if (fontChar != null)
                {
                    // Reusing previous Sprite
                    fontChar.Visible = true;
                }
                else
                {
                    // New Sprite ? Set correct color, opacity, etc...
                    //if( false )
                    //{
                    //    /* WIP: Doesn't support many features yet.
                    //     But this code is super fast. It doesn't create any sprite.
                    //     Ideal for big labels.
                    //     */
                    //    fontChar = m_pReusedChar;
                    //    fontChar.BatchNode = null;
                    //    hasSprite = false;
                    //}
                    //else
                    {
                        fontChar = new CCSprite();
                        fontChar.InitWithTexture(m_pobTextureAtlas.Texture, rect);
                        AddChild(fontChar, i, i);
                    }

                    // Apply label properties
                    fontChar.IsOpacityModifyRGB = m_bIsOpacityModifyRGB;

                    // Color MUST be set before opacity, since opacity might change color if OpacityModifyRGB is on
                    fontChar.UpdateDisplayedColor(m_tDisplayedColor);
                    fontChar.UpdateDisplayedOpacity(m_cDisplayedOpacity);
                }

                // updating previous sprite
                fontChar.SetTextureRect(rect, false, rect.Size);

                // See issue 1343. cast( signed short + unsigned integer ) == unsigned integer (sign is lost!)
                int yOffset = m_pConfiguration.m_nCommonHeight - fontDef.yOffset;
                var fontPos =
                    new CCPoint(
                        (float)nextFontPositionX + fontDef.xOffset + fontDef.rect.Size.Width * 0.5f + kerningAmount,
                        (float)nextFontPositionY + yOffset - rect.Size.Height * 0.5f * CCMacros.CCContentScaleFactor());
                fontChar.Position = CCMacros.CCPointPixelsToPoints(fontPos);

                // update kerning
                nextFontPositionX += fontDef.xAdvance + kerningAmount;
                prev = c;

                if (longestLine < nextFontPositionX)
                {
                    longestLine = nextFontPositionX;
                }

                //if (! hasSprite)
                //{
                //  UpdateQuadFromSprite(fontChar, i);
                //}
            }

            // If the last character processed has an xAdvance which is less that the width of the characters image, then we need
            // to adjust the width of the string to take this into account, or the character will overlap the end of the bounding
            // box
            if (fontDef.xAdvance < fontDef.rect.Size.Width)
            {
                tmpSize.Width = longestLine + fontDef.rect.Size.Width - fontDef.xAdvance;
            }
            else
            {
                tmpSize.Width = longestLine;
            }
            tmpSize.Height = totalHeight;

            tmpSize = new CCSize(
                m_tDimensions.Width > 0 ? m_tDimensions.Width : tmpSize.Width,
                m_tDimensions.Height > 0 ? m_tDimensions.Height : tmpSize.Height
                );

            ContentSize = CCMacros.CCSizePixelsToPoints(tmpSize);
        }