public void TextureAtlas_RemoveRegions_Test()
        {
            var texture = new Texture2D(TestHelper.CreateGraphicsDevice(), 100, 200);
            var atlas   = new TextureAtlas(null, texture);

            var region0 = atlas.CreateRegion("region0", 10, 20, 30, 40);
            var region1 = atlas.CreateRegion("region1", 50, 60, 35, 45);
            var region2 = atlas.CreateRegion("region2", 32, 33, 34, 35);

            Assert.AreSame(texture, atlas.Texture);
            Assert.AreEqual(3, atlas.RegionCount);
            Assert.AreEqual(atlas.RegionCount, atlas.Regions.Count());
            Assert.AreSame(region1, atlas[1]);

            atlas.RemoveRegion(1);

            Assert.AreEqual(2, atlas.Regions.Count());
            Assert.AreSame(region0, atlas[0]);
            Assert.AreSame(region2, atlas[1]);

            atlas.RemoveRegion("region0");

            Assert.AreEqual(1, atlas.Regions.Count());
            Assert.AreSame(region2, atlas[0]);
        }
示例#2
0
        private void LoadTextures()
        {
            var texture = Content.Load <Texture2D>("Textures/Textures_sp");

            _textureRegions = new TextureAtlas("Textures", texture);

            const int textureAtlasConstant = 384;

            _textureRegions.CreateRegion("Tail", 0, 0, textureAtlasConstant, textureAtlasConstant);
            _textureRegions.CreateRegion("Part", textureAtlasConstant, 0, textureAtlasConstant, textureAtlasConstant);
            _textureRegions.CreateRegion("Head", textureAtlasConstant * 2, 0, textureAtlasConstant, textureAtlasConstant);
            _textureRegions.CreateRegion("Fruit", textureAtlasConstant * 3, 0, textureAtlasConstant,
                                         textureAtlasConstant);
            _textureRegions.CreateRegion("Grass", textureAtlasConstant * 4, 0, textureAtlasConstant,
                                         textureAtlasConstant);
            _textureRegions.CreateRegion("TopLeft", 0, textureAtlasConstant * 1, textureAtlasConstant,
                                         textureAtlasConstant);
            _textureRegions.CreateRegion("TopRight", textureAtlasConstant * 1, textureAtlasConstant * 1,
                                         textureAtlasConstant, textureAtlasConstant);
            _textureRegions.CreateRegion("BottomLeft", textureAtlasConstant * 2, textureAtlasConstant * 1,
                                         textureAtlasConstant, textureAtlasConstant);
            _textureRegions.CreateRegion("BottomRight", textureAtlasConstant * 3, textureAtlasConstant * 1,
                                         textureAtlasConstant, textureAtlasConstant);
            _textureRegions.CreateRegion("Tree", textureAtlasConstant * 4, textureAtlasConstant * 1, textureAtlasConstant,
                                         textureAtlasConstant);
        }
        public void TextureAtlas_CreateRegionThatAlreadyExistsThrowsException_Test()
        {
            var texture = new Texture2D(TestHelper.CreateGraphicsDevice(), 100, 200);
            var atlas   = new TextureAtlas(null, texture);

            atlas.CreateRegion("region0", 10, 20, 30, 40);
            Assert.Throws <InvalidOperationException>(() => atlas.CreateRegion("region0", 50, 60, 35, 45));
        }
        public void TextureAtlas_CreateRegionThatAlreadyExistsThrowsException_Test()
        {
            var texture = new Texture2D(TestHelper.CreateGraphicsDevice(), 100, 200);
            var atlas   = new TextureAtlas(texture);

            atlas.CreateRegion("region0", 10, 20, 30, 40);
            atlas.CreateRegion("region0", 50, 60, 35, 45);
        }
        public void TextureAtlas_GetRegionsByName_Test()
        {
            var texture = new Texture2D(TestHelper.CreateGraphicsDevice(), 100, 200);
            var atlas   = new TextureAtlas(null, texture);

            var region0 = atlas.CreateRegion("region0", 10, 20, 30, 40);
            var region1 = atlas.CreateRegion("region1", 50, 60, 35, 45);

            Assert.AreSame(region0, atlas["region0"]);
            Assert.AreSame(region1, atlas["region1"]);
            Assert.AreSame(region0, atlas.GetRegion("region0"));
            Assert.AreSame(region1, atlas.GetRegion("region1"));
        }
        public void TextureAtlas_GetRegion_InvalidIndexThrowsException_Test()
        {
            var texture = new Texture2D(TestHelper.CreateGraphicsDevice(), 100, 200);
            var atlas   = new TextureAtlas(null, texture);

            atlas.CreateRegion("region0", 10, 20, 30, 40);
            Assert.Throws <IndexOutOfRangeException>(() => atlas.GetRegion(-1));
        }
        public void TextureAtlas_GetRegion_InvalidNameThrowsException_Test()
        {
            var texture = new Texture2D(TestHelper.CreateGraphicsDevice(), 100, 200);
            var atlas   = new TextureAtlas(null, texture);

            atlas.CreateRegion("region0", 10, 20, 30, 40);
            Assert.Throws <KeyNotFoundException>(() => atlas.GetRegion("region1"));
        }
        public void TextureAtlas_EnumerateRegions_Test()
        {
            var texture = new Texture2D(TestHelper.CreateGraphicsDevice(), 100, 200);
            var atlas   = new TextureAtlas(null, texture);

            var regions = new TextureRegion2D[3];

            regions[0] = atlas.CreateRegion("region0", 10, 20, 30, 40);
            regions[1] = atlas.CreateRegion("region1", 50, 60, 35, 45);
            regions[2] = atlas.CreateRegion("region2", 32, 33, 34, 35);
            var index = 0;

            foreach (var region in atlas)
            {
                Assert.AreSame(region, regions[index]);
                index++;
            }
        }
        public void TextureAtlas_GetRegion_InvalidIndexThrowsException_Test()
        {
            var texture = new Texture2D(TestHelper.CreateGraphicsDevice(), 100, 200);
            var atlas   = new TextureAtlas(texture);

            atlas.CreateRegion("region0", 10, 20, 30, 40);

            atlas.GetRegion(-1);
        }
        public void TextureAtlas_CreateRegion_Test()
        {
            var texture = new Texture2D(TestHelper.CreateGraphicsDevice(), 100, 200);
            var atlas   = new TextureAtlas(null, texture);

            var region = atlas.CreateRegion("region0", 10, 20, 30, 40);

            Assert.AreSame(texture, region.Texture);
            Assert.AreEqual(10, region.X);
            Assert.AreEqual(20, region.Y);
            Assert.AreEqual(30, region.Width);
            Assert.AreEqual(40, region.Height);
        }
        protected override void LoadContent()
        {
            base.LoadContent();
            TextureAtlas atlas = new TextureAtlas("shapes", Assets.Load <Texture2D>("shapes"));

            atlas.CreateRegion("box", 10, 10, 100, 100);

            Sprite sprite = Assets.Add("PlayerSprite", new Sprite(atlas["box"]));

            //CurrentWorld = new World();
            //CurrentWorld.AddSystems(new RenderSystem());

            //CurrentWorld.CreateEntity("Player", new Component[] { new PositionComponent() { }, new RendererComponent() { Sprite = sprite}});

            //CurrentWorld = ECS.World.LoadFromFile("testW");
            //CurrentWorld.Init();
            //CurrentWorld.SaveToFile("testW");
        }
示例#12
0
        public TextureAtlas Create(Texture2D texture)
        {
            var result = new TextureAtlas(Name, texture);

            foreach (var sd in Regions)
            {
                var bounds = sd.Bounds;

                if (!sd.NinePatchInfo.HasValue)
                {
                    result.CreateRegion(sd.Name, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                }
                else
                {
                    result.CreateNinePatchRegion(sd.Name, bounds.X, bounds.Y, bounds.Width, bounds.Height, sd.NinePatchInfo.Value);
                }
            }

            return(result);
        }
示例#13
0
        public void BuildAtlases()
        {
            Dictionary <int, TileModel> tilemodels = new Dictionary <int, TileModel>();
            var blankTileData = new TileModel(0, null);

            tilemodels.Add(0, blankTileData);

            var globalIndex = 1;

            foreach (string filename in Filenames)
            {
                var texture         = Content.Load <Texture2D>("Tiles/Objects/" + filename);
                var newAtlas        = new TextureAtlas(filename, texture);
                var tilesHorizontal = newAtlas.Texture.Width / Tilesize;
                var tilesVertical   = newAtlas.Texture.Height / Tilesize;

                for (int y = 0; y < tilesVertical; y++)
                {
                    for (int x = 0; x < tilesHorizontal; x++)
                    {
                        var i = (y * tilesHorizontal) + x;
                        newAtlas.CreateRegion(filename + i, x * Tilesize, y * Tilesize, Tilesize, Tilesize);

                        var tileModel = new TileModel
                        {
                            ID        = globalIndex,
                            AtlasName = filename,
                            RegionId  = i,
                        };

                        tilemodels.Add(globalIndex, tileModel);
                        globalIndex++;
                    }
                }

                atlases.Add(filename, newAtlas);
            }

            tileModels = tilemodels;
        }
示例#14
0
        public static TextureAtlas CreateFromFile(ContentManager content, string filepath)
        {
            if (!File.Exists(filepath))
            {
                throw new ArgumentException("Invalid filepath provided!");
            }

            using (var file = new StreamReader(filepath))
            {
                var reader           = new XmlSerializer(typeof(TextureAtlasData));
                var textureAtlasData = (TextureAtlasData)reader.Deserialize(file);

                var sheetPath = textureAtlasData.ImagePath.Replace(".png", "");
                var sheet     = content.Load <Texture2D>(sheetPath);
                var atlas     = new TextureAtlas(sheetPath, sheet);

                foreach (var subTexture in textureAtlasData.SubTextures)
                {
                    atlas.CreateRegion(subTexture.Name.Replace(".png", ""), subTexture.X, subTexture.Y, subTexture.Width, subTexture.Height);
                }

                return(atlas);
            }
        }