using System.IO;
using SlatedGameToolkit.Framework.Graphics.OpenGL;
using SlatedGameToolkit.Framework.Graphics.Textures;
using StbImageSharp;

namespace SlatedGameToolkit.Framework.Loaders
{
    public static class TextureLoader
    {
        /// <summary>
        /// Loads a texture using StbImage library.
        /// Any format supported by StbImage is therefore supported.
        /// </summary>
        /// <param name="path">The path of the texture to load.</param>
        /// <param name="glContext">The OpenGL context to associate the texture with.</param>
        /// <returns>A texture.</returns>
        public static Texture Load2DTexture(string path, GLContext glContext = null)
        {
            using (Stream stream = File.OpenRead(path))
            {
                ImageResult image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
                TextureData textureData = new TextureData(image.Width, image.Height, image.Data);
                return new Texture(textureData, glContext);
            }
        }
    }
}
