Beispiel #1
0
		public Texture2D TextureFromResource(GFXTypes file, int resourceVal, bool transparent = false, bool reloadFromFile = false)
		{
			Texture2D ret;

			var key = new LibraryGraphicPair((int)file, 100 + resourceVal);
			if (!reloadFromFile && cache.ContainsKey(key))
			{
				return cache[key];
			}

			if (cache.ContainsKey(key) && reloadFromFile)
			{
				if (cache[key] != null) cache[key].Dispose();
				cache.Remove(key);
			}

			using (var mem = new System.IO.MemoryStream())
			{
				using (var i = BitmapFromResource(file, resourceVal, transparent))
					i.Save(mem, System.Drawing.Imaging.ImageFormat.Png);

				ret = Texture2D.FromStream(_device, mem);
			}

			//need to double-check that the key isn't already in the cache:
			//  	multiple threads can enter this method simultaneously
			//avoiding a lock because this method is used for every graphic
			if (!cache.ContainsKey(key))
				cache.Add(key, ret);

			return ret;
		}
        public override bool Equals(object obj)
        {
            if (!(obj is LibraryGraphicPair))
            {
                return(false);
            }
            LibraryGraphicPair other = (LibraryGraphicPair)obj;

            return(other.GraphicNumber == GraphicNumber && other.LibraryNumber == LibraryNumber);
        }
        public int CompareTo(object other)
        {
            if (!(other is LibraryGraphicPair))
            {
                return(-1);
            }

            LibraryGraphicPair rhs = (LibraryGraphicPair)other;

            if (rhs.LibraryNumber == LibraryNumber && rhs.GraphicNumber == GraphicNumber)
            {
                return(0);
            }

            return(-1);
        }
Beispiel #4
0
        public Texture2D TextureFromResource(GFXTypes file, int resourceVal, bool transparent = false, bool reloadFromFile = false)
        {
            Texture2D ret;

            var key = new LibraryGraphicPair((int)file, 100 + resourceVal);

            if (!reloadFromFile && _cache.ContainsKey(key))
            {
                return(_cache[key]);
            }

            if (_cache.ContainsKey(key) && reloadFromFile)
            {
                if (_cache[key] != null)
                {
                    _cache[key].Dispose();
                }
                _cache.Remove(key);
            }

            using (var mem = new System.IO.MemoryStream())
            {
                using (var i = BitmapFromResource(file, resourceVal, transparent))
                    i.Save(mem, ImageFormat.Png);

                ret = Texture2D.FromStream(_graphicsDeviceProvider.GraphicsDevice, mem);
            }

            //need to double-check that the key isn't already in the cache:
            //      multiple threads can enter this method simultaneously
            //avoiding a lock because this method is used for every graphic
            if (!_cache.ContainsKey(key))
            {
                _cache.Add(key, ret);
            }
            else
            {
                ret.Dispose();
                ret = _cache[key];
            }

            return(ret);
        }
        public Texture2D TextureFromResource(GFXTypes file, int resourceVal, bool transparent = false, bool reloadFromFile = false)
        {
            Texture2D ret;

            var key = new LibraryGraphicPair((int)file, 100 + resourceVal);

            lock (__cachelock__)
            {
                if (!reloadFromFile && _cache.ContainsKey(key))
                {
                    return(_cache[key]);
                }

                if (_cache.ContainsKey(key) && reloadFromFile)
                {
                    if (_cache[key] != null)
                    {
                        _cache[key].Dispose();
                    }
                    _cache.Remove(key);
                }
            }

            using (var mem = new System.IO.MemoryStream())
            {
                using (var i = BitmapFromResource(file, resourceVal, transparent))
                    i.Save(mem, ImageFormat.Png);

                ret = Texture2D.FromStream(_graphicsDeviceProvider.GraphicsDevice, mem);
            }

            lock (__cachelock__)
            {
                _cache.Add(key, ret);
            }

            return(ret);
        }