Example #1
0
        public Texture2DWrapper Load(AssetLoaderContext context, string assetName)
#endif
        {
            ImageResult result = null;

            using (var stream = context.Open(assetName))
            {
                if (stream.CanSeek)
                {
                    result = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
                }
                else
                {
                    // If stream doesnt provide seek functionaly, use MemoryStream instead
                    using (var ms = new MemoryStream())
                    {
                        stream.CopyTo(ms);
                        ms.Seek(0, SeekOrigin.Begin);
                        result = ImageResult.FromStream(ms, ColorComponents.RedGreenBlueAlpha);
                    }
                }
            }

            // Premultiply Alpha
            var b = result.Data;

            for (var i = 0; i < result.Data.Length; i += 4)
            {
                var a = b[i + 3];
                b[i]     = ApplyAlpha(b[i], a);
                b[i + 1] = ApplyAlpha(b[i + 1], a);
                b[i + 2] = ApplyAlpha(b[i + 2], a);
            }

#if MONOGAME || FNA || STRIDE
            var texture = CrossEngineStuff.CreateTexture(MyraEnvironment.GraphicsDevice, result.Width, result.Height);
            CrossEngineStuff.SetTextureData(texture, new Rectangle(0, 0, result.Width, result.Height), result.Data);
            return(texture);
#else
            var texture = MyraEnvironment.Platform.CreateTexture(result.Width, result.Height);
            MyraEnvironment.Platform.SetTextureData(texture, new Rectangle(0, 0, result.Width, result.Height), result.Data);
            return(new Texture2DWrapper(result.Width, result.Height, texture));
#endif
        }