コード例 #1
0
ファイル: CCTileMapLayer.cs プロジェクト: zhuruvl/CocosSharp
        void UpdateQuadAt(int tileCoordX, int tileCoordY, bool updateBuffer = true)
        {
            int flattenedTileIndex    = FlattenedTileIndex(tileCoordX, tileCoordY);
            CCTileGidAndFlags tileGID = tileGIDAndFlagsArray[flattenedTileIndex];

            CCTileMapVertAndIndexBuffer drawBuffer = drawBufferManager.GetDrawBufferAtIndex(flattenedTileIndex);
            int adjustedTileIndex = flattenedTileIndex - drawBuffer.TileStartIndex;

            if (tileGID.Gid == 0)
            {
                drawBuffer.QuadsVertexBuffer.Data[adjustedTileIndex] = new CCV3F_C4B_T2F_Quad();

                if (updateBuffer)
                {
                    drawBuffer.QuadsVertexBuffer.UpdateBuffer(adjustedTileIndex, 1);
                }
                return;
            }

            float left, right, top, bottom, vertexZ;

            vertexZ = TileVertexZ(tileCoordX, tileCoordY);


            CCSize  tileSize = TileSetInfo.TileTexelSize * CCTileMapLayer.DefaultTexelToContentSizeRatios;
            CCSize  texSize  = TileSetInfo.TilesheetSize;
            CCPoint tilePos  = TilePosition(tileCoordX, tileCoordY);

            var quad = drawBuffer.QuadsVertexBuffer.Data[adjustedTileIndex];

            // vertices
            if ((tileGID.Flags & CCTileFlags.TileDiagonal) != 0)
            {
                left   = tilePos.X;
                right  = tilePos.X + tileSize.Height;
                bottom = tilePos.Y + tileSize.Width;
                top    = tilePos.Y;
            }
            else
            {
                left   = tilePos.X;
                right  = tilePos.X + tileSize.Width;
                bottom = tilePos.Y + tileSize.Height;
                top    = tilePos.Y;
            }

            float temp;

            if ((tileGID.Flags & CCTileFlags.Vertical) != 0)
            {
                temp   = top;
                top    = bottom;
                bottom = temp;
            }

            if ((tileGID.Flags & CCTileFlags.Horizontal) != 0)
            {
                temp  = left;
                left  = right;
                right = temp;
            }

            if ((tileGID.Flags & CCTileFlags.TileDiagonal) != 0)
            {
                // FIXME: not working correcly
                quad.BottomLeft.Vertices  = new CCVertex3F(left, bottom, vertexZ);
                quad.BottomRight.Vertices = new CCVertex3F(left, top, vertexZ);
                quad.TopLeft.Vertices     = new CCVertex3F(right, bottom, vertexZ);
                quad.TopRight.Vertices    = new CCVertex3F(right, top, vertexZ);
            }
            else
            {
                quad.BottomLeft.Vertices  = new CCVertex3F(left, bottom, vertexZ);
                quad.BottomRight.Vertices = new CCVertex3F(right, bottom, vertexZ);
                quad.TopLeft.Vertices     = new CCVertex3F(left, top, vertexZ);
                quad.TopRight.Vertices    = new CCVertex3F(right, top, vertexZ);
            }

            // texcoords
            CCRect tileTexture = TileSetInfo.TextureRectForGID(tileGID.Gid);

            left   = ((tileTexture.Origin.X) / texSize.Width) + 0.5f / texSize.Width;
            right  = left + ((tileTexture.Size.Width) / texSize.Width) - 1.0f / texSize.Width;
            bottom = ((tileTexture.Origin.Y) / texSize.Height) + 0.5f / texSize.Height;
            top    = bottom + ((tileTexture.Size.Height) / texSize.Height) - 1.0f / texSize.Height;

            quad.BottomLeft.TexCoords  = new CCTex2F(left, bottom);
            quad.BottomRight.TexCoords = new CCTex2F(right, bottom);
            quad.TopLeft.TexCoords     = new CCTex2F(left, top);
            quad.TopRight.TexCoords    = new CCTex2F(right, top);

            quad.BottomLeft.Colors  = CCColor4B.White;
            quad.BottomRight.Colors = CCColor4B.White;
            quad.TopLeft.Colors     = CCColor4B.White;
            quad.TopRight.Colors    = CCColor4B.White;

            drawBuffer.QuadsVertexBuffer.Data[adjustedTileIndex] = quad;

            if (updateBuffer)
            {
                drawBuffer.QuadsVertexBuffer.UpdateBuffer(adjustedTileIndex, 1);
            }
        }
コード例 #2
0
ファイル: CCSprite.cs プロジェクト: ununian/CocosSharp
        void UpdateSpriteTextureQuads()
        {
            if (!Visible)
            {
                quad.BottomRight.Vertices =
                    quad.TopLeft.Vertices = quad.TopRight.Vertices = quad.BottomLeft.Vertices = new CCVertex3F(0, 0, 0);
            }
            else
            {
                CCPoint relativeOffset = unflippedOffsetPositionFromCenter;

                if (flipX)
                {
                    relativeOffset.X = -relativeOffset.X;
                }
                if (flipY)
                {
                    relativeOffset.Y = -relativeOffset.Y;
                }

                CCPoint centerPoint = untrimmedSizeInPixels.Center + relativeOffset;
                CCPoint subRectOrigin;
                subRectOrigin.X = centerPoint.X - textureRectInPixels.Size.Width / 2.0f;
                subRectOrigin.Y = centerPoint.Y - textureRectInPixels.Size.Height / 2.0f;

                CCRect subRectRatio = CCRect.Zero;

                if (untrimmedSizeInPixels.Width > 0 && untrimmedSizeInPixels.Height > 0)
                {
                    subRectRatio = new CCRect(
                        subRectOrigin.X / untrimmedSizeInPixels.Width,
                        subRectOrigin.Y / untrimmedSizeInPixels.Height,
                        textureRectInPixels.Size.Width / untrimmedSizeInPixels.Width,
                        textureRectInPixels.Size.Height / untrimmedSizeInPixels.Height);
                }

                // Atlas: Vertex
                float x1 = subRectRatio.Origin.X * ContentSize.Width;
                float y1 = subRectRatio.Origin.Y * ContentSize.Height;
                float x2 = x1 + (subRectRatio.Size.Width * ContentSize.Width);
                float y2 = y1 + (subRectRatio.Size.Height * ContentSize.Height);

                // Don't set z-value: The node's transform will be set to include z offset
                quad.BottomLeft.Vertices  = new CCVertex3F(x1, y1, 0);
                quad.BottomRight.Vertices = new CCVertex3F(x2, y1, 0);
                quad.TopLeft.Vertices     = new CCVertex3F(x1, y2, 0);
                quad.TopRight.Vertices    = new CCVertex3F(x2, y2, 0);

                CCTexture2D tex = batchNode != null ? textureAtlas.Texture : texture;
                if (tex == null)
                {
                    return;
                }

                float atlasWidth  = tex.PixelsWide;
                float atlasHeight = tex.PixelsHigh;

                float left, right, top, bottom;

                if (IsTextureRectRotated)
                {
                    #if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
                    left   = (2 * textureRectInPixels.Origin.X + 1) / (2 * atlasWidth);
                    right  = left + (textureRectInPixels.Size.Height * 2 - 2) / (2 * atlasWidth);
                    top    = (2 * textureRectInPixels.Origin.Y + 1) / (2 * atlasHeight);
                    bottom = top + (textureRectInPixels.Size.Width * 2 - 2) / (2 * atlasHeight);
                    #else
                    left   = textureRectInPixels.Origin.X / atlasWidth;
                    right  = (textureRectInPixels.Origin.X + textureRectInPixels.Size.Height) / atlasWidth;
                    top    = textureRectInPixels.Origin.Y / atlasHeight;
                    bottom = (textureRectInPixels.Origin.Y + textureRectInPixels.Size.Width) / atlasHeight;
                    #endif

                    if (flipX)
                    {
                        CCMacros.CCSwap(ref top, ref bottom);
                    }

                    if (flipY)
                    {
                        CCMacros.CCSwap(ref left, ref right);
                    }

                    quad.BottomLeft.TexCoords.U  = left;
                    quad.BottomLeft.TexCoords.V  = top;
                    quad.BottomRight.TexCoords.U = left;
                    quad.BottomRight.TexCoords.V = bottom;
                    quad.TopLeft.TexCoords.U     = right;
                    quad.TopLeft.TexCoords.V     = top;
                    quad.TopRight.TexCoords.U    = right;
                    quad.TopRight.TexCoords.V    = bottom;
                }
                else
                {
                    #if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
                    left   = (2 * textureRectInPixels.Origin.X + 1) / (2 * atlasWidth);
                    right  = left + (textureRectInPixels.Size.Width * 2 - 2) / (2 * atlasWidth);
                    top    = (2 * textureRectInPixels.Origin.Y + 1) / (2 * atlasHeight);
                    bottom = top + (textureRectInPixels.Size.Height * 2 - 2) / (2 * atlasHeight);
                    #else
                    left   = textureRectInPixels.Origin.X / atlasWidth;
                    right  = (textureRectInPixels.Origin.X + textureRectInPixels.Size.Width) / atlasWidth;
                    top    = textureRectInPixels.Origin.Y / atlasHeight;
                    bottom = (textureRectInPixels.Origin.Y + textureRectInPixels.Size.Height) / atlasHeight;
                    #endif

                    if (flipX)
                    {
                        CCMacros.CCSwap(ref left, ref right);
                    }

                    if (flipY)
                    {
                        CCMacros.CCSwap(ref top, ref bottom);
                    }

                    quad.BottomLeft.TexCoords.U  = left;
                    quad.BottomLeft.TexCoords.V  = bottom;
                    quad.BottomRight.TexCoords.U = right;
                    quad.BottomRight.TexCoords.V = bottom;
                    quad.TopLeft.TexCoords.U     = left;
                    quad.TopLeft.TexCoords.V     = top;
                    quad.TopRight.TexCoords.U    = right;
                    quad.TopRight.TexCoords.V    = top;
                }
            }

            UpdateTransformedSpriteTextureQuads();
        }
コード例 #3
0
ファイル: CCTileMapLayer.cs プロジェクト: zhuruvl/CocosSharp
        public CCSprite ExtractTile(int column, int row, bool addToTileMapLayer = true)
        {
            if (!AreValidTileCoordinates(column, row))
            {
                return(null);
            }

            CCTileGidAndFlags gidAndFlags = TileGIDAndFlags(column, row);
            int flattendedIndex           = FlattenedTileIndex(column, row);

            CCRect   texRect    = TileSetInfo.TextureRectForGID(gidAndFlags.Gid);
            CCSprite tileSprite = new CCSprite(tileSetTexture, texRect);

            tileSprite.ContentSize = texRect.Size * CCTileMapLayer.DefaultTexelToContentSizeRatios;
            tileSprite.Position    = TilePosition(column, row);
            tileSprite.VertexZ     = TileVertexZ(column, row);
            tileSprite.AnchorPoint = CCPoint.Zero;
            tileSprite.Opacity     = Opacity;
            tileSprite.FlipX       = false;
            tileSprite.FlipY       = false;
            tileSprite.Rotation    = 0.0f;

            if ((gidAndFlags.Flags & CCTileFlags.TileDiagonal) != 0)
            {
                CCSize halfContentSize = tileSprite.ContentSize * 0.5f;

                tileSprite.AnchorPoint = CCPoint.AnchorMiddle;
                tileSprite.Position   += new CCPoint(halfContentSize.Width, halfContentSize.Height);

                CCTileFlags horAndVertFlag = gidAndFlags.Flags & (CCTileFlags.Horizontal | CCTileFlags.Vertical);

                // Handle the 4 diagonally flipped states.
                if (horAndVertFlag == CCTileFlags.Horizontal)
                {
                    tileSprite.Rotation = 90.0f;
                }
                else if (horAndVertFlag == CCTileFlags.Vertical)
                {
                    tileSprite.Rotation = 270.0f;
                }
                else if (horAndVertFlag == (CCTileFlags.Vertical | CCTileFlags.Horizontal))
                {
                    tileSprite.Rotation = 90.0f;
                    tileSprite.FlipX    = true;
                }
                else
                {
                    tileSprite.Rotation = 270.0f;
                    tileSprite.FlipX    = true;
                }
            }
            else
            {
                if ((gidAndFlags.Flags & CCTileFlags.Horizontal) != 0)
                {
                    tileSprite.FlipX = true;
                }

                if ((gidAndFlags.Flags & CCTileFlags.Vertical) != 0)
                {
                    tileSprite.FlipY = true;
                }
            }

            if (addToTileMapLayer)
            {
                AddChild(tileSprite, flattendedIndex, flattendedIndex);
            }

            RemoveTile(column, row);

            return(tileSprite);
        }
コード例 #4
0
 public CCSpriteFrame(CCTexture2D texture, CCRect rectInPxls)
     : this(rectInPxls.Size, texture, rectInPxls, rectInPxls.Size)
 {
 }
コード例 #5
0
 public CCSpriteFrame(CCSize contentSize, CCTexture2D texture, CCRect textureRectInPxls, bool rotated = false, CCPoint?offsetInPxls = null)
     : this(contentSize, texture, textureRectInPxls, textureRectInPxls.Size, rotated, offsetInPxls)
 {
 }
コード例 #6
0
 public static bool Equal(ref CCRect rect1, ref CCRect rect2)
 {
     return(rect1.Origin.Equals(rect2.Origin) && rect1.Size.Equals(rect2.Size));
 }
コード例 #7
0
 public bool Equals(CCRect rect)
 {
     return(Origin.Equals(rect.Origin) && Size.Equals(rect.Size));
 }
コード例 #8
0
 public bool IntersectsRect(ref CCRect rect)
 {
     return(!(MaxX < rect.MinX || rect.MaxX < MinX || MaxY < rect.MinY || rect.MaxY < MinY));
 }
コード例 #9
0
 public static bool IntersetsRect(ref CCRect rectA, ref CCRect rectB)
 {
     return
         (!(rectA.MaxX < rectB.MinX || rectB.MaxX < rectA.MinX || rectA.MaxY < rectB.MinY ||
            rectB.MaxY < rectA.MinY));
 }
コード例 #10
0
 public static CCRect Transform(CCRect rect, CCAffineTransform anAffineTransform)
 {
     return(anAffineTransform.Transform(rect));
 }
コード例 #11
0
ファイル: CCViewport.cs プロジェクト: hitesh97/CocosSharp
        internal void UpdateViewport(bool dispatchChange = true)
        {
            if (landscapeScreenSizeInPixels == CCSize.Zero)
            {
                return;
            }

            bool isPortrat = DisplayOrientation.IsPortrait();

            CCRect exactFitRectRatio  = isPortrat ? ExactFitPortraitRatio : ExactFitLandscapeRatio;
            CCSize portraitScreenSize = landscapeScreenSizeInPixels.Inverted;
            CCSize screenSize         = isPortrat ? portraitScreenSize : landscapeScreenSizeInPixels;

            Rectangle exactFitRectPixels = new Rectangle(
                (int)(exactFitRectRatio.Origin.X * screenSize.Width),
                (int)(exactFitRectRatio.Origin.Y * screenSize.Height),
                (int)(exactFitRectRatio.Size.Width * screenSize.Width),
                (int)(exactFitRectRatio.Size.Height * screenSize.Height)
                );

            Rectangle xnaViewportRect = exactFitRectPixels;

            float exactFitAspectRatio        = exactFitRectPixels.Width / exactFitRectPixels.Height;
            float screenAspectRatioLandscape = landscapeScreenSizeInPixels.Width / landscapeScreenSizeInPixels.Height;
            float screenAspectRatioPortrait  = portraitScreenSize.Width / portraitScreenSize.Height;

            switch (ResolutionPolicy)
            {
            case CCViewportResolutionPolicy.AspectFitLandscape:
                // The screen width > screen height
                if (screenAspectRatioLandscape > 1.0f)
                {
                    xnaViewportRect.Height = (int)(xnaViewportRect.Width / screenAspectRatioLandscape);
                }
                else
                {
                    xnaViewportRect.Width = (int)(xnaViewportRect.Height * screenAspectRatioLandscape);
                }
                break;

            case CCViewportResolutionPolicy.AspectFitPortrait:
                // The screen width > screen height
                if (screenAspectRatioPortrait > 1.0f)
                {
                    xnaViewportRect.Height = (int)(xnaViewportRect.Width / screenAspectRatioPortrait);
                }
                else
                {
                    xnaViewportRect.Width = (int)(xnaViewportRect.Height * screenAspectRatioPortrait);
                }
                break;

            case CCViewportResolutionPolicy.AspectFillLandscape:
                // The exact fit viewport width > exact fit viewport height
                if (exactFitAspectRatio > 1.0f)
                {
                    xnaViewportRect.Height = (int)(xnaViewportRect.Width / screenAspectRatioLandscape);
                }
                else
                {
                    xnaViewportRect.Width = (int)(xnaViewportRect.Height * screenAspectRatioLandscape);
                }
                break;

            case CCViewportResolutionPolicy.AspectFillPortrait:
                // The exact fit viewport width > exact fit viewport height
                if (exactFitAspectRatio > 1.0f)
                {
                    xnaViewportRect.Height = (int)(xnaViewportRect.Width / screenAspectRatioPortrait);
                }
                else
                {
                    xnaViewportRect.Width = (int)(xnaViewportRect.Height * screenAspectRatioPortrait);
                }
                break;

            // Defaults to exact fit
            default:
                break;
            }

            xnaViewportRect.X = (int)Math.Floor((exactFitRectPixels.Width - xnaViewportRect.Width) / 2.0f) + exactFitRectPixels.X;
            xnaViewportRect.Y = (int)Math.Floor((exactFitRectPixels.Height - xnaViewportRect.Height) / 2.0f) + exactFitRectPixels.Y;

            viewportInPixels = new CCRect(xnaViewportRect);
            xnaViewport      = new Viewport(xnaViewportRect);

            if (dispatchChange)
            {
                OnViewportChanged(this, null);
            }
        }
コード例 #12
0
        public CCRect Transform(CCRect rect)
        {
            Transform(ref rect);

            return(rect);
        }
コード例 #13
0
ファイル: CCDrawNode.cs プロジェクト: ununian/CocosSharp
 public void DrawRect(CCRect rect, CCColor4B fillColor)
 {
     DrawRect(rect, fillColor, 0, CCColor4B.Transparent);
 }
コード例 #14
0
ファイル: CCDrawNode.cs プロジェクト: ununian/CocosSharp
 public void DrawRect(CCRect rect)
 {
     DrawRect(rect, new CCColor4B(Color.R, Color.G, Color.B, Opacity));
 }
コード例 #15
0
        public static CCRect CCRectFromString(string rectSpec)
        {
            CCRect result = CCRect.Zero;

            do
            {
                if (rectSpec == null)
                {
                    break;
                }

                string content = rectSpec;

                // find the first '{' and the third '}'
                int nPosLeft  = content.IndexOf('{');
                int nPosRight = content.IndexOf('}');
                for (int i = 1; i < 3; ++i)
                {
                    if (nPosRight == -1)
                    {
                        break;
                    }
                    nPosRight = content.IndexOf('}', nPosRight + 1);
                }
                if (nPosLeft == -1 || nPosRight == -1)
                {
                    break;
                }
                content = content.Substring(nPosLeft + 1, nPosRight - nPosLeft - 1);
                int nPointEnd = content.IndexOf('}');
                if (nPointEnd == -1)
                {
                    break;
                }
                nPointEnd = content.IndexOf(',', nPointEnd);
                if (nPointEnd == -1)
                {
                    break;
                }

                // get the point string and size string
                string pointStr = content.Substring(0, nPointEnd);
                string sizeStr  = content.Substring(nPointEnd + 1);
                //, content.Length - nPointEnd
                // split the string with ','
                List <string> pointInfo = new List <string>();

                if (!CCUtils.SplitWithForm(pointStr, pointInfo))
                {
                    break;
                }
                List <string> sizeInfo = new List <string>();
                if (!CCUtils.SplitWithForm(sizeStr, sizeInfo))
                {
                    break;
                }

                float x      = CCUtils.CCParseFloat(pointInfo[0]);
                float y      = CCUtils.CCParseFloat(pointInfo[1]);
                float width  = CCUtils.CCParseFloat(sizeInfo[0]);
                float height = CCUtils.CCParseFloat(sizeInfo[1]);

                result = new CCRect(x, y, width, height);
            } while (false);

            return(result);
        }
コード例 #16
0
ファイル: CCPhysicsWorld.cs プロジェクト: lowsar/CocosSharp
        /** Searches for physics shapes that contains in the rect. */
        public void QueryRect(Func <CCPhysicsWorld, CCPhysicsShape, object, bool> func, CCRect rect, object data)
        {
            cp.AssertWarn(func != null, "func shouldn't be nullptr");
            if (func != null)
            {
                CCRectQueryCallbackInfo info = new CCRectQueryCallbackInfo()
                {
                    world = this,
                    func  = func,
                    data  = data
                };

                CCPhysicsWorldCallback.continues = true;

                this._info.Space.BBQuery(
                    PhysicsHelper.rect2cpbb(rect),
                    new cpShapeFilter(cp.NO_GROUP, cp.ALL_LAYERS, cp.ALL_LAYERS),
                    (s, o) => CCPhysicsWorldCallback.QueryRectCallbackFunc(s, info),
                    data
                    );
            }
        }