Example #1
0
        public void ResourceDisposalHighReferenceCount()
        {
            var bm = new BitmapResource();

            ResourceManager.Add("ResourceDisposalHighReferenceCount", bm);

            ResourceManager.Request <BitmapResource>("ResourceDisposalHighReferenceCount");
            ResourceManager.Request <BitmapResource>("ResourceDisposalHighReferenceCount");

            bm.Dispose();
            Assert.IsFalse(bm.Disposed);

            bm.Dispose();
            Assert.IsTrue(bm.Disposed);
        }
Example #2
0
        internal void BitmapData(ref BitmapResource pResource, int pResolution)
        {
            if (!_updateBitmap)
            {
                return;
            }

            var memory = new byte[pResolution * pResolution * 4];
            var step   = (int)Math.Floor((double)Size / pResolution);

            for (int x = 0; x < pResolution; x++)
            {
                for (int y = 0; y < pResolution; y++)
                {
                    var i     = (pResolution * 4 * y + x * 4);
                    var color = GetBrush(x * step, y * step).Color;
                    memory[i]     = color.B;
                    memory[i + 1] = color.G;
                    memory[i + 2] = color.R;
                    memory[i + 3] = color.A;
                }
            }

            if (pResource != null)
            {
                pResource.Dispose();
            }
            pResource     = ((DirectXGraphicSystem)Game.Graphics).FromByte(memory, pResolution, pResolution);
            _updateBitmap = false;
        }
Example #3
0
        public void ResourceDisposal()
        {
            var bm = new BitmapResource();

            ResourceManager.Add("ResourceDisposal", bm);
            Assert.AreEqual(0, bm.ReferenceCount, "Incorrect initial count");

            ResourceManager.Request <BitmapResource>("ResourceDisposal");
            Assert.AreEqual(1, bm.ReferenceCount, "Incorrect reference count");

            bm.Dispose();

            Assert.AreEqual(0, bm.ReferenceCount, "Incorrect count after dispose");
            Assert.IsTrue(bm.Disposed);
        }
Example #4
0
 public override void Dispose()
 {
     _bitmap.Dispose();
     base.Dispose();
 }
Example #5
0
        /// <summary>Must be called in a loop for the tile set to be fully loaded.</summary>
        /// <returns>Progress between 0 and 1.</returns>
        public IEnumerable <float> LoadIncrementally()
        {
            if (imageFile.Archive == null ||
                (!imageFile.FileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) && !imageFile.FileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) ||
                (maskFile != null && !maskFile.FileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) && !maskFile.FileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)))
            {
                goto fallBack;
            }

            // optimized native code simd-ed multithreaded pipeline
            {
                int  error;
                uint skippedMipMapLevels = (uint)Math.Max(0, (int)graphics.Properties.MapsAndCountersDetailLevel - (int)detailLevel);

                IntPtr imageLoader = ZunTzuLib.CreateImageLoader(imageFile.Archive.FileName, imageFile.FileName, (maskFile != null ? maskFile.FileName : ""), skippedMipMapLevels, 1);
                try {
                    uint width;
                    uint height;
                    if (0 != (error = ZunTzuLib.GetImageDimensions(imageLoader, out width, out height)))
                    {
                        //throw new ApplicationException(string.Format("Error while loading image: code {0}", error));
                        goto fallBack;
                    }

                    size = new SizeF((float)width, (float)height);
                    for (int i = (int)detailLevel; i > 0; --i)
                    {
                        size = new SizeF(size.Width * 2, size.Height * 2);
                    }

                    int mipMapLevelCount =
                        Math.Max(
                            3,                                  // at least 3 mip-maps
                            Math.Max(
                                (int)Math.Ceiling(Math.Log(width / 254, 2) + 1),
                                (int)Math.Ceiling(Math.Log(height / 254, 2) + 1)));
                    tiles = new DXTile[mipMapLevelCount + (int)detailLevel][, ];

                    uint tileCount = 0;
                    for (int mipMapLevel = (int)detailLevel; mipMapLevel < mipMapLevelCount + (int)detailLevel; ++mipMapLevel)
                    {
                        if (mipMapLevel >= (int)graphics.Properties.MapsAndCountersDetailLevel)
                        {
                            uint columnCount = (width + 253) / 254;
                            uint rowCount    = (height + 253) / 254;
                            tiles[mipMapLevel] = new DXTile[columnCount, rowCount];
                            tileCount         += columnCount * rowCount;
                        }
                        width  = (width + 1) / 2;
                        height = (height + 1) / 2;
                    }

                    uint mipmapLevel;                           // regardless of detailLevel (i.e. first mipmap is always zero)
                    uint x;
                    uint y;
                    for (uint i = 0; i < tileCount; ++i)
                    {
                        Texture texture = new Texture(graphics.Device, 256, 256, 1, 0, (maskFile == null ? Format.Dxt1 : Format.Dxt5), Pool.Managed);
                        if (0 != (error = loadNextTexture(imageLoader, texture, out mipmapLevel, out x, out y)))
                        {
                            texture.UnlockRectangle(0);
                            //throw new ApplicationException(string.Format("Error while loading image: code {0}", error));
                            goto fallBack;
                        }
                        texture.UnlockRectangle(0);

                        DXTile tile = new DXTile(this);
                        tiles[mipmapLevel + (int)detailLevel][x, y] = tile;
                        tile.Initialize(maskFile == null ? Format.Dxt1 : Format.Dxt5, texture);

                        yield return((float)(i + 1) / (float)tileCount);
                    }
                } finally {
                    ZunTzuLib.FreeImageLoader(imageLoader);
                }
                yield break;
            }

fallBack:
            {
                BitmapResource image = null;
                try {
                    if (maskFile != null)
                    {
                        image = new BitmapResource(imageFile, PixelFormat.Format32bppArgb);
                        BitmapResource mask = null;
                        try {
                            mask = new BitmapResource(maskFile, PixelFormat.Format24bppRgb);
                            applyMask(image, mask);
                        } finally {
                            if (mask != null)
                            {
                                mask.Dispose();
                            }
                        }
                    }
                    else
                    {
                        image = new BitmapResource(imageFile, PixelFormat.Format24bppRgb);
                    }

                    // TODO: find the correct progress for loading the bitmaps
                    yield return(0.0f);

                    size = new SizeF((float)image.BitmapData.Width, (float)image.BitmapData.Height);
                    for (int i = (int)detailLevel; i > 0; --i)
                    {
                        size = new SizeF(size.Width * 2, size.Height * 2);
                    }

                    foreach (float progress in createMipMappedTilesIncrements(image))
                    {
                        yield return(progress);
                    }
                } finally {
                    if (image != null)
                    {
                        image.Dispose();
                    }
                }
            }
        }