コード例 #1
0
ファイル: CCOrbitCamera.cs プロジェクト: pekayatt/cocos2d-xna
        public override void StartWithTarget(CCNode target)
        {
            base.StartWithTarget(target);

            float r, zenith, azimuth;

            SphericalRadius(out r, out zenith, out azimuth);

            if (float.IsNaN(m_fRadius))
            {
                m_fRadius = r;
            }

            if (float.IsNaN(m_fAngleZ))
            {
                m_fAngleZ = CCMacros.CCRadiansToDegrees(zenith);
            }

            if (float.IsNaN(m_fAngleX))
            {
                m_fAngleX = CCMacros.CCRadiansToDegrees(azimuth);
            }

            m_fRadZ = CCMacros.CCDegreesToRadians(m_fAngleZ);
            m_fRadX = CCMacros.CCDegreesToRadians(m_fAngleX);
        }
コード例 #2
0
        private void updateTexture()
        {
            CCTexture2D tex;

            // Dump the old one
            if (Texture != null)
            {
                Texture.Dispose();
            }

            // let system compute label's width or height when its value is 0
            // refer to cocos2d-x issue #1430
            tex = new CCTexture2D();

            tex.InitWithString(m_pString,
                               CCMacros.CCSizePointsToPixels(m_tDimensions),
                               m_hAlignment,
                               m_vAlignment,
                               m_pFontName,
                               m_fFontSize * CCMacros.CCContentScaleFactor());

            Texture = tex;

            CCRect rect = CCRect.Zero;

            rect.Size = m_pobTexture.ContentSize;
            SetTextureRect(rect);
        }
コード例 #3
0
ファイル: CCTMXLayer.cs プロジェクト: pekayatt/cocos2d-xna
        /* 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);
        }
コード例 #4
0
        private void UpdateAtlasValueAt(CCGridSize pos, Color value, int index)
        {
            int x = pos.X;
            int y = pos.Y;

            float row = (float)(value.R % m_uItemsPerRow);
            float col = (float)(value.R / m_uItemsPerRow);

            float textureWide = (m_pTextureAtlas.Texture.PixelsWide);
            float textureHigh = (m_pTextureAtlas.Texture.PixelsHigh);

            float itemWidthInPixels  = m_uItemWidth * CCMacros.CCContentScaleFactor();
            float itemHeightInPixels = m_uItemHeight * CCMacros.CCContentScaleFactor();

#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
            float left   = (2 * row * itemWidthInPixels + 1) / (2 * textureWide);
            float right  = left + (itemWidthInPixels * 2 - 2) / (2 * textureWide);
            float top    = (2 * col * itemHeightInPixels + 1) / (2 * textureHigh);
            float bottom = top + (itemHeightInPixels * 2 - 2) / (2 * textureHigh);
#else
            float left   = (row * itemWidthInPixels) / textureWide;
            float right  = left + itemWidthInPixels / textureWide;
            float top    = (col * itemHeightInPixels) / textureHigh;
            float bottom = top + itemHeightInPixels / textureHigh;
#endif

            CCV3F_C4B_T2F_Quad quad;

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

            quad.BottomLeft.Vertices.X  = (x * m_uItemWidth);
            quad.BottomLeft.Vertices.Y  = (y * m_uItemHeight);
            quad.BottomLeft.Vertices.Z  = 0.0f;
            quad.BottomRight.Vertices.X = (x * m_uItemWidth + m_uItemWidth);
            quad.BottomRight.Vertices.Y = (y * m_uItemHeight);
            quad.BottomRight.Vertices.Z = 0.0f;
            quad.TopLeft.Vertices.X     = (x * m_uItemWidth);
            quad.TopLeft.Vertices.Y     = (y * m_uItemHeight + m_uItemHeight);
            quad.TopLeft.Vertices.Z     = 0.0f;
            quad.TopRight.Vertices.X    = (x * m_uItemWidth + m_uItemWidth);
            quad.TopRight.Vertices.Y    = (y * m_uItemHeight + m_uItemHeight);
            quad.TopRight.Vertices.Z    = 0.0f;

            var color = new CCColor4B(m_tColor.R, m_tColor.G, m_tColor.B, m_cOpacity);
            quad.TopRight.Colors    = color;
            quad.TopLeft.Colors     = color;
            quad.BottomRight.Colors = color;
            quad.BottomLeft.Colors  = color;

            m_pTextureAtlas.UpdateQuad(ref quad, index);
        }
コード例 #5
0
 protected virtual bool InitWithWidthAndHeight(int w, int h, SurfaceFormat colorFormat, DepthFormat depthFormat, RenderTargetUsage usage)
 {
     m_Width       = (int)Math.Ceiling(w * CCMacros.CCContentScaleFactor());
     m_Height      = (int)Math.Ceiling(h * CCMacros.CCContentScaleFactor());
     m_ColorFormat = colorFormat;
     m_DepthFormat = depthFormat;
     m_Usage       = usage;
     MakeTexture();
     return(true);
 }
コード例 #6
0
        /// <summary>
        /// Creates the full set of bolt segments between the start and end points. Based upon the
        /// algorithm found at http://gamedevelopment.tutsplus.com/tutorials/how-to-generate-shockingly-good-2d-lightning-effects--gamedev-2681
        /// by Michael Hoffman.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="dest"></param>
        /// <returns></returns>
        protected virtual List <CCPoint> CreateBolt(CCPoint source, CCPoint dest)
        {
            var          results   = new List <CCPoint>();
            List <float> seglength = new List <float>();
            CCPoint      tangent   = dest - source;
            CCPoint      normal    = CCPoint.Normalize(new CCPoint(tangent.Y, -tangent.X));
            float        length    = tangent.Length;

            List <float> positions = new List <float>();

            positions.Add(0);

            for (int i = 0; i < length / 4; i++)
            {
                positions.Add(CCMacros.CCRandomBetween0And1());
            }

            positions.Sort();

            CCPoint prevPoint        = source;
            float   prevDisplacement = 0;

            for (int i = 1; i < positions.Count; i++)
            {
                float pos = positions[i];

                // used to prevent sharp angles by ensuring very close positions also have small perpendicular variation.
                float scale = (length * m_Jaggedness) * (pos - positions[i - 1]);

                // defines an envelope. Points near the middle of the bolt can be farther from the central line.
                float envelope = pos > 0.95f ? 20f * (1f - pos) : 1f;

                float displacement = -m_Sway + 2f * m_Sway * CCMacros.CCRandomBetween0And1();
                displacement -= (displacement - prevDisplacement) * (1 - scale);
                displacement *= envelope;

                CCPoint point = source + tangent * pos + normal * displacement;
                // Core segment
                results.Add(prevPoint);
                results.Add(point);
                // Length of this segment
                float ll = (point - prevPoint).Length;
                seglength.Add(ll);
                _TrackLength += ll;
                // Continue to the next segment.
                prevPoint        = point;
                prevDisplacement = displacement;
            }

            results.Add(prevPoint);
            results.Add(dest);
            _SegmentLengths = seglength.ToArray();
            return(results);
        }
コード例 #7
0
ファイル: CCOrbitCamera.cs プロジェクト: pekayatt/cocos2d-xna
        private void Init(float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX)
        {
            m_fRadius      = radius;
            m_fDeltaRadius = deltaRadius;
            m_fAngleZ      = angleZ;
            m_fDeltaAngleZ = deltaAngleZ;
            m_fAngleX      = angleX;
            m_fDeltaAngleX = deltaAngleX;

            m_fRadDeltaZ = CCMacros.CCDegreesToRadians(deltaAngleZ);
            m_fRadDeltaX = CCMacros.CCDegreesToRadians(deltaAngleX);
        }
コード例 #8
0
        public void Set2DProjection()
        {
            CCSize size = m_pTexture.ContentSizeInPixels;

            CCDrawManager.SetViewPort(0, 0,
                                      (int)(size.Width * CCMacros.CCContentScaleFactor()),
                                      (int)(size.Height * CCMacros.CCContentScaleFactor())
                                      );

            /*
             * CCDrawManager.ProjectionMatrix = Matrix.Identity;
             *
             * Matrix orthoMatrix = Matrix.CreateOrthographicOffCenter(
             *  0, size.width * ccMacros.CC_CONTENT_SCALE_FACTOR(),
             *  0, size.height * ccMacros.CC_CONTENT_SCALE_FACTOR(),
             *  -1, 1
             *  );
             *
             * CCDrawManager.MultMatrix(ref orthoMatrix);
             */

            CCDrawManager.ViewMatrix       = Matrix.Identity;
            CCDrawManager.ProjectionMatrix = Matrix.Identity;

            Matrix projection      = Matrix.CreateOrthographicOffCenter(0, size.Width, 0, size.Height, -1024.0f, 1024.0f);
            Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);

            CCDrawManager.WorldMatrix = (halfPixelOffset * projection);


            /*
             * CCDirector *director = CCDirector::sharedDirector();
             *
             * CCSize    size = director->getWinSizeInPixels();
             *
             * glViewport(0, 0, (GLsizei)(size.width * CC_CONTENT_SCALE_FACTOR()), (GLsizei)(size.height * CC_CONTENT_SCALE_FACTOR()) );
             * kmGLMatrixMode(KM_GL_PROJECTION);
             * kmGLLoadIdentity();
             *
             * kmMat4 orthoMatrix;
             * kmMat4OrthographicProjection(&orthoMatrix, 0, size.width * CC_CONTENT_SCALE_FACTOR(), 0, size.height * CC_CONTENT_SCALE_FACTOR(), -1, 1);
             * kmGLMultMatrix( &orthoMatrix );
             *
             * kmGLMatrixMode(KM_GL_MODELVIEW);
             * kmGLLoadIdentity();
             *
             *
             * ccSetProjectionMatrixDirty();
             */
        }
コード例 #9
0
ファイル: CCTMXLayer.cs プロジェクト: pekayatt/cocos2d-xna
        /** initializes a CCTMXLayer with a tileset info, a layer info and a map info */

        public bool InitWithTilesetInfo(CCTMXTilesetInfo tilesetInfo, CCTMXLayerInfo layerInfo, CCTMXMapInfo mapInfo)
        {
            // XXX: is 35% a good estimate ?
            CCSize size = layerInfo.LayerSize;
            float  totalNumberOfTiles = size.Width * size.Height;
            float  capacity           = totalNumberOfTiles * 0.35f + 1; // 35 percent is occupied ?

            CCTexture2D texture = null;

            if (tilesetInfo != null)
            {
                texture = CCTextureCache.SharedTextureCache.AddImage(tilesetInfo.m_sSourceImage);
            }

            if (base.InitWithTexture(texture, (int)capacity))
            {
                // layerInfo
                m_sLayerName          = layerInfo.Name;
                m_tLayerSize          = size;
                m_pTiles              = layerInfo.Tiles;
                m_uMinGID             = layerInfo.MinGID;
                m_uMaxGID             = layerInfo.MaxGID;
                m_cOpacity            = layerInfo.Opacity;
                Properties            = new Dictionary <string, string>(layerInfo.Properties);
                m_fContentScaleFactor = CCDirector.SharedDirector.ContentScaleFactor;

                // tilesetInfo
                m_pTileSet = tilesetInfo;

                // mapInfo
                m_tMapTileSize      = mapInfo.TileSize;
                m_uLayerOrientation = (CCTMXOrientation)mapInfo.Orientation;

                // offset (after layer orientation is set);
                CCPoint offset = CalculateLayerOffset(layerInfo.Offset);
                Position = CCMacros.CCPointPixelsToPoints(offset);

                m_pAtlasIndexArray = new List <int>((int)totalNumberOfTiles);

                ContentSize =
                    CCMacros.CCSizePixelsToPoints(new CCSize(m_tLayerSize.Width * m_tMapTileSize.Width,
                                                             m_tLayerSize.Height * m_tMapTileSize.Height));

                m_bUseAutomaticVertexZ = false;
                m_nVertexZvalue        = 0;

                return(true);
            }
            return(false);
        }
コード例 #10
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);
        }
コード例 #11
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);
        }
コード例 #12
0
        public bool InitWithString(string theString, string fntFile)
        {
            string data = CCFileUtils.GetFileData(fntFile);

            PlistDocument doc  = new PlistDocument(data);
            var           dict = doc.Root as PlistDictionary;

            Debug.Assert(dict["version"].AsInt == 1, "Unsupported version. Upgrade cocos2d version");

            string textureFilename = dict["textureFilename"].AsString;
            int    width           = (int)Math.Ceiling(dict["itemWidth"].AsInt / CCMacros.CCContentScaleFactor());
            int    height          = (int)Math.Ceiling(dict["itemHeight"].AsInt / CCMacros.CCContentScaleFactor());
            var    startChar       = (char)dict["firstChar"].AsInt;

            return(InitWithString(theString, textureFilename, width, height, startChar));
        }
コード例 #13
0
ファイル: CCLabelTTF.cs プロジェクト: krashman/cocos2d-xna
        private void updateTexture()
        {
            CCTexture2D tex;

            // Dump the old one
            if (Texture != null)
            {
                CCTexture2D tmp = Texture;
                Texture = null;
                tmp.Dispose();
            }

            // let system compute label's width or height when its value is 0
            // refer to cocos2d-x issue #1430
            tex = new CCTexture2D();

            var result = tex.InitWithString(m_pString,
                                            m_tDimensions.PointsToPixels(),
                                            m_hAlignment,
                                            m_vAlignment,
                                            m_pFontName,
                                            m_fFontSize * CCMacros.CCContentScaleFactor());

//#if MACOS || IPHONE || IOS
//			// There was a problem loading the text for some reason or another if result is not true
//			// For MonoMac and IOS Applications we will try to create a Native Label automatically
//			// If the font is not found then a default font will be selected by the device and used.
//			if (!result && !string.IsNullOrEmpty(m_pString))
//			{
//				tex = CCLabelUtilities.CreateLabelTexture (m_pString,
//				                                           CCMacros.CCSizePointsToPixels (m_tDimensions),
//				                                           m_hAlignment,
//				                                           m_vAlignment,
//				                                           m_pFontName,
//				                                           m_fFontSize * CCMacros.CCContentScaleFactor (),
//				                                           new CCColor4B(Microsoft.Xna.Framework.Color.White) );
//			}
//#endif
            if (result)
            {
                Texture = tex;

                CCRect rect = CCRect.Zero;
                rect.Size = m_pobTexture.ContentSize;
                SetTextureRect(rect);
            }
        }
コード例 #14
0
ファイル: CCTMXLayer.cs プロジェクト: pekayatt/cocos2d-xna
        /** 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);
                    }
                }
            }
        }
コード例 #15
0
        public void Set2DProjection()
        {
            CCSize size = m_pTexture.ContentSizeInPixels;

            CCDrawManager.SetViewPort(0, 0,
                                      (int)(size.Width * CCMacros.CCContentScaleFactor()),
                                      (int)(size.Height * CCMacros.CCContentScaleFactor())
                                      );

            CCDrawManager.ViewMatrix       = Matrix.Identity;
            CCDrawManager.ProjectionMatrix = Matrix.Identity;

            Matrix projection      = Matrix.CreateOrthographicOffCenter(0, size.Width, 0, size.Height, -1024.0f, 1024.0f);
            Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);

            CCDrawManager.WorldMatrix = (halfPixelOffset * projection);
        }
コード例 #16
0
        public AtlasFastBitmap()
        {
            // Upper Label
            for (int i = 0; i < 100; i++)
            {
                //char str[6] = {0};
                string str;
                //sprintf(str, "-%d-", i);
                str = string.Format("-{0,d}-", i);
                CCLabelBMFont label = new CCLabelBMFont(str, "fonts/bitmapFontTest.fnt");
                AddChild(label);

                CCSize s = CCDirector.SharedDirector.WinSize;

                CCPoint p = new CCPoint(CCMacros.CCRandomBetween0And1() * s.Width, CCMacros.CCRandomBetween0And1() * s.Height);
                label.Position    = p;
                label.AnchorPoint = new CCPoint(0.5f, 0.5f);
            }
        }
コード例 #17
0
ファイル: CCTMXLayer.cs プロジェクト: pekayatt/cocos2d-xna
        /** returns the position in points of a given tile coordinate */

        public CCPoint PositionAt(CCPoint pos)
        {
            CCPoint ret = CCPoint.Zero;

            switch (m_uLayerOrientation)
            {
            case CCTMXOrientation.Ortho:
                ret = PositionForOrthoAt(pos);
                break;

            case CCTMXOrientation.Iso:
                ret = PositionForIsoAt(pos);
                break;

            case CCTMXOrientation.Hex:
                ret = PositionForHexAt(pos);
                break;
            }
            ret = CCMacros.CCPointPixelsToPoints(ret);
            return(ret);
        }
コード例 #18
0
        protected virtual bool InitWithWidthAndHeight(int w, int h, SurfaceFormat colorFormat, DepthFormat depthFormat, RenderTargetUsage usage)
        {
            w = (w * CCMacros.CCContentScaleFactor());
            h = (h * CCMacros.CCContentScaleFactor());

            m_pTexture = new CCTexture2D();
            m_pTexture.SetAliasTexParameters();

            m_pRenderTarget2D = CCDrawManager.CreateRenderTarget(w, h, colorFormat, depthFormat, usage);
            m_pTexture.InitWithTexture(m_pRenderTarget2D);

            m_bFirstUsage = true;

            m_pSprite = new CCSprite(m_pTexture);
            //m_pSprite.scaleY = -1;
            m_pSprite.BlendFunc = new CCBlendFunc(CCMacros.CCDefaultSourceBlending, CCMacros.CCDefaultDestinationBlending); // OGLES.GL_ONE, OGLES.GL_ONE_MINUS_SRC_ALPHA);

            AddChild(m_pSprite);

            return(true);
        }
コード例 #19
0
        protected virtual bool InitWithWidthAndHeight(int w, int h, SurfaceFormat colorFormat, DepthFormat depthFormat, RenderTargetUsage usage)
        {
            w = (int)Math.Ceiling(w * CCMacros.CCContentScaleFactor());
            h = (int)Math.Ceiling(h * CCMacros.CCContentScaleFactor());

            m_pTexture = new CCTexture2D();
            m_pTexture.IsAntialiased = false;

            m_pRenderTarget2D = CCDrawManager.CreateRenderTarget(w, h, colorFormat, depthFormat, usage);
            m_pTexture.InitWithTexture(m_pRenderTarget2D, colorFormat, true, false);

            m_bFirstUsage = true;

            m_pSprite = new CCSprite(m_pTexture);
            //m_pSprite.scaleY = -1;
            m_pSprite.BlendFunc = CCBlendFunc.AlphaBlend;

            AddChild(m_pSprite);

            return(true);
        }
コード例 #20
0
ファイル: CCTMXLayer.cs プロジェクト: pekayatt/cocos2d-xna
        /** 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);
        }
コード例 #21
0
ファイル: CCTMXLayer.cs プロジェクト: pekayatt/cocos2d-xna
        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);
        }
コード例 #22
0
        protected void UpdateSliderPosition(CCPoint location)
        {
            // Clamp the position of the icon within the circle
            CCRect backgroundBox = _background.BoundingBox;

            // Get the center point of the background image
            float centerX = _startPos.X + backgroundBox.Size.Width * 0.5f;
            float centerY = _startPos.Y + backgroundBox.Size.Height * 0.5f;

            // Work out the distance difference between the location and center
            float dx = location.X - centerX;
            float dy = location.Y - centerY;

            // Update angle by using the direction of the location
            var   angle    = (float)Math.Atan2(dy, dx);
            float angleDeg = CCMacros.CCRadiansToDegrees(angle) + 180.0f;

            // use the position / slider width to determin the percentage the dragger is at
            Hue = angleDeg;

            // send Control callback
            SendActionsForControlEvents(CCControlEvent.ValueChanged);
        }
コード例 #23
0
        public virtual CCAffineTransform NodeToParentTransform()
        {
            if (m_bIsTransformDirty)
            {
                // Translate values
                float x = m_tPosition.X;
                float y = m_tPosition.Y;

                if (m_bIgnoreAnchorPointForPosition)
                {
                    x += m_tAnchorPointInPoints.X;
                    y += m_tAnchorPointInPoints.Y;
                }

                // Rotation values
                float c = 1, s = 0;
                if (m_fRotation != 0.0f)
                {
                    float radians = -CCMacros.CCDegreesToRadians(m_fRotation);
                    c = (float)Math.Cos(radians);
                    s = (float)Math.Sin(radians);
                }

                bool needsSkewMatrix = (m_fSkewX != 0f || m_fSkewY != 0f);


                // optimization:
                // inline anchor point calculation if skew is not needed
                if (!needsSkewMatrix && !m_tAnchorPointInPoints.Equals(CCPoint.Zero))
                {
                    x += c * -m_tAnchorPointInPoints.X * m_fScaleX + -s * -m_tAnchorPointInPoints.Y * m_fScaleY;
                    y += s * -m_tAnchorPointInPoints.X * m_fScaleX + c * -m_tAnchorPointInPoints.Y * m_fScaleY;
                }

                // Build Transform Matrix
                //m_tTransform = new CCAffineTransform(
                //    c * m_fScaleX, s * m_fScaleX,
                //    -s * m_fScaleY, c * m_fScaleY,
                //    x, y);

                // Build Transform Matrix
                m_tTransform.a  = c * m_fScaleX;
                m_tTransform.b  = s * m_fScaleX;
                m_tTransform.c  = -s * m_fScaleY;
                m_tTransform.d  = c * m_fScaleY;
                m_tTransform.tx = x;
                m_tTransform.ty = y;

                // XXX: Try to inline skew
                // If skew is needed, apply skew and then anchor point
                if (needsSkewMatrix)
                {
                    var skewMatrix = new CCAffineTransform(
                        1.0f, (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewY)),
                        (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewX)), 1.0f,
                        0.0f, 0.0f);

                    m_tTransform = CCAffineTransform.Concat(skewMatrix, m_tTransform);

                    // adjust anchor point
                    if (!m_tAnchorPointInPoints.Equals(CCPoint.Zero))
                    {
                        m_tTransform = CCAffineTransform.Translate(m_tTransform,
                                                                   -m_tAnchorPointInPoints.X,
                                                                   -m_tAnchorPointInPoints.Y);
                    }
                }

                m_bIsTransformDirty = false;
            }

            return(m_tTransform);
        }
コード例 #24
0
        public virtual CCAffineTransform NodeToParentTransform()
        {
            if (m_bTransformDirty)
            {
                // Translate values
                float x = m_obPosition.X;
                float y = m_obPosition.Y;

                if (m_bIgnoreAnchorPointForPosition)
                {
                    x += m_obAnchorPointInPoints.X;
                    y += m_obAnchorPointInPoints.Y;
                }

                // Rotation values
                // Change rotation code to handle X and Y
                // If we skew with the exact same value for both x and y then we're simply just rotating
                float cx = 1, sx = 0, cy = 1, sy = 0;
                if (m_fRotationX != 0 || m_fRotationY != 0)
                {
                    float radiansX = -CCMacros.CCDegreesToRadians(m_fRotationX);
                    float radiansY = -CCMacros.CCDegreesToRadians(m_fRotationY);
                    cx = (float)Math.Cos(radiansX);
                    sx = (float)Math.Sin(radiansX);
                    cy = (float)Math.Cos(radiansY);
                    sy = (float)Math.Sin(radiansY);
                }

                bool needsSkewMatrix = (m_fSkewX != 0f || m_fSkewY != 0f);

                // optimization:
                // inline anchor point calculation if skew is not needed
                if (!needsSkewMatrix && !m_obAnchorPointInPoints.Equals(CCPoint.Zero))
                {
                    x += cy * -m_obAnchorPointInPoints.X * m_fScaleX + -sx * -m_obAnchorPointInPoints.Y * m_fScaleY;
                    y += sy * -m_obAnchorPointInPoints.X * m_fScaleX + cx * -m_obAnchorPointInPoints.Y * m_fScaleY;
                }

                // Build Transform Matrix
                // Adjusted transform calculation for rotational skew
                m_sTransform.a  = cy * m_fScaleX;
                m_sTransform.b  = sy * m_fScaleX;
                m_sTransform.c  = -sx * m_fScaleY;
                m_sTransform.d  = cx * m_fScaleY;
                m_sTransform.tx = x;
                m_sTransform.ty = y;

                // XXX: Try to inline skew
                // If skew is needed, apply skew and then anchor point
                if (needsSkewMatrix)
                {
                    var skewMatrix = new CCAffineTransform(
                        1.0f, (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewY)),
                        (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewX)), 1.0f,
                        0.0f, 0.0f);

                    m_sTransform = CCAffineTransform.Concat(skewMatrix, m_sTransform);

                    // adjust anchor point
                    if (!m_obAnchorPointInPoints.Equals(CCPoint.Zero))
                    {
                        m_sTransform = CCAffineTransform.Translate(m_sTransform,
                                                                   -m_obAnchorPointInPoints.X,
                                                                   -m_obAnchorPointInPoints.Y);
                    }
                }

                if (m_bAdditionalTransformDirty)
                {
                    m_sTransform.Concat(ref m_sAdditionalTransform);
                    m_bAdditionalTransformDirty = false;
                }

                m_bTransformDirty = false;
            }

            return(m_sTransform);
        }
コード例 #25
0
        protected virtual bool InitWithTexture(CCTexture2D pobTexture, CCRect rect)
        {
            CCRect rectInPixels = CCMacros.CCRectanglePointsToPixels(rect);

            return(InitWithTexture(pobTexture, rectInPixels, false, new CCPoint(0, 0), rectInPixels.Size));
        }
コード例 #26
0
ファイル: CCSprite.cs プロジェクト: migueldeicaza/cocos2d-xna
        private void SetTextureCoords(CCRect rect)
        {
            rect = CCMacros.CCRectanglePointsToPixels(rect);

            CCTexture2D tex = m_pobBatchNode != null ? m_pobTextureAtlas.Texture : m_pobTexture;

            if (tex == null)
            {
                return;
            }

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

            float left, right, top, bottom;

            if (m_bRectRotated)
            {
#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
                left   = (2 * rect.origin.x + 1) / (2 * atlasWidth);
                right  = left + (rect.size.height * 2 - 2) / (2 * atlasWidth);
                top    = (2 * rect.origin.y + 1) / (2 * atlasHeight);
                bottom = top + (rect.size.width * 2 - 2) / (2 * atlasHeight);
#else
                left   = rect.Origin.X / atlasWidth;
                right  = (rect.Origin.X + rect.Size.Height) / atlasWidth;
                top    = rect.Origin.Y / atlasHeight;
                bottom = (rect.Origin.Y + rect.Size.Width) / atlasHeight;
#endif
                // CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL

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

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

                m_sQuad.BottomLeft.TexCoords.U  = left;
                m_sQuad.BottomLeft.TexCoords.V  = top;
                m_sQuad.BottomRight.TexCoords.U = left;
                m_sQuad.BottomRight.TexCoords.V = bottom;
                m_sQuad.TopLeft.TexCoords.U     = right;
                m_sQuad.TopLeft.TexCoords.V     = top;
                m_sQuad.TopRight.TexCoords.U    = right;
                m_sQuad.TopRight.TexCoords.V    = bottom;
            }
            else
            {
#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
                left   = (2 * rect.origin.x + 1) / (2 * atlasWidth);
                right  = left + (rect.size.width * 2 - 2) / (2 * atlasWidth);
                top    = (2 * rect.origin.y + 1) / (2 * atlasHeight);
                bottom = top + (rect.size.height * 2 - 2) / (2 * atlasHeight);
#else
                left   = rect.Origin.X / atlasWidth;
                right  = (rect.Origin.X + rect.Size.Width) / atlasWidth;
                top    = rect.Origin.Y / atlasHeight;
                bottom = (rect.Origin.Y + rect.Size.Height) / atlasHeight;
#endif
                // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL

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

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

                m_sQuad.BottomLeft.TexCoords.U  = left;
                m_sQuad.BottomLeft.TexCoords.V  = bottom;
                m_sQuad.BottomRight.TexCoords.U = right;
                m_sQuad.BottomRight.TexCoords.V = bottom;
                m_sQuad.TopLeft.TexCoords.U     = left;
                m_sQuad.TopLeft.TexCoords.V     = top;
                m_sQuad.TopRight.TexCoords.U    = right;
                m_sQuad.TopRight.TexCoords.V    = top;
            }
        }
コード例 #27
0
        public override void UpdateAtlasValues()
        {
            int n = m_sString.Length;

            CCTexture2D texture = m_pTextureAtlas.Texture;

            float textureWide = texture.PixelsWide;
            float textureHigh = texture.PixelsHigh;

            float itemWidthInPixels  = m_uItemWidth * CCMacros.CCContentScaleFactor();
            float itemHeightInPixels = m_uItemHeight * CCMacros.CCContentScaleFactor();

            if (m_bIgnoreContentScaleFactor)
            {
                itemWidthInPixels  = m_uItemWidth;
                itemHeightInPixels = m_uItemHeight;
            }

            for (int i = 0; i < n; i++)
            {
                var   a   = (char)(m_sString[i] - m_cMapStartChar);
                float row = (a % m_uItemsPerRow);
                float col = (a / m_uItemsPerRow);

#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
                // Issue #938. Don't use texStepX & texStepY
                float left   = (2 * row * itemWidthInPixels + 1) / (2 * textureWide);
                float right  = left + (itemWidthInPixels * 2 - 2) / (2 * textureWide);
                float top    = (2 * col * itemHeightInPixels + 1) / (2 * textureHigh);
                float bottom = top + (itemHeightInPixels * 2 - 2) / (2 * textureHigh);
#else
                float left   = row * itemWidthInPixels / textureWide;
                float right  = left + itemWidthInPixels / textureWide;
                float top    = col * itemHeightInPixels / textureHigh;
                float bottom = top + itemHeightInPixels / textureHigh;
#endif
                // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL

                CCV3F_C4B_T2F_Quad quad;

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

                quad.BottomLeft.Vertices.X  = i * m_uItemWidth;
                quad.BottomLeft.Vertices.Y  = 0.0f;
                quad.BottomLeft.Vertices.Z  = 0.0f;
                quad.BottomRight.Vertices.X = i * m_uItemWidth + m_uItemWidth;
                quad.BottomRight.Vertices.Y = 0.0f;
                quad.BottomRight.Vertices.Z = 0.0f;
                quad.TopLeft.Vertices.X     = i * m_uItemWidth;
                quad.TopLeft.Vertices.Y     = m_uItemHeight;
                quad.TopLeft.Vertices.Z     = 0.0f;
                quad.TopRight.Vertices.X    = i * m_uItemWidth + m_uItemWidth;
                quad.TopRight.Vertices.Y    = m_uItemHeight;
                quad.TopRight.Vertices.Z    = 0.0f;


                quad.TopLeft.Colors = quad.TopRight.Colors = quad.BottomLeft.Colors = quad.BottomRight.Colors =
                    new CCColor4B(_displayedColor.R, _displayedColor.G, _displayedColor.B, _displayedOpacity);

                m_pTextureAtlas.UpdateQuad(ref quad, i);
            }
        }
コード例 #28
0
        // pointRect should be in Texture coordinates, not pixel coordinates
        private void InitTexCoordsWithRect(CCRect pointRect)
        {
            // convert to Tex coords

            var rect = new CCRect(
                pointRect.Origin.X * CCMacros.CCContentScaleFactor(),
                pointRect.Origin.Y * CCMacros.CCContentScaleFactor(),
                pointRect.Size.Width * CCMacros.CCContentScaleFactor(),
                pointRect.Size.Height * CCMacros.CCContentScaleFactor());

            float wide = pointRect.Size.Width;
            float high = pointRect.Size.Height;

            if (m_pTexture != null)
            {
                wide = m_pTexture.PixelsWide;
                high = m_pTexture.PixelsHigh;
            }

#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
            float left   = (rect.origin.x * 2 + 1) / (wide * 2);
            float bottom = (rect.origin.y * 2 + 1) / (high * 2);
            float right  = left + (rect.size.width * 2 - 2) / (wide * 2);
            float top    = bottom + (rect.size.height * 2 - 2) / (high * 2);
        #else
            float left   = rect.Origin.X / wide;
            float bottom = rect.Origin.Y / high;
            float right  = left + rect.Size.Width / wide;
            float top    = bottom + rect.Size.Height / high;
#endif
            // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL

            // Important. Texture in cocos2d are inverted, so the Y component should be inverted
            float tmp = top;
            top    = bottom;
            bottom = tmp;

            CCV3F_C4B_T2F_Quad[] quads;
            int start, end;
            if (m_pBatchNode != null)
            {
                quads = m_pBatchNode.TextureAtlas.m_pQuads.Elements;
                m_pBatchNode.TextureAtlas.Dirty = true;
                start = m_uAtlasIndex;
                end   = m_uAtlasIndex + m_uTotalParticles;
            }
            else
            {
                quads = m_pQuads.Elements;
                start = 0;
                end   = m_uTotalParticles;
            }

            for (int i = start; i < end; i++)
            {
                // bottom-left vertex:
                quads[i].BottomLeft.TexCoords.U = left;
                quads[i].BottomLeft.TexCoords.V = bottom;
                // bottom-right vertex:
                quads[i].BottomRight.TexCoords.U = right;
                quads[i].BottomRight.TexCoords.V = bottom;
                // top-left vertex:
                quads[i].TopLeft.TexCoords.U = left;
                quads[i].TopLeft.TexCoords.V = top;
                // top-right vertex:
                quads[i].TopRight.TexCoords.U = right;
                quads[i].TopRight.TexCoords.V = top;
            }
        }
コード例 #29
0
 public void DrawCircle(CCPoint center, float radius, CCColor4B color)
 {
     DrawCircle(center, radius, CCMacros.CCDegreesToRadians(360f), 360, color);
 }
コード例 #30
0
        public static void SetDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy)
        {
            Debug.Assert(resolutionPolicy != ResolutionPolicy.UnKnown, "should set resolutionPolicy");

            if (width == 0.0f || height == 0.0f)
            {
                return;
            }

            m_obDesignResolutionSize.Width  = width;
            m_obDesignResolutionSize.Height = height;

            m_fScaleX = m_obScreenSize.Width / m_obDesignResolutionSize.Width;
            m_fScaleY = m_obScreenSize.Height / m_obDesignResolutionSize.Height;

            if (resolutionPolicy == ResolutionPolicy.NoBorder)
            {
                m_fScaleX = m_fScaleY = Math.Max(m_fScaleX, m_fScaleY);
            }

            if (resolutionPolicy == ResolutionPolicy.ShowAll)
            {
                m_fScaleX = m_fScaleY = Math.Min(m_fScaleX, m_fScaleY);
            }

            // calculate the rect of viewport
            float viewPortW = m_obDesignResolutionSize.Width * m_fScaleX;
            float viewPortH = m_obDesignResolutionSize.Height * m_fScaleY;

            m_obViewPortRect = new CCRect((m_obScreenSize.Width - viewPortW) / 2, (m_obScreenSize.Height - viewPortH) / 2, viewPortW, viewPortH);

            m_eResolutionPolicy = resolutionPolicy;

            // reset director's member variables to fit visible rect
            CCDirector.SharedDirector.m_obWinSizeInPoints = Size;
            CCDirector.SharedDirector.m_obWinSizeInPixels = new CCSize(m_obDesignResolutionSize.Width * CCMacros.CCContentScaleFactor(),
                                                                       m_obDesignResolutionSize.Height * CCMacros.CCContentScaleFactor());
            CCDirector.SharedDirector.CreateStatsLabel();
            CCDirector.SharedDirector.SetGlDefaultValues();
        }