示例#1
0
        /// <summary>
        /// Gets and loads the texture.
        /// Texture is unloaded when nobody is using it.
        /// </summary>
        /// <typeparam name="T">MyTexture2D or MyTextureCube</typeparam>
        /// <param name="path">The path.</param>
        /// <param name="loadedCallback">Callback that is invoked when texture is really loaded and ready for use.</param>
        /// <param name="loadingMode">The loading mode viz. LoadingMode.</param>
        /// <param name="flags">The flags.</param>
        /// <returns></returns>
        public static T GetTexture <T>(string path, string contentDir = "", TextureLoadedHandler loadedCallback = null, LoadingMode loadingMode = LoadingMode.Immediate, TextureFlags flags = TextureFlags.None)
            where T : MyTexture
        {
            Debug.Assert(path != null, "Texture path cannot be null!");

            path = path ?? String.Empty;

            if (OverrideLoadingMode != null)
            {
                loadingMode = OverrideLoadingMode.Value;
            }

            MyTexture texture;

            if (!m_textures.TryGetValue(Path.Combine(contentDir, path), out texture))
            {
                return(LoadTexture <T>(path, contentDir, loadedCallback, loadingMode, flags));
            }

            if (texture != null)
            {
                return((T)texture);
            }

            lock (m_textures)
            {
                m_textures.Remove(Path.Combine(contentDir, path));

                DbgWatchLoadedTextures();
            }

            return(LoadTexture <T>(path, contentDir, loadedCallback, loadingMode, flags));
        }
示例#2
0
        /// <summary>
        /// Loads the texture.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">The path.</param>
        /// <param name="loadedCallback">The loaded callback.</param>
        /// <param name="loadingMode">The loading mode.</param>
        /// <param name="flags">The flags.</param>
        /// <returns></returns>
        private static T LoadTexture <T>(string path, TextureLoadedHandler loadedCallback, LoadingMode loadingMode, TextureFlags flags) where T : MyTexture
        {
            using (TexturesLock.AcquireExclusiveUsing())
            {
                MyTexture texture;
                if (typeof(T) == typeof(MyTexture2D))
                {
                    texture = new MyTexture2D(path, GetLoadMethod(loadingMode), flags);
                }
                else if (typeof(T) == typeof(MyTextureCube))
                {
                    texture = new MyTextureCube(path, GetLoadMethod(loadingMode), flags);
                }
                else
                {
                    throw new ArgumentException("Unsupported texture type", "T");
                }

                if (loadedCallback != null)
                {
                    texture.TextureLoaded += loadedCallback;
                }

                switch (loadingMode)
                {
                case LoadingMode.Immediate:
                {
                    if (!texture.Load(Quality))
                    {
                        return(null);
                    }

                    break;
                }

                case LoadingMode.Background:
                {
                    LoadTextureInBackground(texture);
                }
                break;
                }



                if (m_textures.ContainsKey(path))
                {
                    m_textures[path] = texture;
                }
                else
                {
                    m_textures.Add(path, texture);
                }

                DbgWatchLoadedTextures();

                return((T)texture);
            }
        }
示例#3
0
        /// <summary>
        /// Loads the texture.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">The path.</param>
        /// <param name="loadedCallback">The loaded callback.</param>
        /// <param name="loadingMode">The loading mode.</param>
        /// <param name="flags">The flags.</param>
        /// <returns></returns>
        private static T LoadTexture <T>(string path, string contentDir, TextureLoadedHandler loadedCallback, LoadingMode loadingMode, TextureFlags flags) where T : MyTexture
        {
            MyTexture texture;

            if (TexturesWithIgnoredQuality.Contains(Path.Combine(contentDir, path)))
            {
                flags |= TextureFlags.IgnoreQuality;
            }

            if (typeof(T) == typeof(MyTexture2D))
            {
                texture = new MyTexture2D(contentDir, path, GetLoadMethod(loadingMode), flags);
            }
            else if (typeof(T) == typeof(MyTextureCube))
            {
                texture = new MyTextureCube(contentDir, path, GetLoadMethod(loadingMode), flags);
            }
            else
            {
                throw new ArgumentException("Unsupported texture type", "T");
            }

            if (loadedCallback != null)
            {
                texture.TextureLoaded += loadedCallback;
            }

            switch (loadingMode)
            {
            case LoadingMode.Immediate:
            {
                if (!texture.Load(MyRenderConstants.RenderQualityProfile.TextureQuality, (flags & TextureFlags.CanBeMissing) > 0))
                {
                    return(null);
                }

                break;
            }

            case LoadingMode.Background:
            {
                LoadTextureInBackground(texture);
            }
            break;
            }

            lock (m_textures)
            {
                m_textures[Path.Combine(contentDir, path)] = texture;

                DbgWatchLoadedTextures();
            }

            return((T)texture);
        }
示例#4
0
        /// <summary>
        /// Gets and loads the texture.
        /// Texture is unloaded when nobody is using it.
        /// </summary>
        /// <typeparam name="T">MyTexture2D or MyTextureCube</typeparam>
        /// <param name="path">The path.</param>
        /// <param name="loadedCallback">Callback that is invoked when texture is really loaded and ready for use.</param>
        /// <param name="loadingMode">The loading mode viz. LoadingMode.</param>
        /// <param name="flags">The flags.</param>
        /// <returns></returns>
        public static T GetTexture <T>(string path, TextureLoadedHandler loadedCallback = null, LoadingMode loadingMode = (MyFakes.LOAD_TEXTURES_IMMEDIATELY ? LoadingMode.Immediate : LoadingMode.Lazy)
                                       , TextureFlags flags = TextureFlags.None) where T : MyTexture
        {
            if (OverrideLoadingMode != null)
            {
                loadingMode = OverrideLoadingMode.Value;
            }

            MyTexture texture;

            TexturesLock.AcquireShared();

            if (!m_textures.TryGetValue(path, out texture))
            {
                TexturesLock.ReleaseShared();
                return(LoadTexture <T>(path, loadedCallback, loadingMode, flags));
            }
            TexturesLock.ReleaseShared();

            DbgWatchLoadedTextures();

            return((T)texture);
        }