Exemple #1
0
        /// <summary>
        /// Loads a texture from the specified relative filepath and maps it to the given identifier.
        /// </summary>
        /// <param name="filepath">The relative filepath of the texture.</param>
        /// <param name="identifier">The texture identifier.</param>
        /// <returns>Success if the texture was loaded and mapped to the identifier.</returns>
        public static RwResult LoadSharedTexture(string filepath, string identifier)
        {
            if (SharedTextures == null)
            {
                SharedTextures = new Dictionary <int, Bitmap>();
            }

            string lowIdentifier = identifier.ToString();

            if (TextureIdentifierMappings.ContainsKey(lowIdentifier))
            {
                return(RwResult.DuplicateAssetIdentifier);
            }

            if (!File.Exists(filepath))
            {
                return(RwResult.FileNotFound);
            }

            Bitmap loadedTexture = null;

            try
            {
                string extension = Path.GetExtension(filepath).ToLower();

                if (NativeGdiFormats.Contains(extension))
                {
                    loadedTexture = (Bitmap)Bitmap.FromFile(filepath);
                }
                else if (extension == ".svg")
                {
                    SvgDocument svg = SvgDocument.Open(filepath);
                    loadedTexture = svg.Draw();
                }
            }
            catch (OutOfMemoryException memEx)
            {
                return(RwResult.InvalidFileFormat);
            }

            int id = 0;

            if (FreeIds.Count > 0)
            {
                id = FreeIds[0];
                FreeIds.RemoveAt(0);
            }
            else
            {
                id = SharedTextures.Count;
            }

            TextureIdentifierMappings.Add(lowIdentifier, id);
            SharedTextures.Add(id, loadedTexture);

            return(RwResult.Success);
        }
Exemple #2
0
        /// <summary>
        /// Retrieves a texture's ID by its string identifier.
        /// </summary>
        /// <param name="identifier">The texture identifier.</param>
        /// <returns>The ID of the texture if it was found, -1 otherwise.</returns>
        public int GetTextureId(string identifier)
        {
            string lowIdentifier = identifier.ToLower();

            if (TextureIdentifierMappings.ContainsKey(lowIdentifier))
            {
                return(TextureIdentifierMappings[lowIdentifier]);
            }

            return(-1);
        }