Example #1
0
        /// <see cref="UISpriteManagerBase.ScaleSprite"/>
        public override UISprite ScaleSprite(UISprite sprite, RCIntVector pixelSize)
        {
            if (this.ObjectDisposed)
            {
                throw new ObjectDisposedException("XnaSpriteManager");
            }
            if (pixelSize == RCIntVector.Undefined)
            {
                throw new ArgumentNullException("pixelSize");
            }
            if (pixelSize.X <= 0 || pixelSize.Y <= 0)
            {
                throw new ArgumentOutOfRangeException("pixelSize");
            }

            lock (this.lockObj)
            {
                /// Search the sprite in the list.
                XnaSprite spriteToScale = (XnaSprite)sprite;

                /// Create a new bitmap and copy the original to this new with the given pixel size.
                Bitmap scaledBitmap = new Bitmap(spriteToScale.Size.X * pixelSize.X,
                                                 spriteToScale.Size.Y * pixelSize.Y,
                                                 PixelFormat.Format24bppRgb);
                XnaBitmapUtils.CopyBitmapScaled(spriteToScale.RawBitmap, scaledBitmap, spriteToScale.PixelSize, pixelSize);

                /// Create the XnaSprite object and register it to this sprite manager.
                XnaSprite newSprite = new XnaSprite(scaledBitmap, pixelSize, this.platform);
                newSprite.TransparentColor = sprite.TransparentColor;
                this.sprites.Add(newSprite);

                TraceManager.WriteAllTrace("XnaSpriteManager.ScaleSprite: Sprite created", XnaTraceFilters.INFO);
                return(newSprite);
            }
        }
Example #2
0
        /// <see cref="UISprite.TransparentColor_set"/>
        protected override void TransparentColor_set(RCColor newColor)
        {
            if (this.isLocked)
            {
                throw new UIException("Sprite is locked");
            }

            if (newColor == RCColor.Undefined)
            {
                /// The transparent bitmap should be deleted and the original bitmap has to be loaded.
                this.transparentBitmap.Dispose();
                this.transparentBitmap = null;
                TraceManager.WriteAllTrace("XnaSprite: transparent bitmap destroyed", XnaTraceFilters.DETAILS);
            }
            else
            {
                /// The transparent bitmap should be replaced and has to be loaded.
                if (this.transparentBitmap != null)
                {
                    this.transparentBitmap.Dispose();
                }
                this.transparentBitmap = new Bitmap(this.rawBitmap.Width, this.rawBitmap.Height, PixelFormat.Format24bppRgb);
                XnaBitmapUtils.CopyBitmapScaled(this.rawBitmap, this.transparentBitmap, this.PixelSize, this.PixelSize);
                this.transparentBitmap.MakeTransparent(Color.FromArgb(newColor.R, newColor.G, newColor.B));
                TraceManager.WriteAllTrace("XnaSprite: transparent bitmap replaced", XnaTraceFilters.DETAILS);
            }
        }
Example #3
0
        /// <see cref="UISpriteManagerBase.LoadSprite"/>
        public override UISprite LoadSprite(byte[] imageData, RCIntVector pixelSize)
        {
            if (this.ObjectDisposed)
            {
                throw new ObjectDisposedException("XnaSpriteManager");
            }
            if (null == imageData)
            {
                throw new ArgumentNullException("imageData");
            }
            if (pixelSize == RCIntVector.Undefined)
            {
                throw new ArgumentNullException("pixelSize");
            }
            if (pixelSize.X <= 0 || pixelSize.Y <= 0)
            {
                throw new ArgumentOutOfRangeException("pixelSize");
            }

            /// Load the bitmap from the given byte array.
            Stream byteStream   = new MemoryStream(imageData);
            Bitmap loadedBitmap = (Bitmap)Image.FromStream(byteStream);

            byteStream.Close();
            if (loadedBitmap.PixelFormat != PixelFormat.Format24bppRgb)
            {
                throw new ArgumentException("Pixel format of the given Bitmap must be PixelFormat.Format24bppRgb",
                                            "originalBmp");
            }

            /// Create a new bitmap and copy the original to this new with the given pixel size.
            Bitmap scaledBitmap = new Bitmap(loadedBitmap.Width * pixelSize.X,
                                             loadedBitmap.Height * pixelSize.Y,
                                             PixelFormat.Format24bppRgb);

            XnaBitmapUtils.CopyBitmapScaled(loadedBitmap, scaledBitmap, new RCIntVector(1, 1), pixelSize);
            loadedBitmap.Dispose();

            lock (this.lockObj)
            {
                /// Create the XnaSprite object and register it to this sprite manager.
                XnaSprite newSprite = new XnaSprite(scaledBitmap, pixelSize, this.platform);
                this.sprites.Add(newSprite);

                TraceManager.WriteAllTrace("XnaSpriteManager.LoadSprite: Sprite created", XnaTraceFilters.INFO);
                return(newSprite);
            }
        }
        /// <see cref="IUIRenderContext.RenderSprite"/>
        public void RenderSprite(UISprite sprite, RCIntVector position, RCIntRectangle section)
        {
            if (section == RCIntRectangle.Undefined)
            {
                this.RenderSprite(sprite, position);
                return;
            }

            if (this.isClosed)
            {
                throw new UIException("Render context unavailable!");
            }
            if (sprite == null)
            {
                throw new ArgumentNullException("sprite");
            }
            if (position == RCIntVector.Undefined)
            {
                throw new ArgumentNullException("position");
            }

            XnaSprite srcSprite  = (XnaSprite)sprite;
            Bitmap    sectionBmp = new Bitmap(section.Width * this.targetSprite.PixelSize.X,
                                              section.Height * this.targetSprite.PixelSize.Y,
                                              PixelFormat.Format24bppRgb);

            XnaBitmapUtils.CopyBitmapScaled(srcSprite.RawBitmap, sectionBmp,
                                            sprite.PixelSize, this.targetSprite.PixelSize,
                                            section, new RCIntVector(0, 0));
            if (srcSprite.TransparentColor != RCColor.Undefined)
            {
                sectionBmp.MakeTransparent(Color.FromArgb(srcSprite.TransparentColor.R,
                                                          srcSprite.TransparentColor.G,
                                                          srcSprite.TransparentColor.B));
            }
            this.targetGC.DrawImageUnscaled(sectionBmp,
                                            position.X * this.targetSprite.PixelSize.X,
                                            position.Y * this.targetSprite.PixelSize.Y);
            sectionBmp.Dispose();
        }
Example #5
0
        /// <see cref="UISpriteManagerBase.ShrinkSprite"/>
        public override UISprite ShrinkSprite(UISprite sprite, RCIntVector spriteSize, RCIntVector pixelSize)
        {
            if (this.ObjectDisposed)
            {
                throw new ObjectDisposedException("XnaSpriteManager");
            }
            if (spriteSize == RCIntVector.Undefined)
            {
                throw new ArgumentNullException("spriteSize");
            }
            if (pixelSize == RCIntVector.Undefined)
            {
                throw new ArgumentNullException("pixelSize");
            }

            lock (this.lockObj)
            {
                /// Search the sprite in the list.
                XnaSprite spriteToShrink = (XnaSprite)sprite;

                /// Create a copy of the original bitmap with pixelsize (1, 1) if necessary.
                Bitmap bitmapToShrink;
                if (spriteToShrink.PixelSize != new RCIntVector(1, 1))
                {
                    bitmapToShrink = new Bitmap(spriteToShrink.Size.X, spriteToShrink.Size.Y, PixelFormat.Format24bppRgb);
                    XnaBitmapUtils.CopyBitmapScaled(spriteToShrink.RawBitmap, bitmapToShrink, spriteToShrink.PixelSize, new RCIntVector(1, 1));
                }
                else
                {
                    bitmapToShrink = spriteToShrink.RawBitmap;
                }

                /// Create the shrinked bitmap with pixelsize (1, 1).
                Bitmap   shrinkedBitmap = new Bitmap(spriteSize.X, spriteSize.Y, PixelFormat.Format24bppRgb);
                Graphics gc             = Graphics.FromImage(shrinkedBitmap);
                gc.InterpolationMode = InterpolationMode.NearestNeighbor;
                gc.DrawImage(bitmapToShrink, new Rectangle(0, 0, spriteSize.X, spriteSize.Y), new Rectangle(0, 0, bitmapToShrink.Width, bitmapToShrink.Height), GraphicsUnit.Pixel);
                gc.Dispose();

                /// Scale the shrinked bitmap to the target pixel size if necessary.
                Bitmap scaledShrinkedBitmap;
                if (pixelSize != new RCIntVector(1, 1))
                {
                    scaledShrinkedBitmap = new Bitmap(shrinkedBitmap.Width * pixelSize.X,
                                                      shrinkedBitmap.Height * pixelSize.Y,
                                                      PixelFormat.Format24bppRgb);
                    XnaBitmapUtils.CopyBitmapScaled(shrinkedBitmap, scaledShrinkedBitmap, new RCIntVector(1, 1), pixelSize);
                }
                else
                {
                    scaledShrinkedBitmap = shrinkedBitmap;
                }

                /// Create the XnaSprite object and register it to this sprite manager.
                XnaSprite shrinkedSprite = new XnaSprite(scaledShrinkedBitmap, pixelSize, this.platform);
                shrinkedSprite.TransparentColor = sprite.TransparentColor;
                this.sprites.Add(shrinkedSprite);

                /// Cleanup if necessary.
                if (bitmapToShrink != spriteToShrink.RawBitmap)
                {
                    bitmapToShrink.Dispose();
                }
                if (shrinkedBitmap != scaledShrinkedBitmap)
                {
                    shrinkedBitmap.Dispose();
                }

                TraceManager.WriteAllTrace("XnaSpriteManager.ShrinkedSprite: Sprite shrinked", XnaTraceFilters.INFO);
                return(shrinkedSprite);
            }
        }