Exemplo n.º 1
0
        internal TiledMapTileset(ContentReader input)
        {
            Name = input.GetRelativeAssetName(input.ReadString());
            var animatedTiles = new List <TiledMapTilesetAnimatedTile>();
            var tiles         = new List <TiledMapTilesetTile>();

            _animatedTilesByLocalTileIdentifier = new Dictionary <int, TiledMapTilesetAnimatedTile>();

            try
            {
                Texture = input.ContentManager.Load <Texture2D>(Name);
            }
            catch (ContentLoadException)
            {
                // ignore error, texture is optional
            }

            FirstGlobalIdentifier = input.ReadInt32();
            TileWidth             = input.ReadInt32();
            TileHeight            = input.ReadInt32();
            TileCount             = input.ReadInt32();
            Spacing    = input.ReadInt32();
            Margin     = input.ReadInt32();
            Columns    = input.ReadInt32();
            Properties = new TiledMapProperties();

            var explicitTileCount = input.ReadInt32();

            for (var tileIndex = 0; tileIndex < explicitTileCount; tileIndex++)
            {
                var localTileIdentifier  = input.ReadInt32();
                var animationFramesCount = input.ReadInt32();

                TiledMapTilesetTile tilesetTile;

                if (animationFramesCount <= 0)
                {
                    tilesetTile = new TiledMapTilesetTile(localTileIdentifier, input);
                }
                else
                {
                    var animatedTilesetTile = new TiledMapTilesetAnimatedTile(this, input, localTileIdentifier, animationFramesCount);
                    animatedTiles.Add(animatedTilesetTile);
                    _animatedTilesByLocalTileIdentifier.Add(localTileIdentifier, animatedTilesetTile);
                    tilesetTile = animatedTilesetTile;
                }

                tiles.Add(tilesetTile);

                input.ReadTiledMapProperties(tilesetTile.Properties);
            }

            input.ReadTiledMapProperties(Properties);

            Tiles         = new ReadOnlyCollection <TiledMapTilesetTile>(tiles);
            AnimatedTiles = new ReadOnlyCollection <TiledMapTilesetAnimatedTile>(animatedTiles);
        }
Exemplo n.º 2
0
        internal TiledMapImageLayer(ContentReader input)
            : base(input)
        {
            var textureAssetName = input.GetRelativeAssetName(input.ReadString());

            Texture = input.ContentManager.Load <Texture2D>(textureAssetName);
            var x = input.ReadSingle();
            var y = input.ReadSingle();

            Position = new Vector2(x, y);
        }
        protected override List <TextureAtlas> Read(ContentReader reader, List <TextureAtlas> existingInstance)
        {
            String              assetName;
            Texture2D           texture;
            List <TextureAtlas> atlasList = new List <TextureAtlas>();
            int           textureCount    = 0;
            int           subTextCount    = 0;
            TextureAtlas  curAtlas;
            int           textWidth;
            int           textHeight;
            SurfaceFormat format = SurfaceFormat.Bgr32;

            Microsoft.Xna.Framework.Graphics.TextureFilter minFilter = Microsoft.Xna.Framework.Graphics.TextureFilter.Anisotropic;
            Microsoft.Xna.Framework.Graphics.TextureFilter maxFilter = Microsoft.Xna.Framework.Graphics.TextureFilter.Anisotropic;
            LibGDXAtlasExtender.Model.KeyEnums.Repeat      repeat    = LibGDXAtlasExtender.Model.KeyEnums.Repeat.none;
            String buffStr;

            textureCount = reader.ReadInt32();

            for (var i = 0; i < textureCount; i++)
            {
                assetName  = reader.GetRelativeAssetName(reader.ReadString());
                textWidth  = reader.ReadInt32();
                textHeight = reader.ReadInt32();
                buffStr    = reader.ReadString();
                KeyTypeUtilities.Parse <SurfaceFormat>(buffStr, ref format);
                buffStr = reader.ReadString();
                KeyTypeUtilities.Parse <Microsoft.Xna.Framework.Graphics.TextureFilter>(buffStr, ref minFilter);
                buffStr = reader.ReadString();
                KeyTypeUtilities.Parse <Microsoft.Xna.Framework.Graphics.TextureFilter>(buffStr, ref maxFilter);
                buffStr = reader.ReadString();
                KeyTypeUtilities.Parse <LibGDXAtlasExtender.Model.KeyEnums.Repeat>(buffStr, ref repeat);

                Console.WriteLine("Assset Name: " + assetName);
                texture  = reader.ContentManager.Load <Texture2D>(assetName);
                curAtlas = new TextureAtlas(texture, textWidth, textHeight, format, minFilter, maxFilter, repeat);

                subTextCount = reader.ReadInt32();

                for (int s = 0; s < subTextCount; s++)
                {
                    curAtlas.CreateRegion(reader.ReadString(), reader.ReadBoolean(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(),
                                          reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
                }
                Console.WriteLine("Width: {0} Height: {1}", curAtlas.GetRegion(0).Width, curAtlas.GetRegion(0).Height);

                atlasList.Add(curAtlas);
            }

            return(atlasList);
        }
Exemplo n.º 4
0
        protected override BitmapFont Read(ContentReader reader, BitmapFont existingInstance)
        {
            int textureAssetCount = reader.ReadInt32();

            Texture2D[] textures = new Texture2D[textureAssetCount];

            for (var i = 0; i < textureAssetCount; i++)
            {
                string assetName = reader.GetRelativeAssetName(reader.ReadString());
                textures[i] = reader.ContentManager.Load <Texture2D>(assetName);
            }

            int lineHeight   = reader.ReadInt32();
            int regionCount  = reader.ReadInt32();
            var characterMap = new Dictionary <int, BitmapFontRegion>(regionCount);

            for (int r = 0; r < regionCount; r++)
            {
                int character     = reader.ReadInt32();
                int textureIndex  = reader.ReadInt32();
                int x             = reader.ReadInt32();
                int y             = reader.ReadInt32();
                int width         = reader.ReadInt32();
                int height        = reader.ReadInt32();
                int xOffset       = reader.ReadInt32();
                int yOffset       = reader.ReadInt32();
                int xAdvance      = reader.ReadInt32();
                var textureRegion = new TextureRegion2D(textures[textureIndex], x, y, width, height);

                var bmpReg = new BitmapFontRegion(textureRegion, character, xOffset, yOffset, xAdvance);
                characterMap.Add(character, bmpReg);
            }

            int kerningsCount = reader.ReadInt32();

            for (int k = 0; k < kerningsCount; k++)
            {
                int first  = reader.ReadInt32();
                int second = reader.ReadInt32();
                int amount = reader.ReadInt32();

                if (characterMap.TryGetValue(first, out BitmapFontRegion region))
                {
                    region.Kernings.Add(second, amount);
                }
            }

            return(new BitmapFont(reader.AssetName, characterMap, lineHeight));
        }
Exemplo n.º 5
0
        internal TiledMapLayerModel(ContentReader reader, bool isDynamicData = false)
        {
            var graphicsDevice = reader.GetGraphicsDevice();

            LayerName = reader.ReadString();
            var textureAssetName = reader.GetRelativeAssetName(reader.ReadString());

            Texture = reader.ContentManager.Load <Texture2D>(textureAssetName);

            var vertexCount = reader.ReadInt32();
            var vertices    = new VertexPositionTexture[vertexCount];

            for (var i = 0; i < vertexCount; i++)
            {
                var x = reader.ReadSingle();
                var y = reader.ReadSingle();
                var textureCoordinateX = reader.ReadSingle();
                var textureCoordinateY = reader.ReadSingle();
                vertices[i] = new VertexPositionTexture(new Vector3(x, y, 0), new Vector2(textureCoordinateX, textureCoordinateY));
            }

            if (isDynamicData)
            {
                Vertices = vertices;
            }

            VertexBuffer = isDynamicData
                ? new DynamicVertexBuffer(graphicsDevice, VertexPositionTexture.VertexDeclaration, vertexCount, BufferUsage.WriteOnly)
                : new VertexBuffer(graphicsDevice, VertexPositionTexture.VertexDeclaration, vertexCount, BufferUsage.WriteOnly);
            VertexBuffer.SetData(vertices, 0, vertexCount);

            var indexCount = reader.ReadInt32();
            var indices    = new ushort[indexCount];

            for (var i = 0; i < indexCount; i++)
            {
                indices[i] = reader.ReadUInt16();
            }

            IndexBuffer = isDynamicData
                ? new DynamicIndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, indexCount, BufferUsage.WriteOnly)
                : new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, indexCount, BufferUsage.WriteOnly);
            IndexBuffer.SetData(indices, 0, indexCount);

            TrianglesCount = indexCount / 3;
        }
Exemplo n.º 6
0
        protected override SpriteSheetAnimationFactory Read(ContentReader reader, SpriteSheetAnimationFactory existingInstance)
        {
            var relativeName          = reader.ReadString();
            var textureAtlasAssetName = reader.GetRelativeAssetName(relativeName);
            var textureAtlas          = reader.ContentManager.Load <TextureAtlas>(textureAtlasAssetName);
            var frameCount            = reader.ReadInt32();
            var regions = new List <TextureRegion2D>();

            for (var i = 0; i < frameCount; i++)
            {
                var frameName     = reader.ReadString();
                var textureRegion = textureAtlas[frameName];
                regions.Add(textureRegion);
            }

            var animationFactory = new SpriteSheetAnimationFactory(regions);
            var animationCount   = reader.ReadInt32();

            for (var i = 0; i < animationCount; i++)
            {
                var name            = reader.ReadString();
                var framesPerSecond = reader.ReadInt32();
                var isLooping       = reader.ReadBoolean();
                var isReversed      = reader.ReadBoolean();
                var isPingPong      = reader.ReadBoolean();
                var frameDuration   = 1.0f / framesPerSecond;
                var frameIndexCount = reader.ReadInt32();
                var frameIndicies   = new int[frameIndexCount];

                for (var f = 0; f < frameIndexCount; f++)
                {
                    var frameIndex = reader.ReadInt32();
                    frameIndicies[f] = frameIndex;
                }

                var animationData = new SpriteSheetAnimationData(frameIndicies, frameDuration, isLooping, isReversed,
                                                                 isPingPong);
                animationFactory.Add(name, animationData);
            }

            return(animationFactory);
        }
Exemplo n.º 7
0
        protected override TextureAtlas Read(ContentReader reader, TextureAtlas existingInstance)
        {
            var assetName = reader.GetRelativeAssetName(reader.ReadString());
            var texture   = reader.ContentManager.Load <Texture2D>(assetName);
            var atlas     = new TextureAtlas(assetName, texture);

            var regionCount = reader.ReadInt32();

            for (var i = 0; i < regionCount; i++)
            {
                atlas.CreateRegion(
                    reader.ReadString(),
                    reader.ReadInt32(),
                    reader.ReadInt32(),
                    reader.ReadInt32(),
                    reader.ReadInt32());
            }

            return(atlas);
        }
        protected override TextureAtlas Read(ContentReader reader, TextureAtlas existingInstance)
        {
            var texturePackerFile = Load(reader);
            var assetName         = reader.GetRelativeAssetName(texturePackerFile.Metadata.Image);
            var texture           = reader.ContentManager.Load <Texture2D>(assetName);
            var atlas             = new TextureAtlas(assetName, texture);

            var regionCount = texturePackerFile.Regions.Count;

            for (var i = 0; i < regionCount; i++)
            {
                atlas.CreateRegion(
                    ContentReaderExtensions.RemoveExtension(texturePackerFile.Regions[i].Filename),
                    texturePackerFile.Regions[i].Frame.X,
                    texturePackerFile.Regions[i].Frame.Y,
                    texturePackerFile.Regions[i].Frame.Width,
                    texturePackerFile.Regions[i].Frame.Height);
            }

            return(atlas);
        }
Exemplo n.º 9
0
        protected override TextureAtlas Read(ContentReader reader, TextureAtlas existingInstance)
        {
            var assetName = reader.GetRelativeAssetName(reader.ReadString());
            var texture   = reader.ContentManager.Load <Texture2D>(assetName);
            var atlas     = new TextureAtlas(assetName, texture);

            var regionCount = reader.ReadInt32();

            for (var i = 0; i < regionCount; i++)
            {
                var name   = reader.ReadString();
                var bounds = new Rectangle(reader.ReadInt32(),
                                           reader.ReadInt32(),
                                           reader.ReadInt32(),
                                           reader.ReadInt32());

                var isNinePatch = reader.ReadBoolean();
                if (!isNinePatch)
                {
                    atlas.CreateRegion(name, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                }
                else
                {
                    var thickness = new Thickness
                    {
                        Left   = reader.ReadInt32(),
                        Right  = reader.ReadInt32(),
                        Top    = reader.ReadInt32(),
                        Bottom = reader.ReadInt32()
                    };

                    atlas.CreateNinePatchRegion(name, bounds.X, bounds.Y, bounds.Width, bounds.Height, thickness);
                }
            }

            return(atlas);
        }
        protected override Atlas Read(ContentReader reader, Atlas existingInstance)
        {
            string    file        = reader.ReadString();
            Texture2D t           = reader.ContentManager.Load <Texture2D>(reader.GetRelativeAssetName(file));
            int       regionCount = reader.ReadInt32();

            Dictionary <string, TextureRegion2D> objects = new Dictionary <string, TextureRegion2D>();

            for (int i = 0; i < regionCount; i++)
            {
                string name       = reader.ReadString();
                int    position_x = reader.ReadInt32();
                int    position_y = reader.ReadInt32();
                int    width      = reader.ReadInt32();
                int    height     = reader.ReadInt32();

                TextureRegion2D region = new TextureRegion2D(t, position_x, position_y, width, height);
                objects.Add(name, region);
            }

            Atlas atlas = new Atlas(objects);

            return(atlas);
        }
Exemplo n.º 11
0
        protected override BitmapFont Read(ContentReader reader, BitmapFont existingInstance)
        {
            var textureAssetCount = reader.ReadInt32();
            var assets            = new List <string>();

            for (var i = 0; i < textureAssetCount; i++)
            {
                var assetName = reader.ReadString();
                assets.Add(assetName);
            }

            var textures = assets
                           .Select(textureName => reader.ContentManager.Load <Texture2D>(reader.GetRelativeAssetName(textureName)))
                           .ToArray();

            var lineHeight  = reader.ReadInt32();
            var regionCount = reader.ReadInt32();
            var regions     = new BitmapFontRegion[regionCount];

            for (var r = 0; r < regionCount; r++)
            {
                var character     = reader.ReadInt32();
                var textureIndex  = reader.ReadInt32();
                var x             = reader.ReadInt32();
                var y             = reader.ReadInt32();
                var width         = reader.ReadInt32();
                var height        = reader.ReadInt32();
                var xOffset       = reader.ReadInt32();
                var yOffset       = reader.ReadInt32();
                var xAdvance      = reader.ReadInt32();
                var textureRegion = new TextureRegion2D(textures[textureIndex], x, y, width, height);
                regions[r] = new BitmapFontRegion(textureRegion, character, xOffset, yOffset, xAdvance);
            }

            return(new BitmapFont(reader.AssetName, regions, lineHeight));
        }
Exemplo n.º 12
0
        protected override BitmapFont Read(ContentReader reader, BitmapFont existingInstance)
        {
            var textureAssetCount = reader.ReadInt32();
            var assets            = new List <string>();

            for (var i = 0; i < textureAssetCount; i++)
            {
                var assetName = reader.ReadString();
                assets.Add(assetName);
            }

            var textures = assets
                           .Select(textureName => reader.ContentManager.Load <Texture2D>(reader.GetRelativeAssetName(textureName)))
                           .ToArray();

            var lineHeight  = reader.ReadInt32();
            var regionCount = reader.ReadInt32();
            var regions     = new BitmapFontRegion[regionCount];

            for (var r = 0; r < regionCount; r++)
            {
                var character     = reader.ReadInt32();
                var textureIndex  = reader.ReadInt32();
                var x             = reader.ReadInt32();
                var y             = reader.ReadInt32();
                var width         = reader.ReadInt32();
                var height        = reader.ReadInt32();
                var xOffset       = reader.ReadInt32();
                var yOffset       = reader.ReadInt32();
                var xAdvance      = reader.ReadInt32();
                var textureRegion = new TextureRegion2D(textures[textureIndex], x, y, width, height);
                regions[r] = new BitmapFontRegion(textureRegion, character, xOffset, yOffset, xAdvance);
            }

            var characterMap  = regions.ToDictionary(r => r.Character);
            var kerningsCount = reader.ReadInt32();

            for (var k = 0; k < kerningsCount; k++)
            {
                var first  = reader.ReadInt32();
                var second = reader.ReadInt32();
                var amount = reader.ReadInt32();

                // Find region
                BitmapFontRegion region;

                if (!characterMap.TryGetValue(first, out region))
                {
                    continue;
                }

                region.Kernings[second] = amount;
            }

            return(new BitmapFont(reader.AssetName, regions, lineHeight));
        }