Exemplo n.º 1
0
        internal static SFML.Graphics.Font Load(string path)
        {
            path = FileHandling.GetAbsoluteFilePath(path);
            if (!Files.FileExists(path))
            {
                throw new FileNotFoundException(path + " not found.");
            }
            if (fonts.ContainsKey(path))
            {
                return(fonts[path]);
            }

            if (Files.IsUsingDataPack(path))
            {
                var stream = new MemoryStream(Files.LoadFileBytes(path));
                fonts.Add(path, new SFML.Graphics.Font(stream)); // SFML fix? Might be memory leaking when you have a lot of fonts.
                //stream.Close();
                //fonts.Add(path, new SFML.Graphics.Font(Files.LoadFileBytes(path))); // SFML fix?
            }
            else
            {
                if (File.Exists(path))
                {
                    fonts.Add(path, new SFML.Graphics.Font(path));                    // Cant load font with bytes from path?
                }
                else
                {                                                                             // This should work because we already checked FileExists above
                    fonts.Add(path, new SFML.Graphics.Font(Files.AssetsFolderPrefix + path)); // Cant load font with bytes from path?
                }
            }
            return(fonts[path]);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Load a file as a memory stream from local files or packed data.
 /// Probably don't use this a lot it probably is memory leak city.
 /// </summary>
 /// <param name="path">The path to load from.</param>
 /// <returns>The stream.</returns>
 public static Stream LoadFileStream(string path)
 {
     path = FileHandling.GetAbsoluteFilePath(path);
     if (FileExists(path))
     {
         return(new MemoryStream(LoadFileBytes(path)));
     }
     return(null);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Create an OgmoProject from a source .oep file.
        /// </summary>
        /// <param name="source">The path to the .oep file.</param>
        /// <param name="imagePath">The default image path to use for loading tilemaps.</param>
        public OgmoProject(string source, string imagePath = "")
        {
            source = FileHandling.GetAbsoluteFilePath(source);
            if (!File.Exists(source))
            {
                throw new ArgumentException("Ogmo project file could not be found.");
            }

            if (imagePath == "")
            {
                UseAtlas = true;
            }
            ImagePath = imagePath;

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(source);

            BackgroundColor = new Color(xmlDoc["project"]["BackgroundColor"]);
            GridColor       = new Color(xmlDoc["project"]["GridColor"]);

            var xmlLayers = xmlDoc.GetElementsByTagName("LayerDefinition");

            foreach (XmlElement x in xmlLayers)
            {
                var layer = new OgmoLayer(x);
                Layers.Add(layer.Name, layer);
            }

            //I dont know if I need to do this
            var xmlEntities = xmlDoc.GetElementsByTagName("EntityDefinition");

            foreach (XmlElement x in xmlEntities)
            {
            }

            var xmlTilesets = xmlDoc.GetElementsByTagName("Tileset");

            foreach (XmlElement x in xmlTilesets)
            {
                TileMaps.Add(x["Name"].InnerText, x["FilePath"].InnerText);
            }

            //var xmlLevelValues = xmlDoc.GetElementsByTagName("ValueDefinitions");
            //dirty dirty hack because there should only be one element with that name
            //and for SOME REASON I can't just grab an XmlElement, I have to grab a NodeList and enumerate it for my element. What gives, microsoft?
            var xmlLevelValues = xmlDoc.GetElementsByTagName("LevelValueDefinitions")[0] as XmlElement;

            foreach (XmlElement x in xmlLevelValues.GetElementsByTagName("ValueDefinition"))
            {
                levelValueTypes.Add(x.Attributes["Name"].Value, x.Attributes["xsi:type"].Value);
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Check if a file is being loaded from a local file or the packed data.
 /// Note that the game will attempt to load from local files before packed data.
 /// </summary>
 /// <param name="path">The path to check.</param>
 /// <returns>True if the data is coming from the packed file.</returns>
 public static bool IsUsingDataPack(string path)
 {
     path = FileHandling.GetAbsoluteFilePath(path);
     if (File.Exists(path))
     {
         return(false);
     }
     if (File.Exists(AssetsFolderPrefix + path))
     {
         return(false);
     }
     return(Data.ContainsKey(path));
 }
Exemplo n.º 5
0
 public static SoundBuffer Load(string path)
 {
     path = FileHandling.GetAbsoluteFilePath(path);
     if (!Files.FileExists(path))
     {
         throw new FileNotFoundException(path + " not found.");
     }
     if (sounds.ContainsKey(path))
     {
         return(sounds[path]);
     }
     sounds.Add(path, new SoundBuffer(Files.LoadFileBytes(path)));
     return(sounds[path]);
 }
Exemplo n.º 6
0
 internal static SFML.Graphics.Texture Load(string path)
 {
     path = FileHandling.GetAbsoluteFilePath(path);
     //if (!File.Exists(source)) throw new FileNotFoundException("Texture path " + source + " not found.");
     if (!Files.FileExists(path))
     {
         throw new FileNotFoundException("Texture path " + path + " not found.");
     }
     if (textures.ContainsKey(path))
     {
         return(textures[path]);
     }
     textures.Add(path, new SFML.Graphics.Texture(Files.LoadFileBytes(path)));
     return(textures[path]);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Load a file as a byte array from local files or packed data.
 /// </summary>
 /// <param name="path">The path to load from.</param>
 /// <returns>The byte array of the data from the file.</returns>
 public static byte[] LoadFileBytes(string path)
 {
     path = FileHandling.GetAbsoluteFilePath(path);
     if (File.Exists(path))
     {
         return(File.ReadAllBytes(path));
     }
     if (File.Exists(AssetsFolderPrefix + path))
     {
         return(File.ReadAllBytes(AssetsFolderPrefix + path));
     }
     if (Data.ContainsKey(path))
     {
         return(Data[path]);
     }
     return(null);
 }
Exemplo n.º 8
0
        /// <summary>
        /// Reads data from a uncompressed packed file
        /// </summary>
        /// <param name="path">The path to the packed data file.</param>
        public static void LoadPackedData(string path)
        {
            path = FileHandling.GetAbsoluteFilePath(path);
            if (!File.Exists(path))
            {
                throw new FileNotFoundException("Cannot find packed data file " + path);
            }

            Data.Clear();
            var bytes   = new BinaryReader(File.Open(path, FileMode.Open));
            int length  = (int)bytes.BaseStream.Length;
            var reading = bytes.ReadBoolean();

            while (reading)
            {
                var filepath = bytes.ReadString();
                var fileSize = bytes.ReadInt32();
                var data     = bytes.ReadBytes(fileSize);

                Data.Add(filepath, data);
                //Console.WriteLine("Reading data {0}", filepath);
                reading = bytes.ReadBoolean();
            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// Load data into a Scene from a source .oel file.
 /// </summary>
 /// <param name="path">The oel to load.</param>
 /// <param name="scene">The Scene to load into.</param>
 public void LoadLevelFromFile(string path, Scene scene)
 {
     path = FileHandling.GetAbsoluteFilePath(path);
     LoadLevel(File.ReadAllText(path), scene);
 }
Exemplo n.º 10
0
        /// <summary>
        /// Load level data from a string into a Scene.
        /// </summary>
        /// <param name="data">The level data to load.</param>
        /// <param name="scene">The Scene to load into.</param>
        public void LoadLevel(string data, Scene scene)
        {
            data = FileHandling.GetAbsoluteFilePath(data);
            Entities.Clear();

            CurrentLevel = data;

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(data);

            var xmlLevel = xmlDoc["level"];

            scene.Width  = int.Parse(xmlDoc["level"].Attributes["width"].Value);
            scene.Height = int.Parse(xmlDoc["level"].Attributes["height"].Value);

            int i = 0;

            foreach (var layer in Layers.Values)
            {
                if (layer.Type == "GridLayerDefinition")
                {
                    var Entity = new Entity();

                    var grid = new GridCollider(scene.Width, scene.Height, layer.GridWidth, layer.GridHeight);

                    grid.LoadString(xmlLevel[layer.Name].InnerText);
                    if (ColliderTags.ContainsKey(layer.Name))
                    {
                        grid.AddTag(ColliderTags[layer.Name]);
                    }

                    if (DisplayGrids)
                    {
                        var tilemap = new Tilemap(scene.Width, scene.Height, layer.GridWidth, layer.GridHeight);
                        tilemap.LoadString(xmlLevel[layer.Name].InnerText, layer.Color);
                        Entity.AddGraphic(tilemap);
                    }

                    Entity.AddCollider(grid);

                    scene.Add(Entity);
                    Entities.Add(layer.Name, Entity);
                }
                if (layer.Type == "TileLayerDefinition")
                {
                    var Entity = new Entity();

                    var xmlTiles = xmlLevel[layer.Name];

                    var tileset = xmlTiles.Attributes["tileset"].Value;

                    var tilepath = ImagePath + TileMaps[tileset];

                    foreach (var kv in assetMappings)
                    {
                        var find    = kv.Key;
                        var replace = kv.Value;

                        if (tilepath.EndsWith(find))
                        {
                            tilepath = replace;
                            break;
                        }
                    }

                    var tilemap = new Tilemap(tilepath, scene.Width, scene.Height, layer.GridWidth, layer.GridHeight);

                    var exportMode = xmlTiles.Attributes["exportMode"].Value;
                    switch (exportMode)
                    {
                    case "CSV":
                        tilemap.LoadCSV(xmlTiles.InnerText);
                        break;

                    case "XMLCoords":
                        foreach (XmlElement t in xmlTiles)
                        {
                            tilemap.SetTile(t);
                        }
                        break;
                    }

                    tilemap.Update();

                    Entity.AddGraphic(tilemap);

                    Entity.Layer = BaseTileDepth - i * TileDepthIncrement;
                    i++;

                    scene.Add(Entity);
                    Entities.Add(layer.Name, Entity);
                }
                if (layer.Type == "EntityLayerDefinition")
                {
                    var xmlEntities = xmlLevel[layer.Name];

                    if (xmlEntities != null)
                    {
                        foreach (XmlElement e in xmlEntities)
                        {
                            CreateEntity(e, scene);
                        }
                    }
                }
            }

            if (UseCameraBounds)
            {
                scene.CameraBounds    = new Rectangle(0, 0, scene.Width, scene.Height);
                scene.UseCameraBounds = true;
            }
        }