Beispiel #1
0
        /// <summary>
        /// removes a resource element by object.
        /// </summary>
        /// <param name="resource">resource element object</param>
        public bool RemoveResourceByObject(object resource, bool disposing)
        {
            //  Finding the resource in storage by object
            GameResourceBase res = FindResource(resource);

            if (res != null)
            {
                return(RemoveResource(res, disposing));
            }

            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// adds a resource element.
        /// </summary>
        /// <param name="resource">resource element</param>
        public bool AddResource(GameResourceBase resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException("resource");
            }

            if (string.IsNullOrEmpty(resource.Key))
            {
                throw new ArgumentException("The resource contains an invalid key.");
            }
            else if (ResourceStorage.ContainsKey(resource.Key))
            {
                throw new ArgumentException(
                          "The resource is already in the manager.");
            }

            ResourceStorage.Add(resource.Key, resource);

            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// loads a resource file in the content folder.
        /// </summary>
        /// <typeparam name="T">resource type (i.e. Model or Texture2D)</typeparam>
        /// <param name="key">resource key name</param>
        /// <param name="filePath">resource file name</param>
        /// <returns>resource element</returns>
        public GameResourceBase LoadContent <T>(string key, string filePath)
        {
            if (FrameworkCore.Game.GraphicsDevice == null)
            {
                throw new InvalidOperationException("No graphics device.");
            }

            GameResourceBase resource = FindResourceByKey(key);

            if (resource != null)
            {
                return(resource);
            }

            //  loads a resource by content manager
            T obj = contentManager.Load <T>(filePath);

            if (obj == null)
            {
                throw new ArgumentException("Fail to load content (" + key +
                                            " : " + filePath + ")");
            }

            if (obj is Texture2D)
            {
                resource = new GameResourceTexture2D(key, filePath,
                                                     obj as Texture2D);
            }
            else if (obj is Model)
            {
                resource = new GameResourceModel(key, filePath, obj as Model);
            }
            else if (obj is AnimationSequence)
            {
                resource = new GameResourceAnimation(key, filePath,
                                                     obj as AnimationSequence);
            }
            else if (obj is SpriteFont)
            {
                resource = new GameResourceFont(key, filePath, obj as SpriteFont);
            }
            else if (obj is Effect)
            {
                resource = new GameResourceEffect(key, filePath, obj as Effect);
            }
            else
            {
                throw new NotSupportedException("Not supported the resource");
            }

            if (debugTrace)
            {
                System.Diagnostics.Debug.WriteLine(
                    string.Format("Load Resource : {0} ({1})",
                                  filePath, resource.ToString()));
            }

            if (AddResource(resource))
            {
                return(resource);
            }

            return(null);
        }
Beispiel #4
0
 /// <summary>
 /// removes a resource element by object.
 /// </summary>
 /// <param name="resource">a resource element</param>
 public bool RemoveResource(GameResourceBase resource, bool disposing)
 {
     return(RemoveResource(resource.Key, disposing));
 }