static AssetManager()
 {
     DashCMD.AddCommand("unusedfonts", "Locates any unused fonts",
                        (args) =>
     {
         string[] files = Directory.GetFiles(GLoader.GetContentRelativePath("Fonts"));
         foreach (string file in files)
         {
             if (file.EndsWith(".fnt"))
             {
                 if (!fonts.ContainsKey(file))
                 {
                     DashCMD.WriteStandard("[unusedfonts] {0}", Path.GetFileNameWithoutExtension(file));
                 }
             }
         }
     });
 }
        public static AudioBuffer LoadSound(string filePath)
        {
            if (GlobalNetwork.IsServer)
            {
                return(null);
            }

            filePath = GLoader.GetContentRelativePath(Path.Combine("Sounds", filePath));

            AudioBuffer buffer;

            if (sound.TryGetValue(filePath, out buffer))
            {
                return(buffer);
            }
            else
            {
                if (!File.Exists(filePath))
                {
                    return(null);
                }

                string ext = Path.GetExtension(filePath);

                switch (ext)
                {
                case ".wav":
                    buffer = new WavFile(filePath);
                    break;

                case ".ogg":
                    buffer = new OggFile(filePath);
                    break;

                default:
                    throw new IOException($"Audio files with extension {ext} are not supported!");
                }

                sound.Add(filePath, buffer);
                return(buffer);
            }
        }
        public static BMPFont LoadFont(string fileName)
        {
            if (GlobalNetwork.IsServer)
            {
                return(null);
            }

            string filePath = GLoader.GetContentRelativePath(Path.Combine("Fonts", fileName + ".fnt"));

            BMPFont font;

            if (fonts.TryGetValue(filePath, out font))
            {
                return(font);
            }
            else
            {
                font = new BMPFont(filePath);
                fonts.Add(filePath, font);
                return(font);
            }
        }
Beispiel #4
0
        public static TextureMesh LoadOBJ(string filePath, BufferUsageHint usageHint = BufferUsageHint.StaticDraw)
        {
            string finalPath = GLoader.GetContentRelativePath(filePath);

            string           line;
            List <OBJVertex> vertices = new List <OBJVertex>();
            List <Vector2>   uvs      = new List <Vector2>();
            List <Vector3>   normals  = new List <Vector3>();
            List <uint>      indexes  = new List <uint>();

            try
            {
                using (StreamReader sr = new StreamReader(File.Open(finalPath, FileMode.Open, FileAccess.Read)))
                {
                    // Read Part one of the OBJ file (till the f's start)
                    int i = 0;
                    while (true)
                    {
                        // Get the current line
                        line = sr.ReadLine();

                        // Split it by spaces
                        string[] currentLine = line.Split(' ');

                        // Handle Vertex
                        if (line.StartsWith("v "))
                        {
                            float x, y, z;
                            if (!float.TryParse(currentLine[1], out x))
                            {
                                LogFloatParseError(i, line);
                            }
                            if (!float.TryParse(currentLine[2], out y))
                            {
                                LogFloatParseError(i, line);
                            }
                            if (!float.TryParse(currentLine[3], out z))
                            {
                                LogFloatParseError(i, line);
                            }

                            vertices.Add(new OBJVertex(vertices.Count, new Vector3(x, y, z)));
                        }
                        // Handle UV
                        else if (line.StartsWith("vt "))
                        {
                            float x, y;
                            if (!float.TryParse(currentLine[1], out x))
                            {
                                LogFloatParseError(i, line);
                            }
                            if (!float.TryParse(currentLine[2], out y))
                            {
                                LogFloatParseError(i, line);
                            }

                            uvs.Add(new Vector2(x, y));
                        }
                        // Handle Normal
                        else if (line.StartsWith("vn "))
                        {
                            float x, y, z;
                            if (!float.TryParse(currentLine[1], out x))
                            {
                                LogFloatParseError(i, line);
                            }
                            if (!float.TryParse(currentLine[2], out y))
                            {
                                LogFloatParseError(i, line);
                            }
                            if (!float.TryParse(currentLine[3], out z))
                            {
                                LogFloatParseError(i, line);
                            }

                            normals.Add(new Vector3(x, y, z));
                        }
                        // Handle Index Start
                        else if (line.StartsWith("f "))
                        {
                            break;
                        }
                        else if (!line.StartsWith("# "))
                        {
                            DashCMD.WriteWarning("Unrecognized OBJ line: {0}", line);
                        }

                        i++;
                    }

                    // Read the indexes
                    while (line != null)
                    {
                        // Skip non-index lines
                        if (!line.StartsWith("f "))
                        {
                            line = sr.ReadLine();
                            continue;
                        }

                        // Split the current line by spaces
                        string[] currentLine = line.Split(' ');
                        // Get each vertex
                        string[] vertex1 = currentLine[1].Split('/');
                        string[] vertex2 = currentLine[2].Split('/');
                        string[] vertex3 = currentLine[3].Split('/');

                        // Process each vertex to the arrays
                        ProcessVertex(vertex1, vertices, indexes);
                        ProcessVertex(vertex2, vertices, indexes);
                        ProcessVertex(vertex3, vertices, indexes);

                        // Move to the next line
                        // This is at the end because the first line this loop will read,
                        // will be the previous one that broke out of the part 1 loop.
                        line = sr.ReadLine();
                    }
                }
            }
            catch (IOException)
            {
                // TEMP:
                throw;
            }

            // Remove and unused vertices
            RemoveUnusedVertices(vertices);

            // Finalize
            float[] finalVertices = new float[vertices.Count * 3];
            float[] finalUvs      = new float[vertices.Count * 2];
            float[] finalNormals  = new float[vertices.Count * 3];

            float furthest = ConvertDataToArrays(vertices, uvs, normals, finalVertices, finalUvs, finalNormals);

            uint[] finalIndexes = indexes.ToArray();

            // Load mesh to a VAO
            return(new TextureMesh(usageHint, finalVertices, finalIndexes, finalUvs, finalNormals));
        }