Example #1
0
        private RawTexture loadPng(Stream stream)
        {
            RawTexture t      = new RawTexture();
            var        reader = new PngReader();

            t.Pixels      = reader.Read(stream);
            t.PixelFormat = OpenTK.Graphics.ES20.PixelFormat.Rgba;
            t.Width       = reader.Width;
            t.Height      = reader.Height;
            return(t);
        }
Example #2
0
        private RawTexture getTexturePage(int texturePage)
        {
            RawTexture t;

            if (!texturePages.TryGetValue(texturePage, out t))
            {
                t = new RawTexture();
                using (var stream = store.GetStream($@"{assetName}_{texturePage}.png"))
                {
                    var reader = new PngReader();
                    t.Pixels      = reader.Read(stream);
                    t.PixelFormat = OpenTK.Graphics.ES20.PixelFormat.Rgba;
                    t.Width       = reader.Width;
                    t.Height      = reader.Height;
                }
                texturePages[texturePage] = t;
            }
            return(t);
        }
Example #3
0
        /// <summary>
        /// Tries to load an asset with the specified name and type.
        /// </summary>
        /// <param name="name">The name of the asset.</param>
        /// <param name="asset">The returned asset, or null if it couldn't be loaded.</param>
        /// <typeparam name="T">The type of the asset (can be Texture2D, Font, SoundEffect, Song or Effect)</typeparam>
        /// <returns>true if the asset was loaded</returns>
        public bool TryLoadAsset <T>(string name, out T asset, out Exception errorReason, bool keepAfterStateChange = false) where T : class, IDisposable
        {
            asset       = null;
            errorReason = null;

            if (_loadedAssets.ContainsKey(name))
            {
                if (_loadedAssets[name].Asset is T)
                {
                    asset = (T)_loadedAssets[name].Asset;
                    return(true);
                }

                return(false);
            }

            try
            {
                if (typeof(T) == typeof(Texture2D))
                {
                    using (Stream stream = Game.Activity.Assets.Open(name))
                    {
                        MemoryStream copiedStream = new MemoryStream();
                        stream.CopyTo(copiedStream);

                        PngReader reader  = new PngReader();
                        Texture2D texture = reader.Read(copiedStream, Game.GraphicsDevice);
                        Color[]   data    = new Color[texture.Width * texture.Height];
                        texture.GetData(data);

                        for (int i = 0; i < data.Length; i++)
                        {
                            data[i] = Color.FromNonPremultiplied(data[i].ToVector4());
                        }

                        Texture2D newTexture = new Texture2D(Game.GraphicsDevice, texture.Width, texture.Height);
                        newTexture.SetData(data);
                        texture.Dispose();

                        asset = newTexture as T;
                        _loadedAssets.Add(name, new LoadedAsset(asset, name, keepAfterStateChange));
                        return(true);
                    }
                }


                if (typeof(T) == typeof(Font))
                {
                    using (Stream stream = Game.Activity.Assets.Open(name))
                    {
                        asset = new Font(Game.GraphicsDevice, stream) as T;
                        _loadedAssets.Add(name, new LoadedAsset(asset, name, keepAfterStateChange));
                        return(true);
                    }
                }


                if (typeof(T) == typeof(SoundEffect))
                {
                    using (Stream stream = Game.Activity.Assets.Open(name))
                    {
                        asset = SoundEffect.FromStream(stream) as T;
                        _loadedAssets.Add(name, new LoadedAsset(asset, name, keepAfterStateChange));
                        return(true);
                    }
                }

                if (typeof(T) == typeof(Song))
                {
                    // https://stackoverflow.com/questions/5813657/xna-4-song-fromuri-containing-spaces/5829463#5829463
                    ConstructorInfo ctor = typeof(Song).GetConstructor(
                        BindingFlags.NonPublic | BindingFlags.Instance, null,
                        new[] { typeof(string) }, null);
                    asset = ctor.Invoke(new object[] { name }) as T;

                    LoadedAsset a = new LoadedAsset(asset, name, keepAfterStateChange);
                    _loadedAssets.Add(name, a);
                    return(true);
                }

                if (typeof(T) == typeof(Effect))
                {
                    using (Stream stream = Game.Activity.Assets.Open(name))
                    {
                        MemoryStream s = new MemoryStream();
                        stream.CopyTo(s);
                        asset = new Effect(Game.GraphicsDevice, s.ToArray()) as T;
                        _loadedAssets.Add(name, new LoadedAsset(asset, name, keepAfterStateChange));
                        return(true);
                    }
                }

                return(false);
            }
            catch (Exception e)
            {
                errorReason = e;
                return(false);
            }
        }